/var/www/html_de/wp-content/plugins/wp-smtp/tests/Integration/Logger/EmailLoggingTest.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php

namespace Integration\Logger;

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

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

    private 
LogsRepository $logs_repository;
    private 
ProvidersRepository $providers_repository;

    public function 
setUp(): void {
        
parent::setUp();
        
reset_phpmailer_instance();
        
$this->logs_repository      = new LogsRepository();
        
$this->providers_repository = new ProvidersRepository();
    }

    public function 
testLogEmailSent(): void {
        
$this->tester->haveSuccessfulConnection(
            [
                
'is_active'  => true,
                
'is_default' => true,
                
'from_email' => '[email protected]',
                
'from_name'  => 'Solid Sender',
            ]
        );
        
$connection $this->providers_repository->get_default_provider();

        
wp_mail'[email protected]''Solid Mail Test''Test body' );

        
$logs $this->logs_repository->get_email_logs();
        
$this->assertCount1$logs );

        
$log $logs[0];
        unset( 
$log['mail_id'], $log['timestamp'] );
        
        
$this->assertSame(
            [
                
'to'            => [ '[email protected]],
                
'subject'       => 'Solid Mail Test',
                
'message'       => 'Test body',
                
'headers'       => [],
                
'content_type'  => 'text/plain',
                
'error'         => '',
                
'connection_id' => $connection->get_id(),
                
'from_email'    => '[email protected]',
                
'from_name'     => 'Solid Sender',
            ],
            
$log 
        
);
    }

    public function 
testLogHtmlEmail(): void {
        
$this->tester->haveSuccessfulConnection(
            [
                
'is_active'  => true,
                
'is_default' => true,
                
'from_email' => '[email protected]',
                
'from_name'  => 'Solid Sender',
            ]
        );
        
$connection $this->providers_repository->get_default_provider();

        
wp_mail(
            
'[email protected]',
            
'Solid Mail Test',
            
'<h1>Hello!</h1>',
            [ 
'Content-Type: text/html; charset=utf-8' ]
        );

        
$logs $this->logs_repository->get_email_logs();
        
$this->assertCount1$logs );

        
$log $logs[0];
        unset( 
$log['mail_id'], $log['timestamp'] );

        
$this->assertSame(
            [
                
'to'            => [ '[email protected]],
                
'subject'       => 'Solid Mail Test',
                
'message'       => '<h1>Hello!</h1>',
                
'headers'       => [],
                
'content_type'  => 'text/html',
                
'error'         => '',
                
'connection_id' => $connection->get_id(),
                
'from_email'    => '[email protected]',
                
'from_name'     => 'Solid Sender',
            ],
            
$log 
        
);
    }

    public function 
testLogHtmlEmailWithoutSolidMail(): void {

        
add_filter(
            
'wp_mail_content_type',
            static function () {
                return 
'text/html';
            }
        );

        
wp_mail(
            
'[email protected]',
            
'Solid Mail Test',
            
'<h1>Hello!</h1>',
        );

        
$logs $this->logs_repository->get_email_logs();
        
$this->assertCount1$logs );

        
$log $logs[0];
        unset( 
$log['mail_id'], $log['timestamp'] );

        
$this->assertSame(
            [
                
'to'            => [ '[email protected]],
                
'subject'       => 'Solid Mail Test',
                
'message'       => '<h1>Hello!</h1>',
                
'headers'       => [],
                
'content_type'  => 'text/html',
                
'error'         => '',
                
'connection_id' => 'external',
                
'from_email'    => '[email protected]',
                
'from_name'     => 'WordPress',
            ],
            
$log 
        
);
    }

    public function 
testLogEmailSentWithoutSolidMailConnection(): void {
        
wp_mail'[email protected]''Regular Mail Test''Test body' );

        
$logs $this->logs_repository->get_email_logs();
        
$this->assertCount1$logs );

        
$log $logs[0];
        unset( 
$log['mail_id'], $log['timestamp'] );

        
$this->assertSame(
            [
                
'to'            => [ '[email protected]],
                
'subject'       => 'Regular Mail Test',
                
'message'       => 'Test body',
                
'headers'       => [],
                
'content_type'  => 'text/plain',
                
'error'         => '',
                
'connection_id' => 'external',
                
'from_email'    => '[email protected]',
                
'from_name'     => 'WordPress',
            ],
            
$log 
        
);
    }

    public function 
testLogEmailFailure(): void {
        
$this->tester->haveFailedConnection(
            [
                
'is_active'  => true,
                
'is_default' => true,
                
'from_email' => '[email protected]',
                
'from_name'  => 'Solid Sender',
            ]
        );
        
$connection $this->providers_repository->get_default_provider();

        
wp_mail'[email protected]''Failure Subject''Failure Body' );

        
$logs $this->logs_repository->get_email_logs();
        
$this->assertCount1$logs );

        
$log $logs[0];
        unset( 
$log['mail_id'], $log['timestamp'] );

        
$this->assertSame(
            [
                
'to'            => [ '[email protected]],
                
'subject'       => 'Failure Subject',
                
'message'       => 'Failure Body',
                
'headers'       => [],
                
'content_type'  => 'text/plain',
                
'error'         => 'Fake Error',
                
'connection_id' => $connection->get_id(),
                
'from_email'    => '[email protected]',
                
'from_name'     => 'Solid Sender',
            ],
            
$log 
        
);
    }

    public function 
testLogEmailFailureWithoutSolidMailConnection(): void {
        
wp_mail'wrong@@email-address.com''Failure Subject''Failure Body' );

        
$logs $this->logs_repository->get_email_logs();
        
$this->assertCount1$logs );

        
$log $logs[0];
        unset( 
$log['mail_id'], $log['timestamp'] );

        
$this->assertSame(
            [
                
'to'            => [ 'wrong@@email-address.com' ],
                
'subject'       => 'Failure Subject',
                
'message'       => 'Failure Body',
                
'headers'       => [],
                
'content_type'  => 'text/plain',
                
'error'         => 'You must provide at least one recipient email address.',
                
'connection_id' => 'external',
                
'from_email'    => '[email protected]',
                
'from_name'     => 'WordPress',
            ],
            
$log 
        
);
    }
}