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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
<?php
namespace Integration\Ajax;
use lucatume\WPBrowser\TestCase\WPAjaxTestCase; use SolidWP\Mail\AbstractController; use SolidWP\Mail\Connectors\ConnectorSMTP; use SolidWP\Mail\Repository\ProvidersRepository;
class ConnectionCrudTest extends WPAjaxTestCase {
public function setUp(): void { // update for prevent the migration script triggered. update_option( AbstractController::OPTION_VERSION_NAME, WPSMTP_VERSION ); parent::setUp(); }
public function tearDown(): void { delete_option( ProvidersRepository::OPTION_NAME ); parent::tearDown(); }
/** * Returns a base $_POST array with default values. * * @param array $overrides Array of values to override in the base array. * * @return array */ private function getBasePostData( array $overrides = [] ): array { $base_post = [ 'id' => '', 'from_email' => '[email protected]', 'from_name' => 'Solid Email Test', 'smtp_host' => 'smtp.freesmtpservers.com', 'smtp_port' => '25', 'smtp_secure' => '', 'smtp_auth' => 'no', 'smtp_username' => '[email protected]', 'smtp_password' => 'smtp_password', 'name' => 'other', 'description' => '', 'solidwp_mail_connections_nonce' => wp_create_nonce( 'save_connection' ), ];
return array_merge( $base_post, $overrides ); }
public function testSaveConnectionSuccess(): void { // Become an administrator. $this->_setRole( 'administrator' );
// Simulate $_POST data for a successful save. $_POST = $this->getBasePostData();
// Make the request. try { $this->_handleAjax( 'solidwp_mail_save_connection' ); } catch ( \WPAjaxDieContinueException $e ) { unset( $e ); }
// Verify the response for successful save. $response = json_decode( $this->_last_response, true );
$this->assertArrayHasKey( 'success', $response ); $this->assertTrue( $response['success'] ); $this->assertArrayHasKey( 'data', $response ); $this->assertNotEmpty( $response['data'] ); $this->assertCount( 1, $response['data'] );
// Check the returned data structure. $savedConnection = reset( $response['data'] ); $this->assertArrayHasKey( 'id', $savedConnection ); $this->assertArrayHasKey( 'name', $savedConnection ); $this->assertEquals( 'other', $savedConnection['name'] ); $this->assertArrayHasKey( 'from_email', $savedConnection ); $this->assertEquals( '[email protected]', $savedConnection['from_email'] ); $this->assertArrayHasKey( 'from_name', $savedConnection ); $this->assertEquals( 'Solid Email Test', $savedConnection['from_name'] ); $this->assertArrayHasKey( 'smtp_host', $savedConnection ); $this->assertEquals( 'smtp.freesmtpservers.com', $savedConnection['smtp_host'] ); $this->assertArrayHasKey( 'smtp_port', $savedConnection ); $this->assertEquals( 25, $savedConnection['smtp_port'] ); $this->assertArrayHasKey( 'smtp_auth', $savedConnection ); $this->assertEquals( 'no', $savedConnection['smtp_auth'] ); $this->assertArrayHasKey( 'smtp_username', $savedConnection ); $this->assertEquals( '[email protected]', $savedConnection['smtp_username'] ); $this->assertArrayHasKey( 'smtp_password', $savedConnection ); $this->assertEquals( 'smtp_password', $savedConnection['smtp_password'] );
delete_option( ProvidersRepository::OPTION_NAME ); }
public function testSaveConnectionMissingFromEmail(): void { // Become an administrator. $this->_setRole( 'administrator' );
// Simulate $_POST data with missing from_email. $_POST = $this->getBasePostData( [ 'from_email' => '' ] );
// Make the request. try { $this->_handleAjax( 'solidwp_mail_save_connection' ); } catch ( \WPAjaxDieContinueException $e ) { unset( $e ); }
// Verify the response for validation error. $response = json_decode( $this->_last_response, true ); $this->assertArrayHasKey( 'success', $response ); $this->assertFalse( $response['success'] ); $this->assertEquals( 'From Email is not a valid email address', $response['data']['from_email'] ); }
public function testSaveConnectionInvalidFromEmail(): void { // Become an administrator. $this->_setRole( 'administrator' );
// Simulate $_POST data with invalid from_email. $_POST = $this->getBasePostData( [ 'from_email' => 'invalid-email' ] );
// Make the request. try { $this->_handleAjax( 'solidwp_mail_save_connection' ); } catch ( \WPAjaxDieContinueException $e ) { unset( $e ); }
// Verify the response for validation error. $response = json_decode( $this->_last_response, true ); $this->assertArrayHasKey( 'success', $response ); $this->assertFalse( $response['success'] ); $this->assertEquals( 'From Email is not a valid email address', $response['data']['from_email'] ); }
public function testSaveConnectionMissingFromName(): void { // Become an administrator. $this->_setRole( 'administrator' );
// Simulate $_POST data with missing from_name. $_POST = $this->getBasePostData( [ 'from_name' => '' ] );
// Make the request. try { $this->_handleAjax( 'solidwp_mail_save_connection' ); } catch ( \WPAjaxDieContinueException $e ) { unset( $e ); }
// Verify the response for validation error. $response = json_decode( $this->_last_response, true ); $this->assertArrayHasKey( 'success', $response ); $this->assertFalse( $response['success'] ); $this->assertEquals( 'From Name is required', $response['data']['from_name'] ); }
public function testSaveConnectionMissingSmtpHost(): void { // Become an administrator. $this->_setRole( 'administrator' );
// Simulate $_POST data with missing smtp_host. $_POST = $this->getBasePostData( [ 'smtp_host' => '' ] );
// Make the request. try { $this->_handleAjax( 'solidwp_mail_save_connection' ); } catch ( \WPAjaxDieContinueException $e ) { unset( $e ); }
// Verify the response for validation error. $response = json_decode( $this->_last_response, true ); $this->assertArrayHasKey( 'success', $response ); $this->assertFalse( $response['success'] ); $this->assertEquals( 'SMTP Host is required', $response['data']['smtp_host'] ); }
public function testUpdateConnection(): void { // Create something first. $repository = new ProvidersRepository();
$repository->save( new ConnectorSMTP( $this->getBasePostData() ) );
$providers = $repository->get_all_providers(); $created = array_shift( $providers );
// Become an administrator. $this->_setRole( 'administrator' );
// Simulate $_POST data for updating a connection. $_POST = $this->getBasePostData( [ 'id' => $created->get_id(), 'from_email' => '[email protected]', 'from_name' => 'Updated Solid Email Test', 'smtp_host' => 'updated.smtp.freesmtpservers.com', 'smtp_port' => '587', 'smtp_secure' => 'tls', 'smtp_auth' => 'yes', 'smtp_username' => '[email protected]', 'smtp_password' => 'updated_smtp_password', 'name' => 'other', 'description' => '', ] );
// Make the request. try { $this->_handleAjax( 'solidwp_mail_save_connection' ); } catch ( \WPAjaxDieContinueException $e ) { unset( $e ); }
// Verify the response for successful update. $response = json_decode( $this->_last_response, true ); $this->assertArrayHasKey( 'success', $response ); $this->assertTrue( $response['success'] ); $this->assertArrayHasKey( 'data', $response );
// Check the returned data structure. $updatedConnection = reset( $response['data'] ); $this->assertArrayHasKey( 'id', $updatedConnection ); $this->assertEquals( $created->get_id(), $updatedConnection['id'] ); $this->assertArrayHasKey( 'name', $updatedConnection ); $this->assertEquals( 'other', $updatedConnection['name'] ); $this->assertArrayHasKey( 'from_email', $updatedConnection ); $this->assertEquals( '[email protected]', $updatedConnection['from_email'] ); $this->assertArrayHasKey( 'from_name', $updatedConnection ); $this->assertEquals( 'Updated Solid Email Test', $updatedConnection['from_name'] ); $this->assertArrayHasKey( 'smtp_host', $updatedConnection ); $this->assertEquals( 'updated.smtp.freesmtpservers.com', $updatedConnection['smtp_host'] ); $this->assertArrayHasKey( 'smtp_port', $updatedConnection ); $this->assertEquals( 587, $updatedConnection['smtp_port'] ); $this->assertArrayHasKey( 'smtp_auth', $updatedConnection ); $this->assertEquals( 'yes', $updatedConnection['smtp_auth'] ); $this->assertArrayHasKey( 'smtp_username', $updatedConnection ); $this->assertEquals( '[email protected]', $updatedConnection['smtp_username'] ); $this->assertArrayHasKey( 'smtp_password', $updatedConnection ); $this->assertEquals( 'updated_smtp_password', $updatedConnection['smtp_password'] ); $this->assertArrayHasKey( 'smtp_secure', $updatedConnection ); $this->assertEquals( 'tls', $updatedConnection['smtp_secure'] ); } }
|