|
| 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