|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @group admin |
| 5 | + * @group misc |
| 6 | + * @covers ::set_screen_options |
| 7 | + */ |
| 8 | +class Tests_set_screen_options extends WP_UnitTestCase { |
| 9 | + |
| 10 | + /** |
| 11 | + * @ticket 65183 |
| 12 | + */ |
| 13 | + public function test_set_screen_options_no_data() { |
| 14 | + // Suppress doing_it_wrong if any. |
| 15 | + $result = set_screen_options(); |
| 16 | + $this->assertNull( $result, 'set_screen_options should return null when wp_screen_options is not set in $_POST.' ); |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * @ticket 65183 |
| 21 | + * @dataProvider data_set_screen_options |
| 22 | + * |
| 23 | + * Tests saving various screen options, including those that require filters. |
| 24 | + * |
| 25 | + * @param string $option The option name. |
| 26 | + * @param mixed $value The option value. |
| 27 | + * @param mixed $expected The expected stored value in user meta. |
| 28 | + */ |
| 29 | + public function test_set_screen_options( $option, $value, $expected ) { |
| 30 | + $user_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); |
| 31 | + wp_set_current_user( $user_id ); |
| 32 | + |
| 33 | + $_POST['wp_screen_options'] = array( |
| 34 | + 'option' => $option, |
| 35 | + 'value' => $value, |
| 36 | + ); |
| 37 | + |
| 38 | + // Mock request method. |
| 39 | + $_SERVER['REQUEST_METHOD'] = 'POST'; |
| 40 | + |
| 41 | + // Set referer and nonce. |
| 42 | + $_SERVER['HTTP_REFERER'] = admin_url( 'edit.php?pagenum=2' ); |
| 43 | + $_POST['screenoptionnonce'] = wp_create_nonce( 'screen-options-nonce' ); |
| 44 | + $_REQUEST['screenoptionnonce'] = $_POST['screenoptionnonce']; |
| 45 | + $_REQUEST['_wp_http_referer'] = $_SERVER['HTTP_REFERER']; |
| 46 | + |
| 47 | + if ( 'my_custom_page' === $option || 'layout_columns' === $option ) { |
| 48 | + add_filter( 'set-screen-option', array( $this, 'filter_set_screen_option_custom' ), 10, 3 ); |
| 49 | + } |
| 50 | + |
| 51 | + // Intercept redirect to prevent exit. |
| 52 | + add_filter( 'wp_redirect', array( $this, 'intercept_redirect' ) ); |
| 53 | + |
| 54 | + try { |
| 55 | + set_screen_options(); |
| 56 | + } catch ( Exception $e ) { |
| 57 | + if ( 'Redirected' === $e->getMessage() ) { |
| 58 | + // Success! |
| 59 | + } else { |
| 60 | + $this->fail( 'The function failed with message: ' . $e->getMessage() ); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + $meta = get_user_meta( $user_id, $option, true ); |
| 65 | + $this->assertEquals( $expected, $meta ); |
| 66 | + } |
| 67 | + |
| 68 | + public function intercept_redirect( $location ) { |
| 69 | + throw new Exception( 'Redirected' ); |
| 70 | + } |
| 71 | + |
| 72 | + public function filter_set_screen_option_custom( $status, $option, $value ) { |
| 73 | + if ( 'my_custom_page' === $option || 'layout_columns' === $option ) { |
| 74 | + return $value; |
| 75 | + } |
| 76 | + return $status; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Data provider for test_set_screen_options. |
| 81 | + * |
| 82 | + * @return array<string, array{ |
| 83 | + * option: string, |
| 84 | + * value: mixed, |
| 85 | + * expected: mixed, |
| 86 | + * }> |
| 87 | + */ |
| 88 | + public static function data_set_screen_options(): array { |
| 89 | + return array( |
| 90 | + 'edit_per_page' => array( |
| 91 | + 'option' => 'edit_per_page', |
| 92 | + 'value' => '20', |
| 93 | + 'expected' => 20, |
| 94 | + ), |
| 95 | + 'edit_per_page_invalid' => array( |
| 96 | + 'option' => 'edit_per_page', |
| 97 | + 'value' => '1000', // Max is 999. |
| 98 | + 'expected' => '', |
| 99 | + ), |
| 100 | + 'upload_per_page' => array( |
| 101 | + 'option' => 'upload_per_page', |
| 102 | + 'value' => '50', |
| 103 | + 'expected' => 50, |
| 104 | + ), |
| 105 | + 'custom_option_with_filter' => array( |
| 106 | + 'option' => 'my_custom_page', |
| 107 | + 'value' => '10', |
| 108 | + 'expected' => 10, |
| 109 | + ), |
| 110 | + 'layout_columns' => array( |
| 111 | + 'option' => 'layout_columns', |
| 112 | + 'value' => '2', |
| 113 | + 'expected' => 2, |
| 114 | + ), |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * @ticket 65183 |
| 120 | + * |
| 121 | + * Tests that set-screen-option filter is correctly applied for custom options. |
| 122 | + */ |
| 123 | + public function test_set_screen_options_with_filters() { |
| 124 | + $user_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); |
| 125 | + wp_set_current_user( $user_id ); |
| 126 | + |
| 127 | + $_POST['wp_screen_options'] = array( |
| 128 | + 'option' => 'custom_per_page', |
| 129 | + 'value' => '25', |
| 130 | + ); |
| 131 | + $_SERVER['HTTP_REFERER'] = admin_url( 'admin.php' ); |
| 132 | + $_SERVER['REQUEST_METHOD'] = 'POST'; |
| 133 | + $_REQUEST['screenoptionnonce'] = wp_create_nonce( 'screen-options-nonce' ); |
| 134 | + $_REQUEST['_wp_http_referer'] = $_SERVER['HTTP_REFERER']; |
| 135 | + |
| 136 | + // Intercept redirect to prevent exit. |
| 137 | + add_filter( 'wp_redirect', array( $this, 'intercept_redirect' ) ); |
| 138 | + |
| 139 | + // Filter ending with _page. |
| 140 | + add_filter( 'set-screen-option', array( $this, 'filter_set_screen_option' ), 10, 3 ); |
| 141 | + |
| 142 | + try { |
| 143 | + set_screen_options(); |
| 144 | + } catch ( Exception $e ) { |
| 145 | + if ( 'Redirected' === $e->getMessage() ) { |
| 146 | + // Success! |
| 147 | + } else { |
| 148 | + $this->fail( 'The function failed with message: ' . $e->getMessage() ); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + $this->assertEquals( 25, get_user_meta( $user_id, 'custom_per_page', true ) ); |
| 153 | + } |
| 154 | + |
| 155 | + public function filter_set_screen_option( $status, $option, $value ) { |
| 156 | + if ( 'custom_per_page' === $option ) { |
| 157 | + return $value; |
| 158 | + } |
| 159 | + return $status; |
| 160 | + } |
| 161 | +} |
0 commit comments