diff --git a/tests/phpunit/tests/abilities-api/wpAbilitiesRegistry.php b/tests/phpunit/tests/abilities-api/wpAbilitiesRegistry.php index 7d98cb2780454..83c362973683b 100644 --- a/tests/phpunit/tests/abilities-api/wpAbilitiesRegistry.php +++ b/tests/phpunit/tests/abilities-api/wpAbilitiesRegistry.php @@ -761,4 +761,26 @@ static function ( $args, $name ) { $unfiltered_ability = $this->registry->register( 'test/another-ability', self::$test_ability_args ); $this->assertNotSame( $filtered_ability->get_label(), $unfiltered_ability->get_label(), 'The filter incorrectly modified the args for an ability it should not have.' ); } + + /** + * Tests that the registry cannot be serialized or unserialized. + * + * @ticket 66549 + * + * @covers WP_Abilities_Registry::__sleep + * @covers WP_Abilities_Registry::__wakeup + */ + public function test_registry_cannot_be_serialized_or_unserialized() { + try { + serialize( $this->registry ); + $this->fail( 'Serializing the registry should throw an exception.' ); + } catch ( LogicException $e ) { + $this->assertSame( 'WP_Abilities_Registry should never be serialized.', $e->getMessage() ); + } + + $this->expectException( LogicException::class ); + $this->expectExceptionMessage( 'WP_Abilities_Registry should never be unserialized.' ); + + unserialize( 'O:21:"WP_Abilities_Registry":0:{}' ); + } } diff --git a/tests/phpunit/tests/abilities-api/wpAbility.php b/tests/phpunit/tests/abilities-api/wpAbility.php index 8fdb67e29cd7f..370b60260c40a 100644 --- a/tests/phpunit/tests/abilities-api/wpAbility.php +++ b/tests/phpunit/tests/abilities-api/wpAbility.php @@ -2866,4 +2866,274 @@ public function test_check_permissions_custom_class_with_required_capabilities_o $this->assertTrue( $ability->check_permissions() ); } + + /** + * Tests that an unknown property is reported and ignored. + * + * @ticket 66549 + * + * @covers WP_Ability::__construct + */ + public function test_unknown_property_is_reported_and_ignored() { + $messages = array(); + + add_action( + 'doing_it_wrong_run', + static function ( $the_method, $message ) use ( &$messages ) { + $messages[ $the_method ] = $message; + }, + 10, + 2 + ); + + $this->setExpectedIncorrectUsage( 'WP_Ability::__construct' ); + + $ability = new WP_Ability( + self::$test_ability_name, + array_merge( self::$test_ability_properties, array( 'unknown_property' => 'value' ) ) + ); + + $this->assertFalse( property_exists( $ability, 'unknown_property' ), 'The unknown property should be ignored.' ); + $this->assertSame( 'Calculator', $ability->get_label(), 'The known properties should still be set.' ); + $this->assertArrayHasKey( 'WP_Ability::__construct', $messages ); + $this->assertStringContainsString( 'unknown_property', $messages['WP_Ability::__construct'] ); + $this->assertStringContainsString( '' . self::$test_ability_name . '', $messages['WP_Ability::__construct'] ); + } + + /** + * Tests that the category is required. + * + * @ticket 66549 + * + * @covers WP_Ability::prepare_properties + * + * @dataProvider data_invalid_categories + * + * @param array $overrides The properties to merge into the test ability properties. + */ + public function test_category_is_required( array $overrides ) { + $this->expectException( InvalidArgumentException::class ); + $this->expectExceptionMessage( 'The ability properties must contain a `category` string.' ); + + new WP_Ability( self::$test_ability_name, array_merge( self::$test_ability_properties, $overrides ) ); + } + + /** + * Data provider for categories that are not valid. + * + * @return array}> + */ + public static function data_invalid_categories(): array { + return array( + 'an empty string' => array( array( 'category' => '' ) ), + 'null' => array( array( 'category' => null ) ), + 'an integer' => array( array( 'category' => 5 ) ), + 'an array' => array( array( 'category' => array( 'math' ) ) ), + ); + } + + /** + * Tests that the category is required when the property is missing. + * + * @ticket 66549 + * + * @covers WP_Ability::prepare_properties + */ + public function test_category_is_required_when_missing() { + $args = self::$test_ability_properties; + unset( $args['category'] ); + + $this->expectException( InvalidArgumentException::class ); + $this->expectExceptionMessage( 'The ability properties must contain a `category` string.' ); + + new WP_Ability( self::$test_ability_name, $args ); + } + + /** + * Tests that input is rejected when the ability has no input schema. + * + * @ticket 66549 + * + * @covers WP_Ability::validate_input + */ + public function test_input_without_an_input_schema_is_rejected() { + $ability = new WP_Ability( self::$test_ability_name, self::$test_ability_properties ); + + $result = $ability->validate_input( 'value' ); + + $this->assertWPError( $result ); + $this->assertSame( 'ability_missing_input_schema', $result->get_error_code() ); + $this->assertSame( + 'Ability "test/calculator" does not define an input schema required to validate the provided input.', + $result->get_error_message() + ); + $this->assertTrue( $ability->validate_input( null ), 'No input is valid without an input schema.' ); + } + + /** + * Tests that executing an ability with input but no input schema returns an error. + * + * @ticket 66549 + * + * @covers WP_Ability::execute + */ + public function test_execute_with_input_and_no_input_schema_returns_an_error() { + $ability = new WP_Ability( self::$test_ability_name, self::$test_ability_properties ); + + $result = $ability->execute( 'value' ); + + $this->assertWPError( $result ); + $this->assertSame( 'ability_missing_input_schema', $result->get_error_code() ); + } + + /** + * Tests that a filter can reject input with `false`. + * + * @ticket 66549 + * + * @covers WP_Ability::validate_input + */ + public function test_validate_input_filter_returning_false_rejects_the_input() { + $ability = new WP_Ability( + self::$test_ability_name, + array_merge( + self::$test_ability_properties, + array( + 'input_schema' => array( 'type' => 'integer' ), + 'output_schema' => array( 'type' => 'integer' ), + ) + ) + ); + + $this->assertTrue( $ability->validate_input( 5 ), 'The input should be valid without the filter.' ); + + add_filter( 'wp_ability_validate_input', '__return_false' ); + + $result = $ability->validate_input( 5 ); + + $this->assertWPError( $result ); + $this->assertSame( 'ability_invalid_input', $result->get_error_code() ); + $this->assertSame( 'Invalid input.', $result->get_error_message() ); + } + + /** + * Tests that a filter can reject output with `false`. + * + * @ticket 66549 + * + * @covers WP_Ability::validate_output + */ + public function test_validate_output_filter_returning_false_rejects_the_output() { + $ability = new WP_Ability( + self::$test_ability_name, + array_merge( + self::$test_ability_properties, + array( + 'output_schema' => array( 'type' => 'integer' ), + 'execute_callback' => static function (): int { + return 7; + }, + ) + ) + ); + + $this->assertSame( 7, $ability->execute(), 'The output should be valid without the filter.' ); + + add_filter( 'wp_ability_validate_output', '__return_false' ); + + $result = $ability->execute(); + + $this->assertWPError( $result ); + $this->assertSame( 'ability_invalid_output', $result->get_error_code() ); + $this->assertSame( 'Invalid output.', $result->get_error_message() ); + } + + /** + * Tests that an ability of a custom class without an execute callback returns an error. + * + * @ticket 66549 + * + * @covers WP_Ability::do_execute + */ + public function test_execute_without_a_valid_execute_callback_returns_an_error() { + $args = self::$test_ability_properties; + unset( $args['execute_callback'] ); + + $result = $this->create_custom_class_ability( $args )->execute(); + + $this->assertWPError( $result ); + $this->assertSame( 'ability_invalid_execute_callback', $result->get_error_code() ); + $this->assertSame( 'Ability "test/calculator" does not have a valid execute callback.', $result->get_error_message() ); + } + + /** + * Tests that a permission callback error is reported but not shown to the caller. + * + * @ticket 66549 + * + * @covers WP_Ability::execute + */ + public function test_permission_callback_error_is_reported_and_not_leaked() { + $executed = false; + $messages = array(); + + add_action( + 'doing_it_wrong_run', + static function ( $the_method, $message ) use ( &$messages ) { + $messages[ $the_method ] = $message; + }, + 10, + 2 + ); + + $this->setExpectedIncorrectUsage( 'WP_Ability::execute' ); + + $ability = new WP_Ability( + self::$test_ability_name, + array_merge( + self::$test_ability_properties, + array( + 'permission_callback' => static function () { + return new WP_Error( 'secret_reason', 'A secret reason.' ); + }, + 'execute_callback' => static function () use ( &$executed ) { + $executed = true; + return 1; + }, + ) + ) + ); + + $result = $ability->execute(); + + $this->assertWPError( $result ); + $this->assertSame( 'ability_invalid_permissions', $result->get_error_code() ); + $this->assertStringNotContainsString( 'secret', $result->get_error_message(), 'The reason should not be shown to the caller.' ); + $this->assertFalse( $executed, 'The ability should not run.' ); + $this->assertSame( 'A secret reason.', $messages['WP_Ability::execute'] ); + } + + /** + * Tests that an ability cannot be serialized or unserialized. + * + * @ticket 66549 + * + * @covers WP_Ability::__sleep + * @covers WP_Ability::__wakeup + */ + public function test_ability_cannot_be_serialized_or_unserialized() { + $ability = new WP_Ability( self::$test_ability_name, self::$test_ability_properties ); + + try { + serialize( $ability ); + $this->fail( 'Serializing an ability should throw an exception.' ); + } catch ( LogicException $e ) { + $this->assertSame( 'WP_Ability should never be serialized.', $e->getMessage() ); + } + + $this->expectException( LogicException::class ); + $this->expectExceptionMessage( 'WP_Ability should never be unserialized.' ); + + unserialize( 'O:10:"WP_Ability":0:{}' ); + } } diff --git a/tests/phpunit/tests/abilities-api/wpAbilityCategoryRegistry.php b/tests/phpunit/tests/abilities-api/wpAbilityCategoryRegistry.php index b2f879e603f1c..8c569ce70abaa 100644 --- a/tests/phpunit/tests/abilities-api/wpAbilityCategoryRegistry.php +++ b/tests/phpunit/tests/abilities-api/wpAbilityCategoryRegistry.php @@ -715,4 +715,123 @@ public function test_category_registry_singleton(): void { $this->assertSame( $instance1, $instance2 ); } + + /** + * Test that the category registry cannot be serialized or unserialized. + * + * @ticket 66549 + * + * @covers WP_Ability_Categories_Registry::__sleep + * @covers WP_Ability_Categories_Registry::__wakeup + */ + public function test_category_registry_cannot_be_serialized_or_unserialized(): void { + try { + serialize( $this->registry ); + $this->fail( 'Serializing the registry should throw an exception.' ); + } catch ( LogicException $e ) { + $this->assertSame( 'WP_Ability_Categories_Registry should never be serialized.', $e->getMessage() ); + } + + $this->expectException( LogicException::class ); + $this->expectExceptionMessage( 'WP_Ability_Categories_Registry should never be unserialized.' ); + + unserialize( 'O:30:"WP_Ability_Categories_Registry":0:{}' ); + } + + /** + * Test that a category cannot be created without a slug. + * + * @ticket 66549 + * + * @covers WP_Ability_Category::__construct + */ + public function test_category_constructor_empty_slug(): void { + $this->expectException( InvalidArgumentException::class ); + $this->expectExceptionMessage( 'The ability category slug cannot be empty.' ); + + new WP_Ability_Category( + '', + array( + 'label' => 'Math', + 'description' => 'Mathematical operations.', + ) + ); + } + + /** + * Test that the registry is not created before the `init` action has fired. + * + * @ticket 66549 + * + * @covers WP_Ability_Categories_Registry::get_instance + */ + public function test_get_instance_before_init_returns_null(): void { + global $wp_actions; + + $original_init_count = $wp_actions['init'] ?? null; + unset( $wp_actions['init'] ); + + $messages = array(); + + add_action( + 'doing_it_wrong_run', + static function ( $the_method, $message ) use ( &$messages ) { + $messages[ $the_method ] = $message; + }, + 10, + 2 + ); + + $this->setExpectedIncorrectUsage( 'WP_Ability_Categories_Registry::get_instance' ); + + try { + $instance = WP_Ability_Categories_Registry::get_instance(); + } finally { + if ( null !== $original_init_count ) { + $wp_actions['init'] = $original_init_count; + } + } + + $this->assertNull( $instance ); + $this->assertStringContainsString( 'init', $messages['WP_Ability_Categories_Registry::get_instance'] ); + } + + /** + * Test that the registry is created once and fires the action to register categories. + * + * @ticket 66549 + * + * @covers WP_Ability_Categories_Registry::get_instance + */ + public function test_get_instance_creates_the_registry_and_fires_the_init_action(): void { + $property = new ReflectionProperty( WP_Ability_Categories_Registry::class, 'instance' ); + if ( PHP_VERSION_ID < 80100 ) { + $property->setAccessible( true ); + } + + $original = $property->getValue(); + $received = array(); + + $property->setValue( null, null ); + + add_action( + 'wp_abilities_api_categories_init', + static function ( $registry ) use ( &$received ) { + $received[] = $registry; + } + ); + + try { + $instance = WP_Ability_Categories_Registry::get_instance(); + $again = WP_Ability_Categories_Registry::get_instance(); + } finally { + $property->setValue( null, $original ); + } + + $this->assertInstanceOf( WP_Ability_Categories_Registry::class, $instance ); + $this->assertSame( $instance, $again, 'The registry should be created once.' ); + $this->assertNotSame( $original, $instance, 'A new registry should be created.' ); + $this->assertCount( 1, $received, 'The action should fire once.' ); + $this->assertSame( $instance, $received[0], 'The action should receive the registry.' ); + } }