/var/www/html_us/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
<?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 );
    }
}