|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * A set of unit tests for the __checked_selected_helper() and associated functions in wp-includes/general-template.php. |
| 4 | + * |
| 5 | + * @group general |
| 6 | + */ |
| 7 | + |
| 8 | +class Tests_General_Template_CheckedSelectedHelper extends WP_UnitTestCase { |
| 9 | + |
| 10 | + /** |
| 11 | + * List of functions using the __checked_selected_helper() function. |
| 12 | + * |
| 13 | + * Doesn't list the conditionally available `readonly` function on purpose. |
| 14 | + * |
| 15 | + * @var array |
| 16 | + */ |
| 17 | + private $child_functions = array( |
| 18 | + 'selected' => true, |
| 19 | + 'checked' => true, |
| 20 | + 'disabled' => true, |
| 21 | + 'wp_readonly' => true, |
| 22 | + ); |
| 23 | + |
| 24 | + /** |
| 25 | + * Tests that the return value for selected() is as expected with equal values. |
| 26 | + * |
| 27 | + * @ticket 53858 |
| 28 | + * @covers ::selected |
| 29 | + */ |
| 30 | + public function test_selected_with_equal_values() { |
| 31 | + $this->assertSame( " selected='selected'", selected( 'foo', 'foo', false ) ); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Tests that the return value for checked() is as expected with equal values. |
| 36 | + * |
| 37 | + * @ticket 53858 |
| 38 | + * @covers ::checked |
| 39 | + */ |
| 40 | + public function test_checked_with_equal_values() { |
| 41 | + $this->assertSame( " checked='checked'", checked( 'foo', 'foo', false ) ); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Tests that the return value for disabled() is as expected with equal values. |
| 46 | + * |
| 47 | + * @ticket 53858 |
| 48 | + * @covers ::disabled |
| 49 | + */ |
| 50 | + public function test_disabled_with_equal_values() { |
| 51 | + $this->assertSame( " disabled='disabled'", disabled( 'foo', 'foo', false ) ); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Tests that the return value for readonly() is as expected with equal values. |
| 56 | + * |
| 57 | + * @ticket 53858 |
| 58 | + * @covers ::readonly |
| 59 | + */ |
| 60 | + public function test_readonly_with_equal_values() { |
| 61 | + if ( ! function_exists( 'readonly' ) ) { |
| 62 | + $this->markTestSkipped( 'readonly() function is not available on PHP 8.1' ); |
| 63 | + } |
| 64 | + |
| 65 | + $this->setExpectedDeprecated( 'readonly' ); |
| 66 | + |
| 67 | + // Call the function via a variable to prevent a parse error for this file on PHP 8.1. |
| 68 | + $fn = 'readonly'; |
| 69 | + $this->assertSame( " readonly='readonly'", $fn( 'foo', 'foo', false ) ); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Tests that the return value for wp_readonly() is as expected with equal values. |
| 74 | + * |
| 75 | + * @ticket 53858 |
| 76 | + * @covers ::wp_readonly |
| 77 | + */ |
| 78 | + public function test_wp_readonly_with_equal_values() { |
| 79 | + $this->assertSame( " readonly='readonly'", wp_readonly( 'foo', 'foo', false ) ); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @dataProvider data_equal_values |
| 84 | + * |
| 85 | + * @ticket 9862 |
| 86 | + * @ticket 51166 |
| 87 | + * @ticket 53858 |
| 88 | + * @covers ::__checked_selected_helper |
| 89 | + * |
| 90 | + * @param mixed $helper One of the values to compare. |
| 91 | + * @param mixed $current The other value to compare. |
| 92 | + */ |
| 93 | + public function test_checked_selected_helper_with_equal_values( $helper, $current ) { |
| 94 | + $this->assertSame( " test='test'", __checked_selected_helper( $helper, $current, false, 'test' ) ); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Data provider. |
| 99 | + * |
| 100 | + * @return array |
| 101 | + */ |
| 102 | + public function data_equal_values() { |
| 103 | + return array( |
| 104 | + 'same value, "foo"; 1: string; 2: string' => array( 'foo', 'foo' ), |
| 105 | + 'same value, 1; 1: string; 2: int' => array( '1', 1 ), |
| 106 | + 'same value, 1; 1: string; 2: float' => array( '1', 1.0 ), |
| 107 | + 'same value, 1; 1: string; 2: bool true' => array( '1', true ), |
| 108 | + 'same value, 1; 1: int; 2: int' => array( 1, 1 ), |
| 109 | + 'same value, 1; 1: int; 2: float' => array( 1, 1.0 ), |
| 110 | + 'same value, 1; 1: int; 2: bool true' => array( 1, true ), |
| 111 | + 'same value, 1; 1: float; 2: bool true' => array( 1.0, true ), |
| 112 | + 'same value, 1; 1: bool true; 2: bool true' => array( true, true ), |
| 113 | + 'same value, 1; 1: float 1.0; 2: float calculation 1.0' => array( 1.0, 3 / 3 ), |
| 114 | + 'same value, 0; 1: string; 2: int' => array( '0', 0 ), |
| 115 | + 'same value, 0; 1: string; 2: float' => array( '0', 0.0 ), |
| 116 | + 'same value, 0; 1: int; 2: int' => array( 0, 0 ), |
| 117 | + 'same value, 0; 1: int; 2: float' => array( 0, 0.0 ), |
| 118 | + 'same value, empty string; 1: string; 2: string' => array( '', '' ), |
| 119 | + 'same value, empty string; 1: empty string; 2: bool false' => array( '', false ), |
| 120 | + 'same value, empty string; 1: bool false; 2: bool false' => array( false, false ), |
| 121 | + 'same value, empty string; 1: empty string; 2: null' => array( '', null ), |
| 122 | + 'same value, empty string; 1: bool false; 2: null' => array( false, null ), |
| 123 | + 'same value, null; 1: null; 2: null' => array( null, null ), |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * @dataProvider data_non_equal_values |
| 129 | + * |
| 130 | + * @ticket 9862 |
| 131 | + * @ticket 51166 |
| 132 | + * @ticket 53858 |
| 133 | + * @covers ::__checked_selected_helper |
| 134 | + * |
| 135 | + * @param mixed $helper One of the values to compare. |
| 136 | + * @param mixed $current The other value to compare. |
| 137 | + */ |
| 138 | + public function test_checked_selected_helper_with_non_equal_values( $helper, $current ) { |
| 139 | + $this->assertSame( '', __checked_selected_helper( $helper, $current, false, 'test' ) ); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Data provider. |
| 144 | + * |
| 145 | + * @return array |
| 146 | + */ |
| 147 | + public function data_non_equal_values() { |
| 148 | + return array( |
| 149 | + '1: string foo; 2: string bar' => array( 'foo', 'bar' ), |
| 150 | + '1: string 0; 2: empty string' => array( '0', '' ), |
| 151 | + '1: string 0; 2: null' => array( '0', null ), |
| 152 | + '1: int 0; 2: empty string' => array( 0, '' ), |
| 153 | + '1: int 0; 2: bool true' => array( 0, true ), |
| 154 | + '1: int 0; 2: bool false' => array( 0, false ), |
| 155 | + '1: int 0; 2: null' => array( 0, null ), |
| 156 | + '1: float 0; 2: empty string' => array( 0.0, '' ), |
| 157 | + '1: float 0; 2: bool true' => array( 0.0, true ), |
| 158 | + '1: float 0; 2: bool false' => array( 0.0, false ), |
| 159 | + '1: float 0; 2: null' => array( 0.0, null ), |
| 160 | + '1: null; 2: bool true' => array( null, true ), |
| 161 | + '1: null 0; 2: string "foo"' => array( null, 'foo' ), |
| 162 | + '1: int 1; 2: float 1.5' => array( 1, 1.5 ), |
| 163 | + ); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Tests that the `$echo` parameter is handled correctly and that even when the output is echoed out, |
| 168 | + * the text is also returned. |
| 169 | + * |
| 170 | + * @ticket 53858 |
| 171 | + * @covers ::__checked_selected_helper |
| 172 | + */ |
| 173 | + public function test_checked_selected_helper_echoes_result_by_default() { |
| 174 | + $expected = " disabled='disabled'"; |
| 175 | + $this->expectOutputString( $expected ); |
| 176 | + $this->assertSame( $expected, disabled( 'foo', 'foo' ) ); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Tests that the function compares against `true` when the second parameter is not passed. |
| 181 | + * |
| 182 | + * @dataProvider data_checked_selected_helper_default_value_for_second_parameter |
| 183 | + * |
| 184 | + * @ticket 53858 |
| 185 | + * @covers ::__checked_selected_helper |
| 186 | + * @covers ::selected |
| 187 | + * @covers ::checked |
| 188 | + * @covers ::disabled |
| 189 | + * @covers ::wp_readonly |
| 190 | + * |
| 191 | + * @param mixed $input Input value |
| 192 | + * @param mixed $expect_output Optional. Whether output is expected. Defaults to false. |
| 193 | + */ |
| 194 | + public function test_checked_selected_helper_default_value_for_second_parameter( $input, $expect_output = false ) { |
| 195 | + $fn = array_rand( $this->child_functions ); |
| 196 | + $expected = ''; |
| 197 | + |
| 198 | + if ( false !== $expect_output ) { |
| 199 | + $expected = " {$fn}='{$fn}'"; |
| 200 | + if ( 'wp_readonly' === $fn ) { |
| 201 | + // Account for the function name not matching the expected output string. |
| 202 | + $expected = " readonly='readonly'"; |
| 203 | + } |
| 204 | + |
| 205 | + // Only set output expectation when output is expected, so the test will fail on unexpected output. |
| 206 | + $this->expectOutputString( $expected ); |
| 207 | + } |
| 208 | + |
| 209 | + // Function will always return the value, even when echoing it out. |
| 210 | + $this->assertSame( $expected, $fn( $input ) ); |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * Data provider. |
| 215 | + * |
| 216 | + * @return array |
| 217 | + */ |
| 218 | + public function data_checked_selected_helper_default_value_for_second_parameter() { |
| 219 | + return array( |
| 220 | + 'truthy; boolean true' => array( |
| 221 | + 'input' => true, |
| 222 | + 'expect_output' => true, |
| 223 | + ), |
| 224 | + 'truthy; int 1' => array( |
| 225 | + 'input' => 1, |
| 226 | + 'expect_output' => true, |
| 227 | + ), |
| 228 | + 'truthy; string 1' => array( |
| 229 | + 'input' => '1', |
| 230 | + 'expect_output' => true, |
| 231 | + ), |
| 232 | + 'truthy, but not equal to true' => array( |
| 233 | + 'input' => 'foo', |
| 234 | + ), |
| 235 | + 'falsy; null' => array( |
| 236 | + 'input' => null, |
| 237 | + ), |
| 238 | + 'falsy; bool false' => array( |
| 239 | + 'input' => false, |
| 240 | + ), |
| 241 | + 'falsy; int 0' => array( |
| 242 | + 'input' => 0, |
| 243 | + ), |
| 244 | + ); |
| 245 | + } |
| 246 | +} |
0 commit comments