-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtest-mailchimp-usage-report.php
218 lines (200 loc) · 6.81 KB
/
test-mailchimp-usage-report.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
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
// phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound
/**
* Class Newsletters Test Mailchimp Usage Reports
*
* @package Newspack_Newsletters
*/
/**
* Test Mailchimp Usage Reports.
*/
class MailchimpUsageReportsTest extends WP_UnitTestCase {
/**
* Mock database
*
* @var array
*/
public static $database;
/**
* 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 = [
'lists' => [
[
'id' => 'test-list-1',
'name' => 'Test List',
'stats' => [ 'member_count' => 42 ],
],
[
'id' => 'test-list-2',
'name' => 'Test List 2',
'stats' => [ 'member_count' => 21 ],
],
],
'reports' => [
[
// Sent at 8am today – should be disregarded.
'id' => 'campaign-today',
'emails_sent' => 121,
'opens' => [ 'unique_opens' => 131 ],
'clicks' => [ 'unique_subscriber_clicks' => 141 ],
'send_time' => gmdate( 'Y-m-d\T08:00:00P' ),
],
[
// Sent at 8am yesterday.
'id' => 'campaign-yesterday',
'emails_sent' => 12,
'opens' => [ 'unique_opens' => 13 ],
'clicks' => [ 'unique_subscriber_clicks' => 14 ],
'send_time' => gmdate( 'Y-m-d\T08:00:00P', strtotime( '-1 day' ) ),
],
[
// Sent at 8am the day before yesterday.
'id' => 'campaign-day-before-yesterday',
'emails_sent' => 22,
'opens' => [ 'unique_opens' => 23 ],
'clicks' => [ 'unique_subscriber_clicks' => 24 ],
'send_time' => gmdate( 'Y-m-d\T08:00:00P', strtotime( '-2 day' ) ),
],
[
// Sent at 8am a week ago.
'id' => 'campaign-week-ago',
'emails_sent' => 32,
'opens' => [ 'unique_opens' => 33 ],
'clicks' => [ 'unique_subscriber_clicks' => 34 ],
'send_time' => gmdate( 'Y-m-d\T08:00:00P', strtotime( '-7 day' ) ),
],
],
];
}
/**
* Setup.
*/
public function set_up() {
delete_option( Newspack_Newsletters_Mailchimp_Usage_Reports::REPORTS_OPTION_NAME );
add_filter( 'mailchimp_mock_get', [ __CLASS__, 'mock_get_response' ], 10, 3 );
}
/**
* Teardown.
*/
public function tear_down() {
delete_option( Newspack_Newsletters_Mailchimp_Usage_Reports::REPORTS_OPTION_NAME );
remove_filter( 'mailchimp_mock_get', [ __CLASS__, 'mock_get_response' ] );
}
public static function mock_get_response( $response, $endpoint, $args = [] ) { // phpcs:ignore Squiz.Commenting.FunctionComment.Missing
if ( preg_match( '/lists\/.*\/activity/', $endpoint ) ) {
$activity = [];
for ( $day_index = 0; $day_index < $args['count']; $day_index++ ) {
$activity[] = [
'day' => gmdate( 'Y-m-d', strtotime( "-$day_index day" ) ),
// To accurately reflect MC API, the sent/opens/clicks data will be empty.
// It will be empty for recent 2-3 days in live API.
'emails_sent' => 0,
'unique_opens' => 0,
'recipient_clicks' => 0,
// As many as the day index, just to be predictable.
'subs' => $day_index,
'unsubs' => $day_index,
];
}
return [
'activity' => $activity,
];
}
switch ( $endpoint ) {
case 'lists':
return [
'lists' => self::$database['lists'],
];
case 'reports':
return [
'reports' => self::$database['reports'],
];
default:
return [];
}
}
public function test_get_usage_report_mailchimp_initial() { // phpcs:ignore Squiz.Commenting.FunctionComment.Missing
$expected_report = new Newspack_Newsletters_Service_Provider_Usage_Report(
[
'emails_sent' => 12,
'opens' => 13,
'clicks' => 14,
'subscribes' => 2,
'unsubscribes' => 2,
'total_contacts' => 63,
]
);
$actual_report = ( new Newspack_Newsletters_Mailchimp_Usage_Reports() )->get_usage_report();
$this->assertEquals( $expected_report->to_array(), $actual_report->to_array() );
}
public function test_get_usage_report_mailchimp_with_prior_data() { // phpcs:ignore Squiz.Commenting.FunctionComment.Missing
// Saved prior data.
$saved_campaign_reports = [
// Values "from yesterday", which will have to be subtracted from the new report's values.
'campaign-yesterday' => [
'emails_sent' => 4,
'opens' => 3,
'clicks' => 2,
'send_time' => gmdate( 'Y-m-d\T08:00:00P', strtotime( '-1 day' ) ),
],
];
update_option( Newspack_Newsletters_Mailchimp_Usage_Reports::REPORTS_OPTION_NAME, $saved_campaign_reports );
$expected_report = new Newspack_Newsletters_Service_Provider_Usage_Report(
[
'emails_sent' => 12 - 4,
'opens' => 13 - 3,
'clicks' => 14 - 2,
'subscribes' => 2,
'unsubscribes' => 2,
'total_contacts' => 63,
]
);
$actual_report = ( new Newspack_Newsletters_Mailchimp_Usage_Reports() )->get_usage_report();
$this->assertEquals( $expected_report->to_array(), $actual_report->to_array() );
}
public function test_get_usage_report_mailchimp_backfill() { // phpcs:ignore Squiz.Commenting.FunctionComment.Missing
$actual_reports = ( new Newspack_Newsletters_Mailchimp_Usage_Reports() )->get_usage_reports( 10 );
$this->assertCount( 10, $actual_reports );
$serialized_actual_reports = array_map(
function( $report ) {
return $report->to_array();
},
$actual_reports
);
$last_report = end( $serialized_actual_reports );
$this->assertEquals( 0, $last_report['emails_sent'] );
$this->assertEquals( 0, $last_report['opens'] );
$this->assertEquals( 0, $last_report['clicks'] );
// Mock API returns 1*<day index> per list, there are two mock lists and day index will be 10 here.
$this->assertEquals( 20, $last_report['unsubscribes'] );
$this->assertEquals( 20, $last_report['subscribes'] );
}
public function test_get_usage_report_mailchimp_backfill_with_prior_data() { // phpcs:ignore Squiz.Commenting.FunctionComment.Missing
// Saved prior data.
$saved_campaign_reports = [
'campaign-week-ago' => [
'emails_sent' => 22,
'opens' => 10,
'clicks' => 10,
'send_time' => gmdate( 'Y-m-d\T08:00:00P', strtotime( '-7 day' ) ),
],
];
update_option( Newspack_Newsletters_Mailchimp_Usage_Reports::REPORTS_OPTION_NAME, $saved_campaign_reports );
$actual_reports = ( new Newspack_Newsletters_Mailchimp_Usage_Reports() )->get_usage_reports( 10 );
$serialized_actual_reports = array_map(
function( $report ) {
return $report->to_array();
},
$actual_reports
);
$last_report = end( $serialized_actual_reports );
$this->assertEquals( 0, $last_report['emails_sent'] );
$this->assertEquals( 0, $last_report['opens'] );
$this->assertEquals( 0, $last_report['clicks'] );
}
}