-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtest-mailchimp-esp-common-methods.php
150 lines (135 loc) · 4.23 KB
/
test-mailchimp-esp-common-methods.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
<?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 MailchimpEspCommonMethodsTest extends Abstract_ESP_Tests {
/**
* The current provider slug.
*
* @var string
*/
protected static $provider = 'mailchimp';
/**
* Set up before class
*/
public static function set_up_before_class() {
update_option( 'newspack_mailchimp_api_key', 'test-us' );
}
/**
* Tear down class
*/
public static function tear_down_after_class() {
delete_option( 'newspack_mailchimp_api_key' );
}
/**
* Sets up the required filters for the get_contact_lists test
*
* @param string $email The email to search for.
* @return void
*/
public function set_up_test_get_contact_lists( $email ) {
add_filter( 'mailchimp_mock_get', [ __CLASS__, 'get_contact_lists_mock_response' ], 10, 3 );
self::$expected_endpoints_reached = [ 'GET: search-members' ];
}
/**
* Tears down the required filters for the get_contact_lists test
*
* @param string $email The email to search for.
* @return void
*/
public function tear_down_test_get_contact_lists( $email ) {
remove_filter( 'mailchimp_mock_get', [ __CLASS__, 'get_contact_lists_mock_response' ], 10, 3 );
}
/**
* Mock responses
*
* @param array $response The api response.
* @param string $endpoint The endpoint being called.
* @param array $args The arguments passed to the endpoint.
* @return array
*/
public static function get_contact_lists_mock_response( $response, $endpoint, $args = [] ) {
$expected_request = false;
$base_member_response = [
'full_name' => 'Sample User',
'list_id' => 'list1',
'email_address' => $args['query'] ?? '',
'id' => '123',
'contact_id' => '123',
'status' => 'subscribed',
];
$base_success_response = [
'exact_matches' => [
'members' => [],
],
];
$response = false;
if ( 'search-members' === $endpoint ) {
if ( ! isset( $args['query'] ) ) {
return $base_success_response;
}
if ( '[email protected]' === $args['query'] ) {
// Simulates a response of a contact with 2 lists.
$expected_request = true;
$response = $base_success_response;
$response['exact_matches']['members'][] = $base_member_response;
$response['exact_matches']['members'][0]['tags'] = [
[
'id' => 'list2',
'name' => 'list2',
],
];
$response['exact_matches']['members'][] = $base_member_response;
// Second Audience.
$response['exact_matches']['members'][1]['list_id'] = 'list3';
$response['exact_matches']['members'][1]['tags'] = [
[
'id' => 'list4',
'name' => 'list4',
],
];
} elseif ( '[email protected]' === $args['query'] ) {
// Simulates a response of a contact with 2 lists.
$expected_request = true;
$response = $base_success_response;
$response['exact_matches']['members'][] = $base_member_response;
$response['exact_matches']['members'][0]['tags'] = [
[
'id' => 'list2',
'name' => 'list2',
],
];
} elseif ( '[email protected]' === $args['query'] ) {
// Simulates a response of a contact with zero lists.
$expected_request = true;
$response = $base_success_response;
$response['exact_matches']['members'][] = $base_member_response;
$response['exact_matches']['members'][0]['status'] = 'unsubscribed';
} elseif ( '[email protected]' === $args['query'] ) {
// Simulates a response of a contact not found.
$expected_request = true;
$response = $base_success_response;
} elseif ( '[email protected]' === $args['query'] ) {
// Simulates an error response from the API.
$expected_request = true;
$response = [
'type' => 'https://mailchimp.com/developer/marketing/docs/errors/',
'title' => 'Resource Not Found',
'status' => 404,
'detail' => 'The requested resource could not be found.',
'instance' => '995c5cb0-3280-4a6e-808b-3b096d0bb219',
];
}
if ( $expected_request ) {
self::$endpoints_reached[] = 'GET: search-members';
}
}
return $response;
}
}