Skip to content

Commit db0af85

Browse files
authored
Adding tests for schedule-post-notifications (#8)
1 parent e65d625 commit db0af85

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

tests/test-rsscloud.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,58 @@ public function test_query_vars_adds_rsscloud() {
3232
$this->assertContains( 'existing_var', $result );
3333
$this->assertContains( 'rsscloud', $result );
3434
}
35+
36+
public function test_add_rss_cloud_element_returns_early_when_not_feed() {
37+
ob_start();
38+
rsscloud_add_rss_cloud_element();
39+
$output = ob_get_clean();
40+
41+
$this->assertEmpty( $output );
42+
}
43+
44+
public function test_add_rss_cloud_element_outputs_cloud_tag_on_feed() {
45+
$this->go_to( get_feed_link( 'rss2' ) );
46+
47+
ob_start();
48+
rsscloud_add_rss_cloud_element();
49+
$output = ob_get_clean();
50+
51+
$this->assertStringContainsString( '<cloud ', $output );
52+
$this->assertStringContainsString( "protocol='http-post'", $output );
53+
$this->assertStringContainsString( 'rsscloud=notify', $output );
54+
}
55+
56+
public function test_add_rss_cloud_element_uses_correct_domain() {
57+
$this->go_to( get_feed_link( 'rss2' ) );
58+
59+
ob_start();
60+
rsscloud_add_rss_cloud_element();
61+
$output = ob_get_clean();
62+
63+
$home = wp_parse_url( get_option( 'home' ) );
64+
$this->assertStringContainsString( "domain='" . $home['host'] . "'", $output );
65+
}
66+
67+
public function test_add_rss_cloud_element_uses_port_from_home_url() {
68+
$this->go_to( get_feed_link( 'rss2' ) );
69+
70+
ob_start();
71+
rsscloud_add_rss_cloud_element();
72+
$output = ob_get_clean();
73+
74+
$home = wp_parse_url( get_option( 'home' ) );
75+
$port = ! empty( $home['port'] ) ? (int) $home['port'] : 80;
76+
$this->assertStringContainsString( "port='" . $port . "'", $output );
77+
}
78+
79+
public function test_parse_request_does_nothing_without_rsscloud_var() {
80+
$wp = new stdClass();
81+
$wp->query_vars = array( 'p' => '1' );
82+
83+
// Should return without calling exit or any notification processing.
84+
rsscloud_parse_request( $wp );
85+
86+
// If we reach this point, the function returned normally.
87+
$this->assertTrue( true );
88+
}
3589
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
/**
3+
* Tests for schedule-post-notifications functions.
4+
*
5+
* @package Rsscloud
6+
*/
7+
8+
class SchedulePostNotificationsTest extends WP_UnitTestCase {
9+
10+
public function set_up() {
11+
parent::set_up();
12+
// Clear any scheduled events from prior tests.
13+
wp_clear_scheduled_hook( 'rsscloud_send_post_notifications_action' );
14+
}
15+
16+
public function test_schedules_cron_event_by_default() {
17+
rsscloud_schedule_post_notifications();
18+
19+
$next = wp_next_scheduled( 'rsscloud_send_post_notifications_action' );
20+
$this->assertNotFalse( $next, 'Expected a cron event to be scheduled.' );
21+
}
22+
23+
public function test_instant_mode_calls_send_directly() {
24+
define( 'RSSCLOUD_NOTIFICATIONS_INSTANT', true );
25+
26+
// Set up a subscriber keyed to the actual feed URL the function will resolve.
27+
$feed_url = get_bloginfo( 'rss2_url' );
28+
$notify_url = 'http://subscriber.example.com/rpc';
29+
rsscloud_update_hub_notifications(
30+
array(
31+
$feed_url => array(
32+
$notify_url => array(
33+
'protocol' => 'http-post',
34+
'status' => 'active',
35+
'failure_count' => 0,
36+
),
37+
),
38+
)
39+
);
40+
41+
$sent = false;
42+
add_filter(
43+
'pre_http_request',
44+
function () use ( &$sent ) {
45+
$sent = true;
46+
return array(
47+
'response' => array(
48+
'code' => 200,
49+
'message' => 'OK',
50+
),
51+
'body' => '',
52+
);
53+
},
54+
10,
55+
3
56+
);
57+
58+
rsscloud_schedule_post_notifications();
59+
60+
$this->assertTrue( $sent, 'Expected notifications to be sent immediately in instant mode.' );
61+
}
62+
63+
public function test_cron_action_hook_is_registered() {
64+
$this->assertSame(
65+
10,
66+
has_action( 'rsscloud_send_post_notifications_action', 'rsscloud_send_post_notifications' )
67+
);
68+
}
69+
70+
public function test_publish_post_hook_is_registered() {
71+
$this->assertSame(
72+
10,
73+
has_action( 'publish_post', 'rsscloud_schedule_post_notifications' )
74+
);
75+
}
76+
}

0 commit comments

Comments
 (0)