-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtest-subscription-lists.php
360 lines (306 loc) · 12.1 KB
/
test-subscription-lists.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php
/**
* Class Newsletters Test Subscription_List
*
* @package Newspack_Newsletters
*/
use Newspack\Newsletters\Subscription_List;
use Newspack\Newsletters\Subscription_Lists;
/**
* Tests the Subscription_List class
*/
class Subscription_Lists_Test extends WP_UnitTestCase {
use Lists_Setup;
/**
* Test get_all
*/
public function test_get_all() {
$all = Subscription_Lists::get_all();
$this->assertSame( 7, count( $all ) );
}
/**
* Test get_configured_for_provider
*/
public function test_get_configured_for_provider() {
$all = Subscription_Lists::get_configured_for_provider( 'mailchimp' );
$this->assertSame( 4, count( $all ) );
foreach ( $all as $list ) {
$this->assertInstanceOf( Subscription_List::class, $list );
$this->assertTrue( $list->is_configured_for_provider( 'mailchimp' ) );
$this->assertNotSame( self::$posts['without_settings'], $list->get_id() );
$this->assertNotSame( self::$posts['mc_invalid'], $list->get_id() );
}
$all = Subscription_Lists::get_configured_for_provider( 'active_campaign' );
$this->assertSame( 3, count( $all ) );
foreach ( $all as $list ) {
$this->assertInstanceOf( Subscription_List::class, $list );
$this->assertTrue( $list->is_configured_for_provider( 'active_campaign' ) );
$this->assertNotSame( self::$posts['only_mailchimp'], $list->get_id() );
}
}
/**
* Test get_configured_for_provider
*/
public function test_get_configured_for_current_provider() {
Newspack_Newsletters::set_service_provider( 'mailchimp' );
$all = Subscription_Lists::get_configured_for_current_provider();
$this->assertSame( 4, count( $all ) );
foreach ( $all as $list ) {
$this->assertInstanceOf( Subscription_List::class, $list );
$this->assertTrue( $list->is_configured_for_provider( 'mailchimp' ) );
$this->assertNotSame( self::$posts['without_settings'], $list->get_id() );
$this->assertNotSame( self::$posts['mc_invalid'], $list->get_id() );
}
Newspack_Newsletters::set_service_provider( 'active_campaign' );
$all = Subscription_Lists::get_configured_for_current_provider();
$this->assertSame( 3, count( $all ) );
foreach ( $all as $list ) {
$this->assertInstanceOf( Subscription_List::class, $list );
$this->assertTrue( $list->is_configured_for_provider( 'active_campaign' ) );
$this->assertNotSame( self::$posts['only_mailchimp'], $list->get_id() );
}
}
/**
* Test get_filtered
*/
public function test_get_filtered() {
Newspack_Newsletters::set_service_provider( 'mailchimp' );
$id = self::$posts['only_mailchimp'];
$all = Subscription_Lists::get_filtered(
function ( $list ) use ( $id ) {
return $list->get_id() === $id;
}
);
$this->assertSame( 1, count( $all ) );
$this->assertSame( $id, $all[0]->get_id() );
}
/**
* Test get_list_by_remote_id
*/
public function test_get_list_by_remote_id() {
$existing = 'xyz-' . self::$posts['remote_mailchimp'];
$found = Subscription_List::from_public_id( $existing );
$this->assertSame( self::$posts['remote_mailchimp'], $found->get_id() );
$this->assertSame( 'mailchimp', $found->get_provider() );
$this->assertSame( $existing, $found->get_remote_id() );
$non_existing = 'asdqwe';
$found = Subscription_List::from_public_id( $non_existing );
$this->assertNull( $found );
}
/**
* Test create_remote_list
*/
public function test_create_remote_list() {
$current_provider = Newspack_Newsletters::get_service_provider();
$remote_id = 'xyz-123';
$title = 'Test Remote List';
$list = Subscription_Lists::create_remote_list( $remote_id, $title );
$this->assertSame( $remote_id, $list->get_remote_id() );
$this->assertSame( $title, $list->get_title() );
$this->assertSame( 'remote', $list->get_type() );
$this->assertSame( $current_provider->service, $list->get_provider() );
$check = Subscription_List::from_public_id( $remote_id );
$this->assertSame( $check->get_id(), $list->get_id() );
}
/**
* Test get_or_create_remote_list
*/
public function test_get_or_create_remote_list() {
$count = count( Subscription_Lists::get_all() );
// existing.
$existing = [
'id' => 'xyz-' . self::$posts['remote_mailchimp'],
'title' => 'Test List 5',
];
$list = Subscription_Lists::get_or_create_remote_list( $existing );
$this->assertSame( self::$posts['remote_mailchimp'], $list->get_id() );
$this->assertSame( 'Test List 5', $list->get_title() );
$count_after_existing = count( Subscription_Lists::get_all() );
$this->assertSame( $count, $count_after_existing );
// new.
Newspack_Newsletters::set_service_provider( 'active_campaign' );
$new = [
'id' => 'xyz-abcde',
'title' => 'New random list',
];
$list = Subscription_Lists::get_or_create_remote_list( $new );
$this->assertSame( 'New random list', $list->get_title() );
$this->assertSame( 'xyz-abcde', $list->get_remote_id() );
$this->assertSame( 'active_campaign', $list->get_provider() );
$count_after_new = count( Subscription_Lists::get_all() );
$this->assertSame( $count + 1, $count_after_new );
}
/**
* Test update title on fetch from ESP
*/
public function test_update_title_when_fetching_from_esp() {
$existing = [
'id' => 'xyz-' . self::$posts['remote_mailchimp'],
'title' => 'Remote mailchimp new title',
];
$list = Subscription_Lists::get_or_create_remote_list( $existing );
$this->assertSame( self::$posts['remote_mailchimp'], $list->get_id() );
$this->assertSame( 'Remote mailchimp new title', $list->get_title() );
$this->assertSame( 'Remote mailchimp new title', $list->get_remote_name() );
// Now we edit the local title.
$list->update( [ 'title' => 'Customized title' ] );
$existing = [
'id' => 'xyz-' . self::$posts['remote_mailchimp'],
'title' => 'Remote mailchimp super new title',
];
$list = Subscription_Lists::get_or_create_remote_list( $existing );
$this->assertSame( self::$posts['remote_mailchimp'], $list->get_id() );
$this->assertSame( 'Customized title', $list->get_title(), 'The local name should not be updated if it was customized' );
$this->assertSame( 'Remote mailchimp super new title', $list->get_remote_name(), 'The remote name should always be updated' );
}
/**
* Test update_lists method
*
* @return void
*/
public function test_update() {
Newspack_Newsletters::set_service_provider( 'mailchimp' );
$count = count( Subscription_Lists::get_all() );
$only_mailchimp = new Subscription_List( self::$posts['only_mailchimp'] );
$remote_active_campaign = new Subscription_List( self::$posts['remote_active_campaign'] );
$new_lists = [
[
'id' => $only_mailchimp->get_public_id(),
'title' => 'New title',
],
[
'id' => 'xyz-' . self::$posts['remote_mailchimp'],
'title' => 'Remote mailchimp new title',
'active' => false,
],
[
'id' => $remote_active_campaign->get_public_id(),
'title' => 'New title for AC',
'active' => true,
],
[
'id' => 'xyz-abcde',
'title' => 'New random list',
'active' => true,
],
];
Subscription_Lists::update_lists( $new_lists );
$new_count = count( Subscription_Lists::get_all() );
// 3 local lists should be marked as deactivated.
// 1 remote list should be deactivated and one should be added.
$this->assertSame( $count + 1, $new_count );
$list = new Subscription_List( self::$posts['without_settings'] );
$this->assertSame( false, $list->is_active() );
$list = new Subscription_List( self::$posts['two_settings'] );
$this->assertSame( false, $list->is_active() );
$list = new Subscription_List( self::$posts['mc_invalid'] );
$this->assertSame( false, $list->is_active() );
$list = new Subscription_List( self::$posts['only_mailchimp'] );
$this->assertSame( false, $list->is_active(), 'If active is not informed it should be set to false' );
$this->assertSame( 'New title', $list->get_title() );
$this->assertSame( self::$posts['only_mailchimp'], $list->get_id() );
$list = Subscription_List::from_public_id( 'xyz-' . self::$posts['remote_mailchimp'] );
$this->assertSame( false, $list->is_active() );
$this->assertSame( 'Remote mailchimp new title', $list->get_title() );
$this->assertSame( self::$posts['remote_mailchimp'], $list->get_id() );
$list = Subscription_List::from_public_id( 'xyz-' . self::$posts['remote_mailchimp_inactive'] );
$this->assertSame( false, $list->is_active() );
$this->assertSame( self::$posts['remote_mailchimp_inactive'], $list->get_id() );
$list = Subscription_List::from_public_id( 'xyz-abcde' );
$this->assertSame( true, $list->is_active() );
$this->assertSame( 'New random list', $list->get_title() );
$list = Subscription_List::from_public_id( self::$conflicting_post_id );
$this->assertSame( true, $list->is_active() );
$this->assertSame( 'New title for AC', $list->get_title() );
$this->assertSame( self::$posts['remote_active_campaign'], $list->get_id() );
}
/**
* Test the migration method
*/
public function test_migration() {
$active_campaign = [
'123' => [
'active' => true,
'title' => 'AC1',
'description' => 'ac 1',
],
'456' => [
'active' => false,
'title' => 'AC2',
'description' => 'ac 2',
],
];
$mailchimp = [
'950aaf1a98' => [
'active' => true,
'title' => 'MC1',
'description' => 'mc 1',
],
'group-6a822fca1c-950aaf1a98' => [
'active' => false,
'title' => 'MC2',
'description' => 'mc 2',
],
'120aaf1a12' => [
'active' => true,
'title' => 'MC3',
'description' => 'mc 3',
],
'tag-14370955-950aaf1a98' => [
'active' => false,
'title' => 'MC4',
'description' => 'mc 4',
],
];
update_option( '_newspack_newsletters_mailchimp_lists', $mailchimp );
update_option( '_newspack_newsletters_active_campaign_lists', $active_campaign );
delete_option( '_newspack_newsletters_lists_migrated' );
Subscription_Lists::migrate_lists();
$list = Subscription_List::from_public_id( '123' );
$this->assertSame( 'AC1', $list->get_title() );
$this->assertSame( 'ac 1', $list->get_description() );
$this->assertTrue( $list->is_active() );
$this->assertSame( 'active_campaign', $list->get_provider() );
$list = Subscription_List::from_public_id( '456' );
$this->assertSame( 'AC2', $list->get_title() );
$this->assertSame( 'ac 2', $list->get_description() );
$this->assertFalse( $list->is_active() );
$this->assertSame( 'active_campaign', $list->get_provider() );
$list = Subscription_List::from_public_id( '950aaf1a98' );
$this->assertSame( 'MC1', $list->get_title() );
$this->assertSame( 'mc 1', $list->get_description() );
$this->assertTrue( $list->is_active() );
$this->assertSame( 'mailchimp', $list->get_provider() );
$list = Subscription_List::from_public_id( 'group-6a822fca1c-950aaf1a98' );
$this->assertSame( 'MC2', $list->get_title() );
$this->assertSame( 'mc 2', $list->get_description() );
$this->assertFalse( $list->is_active() );
$this->assertSame( 'mailchimp', $list->get_provider() );
$list = Subscription_List::from_public_id( '120aaf1a12' );
$this->assertSame( 'MC3', $list->get_title() );
$this->assertSame( 'mc 3', $list->get_description() );
$this->assertTrue( $list->is_active() );
$this->assertSame( 'mailchimp', $list->get_provider() );
$list = Subscription_List::from_public_id( 'tag-14370955-950aaf1a98' );
$this->assertSame( 'MC4', $list->get_title() );
$this->assertSame( 'mc 4', $list->get_description() );
$this->assertFalse( $list->is_active() );
$this->assertSame( 'mailchimp', $list->get_provider() );
}
/**
* Test garbage_collector method
*/
public function test_garbage_collector() {
Subscription_Lists::garbage_collector( [ self::$posts['remote_mailchimp'] ] );
$all_lists = Subscription_Lists::get_all();
$ids = array_map(
function ( $list ) {
return $list->get_id();
},
$all_lists
);
$this->assertContains( self::$posts['remote_mailchimp'], $ids );
$this->assertNotContains( self::$posts['remote_mailchimp_inactive'], $ids );
$this->assertContains( self::$posts['remote_active_campaign'], $ids );
}
}