-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtest-mailchimp-contacts.php
336 lines (311 loc) · 7.32 KB
/
test-mailchimp-contacts.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
/**
* Class Newsletters Test Contact_Add
*
* @package Newspack_Newsletters
*/
use Newspack\Newsletters\Subscription_Lists;
use Newspack\Newsletters\Subscription_List;
/**
* Tests the Contacts Class.
*/
class Newsletters_Contacts_Test extends WP_UnitTestCase {
/**
* Mock database
*
* @var array
*/
public static $database;
/**
* Setup.
*/
public function set_up() {
add_filter( 'mailchimp_mock_get', [ __CLASS__, 'mock_get_response' ], 10, 3 );
add_filter( 'mailchimp_mock_post', [ __CLASS__, 'mock_post_response' ], 10, 3 );
add_filter( 'mailchimp_mock_put', [ __CLASS__, 'mock_put_response' ], 10, 3 );
}
/**
* Teardown.
*/
public function tear_down() {
remove_filter( 'mailchimp_mock_get', [ __CLASS__, 'mock_get_response' ] );
remove_filter( 'mailchimp_mock_post', [ __CLASS__, 'mock_post_response' ] );
remove_filter( 'mailchimp_mock_put', [ __CLASS__, 'mock_put_response' ] );
}
/**
* Test set up.
*/
public static function set_up_before_class() {
// Set an ESP.
\Newspack_Newsletters::set_service_provider( 'mailchimp' );
update_option( 'newspack_mailchimp_api_key', 'test-us1' );
self::$database = [
'members' => [
[
'id' => '123',
'contact_id' => '123',
'email_address' => '[email protected]',
'full_name' => 'Test User',
'list_id' => 'test-list',
'status' => 'subscribed',
],
],
'tags' => [
[
'id' => 42,
'name' => 'Supertag',
],
],
];
Subscription_Lists::get_or_create_remote_list(
[
'id' => 'list1',
'title' => 'List 1',
]
);
Subscription_Lists::get_or_create_remote_list(
[
'id' => 'list2',
'title' => 'List 2',
]
);
Subscription_Lists::get_or_create_remote_list(
[
'id' => Subscription_List::mailchimp_generate_public_id( 'group1', 'list1' ),
'title' => 'Group 1',
]
);
Subscription_Lists::get_or_create_remote_list(
[
'id' => Subscription_List::mailchimp_generate_public_id( '42', 'list1', 'tag' ),
'title' => 'Supertag',
]
);
Subscription_Lists::get_or_create_remote_list(
[
'id' => Subscription_List::mailchimp_generate_public_id( '42', 'list2', 'tag' ),
'title' => 'Supertag',
]
);
}
/**
* Tear down class
*/
public static function tear_down_after_class() {
global $wpdb;
delete_option( 'newspack_mailchimp_api_key' );
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->posts WHERE post_type = %s" , Subscription_Lists::CPT ) ); // phpcs:ignore
}
public static function mock_get_response( $response, $endpoint, $args = [] ) { // phpcs:ignore Squiz.Commenting.FunctionComment.Missing
if ( preg_match( '/lists\/.*\/merge-fields/', $endpoint ) ) {
return [
'merge_fields' => [
[
'tag' => 'FNAME',
'name' => 'Name',
],
],
];
}
if ( preg_match( '/lists\/.*\/tag-search/', $endpoint ) ) {
return [
'tags' => self::$database['tags'],
];
}
switch ( $endpoint ) {
case 'search-members':
$results = array_filter(
self::$database['members'],
function( $member ) use ( $args ) {
return $member['email_address'] === $args['query'];
}
);
return [ 'exact_matches' => [ 'members' => $results ] ];
default:
return [];
}
return [];
}
public static function mock_post_response( $response, $endpoint, $args = [] ) { // phpcs:ignore Squiz.Commenting.FunctionComment.Missing
if ( preg_match( '/lists\/.*\/merge-fields/', $endpoint ) ) {
return [
'status' => 200,
'tag' => 'FNAME',
];
}
return [
'status' => 200,
];
}
public static function mock_put_response( $response, $endpoint, $args = [] ) { // phpcs:ignore Squiz.Commenting.FunctionComment.Missing
$members_endpoint = preg_match( '/lists\/(.*)\/members/', $endpoint, $matches );
if ( $members_endpoint ) {
$list_id = $matches[1];
$result = [
'status' => 'pending',
'list_id' => $list_id,
];
if ( isset( $args['tags'] ) && is_array( $args['tags'] ) ) {
$result['tags'] = array_map(
function( $tag_name ) {
return [
'id' => 42,
'name' => $tag_name,
];
},
$args['tags']
);
}
if ( isset( $args['interests'] ) && is_array( $args['interests'] ) ) {
$result['interests'] = $args['interests'];
}
// Add tags and interests.
return $result;
}
return [
'status' => 200,
];
}
/**
* Subscribe a contact to a single list synchronously.
*/
public function test_subscribe_contact() {
$result = Newspack_Newsletters_Contacts::subscribe(
[
'email' => '[email protected]',
],
[ 'list1' ]
);
$this->assertEquals(
[
'status' => 'pending',
'list_id' => 'list1',
],
$result
);
}
/**
* Subscribe a contact to a single list assynchronously.
*/
public function test_subscribe_contact_async() {
if ( ! defined( 'NEWSPACK_NEWSLETTERS_ASYNC_SUBSCRIPTION_ENABLED' ) ) {
define( 'NEWSPACK_NEWSLETTERS_ASYNC_SUBSCRIPTION_ENABLED', true );
}
$result = Newspack_Newsletters_Contacts::subscribe(
[
'email' => '[email protected]',
],
[ 'list1' ],
true
);
// Async subscription should return true.
$this->assertEquals(
true,
$result
);
// Hook into the async result.
$async_result = null;
add_action(
'newspack_newsletters_contact_subscribed',
function( $provider, $contact, $lists, $result, $is_updating, $context ) use ( &$async_result ) {
$async_result = $result;
},
10,
6
);
// Manually trigger subscription intents processing.
Newspack_Newsletters_Subscription::process_subscription_intents();
$this->assertEquals(
[
'status' => 'pending',
'list_id' => 'list1',
],
$async_result
);
}
/**
* Upsert a contact to a single list.
*/
public function test_upsert_contact_to_single_list() {
$result = Newspack_Newsletters_Contacts::upsert(
[
'email' => '[email protected]',
],
[ 'list1' ]
);
$this->assertEquals(
[
'status' => 'pending',
'list_id' => 'list1',
],
$result
);
}
/**
* Upsert a contact to multiple lists.
*/
public function test_upsert_contact_to_multiple_lists() {
$result = Newspack_Newsletters_Contacts::upsert(
[
'email' => '[email protected]',
],
[ 'list1', 'list2' ]
);
$this->assertEquals(
[
'status' => 'pending',
'list_id' => 'list2',
],
$result
);
}
/**
* Upsert a contact to lists and sublists.
*/
public function test_upsert_contact_to_lists_and_sublists() {
$result = Newspack_Newsletters_Contacts::upsert(
[
'email' => '[email protected]',
],
[ 'list1', 'tag-42-list1', 'group-group1-list1' ]
);
$this->assertEquals(
[
'status' => 'pending',
'list_id' => 'list1',
'interests' => [ 'group1' => true ],
'tags' => [
[
'id' => 42,
'name' => 'Supertag',
],
],
],
$result
);
}
/**
* Upsert a contact to multiple lists and sublists.
*/
public function test_upsert_contact_to_multiple_lists_and_sublists() {
$result = Newspack_Newsletters_Contacts::upsert(
[
'email' => '[email protected]',
],
[ 'list1', 'tag-42-list1', 'group-group1-list1', 'list2', 'tag-42-list2' ]
);
$this->assertEquals(
[
'status' => 'pending',
'list_id' => 'list2',
'tags' => [
[
'id' => 42,
'name' => 'Supertag',
],
],
],
$result
);
}
}