|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Test wp_refresh_post_nonces(). |
| 4 | + * |
| 5 | + * @group admin |
| 6 | + * @group misc |
| 7 | + * |
| 8 | + * @covers ::wp_refresh_post_nonces |
| 9 | + */ |
| 10 | +class Tests_Admin_Includes_Misc_WpRefreshPostNonces extends WP_UnitTestCase { |
| 11 | + |
| 12 | + /** |
| 13 | + * Post ID. |
| 14 | + * |
| 15 | + * @var int |
| 16 | + */ |
| 17 | + protected static $post_id; |
| 18 | + |
| 19 | + /** |
| 20 | + * User ID. |
| 21 | + * |
| 22 | + * @var int |
| 23 | + */ |
| 24 | + protected static $user_id; |
| 25 | + |
| 26 | + /** |
| 27 | + * Set up before class. |
| 28 | + */ |
| 29 | + public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { |
| 30 | + self::$user_id = $factory->user->create( array( 'role' => 'editor' ) ); |
| 31 | + self::$post_id = $factory->post->create( array( 'post_author' => self::$user_id ) ); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Tests wp_refresh_post_nonces() with missing data. |
| 36 | + * |
| 37 | + * @ticket 65197 |
| 38 | + */ |
| 39 | + public function test_wp_refresh_post_nonces_missing_data() { |
| 40 | + $response = array( 'existing' => 'data' ); |
| 41 | + $data = array(); |
| 42 | + |
| 43 | + $result = wp_refresh_post_nonces( $response, $data, 'edit-post' ); |
| 44 | + |
| 45 | + $this->assertSame( $response, $result, 'Response should remain unchanged if wp-refresh-post-nonces is missing.' ); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Tests wp_refresh_post_nonces() with invalid post ID. |
| 50 | + * |
| 51 | + * @ticket 65197 |
| 52 | + * |
| 53 | + * @dataProvider data_wp_refresh_post_nonces_invalid_post_id |
| 54 | + * |
| 55 | + * @param mixed $post_id Invalid post ID. |
| 56 | + * @return void |
| 57 | + */ |
| 58 | + public function test_wp_refresh_post_nonces_invalid_post_id( $post_id ) { |
| 59 | + $response = array(); |
| 60 | + $data = array( |
| 61 | + 'wp-refresh-post-nonces' => array( |
| 62 | + 'post_id' => $post_id, |
| 63 | + ), |
| 64 | + ); |
| 65 | + |
| 66 | + $result = wp_refresh_post_nonces( $response, $data, 'edit-post' ); |
| 67 | + |
| 68 | + $this->assertArrayHasKey( 'wp-refresh-post-nonces', $result ); |
| 69 | + $this->assertSame( array( 'check' => 1 ), $result['wp-refresh-post-nonces'], 'Should return check key for invalid post ID.' ); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Data provider for test_wp_refresh_post_nonces_invalid_post_id. |
| 74 | + * |
| 75 | + * @return array<string, array{ |
| 76 | + * post_id: mixed, |
| 77 | + * }> |
| 78 | + */ |
| 79 | + public function data_wp_refresh_post_nonces_invalid_post_id(): array { |
| 80 | + return array( |
| 81 | + 'zero' => array( 'post_id' => 0 ), |
| 82 | + 'string zero' => array( 'post_id' => '0' ), |
| 83 | + 'non-numeric' => array( 'post_id' => 'abc' ), |
| 84 | + 'negative' => array( 'post_id' => -1 ), |
| 85 | + 'missing post_id' => array( 'post_id' => null ), |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Tests wp_refresh_post_nonces() when the user cannot edit the post. |
| 91 | + * |
| 92 | + * @ticket 65197 |
| 93 | + */ |
| 94 | + public function test_wp_refresh_post_nonces_user_cannot_edit() { |
| 95 | + $other_user_id = self::factory()->user->create( array( 'role' => 'subscriber' ) ); |
| 96 | + wp_set_current_user( $other_user_id ); |
| 97 | + |
| 98 | + $response = array(); |
| 99 | + $data = array( |
| 100 | + 'wp-refresh-post-nonces' => array( |
| 101 | + 'post_id' => self::$post_id, |
| 102 | + ), |
| 103 | + ); |
| 104 | + |
| 105 | + $result = wp_refresh_post_nonces( $response, $data, 'edit-post' ); |
| 106 | + |
| 107 | + $this->assertArrayHasKey( 'wp-refresh-post-nonces', $result ); |
| 108 | + $this->assertSame( array( 'check' => 1 ), $result['wp-refresh-post-nonces'], 'Should return check key if user cannot edit post.' ); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Tests wp_refresh_post_nonces() with successful refresh. |
| 113 | + * |
| 114 | + * @ticket 65197 |
| 115 | + */ |
| 116 | + public function test_wp_refresh_post_nonces_success() { |
| 117 | + wp_set_current_user( self::$user_id ); |
| 118 | + |
| 119 | + $response = array(); |
| 120 | + $data = array( |
| 121 | + 'wp-refresh-post-nonces' => array( |
| 122 | + 'post_id' => self::$post_id, |
| 123 | + ), |
| 124 | + ); |
| 125 | + |
| 126 | + $result = wp_refresh_post_nonces( $response, $data, 'edit-post' ); |
| 127 | + |
| 128 | + $this->assertArrayHasKey( 'wp-refresh-post-nonces', $result ); |
| 129 | + $this->assertArrayHasKey( 'replace', $result['wp-refresh-post-nonces'] ); |
| 130 | + |
| 131 | + $replace = $result['wp-refresh-post-nonces']['replace']; |
| 132 | + |
| 133 | + $this->assertArrayHasKey( 'getpermalinknonce', $replace ); |
| 134 | + $this->assertArrayHasKey( 'samplepermalinknonce', $replace ); |
| 135 | + $this->assertArrayHasKey( 'closedpostboxesnonce', $replace ); |
| 136 | + $this->assertArrayHasKey( '_ajax_linking_nonce', $replace ); |
| 137 | + $this->assertArrayHasKey( '_wpnonce', $replace ); |
| 138 | + |
| 139 | + $this->assertSame( 1, wp_verify_nonce( $replace['getpermalinknonce'], 'getpermalink' ), 'getpermalink nonce should be valid.' ); |
| 140 | + $this->assertSame( 1, wp_verify_nonce( $replace['samplepermalinknonce'], 'samplepermalink' ), 'samplepermalink nonce should be valid.' ); |
| 141 | + $this->assertSame( 1, wp_verify_nonce( $replace['closedpostboxesnonce'], 'closedpostboxes' ), 'closedpostboxes nonce should be valid.' ); |
| 142 | + $this->assertSame( 1, wp_verify_nonce( $replace['_ajax_linking_nonce'], 'internal-linking' ), 'internal-linking nonce should be valid.' ); |
| 143 | + $this->assertSame( 1, wp_verify_nonce( $replace['_wpnonce'], 'update-post_' . self::$post_id ), 'update-post nonce should be valid.' ); |
| 144 | + } |
| 145 | +} |
0 commit comments