Skip to content

Commit 5dfb970

Browse files
committed
Tests: Cover logged-out Heartbeat requests.
Verify the default response and the receive, send, and tick hooks in `wp_ajax_nopriv_heartbeat()`. Developed in: #11826 Props pbearne. See #65236. git-svn-id: https://develop.svn.wordpress.org/trunk@65120 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 18911b8 commit 5dfb970

1 file changed

Lines changed: 171 additions & 0 deletions

File tree

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
<?php
2+
3+
/**
4+
* Admin Ajax functions to be tested.
5+
*/
6+
require_once ABSPATH . 'wp-admin/includes/ajax-actions.php';
7+
8+
/**
9+
* Testing wp_ajax_nopriv_heartbeat() functionality.
10+
*
11+
* @package WordPress
12+
* @subpackage UnitTests
13+
* @since 3.6.0
14+
*
15+
* @group ajax
16+
*
17+
* @covers ::wp_ajax_nopriv_heartbeat
18+
*/
19+
class Tests_wp_ajax_nopriv_heartbeat extends WP_Ajax_UnitTestCase {
20+
21+
/**
22+
* Tests the Heartbeat API in the no-privilege context.
23+
*
24+
* @ticket 65236
25+
*/
26+
public function test_wp_ajax_nopriv_heartbeat(): void {
27+
// Mock the user to be logged out.
28+
wp_set_current_user( 0 );
29+
30+
$_POST = array(
31+
'action' => 'nopriv_heartbeat',
32+
'screen_id' => 'front',
33+
'data' => array(
34+
'test_data' => 'test_value',
35+
),
36+
);
37+
38+
// Make the request.
39+
try {
40+
$this->_handleAjax( 'nopriv_heartbeat' );
41+
} catch ( WPAjaxDieContinueException $e ) {
42+
// Expected exception.
43+
unset( $e );
44+
} catch ( WPAjaxDieStopException $e ) {
45+
// Expected exception.
46+
unset( $e );
47+
} catch ( Exception $e ) {
48+
$this->fail( 'Unexpected exception: ' . $e->getMessage() );
49+
}
50+
51+
if ( empty( $this->_last_response ) ) {
52+
$this->fail( 'Ajax response was empty' );
53+
}
54+
55+
$response = json_decode( $this->_last_response, true );
56+
57+
$this->assertIsArray( $response );
58+
$this->assertArrayHasKey( 'server_time', $response );
59+
$this->assertIsInt( $response['server_time'] );
60+
}
61+
62+
/**
63+
* Tests the 'heartbeat_nopriv_received' filter.
64+
*
65+
* @ticket 65236
66+
*/
67+
public function test_heartbeat_nopriv_received_filter(): void {
68+
wp_set_current_user( 0 );
69+
70+
$_POST = array(
71+
'action' => 'nopriv_heartbeat',
72+
'screen_id' => 'test_screen',
73+
'data' => array(
74+
'foo' => 'bar',
75+
),
76+
);
77+
78+
add_filter(
79+
'heartbeat_nopriv_received',
80+
function ( $response, $data, $screen_id ) {
81+
$response['received_data'] = $data;
82+
$response['received_screen'] = $screen_id;
83+
return $response;
84+
},
85+
10,
86+
3
87+
);
88+
89+
try {
90+
$this->_handleAjax( 'nopriv_heartbeat' );
91+
} catch ( WPAjaxDieContinueException $e ) {
92+
unset( $e );
93+
} catch ( WPAjaxDieStopException $e ) {
94+
unset( $e );
95+
}
96+
97+
$response = json_decode( $this->_last_response, true );
98+
99+
$this->assertSame( array( 'foo' => 'bar' ), $response['received_data'] );
100+
$this->assertSame( 'test_screen', $response['received_screen'] );
101+
}
102+
103+
/**
104+
* Tests the 'heartbeat_nopriv_send' filter.
105+
*
106+
* @ticket 65236
107+
*/
108+
public function test_heartbeat_nopriv_send_filter(): void {
109+
wp_set_current_user( 0 );
110+
111+
$_POST = array(
112+
'action' => 'nopriv_heartbeat',
113+
);
114+
115+
add_filter(
116+
'heartbeat_nopriv_send',
117+
function ( $response, $screen_id ) {
118+
$response['sent_screen'] = $screen_id;
119+
return $response;
120+
},
121+
10,
122+
2
123+
);
124+
125+
try {
126+
$this->_handleAjax( 'nopriv_heartbeat' );
127+
} catch ( WPAjaxDieContinueException $e ) {
128+
unset( $e );
129+
} catch ( WPAjaxDieStopException $e ) {
130+
unset( $e );
131+
}
132+
133+
$response = json_decode( $this->_last_response, true );
134+
135+
// Default screen_id is 'front'.
136+
$this->assertSame( 'front', $response['sent_screen'] );
137+
}
138+
139+
/**
140+
* Tests the 'heartbeat_nopriv_tick' action.
141+
*
142+
* @ticket 65236
143+
*/
144+
public function test_heartbeat_nopriv_tick_action(): void {
145+
wp_set_current_user( 0 );
146+
147+
$_POST = array(
148+
'action' => 'nopriv_heartbeat',
149+
);
150+
151+
$action_called = false;
152+
add_action(
153+
'heartbeat_nopriv_tick',
154+
function ( $response, $screen_id ) use ( &$action_called ) {
155+
$action_called = true;
156+
},
157+
10,
158+
2
159+
);
160+
161+
try {
162+
$this->_handleAjax( 'nopriv_heartbeat' );
163+
} catch ( WPAjaxDieContinueException $e ) {
164+
unset( $e );
165+
} catch ( WPAjaxDieStopException $e ) {
166+
unset( $e );
167+
}
168+
169+
$this->assertTrue( $action_called );
170+
}
171+
}

0 commit comments

Comments
 (0)