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

namespace Integration\REST;

use 
lucatume\WPBrowser\TestCase\WPRestApiTestCase;
use 
SolidWP\Mail\Repository\ProvidersRepository;
use 
SolidWP\Mail\Connectors\ConnectorSMTP;
use 
WP_REST_Request;

class 
ConnectionsReadEndpointsTest extends WPRestApiTestCase {

    protected 
ProvidersRepository $repository;

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

    private function 
createTestConnections(): void {
        
$repository  = new ProvidersRepository();
        
$connections = [
            new 
ConnectorSMTP(
                [
                    
'id'            => 'test_smtp_1',
                    
'name'          => 'other',
                    
'from_email'    => '[email protected]',
                    
'from_name'     => 'Test Sender 1',
                    
'smtp_host'     => 'smtp.example.com',
                    
'smtp_port'     => '587',
                    
'smtp_auth'     => 'yes',
                    
'smtp_username' => 'user1',
                    
'smtp_password' => 'pass1',
                    
'smtp_secure'   => 'tls',
                    
'is_active'     => true,
                ]
            ),
            new 
ConnectorSMTP(
                [
                    
'id'            => 'test_smtp_2',
                    
'name'          => 'other',
                    
'from_email'    => '[email protected]',
                    
'from_name'     => 'Test Sender 2',
                    
'smtp_host'     => 'smtp2.example.com',
                    
'smtp_port'     => '465',
                    
'smtp_auth'     => 'yes',
                    
'smtp_username' => 'user2',
                    
'smtp_password' => 'pass2',
                    
'smtp_secure'   => 'ssl',
                    
'is_active'     => false,
                ]
            ),
        ];

        foreach ( 
$connections as $connection ) {
            
$repository->save$connection );
        }
    }

    public function 
testGetAllConnections(): void {
        
$request  = new WP_REST_Request'GET''/solid-mail/v1/connections' );
        
$response rest_get_server()->dispatch$request );
        
$data     $response->get_data();

        
$this->assertSame200$response->get_status() );
        
$this->assertCount2$data );
        
        
// Sort by ID to ensure consistent order
        
usort(
            
$data,
            function ( 
$a$b ) {
                return 
strcmp$a['id'], $b['id'] );
            }
        );
        
        
$this->assertEquals'test_smtp_1'$data[0]['id'] );
        
$this->assertEquals'[email protected]'$data[0]['from_email'] );
        
$this->assertTrue$data[0]['is_active'] );
        
$this->assertEquals'test_smtp_2'$data[1]['id'] );
        
$this->assertEquals'[email protected]'$data[1]['from_email'] );
        
$this->assertFalse$data[1]['is_active'] );
    }

    public function 
testGetSingleConnection(): void {
        
$request  = new WP_REST_Request'GET''/solid-mail/v1/connections/test_smtp_1' );
        
$response rest_get_server()->dispatch$request );
        
$data     $response->get_data();

        
$this->assertSame200$response->get_status() );
        
$this->assertEquals'test_smtp_1'$data['id'] );
        
$this->assertEquals'[email protected]'$data['from_email'] );
        
$this->assertEquals'Test Sender 1'$data['from_name'] );
        
$this->assertTrue$data['is_active'] );
    }

    public function 
testGetNonExistentConnection(): void {
        
$request  = new WP_REST_Request'GET''/solid-mail/v1/connections/non_existent' );
        
$response rest_get_server()->dispatch$request );

        
$this->assertSame404$response->get_status() );
        
$this->assertEquals'connection_not_found'$response->get_data()['code'] );
    }
}