/var/www/html_us/wp-content/plugins/wp-smtp/tests/Integration/REST/LogsEndpointsTest.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
<?php

namespace Integration\REST;

use 
IntegrationTester;
use 
lucatume\WPBrowser\TestCase\WPRestApiTestCase;
use 
SolidWP\Mail\Repository\LogsRepository;
use 
WP_REST_Request;

/**
 * @property IntegrationTester $tester
 */
class LogsEndpointsTest extends WPRestApiTestCase {

    protected 
LogsRepository $repository;

    public function 
setUp(): void {
        
parent::setUp();
        
$this->repository = new LogsRepository();
        
$this->tester->haveSequentialLogsInDatabase();
    }

    public function 
testUnloggedUserCanNotSeeLogs(): void {
        
wp_set_current_user);

        
$request  = new WP_REST_Request'GET''/solidwp-mail/v1/logs' );
        
$response rest_get_server()->dispatch$request );

        
$this->assertSame401$response->get_status() );
    }

    public function 
testUserWithoutManageOptionsCapabilityCanNotSeeLogs(): void {
        
wp_set_current_user(
            
$this->factory()->user->create( [ 'role' => 'editor' ] )
        );

        
$request  = new WP_REST_Request'GET''/solidwp-mail/v1/logs' );
        
$response rest_get_server()->dispatch$request );

        
$this->assertSame403$response->get_status() );
    }

    public function 
testLogsFetch(): void {
        
wp_set_current_user(
            
$this->factory()->user->create( [ 'role' => 'administrator' ] )
        );

        
$request = new WP_REST_Request'GET''/solidwp-mail/v1/logs' );
        
$request->set_param'page');
        
$request->set_param'sortby''timestamp' );
        
$request->set_param'sort''desc' );
        
$request->set_param'per_page');
        
$response rest_get_server()->dispatch$request );
        
$logs     $response->get_data()['logs'];

        
$this->assertSame200$response->get_status() );
        
$this->assertCount2$logs );
        
$this->assertEquals3$response->get_headers()['X-WP-TotalPages'] );
        
$this->assertEquals5$response->get_headers()['X-WP-Total'] );
        
$this->assertEquals'Test Subject 5'$logs[0]['subject'] );
        
$this->assertEquals'Test Subject 4'$logs[1]['subject'] );
    }

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

        
wp_set_current_user(
            
$this->factory()->user->create( [ 'role' => 'administrator' ] )
        );

        
$log_id  $wpdb->get_var"SELECT mail_id FROM {$wpdb->prefix}wpsmtp_logs WHERE subject = 'Test Subject 1'" );
        
$request = new WP_REST_Request'DELETE''/solidwp-mail/v1/logs/delete' );
        
$request->set_param'logIds', [ $log_id ] );
        
$response rest_get_server()->dispatch$request );

        
$this->assertSame204$response->get_status() );

        
// Verify log is deleted
        
$count $this->repository->count_all_logs();
        
$this->assertEquals4$count );
    }
}