Skip to content

Commit 662315d

Browse files
committed
Merge branch 'trunk' of https://github.com/WordPress/wordpress-develop into trac-64404
2 parents 063c21e + 44f1517 commit 662315d

File tree

6 files changed

+90
-23
lines changed

6 files changed

+90
-23
lines changed

src/wp-includes/abilities-api/class-wp-ability.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,15 @@ protected function prepare_properties( array $args ): array {
277277
);
278278
}
279279

280-
if ( empty( $args['execute_callback'] ) || ! is_callable( $args['execute_callback'] ) ) {
280+
// If we are not overriding `ability_class` parameter during instantiation, then we need to validate the execute_callback.
281+
if ( get_class( $this ) === self::class && ( empty( $args['execute_callback'] ) || ! is_callable( $args['execute_callback'] ) ) ) {
281282
throw new InvalidArgumentException(
282283
__( 'The ability properties must contain a valid `execute_callback` function.' )
283284
);
284285
}
285286

286-
if ( empty( $args['permission_callback'] ) || ! is_callable( $args['permission_callback'] ) ) {
287+
// If we are not overriding `ability_class` parameter during instantiation, then we need to validate the permission_callback.
288+
if ( get_class( $this ) === self::class && ( empty( $args['permission_callback'] ) || ! is_callable( $args['permission_callback'] ) ) ) {
287289
throw new InvalidArgumentException(
288290
__( 'The ability properties must provide a valid `permission_callback` function.' )
289291
);

src/wp-includes/class-wp-styles.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,15 @@ class WP_Styles extends WP_Dependencies {
118118
*/
119119
public function __construct() {
120120
if (
121-
function_exists( 'is_admin' ) && ! is_admin()
122-
&&
123-
function_exists( 'current_theme_supports' ) && ! current_theme_supports( 'html5', 'style' )
121+
(
122+
function_exists( 'is_admin' ) &&
123+
! is_admin()
124+
)
125+
&&
126+
(
127+
function_exists( 'current_theme_supports' ) &&
128+
! current_theme_supports( 'html5', 'style' )
129+
)
124130
) {
125131
$this->type_attr = " type='text/css'";
126132
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Test custom ability class that extends WP_Ability.
4+
*
5+
* This class overrides do_execute() and check_permissions() directly,
6+
* allowing registration without execute_callback or permission_callback.
7+
*/
8+
class Tests_Custom_Ability_Class extends WP_Ability {
9+
10+
/**
11+
* Custom execute implementation that multiplies instead of adds.
12+
*
13+
* @param mixed $input The input data.
14+
* @return int The result of multiplying a and b.
15+
*/
16+
protected function do_execute( $input = null ) {
17+
return $input['a'] * $input['b'];
18+
}
19+
20+
/**
21+
* Custom permission check that always returns true.
22+
*
23+
* @param mixed $input The input data.
24+
* @return bool Always true.
25+
*/
26+
public function check_permissions( $input = null ) {
27+
return true;
28+
}
29+
}

tests/phpunit/tests/abilities-api/wpAbilitiesRegistry.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class Tests_Abilities_API_WpAbilitiesRegistry extends WP_UnitTestCase {
2323
* Set up each test method.
2424
*/
2525
public function set_up(): void {
26+
require_once DIR_TESTDATA . '/../includes/class-tests-custom-ability-class.php';
27+
2628
parent::set_up();
2729

2830
$this->registry = new WP_Abilities_Registry();
@@ -257,6 +259,36 @@ public function test_register_incorrect_execute_callback_type() {
257259
$this->assertNull( $result );
258260
}
259261

262+
/**
263+
* Should allow ability registration with custom ability_class that overrides do_execute.
264+
*
265+
* @ticket 64407
266+
*
267+
* @covers WP_Abilities_Registry::register
268+
* @covers WP_Ability::prepare_properties
269+
*/
270+
public function test_register_with_custom_ability_class_without_execute_callback() {
271+
// Remove execute_callback and permission_callback since the custom class provides its own implementation.
272+
unset( self::$test_ability_args['execute_callback'] );
273+
unset( self::$test_ability_args['permission_callback'] );
274+
275+
self::$test_ability_args['ability_class'] = 'Tests_Custom_Ability_Class';
276+
277+
$result = $this->registry->register( self::$test_ability_name, self::$test_ability_args );
278+
279+
$this->assertInstanceOf( WP_Ability::class, $result, 'Should return a WP_Ability instance.' );
280+
$this->assertInstanceOf( Tests_Custom_Ability_Class::class, $result, 'Should return an instance of the custom class.' );
281+
282+
// Verify the custom execute method works.
283+
$execute_result = $result->execute(
284+
array(
285+
'a' => 5,
286+
'b' => 3,
287+
)
288+
);
289+
$this->assertSame( 15, $execute_result, 'Custom do_execute should multiply instead of add.' );
290+
}
291+
260292
/**
261293
* Should reject ability registration without an execute callback.
262294
*

tests/phpunit/tests/dependencies/styles.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function test_wp_enqueue_style() {
6868
$expected .= "<link rel='stylesheet' id='no-deps-null-version-css' href='http://example.com' type='text/css' media='all' />\n";
6969
$expected .= "<link rel='stylesheet' id='no-deps-null-version-print-media-css' href='http://example.com' type='text/css' media='print' />\n";
7070

71-
$this->assertSame( $expected, get_echo( 'wp_print_styles' ) );
71+
$this->assertEqualHTML( $expected, get_echo( 'wp_print_styles' ) );
7272

7373
// No styles left to print.
7474
$this->assertSame( '', get_echo( 'wp_print_styles' ) );
@@ -88,7 +88,7 @@ public function test_wp_enqueue_style_with_html5_support_does_not_contain_type_a
8888
$ver = get_bloginfo( 'version' );
8989
$expected = "<link rel='stylesheet' id='no-deps-no-version-css' href='http://example.com?ver=$ver' media='all' />\n";
9090

91-
$this->assertSame( $expected, get_echo( 'wp_print_styles' ) );
91+
$this->assertEqualHTML( $expected, get_echo( 'wp_print_styles' ) );
9292
}
9393

9494
/**
@@ -103,7 +103,7 @@ public function test_awkward_handles_are_supported_consistently( $handle ) {
103103

104104
$expected = "<link rel='stylesheet' id='$handle-css' href='http://example.com' type='text/css' media='all' />\n";
105105

106-
$this->assertSame( $expected, get_echo( 'wp_print_styles' ) );
106+
$this->assertEqualHTML( $expected, get_echo( 'wp_print_styles' ) );
107107
}
108108

109109
/**
@@ -157,7 +157,7 @@ public function test_protocols() {
157157
$expected .= "<link rel='stylesheet' id='reset-css-ftp-css' href='{$wp_styles->base_url}ftp://yui.yahooapis.com/2.8.1/build/reset/reset-min.css?ver=$ver' type='text/css' media='all' />\n";
158158

159159
// Go!
160-
$this->assertSame( $expected, get_echo( 'wp_print_styles' ) );
160+
$this->assertEqualHTML( $expected, get_echo( 'wp_print_styles' ) );
161161

162162
// No styles left to print.
163163
$this->assertSame( '', get_echo( 'wp_print_styles' ) );
@@ -186,8 +186,7 @@ public function test_inline_styles() {
186186
wp_enqueue_style( 'handle', 'http://example.com', array(), 1 );
187187
wp_add_inline_style( 'handle', $style );
188188

189-
// No styles left to print.
190-
$this->assertSame( $expected, get_echo( 'wp_print_styles' ) );
189+
$this->assertEqualHTML( $expected, get_echo( 'wp_print_styles' ) );
191190
}
192191

193192
/**
@@ -215,7 +214,7 @@ public function test_inline_styles_concat() {
215214
wp_add_inline_style( 'handle', $style );
216215

217216
wp_print_styles();
218-
$this->assertSame( $expected, $wp_styles->print_html );
217+
$this->assertEqualHTML( $expected, $wp_styles->print_html );
219218
}
220219

221220
/**
@@ -233,7 +232,7 @@ public function test_inline_styles_concat() {
233232
* @param string $expected Expected result.
234233
*/
235234
public function test_normalize_relative_css_links( $css, $expected ) {
236-
$this->assertSame(
235+
$this->assertEqualHTML(
237236
$expected,
238237
_wp_normalize_relative_css_links( $css, site_url( 'wp-content/themes/test/style.css' ) )
239238
);
@@ -311,8 +310,7 @@ public function test_multiple_inline_styles() {
311310
wp_add_inline_style( 'handle', $style1 );
312311
wp_add_inline_style( 'handle', $style2 );
313312

314-
// No styles left to print.
315-
$this->assertSame( $expected, get_echo( 'wp_print_styles' ) );
313+
$this->assertEqualHTML( $expected, get_echo( 'wp_print_styles' ) );
316314
}
317315

318316
/**
@@ -337,7 +335,7 @@ public function test_plugin_doing_inline_styles_wrong() {
337335

338336
wp_add_inline_style( 'handle', "<style>{$style}</style>" );
339337

340-
$this->assertSame( $expected, get_echo( 'wp_print_styles' ) );
338+
$this->assertEqualHTML( $expected, get_echo( 'wp_print_styles' ) );
341339
}
342340

343341
/**
@@ -351,7 +349,7 @@ public function test_unnecessary_style_tags() {
351349

352350
wp_enqueue_style( 'handle', 'http://example.com', array(), 1 );
353351

354-
$this->assertSame( $expected, get_echo( 'wp_print_styles' ) );
352+
$this->assertEqualHTML( $expected, get_echo( 'wp_print_styles' ) );
355353
}
356354

357355
/**
@@ -361,12 +359,12 @@ public function test_unnecessary_style_tags() {
361359
* @expectedDeprecated WP_Dependencies->add_data()
362360
*/
363361
public function test_conditional_inline_styles_are_also_conditional() {
364-
$expected = '';
365362
wp_enqueue_style( 'handle', 'http://example.com', array(), 1 );
366363
wp_style_add_data( 'handle', 'conditional', 'IE' );
367364
wp_add_inline_style( 'handle', 'a { color: blue; }' );
368365

369-
$this->assertSameIgnoreEOL( $expected, get_echo( 'wp_print_styles' ) );
366+
// Conditional styles are disabled.
367+
$this->assertSame( '', get_echo( 'wp_print_styles' ) );
370368
}
371369

372370
/**
@@ -399,7 +397,7 @@ public function test_wp_add_inline_style_for_handle_without_source() {
399397
wp_enqueue_style( 'handle-three' );
400398
wp_add_inline_style( 'handle-three', $style );
401399

402-
$this->assertSame( $expected, get_echo( 'wp_print_styles' ) );
400+
$this->assertEqualHTML( $expected, get_echo( 'wp_print_styles' ) );
403401
}
404402

405403
/**

tests/phpunit/tests/script-modules/wpScriptModules.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,7 @@ function ( $data ) {
12341234
</script>
12351235
12361236
HTML;
1237-
$this->assertSame( $expected, $actual );
1237+
$this->assertEqualHTML( $expected, $actual );
12381238
}
12391239

12401240
/**
@@ -1259,7 +1259,7 @@ function ( $data ) {
12591259
</script>
12601260
12611261
HTML;
1262-
$this->assertSame( $expected, $actual );
1262+
$this->assertEqualHTML( $expected, $actual );
12631263
}
12641264

12651265
/**
@@ -1332,7 +1332,7 @@ function ( $data ) use ( $input ) {
13321332
13331333
HTML;
13341334

1335-
$this->assertSame( $expected, $actual );
1335+
$this->assertEqualHTML( $expected, $actual );
13361336
}
13371337

13381338
/**

0 commit comments

Comments
 (0)