/var/www/html_de/wp-content/plugins/wp-smtp/tests/Integration/Db/LogsRepositoryTest.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php

namespace Integration\Db;

use 
IntegrationTester;
use 
lucatume\WPBrowser\TestCase\WPTestCase;
use 
SolidWP\Mail\Repository\LogsRepository;
use 
SolidWP\Mail\App;

/**
 * @property IntegrationTester $tester
 */
class LogsRepositoryTest extends WPTestCase {

    protected 
LogsRepository $repository;

    public function 
setUp(): void {
        
parent::setUp();
        
$this->repository = new LogsRepository();
        
App::container()->setVar'LOGS_PER_PAGE'); // Set the log limit for pagination tests
        
$this->tester->haveSequentialLogsInDatabase();
    }

    public function 
testGetEmailLogs(): void {
        
// Test default sorting (by timestamp DESC) with pagination
        
$logs $this->repository->get_email_logs(
            [
                
'orderby' => 'timestamp',
                
'order'   => 'desc',
            ]
        );

        
$this->assertCount2$logs ); // LOG_LIMIT is set to 2
        
$this->assertEquals'Test Subject 5'$logs[0]['subject'] );
        
$this->assertEquals'Test Subject 4'$logs[1]['subject'] );

        
// Test sorting by timestamp ASC with pagination
        
$logs $this->repository->get_email_logs(
            [
                
'orderby' => 'timestamp',
                
'order'   => 'asc',
            ] 
        );

        
$this->assertCount2$logs ); // LOG_LIMIT is set to 2
        
$this->assertEquals'Test Subject 1'$logs[0]['subject'] );
        
$this->assertEquals'Test Subject 2'$logs[1]['subject'] );
    }

    public function 
testPaging(): void {
        
// Test first page
        
$logs $this->repository->get_email_logs(
            [
                
'page'    => 1,
                
'orderby' => 'timestamp',
                
'order'   => 'desc',
            ]
        );
        
$this->assertCount2$logs );
        
$this->assertEquals'Test Subject 5'$logs[0]['subject'] );
        
$this->assertEquals'Test Subject 4'$logs[1]['subject'] );

        
// Test second page
        
$logs $this->repository->get_email_logs(
            [
                
'page'    => 2,
                
'orderby' => 'timestamp',
                
'order'   => 'desc',
            ]
        );
        
$this->assertCount2$logs );
        
$this->assertEquals'Test Subject 3'$logs[0]['subject'] );
        
$this->assertEquals'Test Subject 2'$logs[1]['subject'] );

        
// Test third page
        
$logs $this->repository->get_email_logs(
            [
                
'page'    => 3,
                
'orderby' => 'timestamp',
                
'order'   => 'desc',
            ]
        );
        
$this->assertCount1$logs );
        
$this->assertEquals'Test Subject 1'$logs[0]['subject'] );
    }

    public function 
testSearch(): void {
        
$logs $this->repository->get_email_logs( [ 'search_term' => 'Test Subject 1' ] );

        
$this->assertCount1$logs );
        
$this->assertEquals'Test Subject 1'$logs[0]['subject'] );
    }


    public function 
testDeleteLog(): void {
        global 
$wpdb;

        
$log_id  $wpdb->get_var"SELECT mail_id FROM {$wpdb->prefix}wpsmtp_logs WHERE subject = 'Test Subject 1'" );
        
$deleted $this->repository->delete_log( (int) $log_id );

        
$this->assertTrue$deleted );
        
$total_logs $this->repository->count_all_logs();
        
$this->assertEquals4$total_logs );
    }

    public function 
testCountAllLogs(): void {
        
$total_logs $this->repository->count_all_logs();

        
$this->assertEquals5$total_logs );
    }

    public function 
testGetEmailLogsByDate(): void {
        global 
$wpdb;

        
// Get timestamps for the first and third logs
        
$first_log_timestamp $wpdb->get_var"SELECT timestamp FROM {$wpdb->prefix}wpsmtp_logs WHERE subject = 'Test Subject 1'" );
        
$third_log_timestamp $wpdb->get_var"SELECT timestamp FROM {$wpdb->prefix}wpsmtp_logs WHERE subject = 'Test Subject 3'" );

        
$from_timestamp strtotime$first_log_timestamp );
        
$to_timestamp   strtotime$third_log_timestamp );

        
$logs $this->repository->get_email_logs_by_date$from_timestamp$to_timestamp );

        
// Should return logs 1, 2, and 3 (ordered by timestamp ASC)
        
$this->assertCount3$logs );
        
$this->assertEquals'Test Subject 1'$logs[0]['subject'] );
        
$this->assertEquals'Test Subject 2'$logs[1]['subject'] );
        
$this->assertEquals'Test Subject 3'$logs[2]['subject'] );
    }

    public function 
testGetEmailLogsByDateWithInvalidParams(): void {
        
$logs $this->repository->get_email_logs_by_date0100 );
        
$this->assertEmpty$logs );

        
$logs $this->repository->get_email_logs_by_date( -1100 );
        
$this->assertEmpty$logs );

        
$logs $this->repository->get_email_logs_by_date200100 ); // from > to
        
$this->assertEmpty$logs );
    }

    public function 
testDeleteLogs(): void {
        global 
$wpdb;

        
// Get IDs of first two logs
        
$log_ids $wpdb->get_col"SELECT mail_id FROM {$wpdb->prefix}wpsmtp_logs WHERE subject IN ('Test Subject 1', 'Test Subject 2')" );

        
$this->assertCount2$log_ids );

        
$deleted $this->repository->delete_logs$log_ids );
        
$this->assertTrue$deleted );

        
// Verify logs are deleted
        
$total_logs $this->repository->count_all_logs();
        
$this->assertEquals3$total_logs );

        
// Verify specific logs are gone
        
$remaining_subjects $wpdb->get_col"SELECT subject FROM {$wpdb->prefix}wpsmtp_logs ORDER BY subject" );
        
$this->assertEquals( [ 'Test Subject 3''Test Subject 4''Test Subject 5' ], $remaining_subjects );
    }

    public function 
testDeleteLogsWithEmptyArray(): void {
        
$deleted $this->repository->delete_logs( [] );
        
$this->assertFalse$deleted );

        
// Verify no logs were deleted
        
$total_logs $this->repository->count_all_logs();
        
$this->assertEquals5$total_logs );
    }

    public function 
testClearAllLogs(): void {
        
// Verify we have logs initially
        
$total_logs $this->repository->count_all_logs();
        
$this->assertEquals5$total_logs );

        
$cleared $this->repository->clear_all_logs();
        
$this->assertTrue$cleared );

        
// Verify all logs are cleared
        
$total_logs $this->repository->count_all_logs();
        
$this->assertEquals0$total_logs );
    }
}