-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtest-subscription-list.php
658 lines (573 loc) · 20.4 KB
/
test-subscription-list.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
<?php
/**
* Class Newsletters Test Subscription_List
*
* @package Newspack_Newsletters
*/
use Newspack\Newsletters\Subscription_List;
/**
* Tests the Subscription_List class
*/
class Subscription_List_Test extends WP_UnitTestCase {
use Lists_Setup;
/**
* Data provider for test_static_methods
*
* @return array
*/
public function static_methods_data() {
return [
[
Subscription_List::PUBLIC_ID_PREFIX . '123',
true,
123,
],
[
Subscription_List::PUBLIC_ID_PREFIX . '2',
true,
2,
],
[
Subscription_List::PUBLIC_ID_PREFIX . '0',
false,
null,
],
[
Subscription_List::PUBLIC_ID_PREFIX . '12d',
false,
null,
],
[
'a' . Subscription_List::PUBLIC_ID_PREFIX . '2',
false,
null,
],
[
Subscription_List::PUBLIC_ID_PREFIX . '2.3',
false,
null,
],
[
'',
false,
null,
],
[
true,
false,
null,
],
[
array(),
false,
null,
],
[
array( 'test' ),
false,
null,
],
[
(object) array( 'test' ),
false,
null,
],
[
null,
false,
null,
],
];
}
/**
* Tests the public ID static methods
*
* @param mixed $input The input for the methods.
* @param mixed $is_local_public_id The expected result of is_local_public_id.
* @param mixed $extracted_id The expected result of get_id_from_local_public_id.
* @return void
* @dataProvider static_methods_data
*/
public function test_static_methods( $input, $is_local_public_id, $extracted_id ) {
$this->assertSame( $is_local_public_id, Subscription_List::is_local_public_id( $input ) );
$this->assertSame( $extracted_id, Subscription_List::get_id_from_local_public_id( $input ) );
}
/**
* Tests constructor with ID
*/
public function test_constructor_with_id() {
$list = new Subscription_List( self::$posts['without_settings'] );
$this->assertInstanceOf( Subscription_List::class, $list );
$this->assertSame( self::$posts['without_settings'], $list->get_id() );
$this->assertSame( 'Description 1', $list->get_description() );
}
/**
* Tests constructor with object
*/
public function test_constructor_with_object() {
$list = new Subscription_List( get_post( self::$posts['without_settings'] ) );
$this->assertInstanceOf( Subscription_List::class, $list );
$this->assertSame( self::$posts['without_settings'], $list->get_id() );
$this->assertSame( 'Description 1', $list->get_description() );
}
/**
* Tests constructor with public_id
*/
public function test_constructor_with_public_id() {
$list = Subscription_List::from_public_id( Subscription_List::PUBLIC_ID_PREFIX . self::$posts['without_settings'] );
$this->assertInstanceOf( Subscription_List::class, $list );
$this->assertSame( self::$posts['without_settings'], $list->get_id() );
$this->assertSame( 'Description 1', $list->get_description() );
}
/**
* Tests constructor with public_id
*/
public function test_constructor_with_non_existent_public_id() {
$list = Subscription_List::from_public_id( 'asdqwe' );
$this->assertNull( $list );
}
/**
* Tests constructor with remote_id
*/
public function test_constructor_with_remote_id() {
$list = Subscription_List::from_remote_id( 'xyz-' . self::$posts['remote_mailchimp'] );
$this->assertInstanceOf( Subscription_List::class, $list );
$this->assertSame( self::$posts['remote_mailchimp'], $list->get_id() );
$this->assertSame( 'Description 5', $list->get_description() );
}
/**
* Tests constructor with remote_id
*/
public function test_constructor_with_non_existent_remote_id() {
$list = Subscription_List::from_remote_id( 'asdqwe' );
$this->assertNull( $list );
}
/**
* Tests constructor with object
*/
public function test_constructor_with_invalid() {
$this->expectException( \InvalidArgumentException::class );
$list = new Subscription_List( 9999 );
}
/**
* Tests get_public_id
*/
public function test_get_public_id() {
$list = new Subscription_List( self::$posts['without_settings'] );
$this->assertSame( Subscription_List::PUBLIC_ID_PREFIX . self::$posts['without_settings'], $list->get_public_id() );
}
/**
* Test get_all_providers_settings
*/
public function test_get_all_providers_settings() {
$list = new Subscription_List( self::$posts['without_settings'] );
$this->assertEquals( [], $list->get_all_providers_settings() );
$list = new Subscription_List( self::$posts['two_settings'] );
$this->assertEquals(
[
'mailchimp' => [
'list' => 'mc_list',
'tag_id' => 12,
'tag_name' => 'MC Tag',
],
'active_campaign' => [
'list' => 'ac_list',
'tag_id' => 13,
'tag_name' => 'AC Tag',
],
],
$list->get_all_providers_settings()
);
}
/**
* Test get_all_providers_settings
*/
public function test_get_providers_settings() {
$list = new Subscription_List( self::$posts['without_settings'] );
$this->assertNull( $list->get_provider_settings( 'mailchimp' ) );
$this->assertNull( $list->get_provider_settings( 'active_campaign' ) );
$this->assertNull( $list->get_provider_settings( 'anything' ) );
$list = new Subscription_List( self::$posts['only_mailchimp'] );
$this->assertNotNull( $list->get_provider_settings( 'mailchimp' ) );
$this->assertNull( $list->get_provider_settings( 'active_campaign' ) );
$this->assertNull( $list->get_provider_settings( 'anything' ) );
$list = new Subscription_List( self::$posts['two_settings'] );
$this->assertNotNull( $list->get_provider_settings( 'mailchimp' ) );
$this->assertNotNull( $list->get_provider_settings( 'active_campaign' ) );
$this->assertNull( $list->get_provider_settings( 'anything' ) );
$this->assertSame(
[
'list' => 'ac_list',
'tag_id' => 13,
'tag_name' => 'AC Tag',
],
$list->get_provider_settings( 'active_campaign' )
);
}
/**
* Test get_current_provider_settings
*/
public function test_get_current_provider_settings() {
Newspack_Newsletters::set_service_provider( 'mailchimp' );
$list = new Subscription_List( self::$posts['without_settings'] );
$this->assertNull( $list->get_current_provider_settings() );
$list = new Subscription_List( self::$posts['only_mailchimp'] );
$this->assertNotNull( $list->get_current_provider_settings() );
$list = new Subscription_List( self::$posts['two_settings'] );
$this->assertNotNull( $list->get_current_provider_settings() );
Newspack_Newsletters::set_service_provider( 'active_campaign' );
$list = new Subscription_List( self::$posts['without_settings'] );
$this->assertNull( $list->get_current_provider_settings() );
$list = new Subscription_List( self::$posts['only_mailchimp'] );
$this->assertNull( $list->get_current_provider_settings() );
$list = new Subscription_List( self::$posts['two_settings'] );
$this->assertNotNull( $list->get_current_provider_settings() );
}
/**
* Test get_configured_providers method
*/
public function test_get_configured_providers() {
$list = new Subscription_List( self::$posts['without_settings'] );
$this->assertSame( [], $list->get_configured_providers() );
$list = new Subscription_List( self::$posts['only_mailchimp'] );
$this->assertSame( [ 'mailchimp' ], $list->get_configured_providers() );
$list = new Subscription_List( self::$posts['two_settings'] );
$this->assertSame( [ 'mailchimp', 'active_campaign' ], $list->get_configured_providers() );
$list = new Subscription_List( self::$posts['mc_invalid'] );
$this->assertSame( [ 'active_campaign' ], $list->get_configured_providers() );
}
/**
* Test get_configured_providers_names method
*/
public function test_get_configured_providers_names() {
$list = new Subscription_List( self::$posts['without_settings'] );
$this->assertSame( [], $list->get_configured_providers_names() );
$list = new Subscription_List( self::$posts['only_mailchimp'] );
$this->assertSame( [ 'Mailchimp' ], $list->get_configured_providers_names() );
$list = new Subscription_List( self::$posts['two_settings'] );
$this->assertSame( [ 'Mailchimp', 'Active Campaign' ], $list->get_configured_providers_names() );
$list = new Subscription_List( self::$posts['mc_invalid'] );
$this->assertSame( [ 'Active Campaign' ], $list->get_configured_providers_names() );
}
/**
* Test get_other_configured_providers method
*/
public function test_get_other_configured_providers() {
Newspack_Newsletters::set_service_provider( 'mailchimp' );
$list = new Subscription_List( self::$posts['without_settings'] );
$this->assertSame( [], $list->get_other_configured_providers() );
$list = new Subscription_List( self::$posts['only_mailchimp'] );
$this->assertSame( [], $list->get_other_configured_providers() );
$list = new Subscription_List( self::$posts['two_settings'] );
$this->assertSame( [ 'active_campaign' ], $list->get_other_configured_providers() );
$list = new Subscription_List( self::$posts['mc_invalid'] );
$this->assertSame( [ 'active_campaign' ], $list->get_other_configured_providers() );
Newspack_Newsletters::set_service_provider( 'active_campaign' );
$list = new Subscription_List( self::$posts['only_mailchimp'] );
$this->assertSame( [ 'mailchimp' ], $list->get_other_configured_providers() );
$list = new Subscription_List( self::$posts['two_settings'] );
$this->assertSame( [ 'mailchimp' ], $list->get_other_configured_providers() );
$list = new Subscription_List( self::$posts['mc_invalid'] );
$this->assertSame( [], $list->get_other_configured_providers() );
}
/**
* Test get_other_configured_providers_names method
*/
public function test_get_other_configured_providers_names() {
Newspack_Newsletters::set_service_provider( 'mailchimp' );
$list = new Subscription_List( self::$posts['without_settings'] );
$this->assertSame( [], $list->get_other_configured_providers_names() );
$list = new Subscription_List( self::$posts['only_mailchimp'] );
$this->assertSame( [], $list->get_other_configured_providers_names() );
$list = new Subscription_List( self::$posts['two_settings'] );
$this->assertSame( [ 'Active Campaign' ], $list->get_other_configured_providers_names() );
$list = new Subscription_List( self::$posts['mc_invalid'] );
$this->assertSame( [ 'Active Campaign' ], $list->get_other_configured_providers_names() );
Newspack_Newsletters::set_service_provider( 'active_campaign' );
$list = new Subscription_List( self::$posts['only_mailchimp'] );
$this->assertSame( [ 'Mailchimp' ], $list->get_other_configured_providers_names() );
$list = new Subscription_List( self::$posts['two_settings'] );
$this->assertSame( [ 'Mailchimp' ], $list->get_other_configured_providers_names() );
$list = new Subscription_List( self::$posts['mc_invalid'] );
$this->assertSame( [], $list->get_other_configured_providers_names() );
}
/**
* Test has_other_configured_providers method
*/
public function test_has_other_configured_providers() {
Newspack_Newsletters::set_service_provider( 'mailchimp' );
$list = new Subscription_List( self::$posts['without_settings'] );
$this->assertFalse( $list->has_other_configured_providers() );
$list = new Subscription_List( self::$posts['only_mailchimp'] );
$this->assertFalse( $list->has_other_configured_providers() );
$list = new Subscription_List( self::$posts['two_settings'] );
$this->assertTrue( $list->has_other_configured_providers() );
$list = new Subscription_List( self::$posts['mc_invalid'] );
$this->assertTrue( $list->has_other_configured_providers() );
Newspack_Newsletters::set_service_provider( 'active_campaign' );
$list = new Subscription_List( self::$posts['only_mailchimp'] );
$this->assertTrue( $list->has_other_configured_providers() );
$list = new Subscription_List( self::$posts['two_settings'] );
$this->assertTrue( $list->has_other_configured_providers() );
$list = new Subscription_List( self::$posts['mc_invalid'] );
$this->assertFalse( $list->has_other_configured_providers() );
}
/**
* Test update_current_provider_settings method
*/
public function test_update_current_provider_settings() {
Newspack_Newsletters::set_service_provider( 'mailchimp' );
$list = new Subscription_List( self::$posts['without_settings'] );
$this->assertNotFalse( $list->update_current_provider_settings( '', 'test', 'test' ) );
$this->assertNotEmpty( $list->get_current_provider_settings()['error'] );
$this->assertNotFalse( $list->update_current_provider_settings( '', 'test', 'test', 'error' ) );
$this->assertSame( 'error', $list->get_current_provider_settings()['error'] );
$this->assertNotFalse( $list->update_current_provider_settings( '123', 123, 'test' ) );
$this->assertSame(
[
'list' => '123',
'tag_id' => 123,
'tag_name' => 'test',
],
$list->get_current_provider_settings()
);
}
/**
* Data provider for test_is_configured
*/
public function is_configured_data() {
return [
[
[
'list' => '123',
'tag_id' => 123,
'tag_name' => 'test',
],
true,
],
[
[
'list' => '123',
'tag_id' => 123,
'tag_name' => 'test',
'error' => '',
],
true,
],
[
[
'list' => '123',
'tag_id' => 123,
'tag_name' => 'test',
'error' => 'Error',
],
false,
],
[
[
'list' => '',
'tag_id' => 123,
'tag_name' => 'test',
],
false,
],
[
[
'list' => '123',
'tag_id' => 0,
'tag_name' => 'test',
],
false,
],
];
}
/**
* Test is_configured_for_provider
*
* @param mixed $meta The metadata for provider.
* @param mixed $expected The expected value for is_configured_for_provider.
* @return void
* @dataProvider is_configured_data
*/
public function test_is_configured( $meta, $expected ) {
$post_id = self::create_post( 99 );
update_post_meta( $post_id, Subscription_List::META_KEY, [ 'mailchimp' => $meta ] );
$list = new Subscription_List( $post_id );
$this->assertSame( $expected, $list->is_configured_for_provider( 'mailchimp' ) );
}
/**
* Test some methods that behave differently with local and remote lists
*/
public function test_is_configured_for_provider_2() {
$list = new Subscription_List( self::$posts['without_settings'] );
$this->assertFalse( $list->is_configured_for_provider( 'mailchimp' ) );
$this->assertSame( 'local', $list->get_type() );
$this->assertSame( Subscription_List::PUBLIC_ID_PREFIX . $list->get_id(), $list->get_public_id() );
$this->assertSame( true, $list->is_active() );
$this->assertSame( true, $list->is_local() );
$this->assertSame( '', $list->get_provider() );
$this->assertSame( '', $list->get_remote_id() );
$list = new Subscription_List( self::$posts['only_mailchimp'] );
$this->assertTrue( $list->is_configured_for_provider( 'mailchimp' ) );
$this->assertSame( 'local', $list->get_type() );
$this->assertSame( Subscription_List::PUBLIC_ID_PREFIX . $list->get_id(), $list->get_public_id() );
$this->assertSame( true, $list->is_active() );
$this->assertSame( true, $list->is_local() );
$this->assertSame( '', $list->get_provider() );
$this->assertSame( '', $list->get_remote_id() );
$list = new Subscription_List( self::$posts['two_settings'] );
$this->assertTrue( $list->is_configured_for_provider( 'mailchimp' ) );
$this->assertSame( 'local', $list->get_type() );
$this->assertSame( Subscription_List::PUBLIC_ID_PREFIX . $list->get_id(), $list->get_public_id() );
$this->assertSame( true, $list->is_active() );
$this->assertSame( true, $list->is_local() );
$this->assertSame( '', $list->get_provider() );
$this->assertSame( '', $list->get_remote_id() );
$list = new Subscription_List( self::$posts['mc_invalid'] );
$this->assertFalse( $list->is_configured_for_provider( 'mailchimp' ) );
$this->assertSame( 'local', $list->get_type() );
$this->assertSame( Subscription_List::PUBLIC_ID_PREFIX . $list->get_id(), $list->get_public_id() );
$this->assertSame( true, $list->is_active() );
$this->assertSame( true, $list->is_local() );
$this->assertSame( '', $list->get_provider() );
$this->assertSame( '', $list->get_remote_id() );
$list = new Subscription_List( self::$posts['remote_mailchimp'] );
$this->assertTrue( $list->is_configured_for_provider( 'mailchimp' ) );
$this->assertSame( 'remote', $list->get_type() );
$this->assertSame( 'xyz-' . $list->get_id(), $list->get_public_id() );
$this->assertSame( true, $list->is_active() );
$this->assertSame( false, $list->is_local() );
$this->assertSame( 'mailchimp', $list->get_provider() );
$this->assertSame( 'xyz-' . $list->get_id(), $list->get_remote_id() );
$list = new Subscription_List( self::$posts['remote_mailchimp_inactive'] );
$this->assertTrue( $list->is_configured_for_provider( 'mailchimp' ) );
$this->assertFalse( $list->is_configured_for_provider( 'active_campaign' ) );
$this->assertTrue( $list->is_configured_for_provider( 'mailchimp' ) );
$this->assertSame( 'remote', $list->get_type() );
$this->assertSame( 'xyz-' . $list->get_id(), $list->get_public_id() );
$this->assertSame( false, $list->is_active() );
$this->assertSame( false, $list->is_local() );
$this->assertSame( 'mailchimp', $list->get_provider() );
$this->assertSame( 'xyz-' . $list->get_id(), $list->get_remote_id() );
$list = new Subscription_List( self::$posts['remote_active_campaign'] );
$this->assertTrue( $list->is_configured_for_provider( 'active_campaign' ) );
$this->assertFalse( $list->is_configured_for_provider( 'mailchimp' ) );
$this->assertSame( 'remote', $list->get_type() );
$this->assertSame( (string) self::$conflicting_post_id, $list->get_public_id() );
$this->assertSame( true, $list->is_active() );
$this->assertSame( false, $list->is_local() );
$this->assertSame( 'active_campaign', $list->get_provider() );
$this->assertSame( (string) self::$conflicting_post_id, $list->get_remote_id() );
}
/**
* Data provider for tests type handling
*
* @return array
*/
public function type_data_provider() {
return [
[
'local',
'local',
],
[
'asd',
'local',
],
[
[ 'array' ],
'local',
],
[
'',
'local',
],
[
null,
'local',
],
[
false,
'local',
],
[
'remote',
'remote',
],
];
}
/**
* Ensures that lists are considered local by default
*
* @param mixed $db_value The value present in the database.
* @param string $expected The expected output.
* @return void
* @dataProvider type_data_provider
*/
public function test_get_type_returns_local_by_default( $db_value, $expected ) {
$post_id = self::create_post( 99 );
if ( $db_value ) {
update_post_meta( $post_id, Subscription_List::TYPE_META, $db_value );
}
$list = new Subscription_List( $post_id );
$this->assertSame( $expected, $list->get_type() );
}
/**
* Ensures that lists types default to local when saving
*
* @param mixed $value The value to be saved.
* @param string $expected The expected value on the DB.
* @return void
* @dataProvider type_data_provider
*/
public function test_get_type_saves_local_by_default( $value, $expected ) {
$post_id = self::create_post( 99 );
$list = new Subscription_List( $post_id );
$list->set_type( $value );
$db_value = get_post_meta( $post_id, Subscription_List::TYPE_META, true );
$this->assertSame( $expected, $db_value );
}
/**
* Tests the update method
*/
public function test_update() {
$list = new Subscription_List( self::$posts['only_mailchimp'] );
// only active.
$this->assertTrue( $list->update( [ 'active' => false ] ) );
$this->assertFalse( $list->is_active() );
// make sure it persisted.
$list = new Subscription_List( self::$posts['only_mailchimp'] );
$this->assertFalse( $list->is_active() );
$this->assertSame( 'Test List 2', $list->get_title() );
// active and title.
$this->assertTrue(
$list->update(
[
'active' => true,
'title' => 'New Title',
]
)
);
$this->assertTrue( $list->is_active() );
$this->assertSame( 'New Title', $list->get_title() );
// make sure it persisted.
$list = new Subscription_List( self::$posts['only_mailchimp'] );
$this->assertTrue( $list->is_active() );
// active, title and description.
$this->assertTrue(
$list->update(
[
'active' => false,
'title' => 'Super New Title',
'description' => 'New Description',
]
)
);
$this->assertFalse( $list->is_active() );
$this->assertSame( 'Super New Title', $list->get_title() );
$this->assertSame( 'New Description', $list->get_description() );
// make sure it persisted.
$list = new Subscription_List( self::$posts['only_mailchimp'] );
$this->assertFalse( $list->is_active() );
}
}