-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-send-post-notifications.php
More file actions
277 lines (220 loc) · 7.64 KB
/
test-send-post-notifications.php
File metadata and controls
277 lines (220 loc) · 7.64 KB
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
<?php
/**
* Tests for send-post-notifications functions.
*
* @package Rsscloud
*/
class SendPostNotificationsTest extends WP_UnitTestCase {
private $feed_url = 'http://example.org/?feed=rss2';
private $http_requests = array();
public function set_up() {
parent::set_up();
$this->http_requests = array();
}
private function set_notifications( $data ) {
rsscloud_update_hub_notifications( $data );
}
private function build_notifications( $overrides = array() ) {
$notify_url = isset( $overrides['notify_url'] ) ? $overrides['notify_url'] : 'http://subscriber.example.com:80/rpc';
return array(
$this->feed_url => array(
$notify_url => array(
'protocol' => isset( $overrides['protocol'] ) ? $overrides['protocol'] : 'http-post',
'status' => isset( $overrides['status'] ) ? $overrides['status'] : 'active',
'failure_count' => isset( $overrides['failure_count'] ) ? $overrides['failure_count'] : 0,
),
),
);
}
private function mock_http_response( $status_code = 200 ) {
add_filter(
'pre_http_request',
function ( $preempt, $args, $url ) use ( $status_code ) {
$this->http_requests[] = array(
'url' => $url,
'args' => $args,
);
return array(
'response' => array(
'code' => $status_code,
'message' => 'OK',
),
'body' => '',
);
},
10,
3
);
}
private function mock_http_error() {
add_filter(
'pre_http_request',
function ( $preempt, $args, $url ) {
$this->http_requests[] = array(
'url' => $url,
'args' => $args,
);
return new WP_Error( 'http_request_failed', 'Connection refused' );
},
10,
3
);
}
public function test_successful_notification_sends_post_request() {
$this->set_notifications( $this->build_notifications() );
$this->mock_http_response( 200 );
rsscloud_send_post_notifications( $this->feed_url );
$this->assertCount( 1, $this->http_requests );
$this->assertSame( 'http://subscriber.example.com:80/rpc', $this->http_requests[0]['url'] );
$this->assertSame( 'POST', $this->http_requests[0]['args']['method'] );
$this->assertSame( $this->feed_url, $this->http_requests[0]['args']['body']['url'] );
}
public function test_successful_notification_does_not_update_storage() {
$notifications = $this->build_notifications();
$this->set_notifications( $notifications );
$this->mock_http_response( 200 );
rsscloud_send_post_notifications( $this->feed_url );
$this->assertSame( $notifications, rsscloud_get_hub_notifications() );
}
public function test_http_error_increments_failure_count() {
$this->set_notifications( $this->build_notifications() );
$this->mock_http_error();
rsscloud_send_post_notifications( $this->feed_url );
$notify = rsscloud_get_hub_notifications();
$this->assertSame( 1, $notify[ $this->feed_url ]['http://subscriber.example.com:80/rpc']['failure_count'] );
}
public function test_http_status_error_increments_failure_count() {
$this->set_notifications( $this->build_notifications() );
$this->mock_http_response( 500 );
rsscloud_send_post_notifications( $this->feed_url );
$notify = rsscloud_get_hub_notifications();
$this->assertSame( 1, $notify[ $this->feed_url ]['http://subscriber.example.com:80/rpc']['failure_count'] );
}
public function test_exceeding_max_failures_suspends_subscription() {
$this->set_notifications(
$this->build_notifications( array( 'failure_count' => RSSCLOUD_MAX_FAILURES ) )
);
$this->mock_http_error();
rsscloud_send_post_notifications( $this->feed_url );
$notify = rsscloud_get_hub_notifications();
$sub = $notify[ $this->feed_url ]['http://subscriber.example.com:80/rpc'];
$this->assertSame( 'suspended', $sub['status'] );
$this->assertSame( RSSCLOUD_MAX_FAILURES + 1, $sub['failure_count'] );
}
public function test_successful_response_resets_failure_count() {
$this->set_notifications(
$this->build_notifications( array( 'failure_count' => 3 ) )
);
$this->mock_http_response( 200 );
rsscloud_send_post_notifications( $this->feed_url );
$notify = rsscloud_get_hub_notifications();
$this->assertSame( 0, $notify[ $this->feed_url ]['http://subscriber.example.com:80/rpc']['failure_count'] );
}
public function test_suspended_subscription_is_skipped() {
$this->set_notifications(
$this->build_notifications( array( 'status' => 'suspended' ) )
);
$this->mock_http_response( 200 );
rsscloud_send_post_notifications( $this->feed_url );
$this->assertCount( 0, $this->http_requests );
}
public function test_non_http_post_protocol_is_skipped() {
$this->set_notifications(
$this->build_notifications( array( 'protocol' => 'xml-rpc' ) )
);
$this->mock_http_response( 200 );
rsscloud_send_post_notifications( $this->feed_url );
$this->assertCount( 0, $this->http_requests );
}
public function test_no_subscriptions_does_not_error() {
$this->set_notifications( array() );
$this->mock_http_response( 200 );
// Should not produce any errors or warnings.
rsscloud_send_post_notifications( $this->feed_url );
$this->assertCount( 0, $this->http_requests );
}
public function test_uses_custom_port_from_notify_url() {
$this->set_notifications(
$this->build_notifications( array( 'notify_url' => 'http://subscriber.example.com:8080/rpc' ) )
);
$this->mock_http_response( 200 );
rsscloud_send_post_notifications( $this->feed_url );
$this->assertSame( 8080, $this->http_requests[0]['args']['port'] );
}
public function test_defaults_to_port_80_when_not_in_url() {
$this->set_notifications(
$this->build_notifications( array( 'notify_url' => 'http://subscriber.example.com/rpc' ) )
);
$this->mock_http_response( 200 );
rsscloud_send_post_notifications( $this->feed_url );
$this->assertSame( 80, $this->http_requests[0]['args']['port'] );
}
public function test_fires_feed_notifications_action() {
$this->set_notifications( array() );
$fired = false;
add_action(
'rsscloud_feed_notifications',
function ( $url ) use ( &$fired ) {
$fired = $url;
}
);
rsscloud_send_post_notifications( $this->feed_url );
$this->assertSame( $this->feed_url, $fired );
}
public function test_fires_send_notification_action_on_post() {
$this->set_notifications( $this->build_notifications() );
$this->mock_http_response( 200 );
$fired = false;
add_action(
'rsscloud_send_notification',
function () use ( &$fired ) {
$fired = true;
}
);
rsscloud_send_post_notifications( $this->feed_url );
$this->assertTrue( $fired );
}
public function test_fires_notify_failure_action_on_error() {
$this->set_notifications( $this->build_notifications() );
$this->mock_http_error();
$fired = false;
add_action(
'rsscloud_notify_failure',
function () use ( &$fired ) {
$fired = true;
}
);
rsscloud_send_post_notifications( $this->feed_url );
$this->assertTrue( $fired );
}
public function test_fires_suspend_action_when_max_failures_exceeded() {
$this->set_notifications(
$this->build_notifications( array( 'failure_count' => RSSCLOUD_MAX_FAILURES ) )
);
$this->mock_http_error();
$fired = false;
add_action(
'rsscloud_suspend_notification_url',
function () use ( &$fired ) {
$fired = true;
}
);
rsscloud_send_post_notifications( $this->feed_url );
$this->assertTrue( $fired );
}
public function test_fires_reset_failure_count_action_on_recovery() {
$this->set_notifications(
$this->build_notifications( array( 'failure_count' => 2 ) )
);
$this->mock_http_response( 200 );
$fired = false;
add_action(
'rsscloud_reset_failure_count',
function () use ( &$fired ) {
$fired = true;
}
);
rsscloud_send_post_notifications( $this->feed_url );
$this->assertTrue( $fired );
}
}