Skip to content

Commit fff4242

Browse files
Code Modernization: Rename the readonly() function to wp_readonly().
Since PHP 8.1, `readonly` is a reserved keyword and cannot be used as a function name. In order to avoid PHP parser errors, the `readonly()` function was extracted to a separate file and is now only included conditionally on PHP < 8.1. This commit also: * Moves the tests for the `__checked_selected_helper()` function and all the related functions to their own file. * Switches to named data providers. This makes the output when using the `--testdox` option more descriptive and is helpful when trying to debug which data set from a data provider failed the test. * Improves the tests in question to make them feature-complete and expand test coverage. Props jrf, ayeshrajans, haosun, knutsp, swissspidy, SergeyBiryukov. Fixes #53858. git-svn-id: https://develop.svn.wordpress.org/trunk@51586 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 385025b commit fff4242

File tree

6 files changed

+295
-51
lines changed

6 files changed

+295
-51
lines changed

phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
backupGlobals="false"
66
colors="true"
77
beStrictAboutTestsThatDoNotTestAnything="true"
8+
beStrictAboutOutputDuringTests="true"
89
>
910
<testsuites>
1011
<!-- Default test suite to run all tests. -->

src/wp-includes/general-template.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4813,17 +4813,27 @@ function disabled( $disabled, $current = true, $echo = true ) {
48134813
*
48144814
* Compares the first two arguments and if identical marks as readonly
48154815
*
4816-
* @since 4.9.0
4816+
* @since 5.9.0
48174817
*
48184818
* @param mixed $readonly One of the values to compare
48194819
* @param mixed $current (true) The other value to compare if not just true
48204820
* @param bool $echo Whether to echo or just return the string
48214821
* @return string HTML attribute or empty string
48224822
*/
4823-
function readonly( $readonly, $current = true, $echo = true ) {
4823+
function wp_readonly( $readonly, $current = true, $echo = true ) {
48244824
return __checked_selected_helper( $readonly, $current, $echo, 'readonly' );
48254825
}
48264826

4827+
/*
4828+
* Include a compat `readonly()` function on PHP < 8.1. Since PHP 8.1,
4829+
* `readonly` is a reserved keyword and cannot be used as a function name.
4830+
* In order to avoid PHP parser errors, this function was extracted
4831+
* to a separate file and is only included conditionally on PHP < 8.1.
4832+
*/
4833+
if ( PHP_VERSION_ID < 80100 ) {
4834+
require_once __DIR__ . '/php-compat/readonly.php';
4835+
}
4836+
48274837
/**
48284838
* Private helper function for checked, selected, disabled and readonly.
48294839
*
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Conditionally declares a `readonly()` function, which was renamed
4+
* to `wp_readonly()` in WordPress 5.9.0.
5+
*
6+
* In order to avoid PHP parser errors, this function was extracted
7+
* to this separate file and is only included conditionally on PHP 8.1.
8+
*
9+
* Including this file on PHP >= 8.1 results in a fatal error.
10+
*
11+
* @package WordPress
12+
* @since 5.9.0
13+
*/
14+
15+
/**
16+
* Outputs the HTML readonly attribute.
17+
*
18+
* Compares the first two arguments and if identical marks as readonly
19+
*
20+
* This function is deprecated, and cannot be used on PHP >= 8.1.
21+
*
22+
* @since 4.9.0
23+
* @deprecated 5.9.0 Use `wp_readonly` introduced in 5.9.0.
24+
*
25+
* @see wp_readonly()
26+
*
27+
* @param mixed $readonly One of the values to compare
28+
* @param mixed $current (true) The other value to compare if not just true
29+
* @param bool $echo Whether to echo or just return the string
30+
* @return string HTML attribute or empty string
31+
*/
32+
function readonly( $readonly, $current = true, $echo = true ) {
33+
_deprecated_function( __FUNCTION__, '5.9.0', 'wp_readonly()' );
34+
return wp_readonly( $readonly, $current, $echo );
35+
}

tests/phpunit/multisite.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
backupGlobals="false"
66
colors="true"
77
beStrictAboutTestsThatDoNotTestAnything="true"
8+
beStrictAboutOutputDuringTests="true"
89
>
910
<php>
1011
<const name="WP_TESTS_MULTISITE" value="1" />

tests/phpunit/tests/general/template.php

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -571,55 +571,6 @@ function test_get_template_part_passes_arguments_to_template() {
571571
get_template_part( 'template', 'part', array( 'foo' => 'baz' ) );
572572
}
573573

574-
/**
575-
* @ticket 9862
576-
* @ticket 51166
577-
* @dataProvider data_selected_and_checked_with_equal_values
578-
*
579-
* @covers ::selected
580-
* @covers ::checked
581-
*/
582-
function test_selected_and_checked_with_equal_values( $selected, $current ) {
583-
$this->assertSame( " selected='selected'", selected( $selected, $current, false ) );
584-
$this->assertSame( " checked='checked'", checked( $selected, $current, false ) );
585-
}
586-
587-
function data_selected_and_checked_with_equal_values() {
588-
return array(
589-
array( 'foo', 'foo' ),
590-
array( '1', 1 ),
591-
array( '1', true ),
592-
array( 1, 1 ),
593-
array( 1, true ),
594-
array( true, true ),
595-
array( '0', 0 ),
596-
array( 0, 0 ),
597-
array( '', false ),
598-
array( false, false ),
599-
);
600-
}
601-
602-
/**
603-
* @ticket 9862
604-
* @ticket 51166
605-
* @dataProvider data_selected_and_checked_with_non_equal_values
606-
*
607-
* @covers ::selected
608-
* @covers ::checked
609-
*/
610-
function test_selected_and_checked_with_non_equal_values( $selected, $current ) {
611-
$this->assertSame( '', selected( $selected, $current, false ) );
612-
$this->assertSame( '', checked( $selected, $current, false ) );
613-
}
614-
615-
function data_selected_and_checked_with_non_equal_values() {
616-
return array(
617-
array( '0', '' ),
618-
array( 0, '' ),
619-
array( 0, false ),
620-
);
621-
}
622-
623574
/**
624575
* @ticket 44183
625576
*
Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
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

Comments
 (0)