-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtrait-lists-setup.php
141 lines (126 loc) · 3.7 KB
/
trait-lists-setup.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
<?php
/**
* Class Newsletters Test Subscription_List
*
* @package Newspack_Newsletters
*/
use Newspack\Newsletters\Subscription_List;
use Newspack\Newsletters\Subscription_Lists;
/**
* Tests the Subscription_List class
*/
trait Lists_Setup {
/**
* Testing posts
*
* @var array
*/
public static $posts;
/**
* Testing a post that could conlfict with the ID of a list
*
* @var int
*/
public static $conflicting_post_id;
/**
* Sets up testing data
*
* @return void
*/
public static function set_up_before_class() {
$without_settings = self::create_post( 1 );
$only_mailchimp = self::create_post( 2 );
update_post_meta(
$only_mailchimp,
Subscription_List::META_KEY,
[
'mailchimp' => [
'list' => 'mc_list',
'tag_id' => 12,
'tag_name' => 'MC Tag',
],
]
);
$two_settings = self::create_post( 3 );
update_post_meta(
$two_settings,
Subscription_List::META_KEY,
[
'mailchimp' => [
'list' => 'mc_list',
'tag_id' => 12,
'tag_name' => 'MC Tag',
],
'active_campaign' => [
'list' => 'ac_list',
'tag_id' => 13,
'tag_name' => 'AC Tag',
],
]
);
$mc_invalid = self::create_post( 4 );
update_post_meta(
$mc_invalid,
Subscription_List::META_KEY,
[
'mailchimp' => [
'error' => 'Error',
],
'active_campaign' => [
'list' => 'ac_list',
'tag_id' => 13,
'tag_name' => 'AC Tag',
],
]
);
$remote_mailchimp = self::create_post( 5 );
$remote_mailchimp_list = new Subscription_List( $remote_mailchimp );
$remote_mailchimp_list->set_remote_id( 'xyz-' . $remote_mailchimp );
$remote_mailchimp_list->set_type( 'remote' );
$remote_mailchimp_list->set_provider( 'mailchimp' );
$remote_mailchimp_inactive = self::create_post( 6 );
$remote_mailchimp_inactive_list = new Subscription_List( $remote_mailchimp_inactive );
$remote_mailchimp_inactive_list->set_remote_id( 'xyz-' . $remote_mailchimp_inactive );
$remote_mailchimp_inactive_list->set_type( 'remote' );
$remote_mailchimp_inactive_list->set_provider( 'mailchimp' );
$remote_mailchimp_inactive_list->update( [ 'active' => false ] );
/**
* The list and post below make sure that the remote list ID is not confused with the post ID when it is an integer and matches with an existing post.
*/
self::$conflicting_post_id = wp_insert_post(
[
'post_title' => 'Simple Post',
'post_type' => 'post',
'post_status' => 'publish',
]
);
$remote_active_campaign = self::create_post( 7 );
$remote_active_campaign_list = new Subscription_List( $remote_active_campaign );
$remote_active_campaign_list->set_remote_id( self::$conflicting_post_id ); // Active campaign has integer IDs, lets make sure they dont get messed up with post IDs.
$remote_active_campaign_list->set_type( 'remote' );
$remote_active_campaign_list->set_provider( 'active_campaign' );
self::$posts = compact( 'without_settings', 'only_mailchimp', 'two_settings', 'mc_invalid', 'remote_mailchimp', 'remote_mailchimp_inactive', 'remote_active_campaign' );
}
/**
* Tear down class
*/
public static function tear_down_after_class() {
global $wpdb;
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->posts WHERE post_type = %s" , Subscription_Lists::CPT ) ); // phpcs:ignore
}
/**
* Create a test post
*
* @param string|int $index An index to identify the post title and description.
* @return int
*/
public static function create_post( $index ) {
$data = [
'post_title' => 'Test List ' . $index,
'post_content' => 'Description ' . $index,
'post_type' => Subscription_Lists::CPT,
'post_status' => 'publish',
];
return wp_insert_post( $data );
}
}