Skip to content

Commit 83d68ce

Browse files
Tests: Add unit tests for wp_refresh_heartbeat_nonces().
Follow-up to [44275]. Props pbearne. Fixes #65199. git-svn-id: https://develop.svn.wordpress.org/trunk@62356 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 4f7d6c8 commit 83d68ce

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/**
4+
* @group admin
5+
*
6+
* @covers ::wp_refresh_heartbeat_nonces
7+
*/
8+
class Tests_Admin_Includes_Misc_WpRefreshHeartbeatNonces_Test extends WP_UnitTestCase {
9+
10+
/**
11+
* Tests that wp_refresh_heartbeat_nonces() correctly adds nonces to the response.
12+
*
13+
* @ticket 65199
14+
*/
15+
public function test_wp_refresh_heartbeat_nonces() {
16+
$response = array( 'some_data' => 'value' );
17+
18+
$result = wp_refresh_heartbeat_nonces( $response );
19+
20+
$this->assertArrayHasKey( 'rest_nonce', $result, 'The response should contain the rest_nonce.' );
21+
$this->assertArrayHasKey( 'heartbeat_nonce', $result, 'The response should contain the heartbeat_nonce.' );
22+
$this->assertSame( 'value', $result['some_data'], 'Existing data in the response should be preserved.' );
23+
24+
$this->assertNotFalse( wp_verify_nonce( $result['rest_nonce'], 'wp_rest' ), 'The rest_nonce should be valid for "wp_rest".' );
25+
$this->assertNotFalse( wp_verify_nonce( $result['heartbeat_nonce'], 'heartbeat-nonce' ), 'The heartbeat_nonce should be valid for "heartbeat-nonce".' );
26+
}
27+
28+
/**
29+
* Tests that wp_refresh_heartbeat_nonces() overwrites existing nonces if they are already present.
30+
*
31+
* @ticket 65199
32+
*/
33+
public function test_wp_refresh_heartbeat_nonces_overwrites_existing() {
34+
$response = array(
35+
'rest_nonce' => 'old_rest_nonce',
36+
'heartbeat_nonce' => 'old_heartbeat_nonce',
37+
);
38+
39+
$result = wp_refresh_heartbeat_nonces( $response );
40+
41+
$this->assertNotEquals( 'old_rest_nonce', $result['rest_nonce'], 'The rest_nonce should be updated.' );
42+
$this->assertNotEquals( 'old_heartbeat_nonce', $result['heartbeat_nonce'], 'The heartbeat_nonce should be updated.' );
43+
44+
$this->assertNotFalse( wp_verify_nonce( $result['rest_nonce'], 'wp_rest' ), 'The rest_nonce should be valid for "wp_rest".' );
45+
$this->assertNotFalse( wp_verify_nonce( $result['heartbeat_nonce'], 'heartbeat-nonce' ), 'The heartbeat_nonce should be valid for "heartbeat-nonce".' );
46+
}
47+
}

0 commit comments

Comments
 (0)