Skip to content

Commit f4d2f29

Browse files
Tests: Get rid of extra remove_filter()/remove_action() calls in Abilities API tests.
These are redundant: `WP_UnitTestCase_Base::tear_down()` runs `::_restore_hooks()`, which restores `$wp_filter`/`$wp_actions` to a pre-test baseline, so hooks added during a test are removed automatically. Follow-up to [61032]. Props mohamedahamed, gziolo, westonruter, SergeyBiryukov. Fixes #65301. git-svn-id: https://develop.svn.wordpress.org/trunk@62405 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 21fb62d commit f4d2f29

5 files changed

Lines changed: 90 additions & 89 deletions

File tree

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

Lines changed: 74 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -568,18 +568,19 @@ public function test_before_execute_ability_action() {
568568
)
569569
);
570570

571-
$callback = static function ( $ability_name, $input ) use ( &$action_ability_name, &$action_input ) {
572-
$action_ability_name = $ability_name;
573-
$action_input = $input;
574-
};
575-
576-
add_action( 'wp_before_execute_ability', $callback, 10, 2 );
571+
add_action(
572+
'wp_before_execute_ability',
573+
static function ( $ability_name, $input ) use ( &$action_ability_name, &$action_input ) {
574+
$action_ability_name = $ability_name;
575+
$action_input = $input;
576+
},
577+
10,
578+
2
579+
);
577580

578581
$ability = new WP_Ability( self::$test_ability_name, $args );
579582
$result = $ability->execute( 5 );
580583

581-
remove_action( 'wp_before_execute_ability', $callback );
582-
583584
$this->assertSame( self::$test_ability_name, $action_ability_name, 'Action should receive correct ability name' );
584585
$this->assertSame( 5, $action_input, 'Action should receive correct input' );
585586
$this->assertSame( 10, $result, 'Ability should execute correctly' );
@@ -603,18 +604,19 @@ public function test_before_execute_ability_action_no_input() {
603604
)
604605
);
605606

606-
$callback = static function ( $ability_name, $input ) use ( &$action_ability_name, &$action_input ) {
607-
$action_ability_name = $ability_name;
608-
$action_input = $input;
609-
};
610-
611-
add_action( 'wp_before_execute_ability', $callback, 10, 2 );
607+
add_action(
608+
'wp_before_execute_ability',
609+
static function ( $ability_name, $input ) use ( &$action_ability_name, &$action_input ) {
610+
$action_ability_name = $ability_name;
611+
$action_input = $input;
612+
},
613+
10,
614+
2
615+
);
612616

613617
$ability = new WP_Ability( self::$test_ability_name, $args );
614618
$result = $ability->execute();
615619

616-
remove_action( 'wp_before_execute_ability', $callback );
617-
618620
$this->assertSame( self::$test_ability_name, $action_ability_name, 'Action should receive correct ability name' );
619621
$this->assertNull( $action_input, 'Action should receive null input when no input provided' );
620622
$this->assertSame( 42, $result, 'Ability should execute correctly' );
@@ -644,19 +646,20 @@ public function test_after_execute_ability_action() {
644646
)
645647
);
646648

647-
$callback = static function ( $ability_name, $input, $result ) use ( &$action_ability_name, &$action_input, &$action_result ) {
648-
$action_ability_name = $ability_name;
649-
$action_input = $input;
650-
$action_result = $result;
651-
};
652-
653-
add_action( 'wp_after_execute_ability', $callback, 10, 3 );
649+
add_action(
650+
'wp_after_execute_ability',
651+
static function ( $ability_name, $input, $result ) use ( &$action_ability_name, &$action_input, &$action_result ) {
652+
$action_ability_name = $ability_name;
653+
$action_input = $input;
654+
$action_result = $result;
655+
},
656+
10,
657+
3
658+
);
654659

655660
$ability = new WP_Ability( self::$test_ability_name, $args );
656661
$result = $ability->execute( 7 );
657662

658-
remove_action( 'wp_after_execute_ability', $callback );
659-
660663
$this->assertSame( self::$test_ability_name, $action_ability_name, 'Action should receive correct ability name' );
661664
$this->assertSame( 7, $action_input, 'Action should receive correct input' );
662665
$this->assertSame( 21, $action_result, 'Action should receive correct result' );
@@ -683,19 +686,20 @@ public function test_after_execute_ability_action_no_input() {
683686
)
684687
);
685688

686-
$callback = static function ( $ability_name, $input, $result ) use ( &$action_ability_name, &$action_input, &$action_result ) {
687-
$action_ability_name = $ability_name;
688-
$action_input = $input;
689-
$action_result = $result;
690-
};
691-
692-
add_action( 'wp_after_execute_ability', $callback, 10, 3 );
689+
add_action(
690+
'wp_after_execute_ability',
691+
static function ( $ability_name, $input, $result ) use ( &$action_ability_name, &$action_input, &$action_result ) {
692+
$action_ability_name = $ability_name;
693+
$action_input = $input;
694+
$action_result = $result;
695+
},
696+
10,
697+
3
698+
);
693699

694700
$ability = new WP_Ability( self::$test_ability_name, $args );
695701
$result = $ability->execute();
696702

697-
remove_action( 'wp_after_execute_ability', $callback );
698-
699703
$this->assertSame( self::$test_ability_name, $action_ability_name, 'Action should receive correct ability name' );
700704
$this->assertNull( $action_input, 'Action should receive null input when no input provided' );
701705
$this->assertSame( 'test-result', $action_result, 'Action should receive correct result' );
@@ -720,23 +724,23 @@ public function test_actions_not_fired_on_permission_failure() {
720724
)
721725
);
722726

723-
$before_callback = static function () use ( &$before_action_fired ) {
724-
$before_action_fired = true;
725-
};
726-
727-
$after_callback = static function () use ( &$after_action_fired ) {
728-
$after_action_fired = true;
729-
};
727+
add_action(
728+
'wp_before_execute_ability',
729+
static function () use ( &$before_action_fired ) {
730+
$before_action_fired = true;
731+
}
732+
);
730733

731-
add_action( 'wp_before_execute_ability', $before_callback );
732-
add_action( 'wp_after_execute_ability', $after_callback );
734+
add_action(
735+
'wp_after_execute_ability',
736+
static function () use ( &$after_action_fired ) {
737+
$after_action_fired = true;
738+
}
739+
);
733740

734741
$ability = new WP_Ability( self::$test_ability_name, $args );
735742
$result = $ability->execute();
736743

737-
remove_action( 'wp_before_execute_ability', $before_callback );
738-
remove_action( 'wp_after_execute_ability', $after_callback );
739-
740744
$this->assertFalse( $before_action_fired, 'before_execute_ability action should not be fired on permission failure' );
741745
$this->assertFalse( $after_action_fired, 'after_execute_ability action should not be fired on permission failure' );
742746
$this->assertInstanceOf( WP_Error::class, $result, 'Should return WP_Error on permission failure' );
@@ -760,23 +764,23 @@ public function test_after_action_not_fired_on_execution_error() {
760764
)
761765
);
762766

763-
$before_callback = static function () use ( &$before_action_fired ) {
764-
$before_action_fired = true;
765-
};
766-
767-
$after_callback = static function () use ( &$after_action_fired ) {
768-
$after_action_fired = true;
769-
};
767+
add_action(
768+
'wp_before_execute_ability',
769+
static function () use ( &$before_action_fired ) {
770+
$before_action_fired = true;
771+
}
772+
);
770773

771-
add_action( 'wp_before_execute_ability', $before_callback );
772-
add_action( 'wp_after_execute_ability', $after_callback );
774+
add_action(
775+
'wp_after_execute_ability',
776+
static function () use ( &$after_action_fired ) {
777+
$after_action_fired = true;
778+
}
779+
);
773780

774781
$ability = new WP_Ability( self::$test_ability_name, $args );
775782
$result = $ability->execute();
776783

777-
remove_action( 'wp_before_execute_ability', $before_callback );
778-
remove_action( 'wp_after_execute_ability', $after_callback );
779-
780784
$this->assertTrue( $before_action_fired, 'before_execute_ability action should be fired even if execution fails' );
781785
$this->assertFalse( $after_action_fired, 'after_execute_ability action should not be fired when execution returns WP_Error' );
782786
$this->assertInstanceOf( WP_Error::class, $result, 'Should return WP_Error from execution callback' );
@@ -805,23 +809,23 @@ public function test_after_action_not_fired_on_output_validation_error() {
805809
)
806810
);
807811

808-
$before_callback = static function () use ( &$before_action_fired ) {
809-
$before_action_fired = true;
810-
};
811-
812-
$after_callback = static function () use ( &$after_action_fired ) {
813-
$after_action_fired = true;
814-
};
812+
add_action(
813+
'wp_before_execute_ability',
814+
static function () use ( &$before_action_fired ) {
815+
$before_action_fired = true;
816+
}
817+
);
815818

816-
add_action( 'wp_before_execute_ability', $before_callback );
817-
add_action( 'wp_after_execute_ability', $after_callback );
819+
add_action(
820+
'wp_after_execute_ability',
821+
static function () use ( &$after_action_fired ) {
822+
$after_action_fired = true;
823+
}
824+
);
818825

819826
$ability = new WP_Ability( self::$test_ability_name, $args );
820827
$result = $ability->execute();
821828

822-
remove_action( 'wp_before_execute_ability', $before_callback );
823-
remove_action( 'wp_after_execute_ability', $after_callback );
824-
825829
$this->assertTrue( $before_action_fired, 'before_execute_ability action should be fired even if output validation fails' );
826830
$this->assertFalse( $after_action_fired, 'after_execute_ability action should not be fired when output validation fails' );
827831
$this->assertInstanceOf( WP_Error::class, $result, 'Should return WP_Error for output validation failure' );

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -521,13 +521,15 @@ public function test_get_ability_no_init_action(): void {
521521
public function test_get_existing_ability_using_callback() {
522522
$this->simulate_doing_wp_abilities_init_action();
523523

524-
$name = self::$test_ability_name;
525-
$args = self::$test_ability_args;
526-
$callback = static function ( $instance ) use ( $name, $args ) {
527-
wp_register_ability( $name, $args );
528-
};
524+
$name = self::$test_ability_name;
525+
$args = self::$test_ability_args;
529526

530-
add_action( 'wp_abilities_api_init', $callback );
527+
add_action(
528+
'wp_abilities_api_init',
529+
static function ( $instance ) use ( $name, $args ) {
530+
wp_register_ability( $name, $args );
531+
}
532+
);
531533

532534
// Reset the Registry, to ensure it's empty before the test.
533535
$registry_reflection = new ReflectionClass( WP_Abilities_Registry::class );
@@ -539,8 +541,6 @@ public function test_get_existing_ability_using_callback() {
539541

540542
$result = wp_get_ability( $name );
541543

542-
remove_action( 'wp_abilities_api_init', $callback );
543-
544544
$this->assertEquals(
545545
new WP_Ability( $name, $args ),
546546
$result,

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,15 @@ public function test_get_nonexistent_category(): void {
293293
* @ticket 64098
294294
*/
295295
public function test_get_existing_category_using_callback(): void {
296-
$name = self::$test_ability_category_name;
297-
$args = self::$test_ability_category_args;
298-
$callback = static function ( $instance ) use ( $name, $args ) {
299-
wp_register_ability_category( $name, $args );
300-
};
296+
$name = self::$test_ability_category_name;
297+
$args = self::$test_ability_category_args;
301298

302-
add_action( 'wp_abilities_api_categories_init', $callback );
299+
add_action(
300+
'wp_abilities_api_categories_init',
301+
static function ( $instance ) use ( $name, $args ) {
302+
wp_register_ability_category( $name, $args );
303+
}
304+
);
303305

304306
// Reset the Registry, to ensure it's empty before the test.
305307
$registry_reflection = new ReflectionClass( WP_Ability_Categories_Registry::class );
@@ -311,8 +313,6 @@ public function test_get_existing_category_using_callback(): void {
311313

312314
$result = wp_get_ability_category( $name );
313315

314-
remove_action( 'wp_abilities_api_categories_init', $callback );
315-
316316
$this->assertInstanceOf( WP_Ability_Category::class, $result );
317317
$this->assertSame( self::$test_ability_category_name, $result->get_slug() );
318318
}

tests/phpunit/tests/rest-api/wpRestAbilitiesV1CategoriesController.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ public function test_get_item_with_selected_fields(): void {
206206
$response = $this->server->dispatch( $request );
207207
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
208208
$response = apply_filters( 'rest_post_dispatch', $response, $this->server, $request );
209-
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
210209

211210
$this->assertEquals( 200, $response->get_status() );
212211

tests/phpunit/tests/rest-api/wpRestAbilitiesV1ListController.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,6 @@ public function test_get_item_with_selected_fields(): void {
328328
$response = $this->server->dispatch( $request );
329329
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
330330
$response = apply_filters( 'rest_post_dispatch', $response, $this->server, $request );
331-
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
332331

333332
$this->assertEquals( 200, $response->get_status() );
334333

@@ -349,7 +348,6 @@ public function test_get_item_with_embed_context(): void {
349348
$response = $this->server->dispatch( $request );
350349
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
351350
$response = apply_filters( 'rest_post_dispatch', $response, $this->server, $request );
352-
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
353351

354352
$this->assertEquals( 200, $response->get_status() );
355353

0 commit comments

Comments
 (0)