-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtest-constant-contact-esp-common-methods.php
150 lines (128 loc) · 4.01 KB
/
test-constant-contact-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 ConstantContact Usage Reports
*
* @package Newspack_Newsletters
*/
/**
* Test ConstantContact Usage Reports.
*/
class ConstantContactEspCommonMethodsTest extends Abstract_ESP_Tests {
/**
* The current provider slug.
*
* @var string
*/
protected static $provider = 'constant_contact';
/**
* Set up before class
*/
public static function set_up_before_class() {
Newspack_Newsletters_Constant_Contact::instance()->set_api_credentials(
[
'api_key' => 'asdasd',
'api_secret' => 'asdasd',
]
);
}
/**
* 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( 'pre_http_request', [ __CLASS__, 'get_contact_lists_mock_response' ], 10, 3 );
self::$expected_endpoints_reached = [ 'GET: contacts' ];
}
/**
* 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( 'pre_http_request', [ __CLASS__, 'get_contact_lists_mock_response' ], 10, 3 );
}
/**
* Mock responses
*
* @param array $response The api response.
* @param array $args The arguments passed to the endpoint.
* @param string $url The endpoint url.
* @return array
*/
public static function get_contact_lists_mock_response( $response, $args, $url ) {
$expected_request = false;
$parsed_url = wp_parse_url( $url );
$parsed_args = wp_parse_args( $parsed_url['query'] );
// Force an error if any of the below parameters change to ensure tests will be updated if the request changes.
if (
'api.cc.email' !== $parsed_url['host'] ||
'/v3/contacts' !== $parsed_url['path'] ||
'custom_fields,list_memberships,taggings' !== $parsed_args['include'] ||
'all' !== $parsed_args['status']
) {
return [ 'error' ];
}
$base_member_response = [
'contact_id' => '123',
'email_address' => $parsed_args['email'] ?? '',
'taggings' => [],
'list_memberships' => [],
'custom_fields' => [
'field_id' => 'value',
],
];
$response = [
'body' => '',
'response' => [
'code' => 200,
'message' => 'OK',
],
];
$body = null;
if ( '[email protected]' === $parsed_args['email'] ) {
// Simulates a response of a contact with 2 lists.
$expected_request = true;
$member = $base_member_response;
$member['list_memberships'] = [ 'list1' ];
$member['taggings'] = [ 'list2', 'list3', 'list4' ];
$body = (object) [ 'contacts' => [ (object) $member ] ];
} elseif ( '[email protected]' === $parsed_args['email'] ) {
// Simulates a response of a contact with 2 lists.
$expected_request = true;
$member = $base_member_response;
$member['list_memberships'] = [ 'list1' ];
$member['taggings'] = [ 'list2' ];
$body = (object) [ 'contacts' => [ (object) $member ] ];
} elseif ( '[email protected]' === $parsed_args['email'] ) {
// Simulates a response of a contact with zero lists.
$expected_request = true;
$body = (object) [ 'contacts' => [ (object) $base_member_response ] ];
} elseif ( '[email protected]' === $parsed_args['email'] ) {
// Simulates a response of a contact not found.
$expected_request = true;
$body = (object) [ 'contacts' => [] ];
} elseif ( '[email protected]' === $parsed_args['email'] ) {
// Simulates an error response from the API.
$expected_request = true;
$body = [
(object) [
'error_key' => 'error_key',
'error_message' => 'error_message',
],
];
$response['response'] = [
'code' => 400,
'message' => 'Error',
];
}
if ( $expected_request ) {
self::$endpoints_reached[] = 'GET: contacts';
}
$response['body'] = wp_json_encode( $body );
return $response;
}
}