From b764725010a37b2f62ee197a60f0223763d85e76 Mon Sep 17 00:00:00 2001 From: Md Abul Bashar Date: Fri, 6 Nov 2026 01:29:10 +0600 Subject: [PATCH 1/2] Tests: Add unit coverage for the core abilities registered in abilities.php. Adds tests for the permission of `core/get-site-info`, the label and description of the `site`, `user` and `content` ability categories, the category and annotations of the core information abilities, and the `roles` returned by `core/get-user-info`. See #66493. --- .../abilities-api/wpRegisterCoreAbilities.php | 206 ++++++++++++++++++ 1 file changed, 206 insertions(+) diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreAbilities.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreAbilities.php index b5b44c5be02cb..843f26b41de7d 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreAbilities.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreAbilities.php @@ -532,6 +532,212 @@ public static function data_core_ability_field_lists(): array { return $datasets; } + /** + * Tests that the `core/get-site-info` ability is available to administrators. + * + * @ticket 66493 + */ + public function test_core_get_site_info_allows_administrator(): void { + $admin_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); + wp_set_current_user( $admin_id ); + + $this->assertTrue( current_user_can( 'manage_options' ) ); + + $ability = wp_get_ability( 'core/get-site-info' ); + + $this->assertTrue( $ability->check_permissions() ); + $this->assertIsArray( $ability->execute() ); + } + + /** + * Tests that the `core/get-site-info` ability is denied to users who cannot manage options. + * + * @ticket 66493 + */ + public function test_core_get_site_info_requires_manage_options(): void { + $user_id = self::factory()->user->create( array( 'role' => 'subscriber' ) ); + wp_set_current_user( $user_id ); + + $this->assertFalse( current_user_can( 'manage_options' ) ); + + $ability = wp_get_ability( 'core/get-site-info' ); + + $this->assertFalse( $ability->check_permissions() ); + + $result = $ability->execute(); + + $this->assertWPError( $result ); + $this->assertSame( 'ability_invalid_permissions', $result->get_error_code() ); + } + + /** + * Tests that the `core/get-site-info` ability is denied to logged-out visitors. + * + * @ticket 66493 + */ + public function test_core_get_site_info_denies_logged_out_visitor(): void { + wp_set_current_user( 0 ); + + $ability = wp_get_ability( 'core/get-site-info' ); + + $this->assertFalse( $ability->check_permissions() ); + + $result = $ability->execute(); + + $this->assertWPError( $result ); + $this->assertSame( 'ability_invalid_permissions', $result->get_error_code() ); + } + + /** + * Tests that managing options is enough to use the `core/get-site-info` ability. + * + * @ticket 66493 + */ + public function test_core_get_site_info_allows_delegated_manage_options(): void { + $user_id = self::factory()->user->create( array( 'role' => 'subscriber' ) ); + $user = wp_set_current_user( $user_id ); + $user->add_cap( 'manage_options' ); + + $this->assertFalse( current_user_can( 'edit_posts' ) ); + + $ability = wp_get_ability( 'core/get-site-info' ); + + $this->assertTrue( $ability->check_permissions() ); + $this->assertIsArray( $ability->execute() ); + } + + /** + * Tests the label and description of the core ability categories. + * + * @ticket 66493 + * + * @dataProvider data_core_ability_categories + * + * @param string $slug Category slug. + * @param string $label Expected label. + * @param string $description Expected description. + */ + public function test_core_ability_categories_are_registered( string $slug, string $label, string $description ): void { + $category = wp_get_ability_category( $slug ); + + $this->assertInstanceOf( WP_Ability_Category::class, $category ); + $this->assertSame( $slug, $category->get_slug() ); + $this->assertSame( $label, $category->get_label() ); + $this->assertSame( $description, $category->get_description() ); + } + + /** + * Data provider for the core ability categories. + * + * @return array[] + */ + public static function data_core_ability_categories(): array { + return array( + 'site' => array( 'site', 'Site', 'Abilities that retrieve or modify site information and settings.' ), + 'user' => array( 'user', 'User', 'Abilities that retrieve or modify user information and settings.' ), + 'content' => array( 'content', 'Content', 'Abilities that retrieve or manage posts and other content.' ), + ); + } + + /** + * Tests the category of each core information ability. + * + * @ticket 66493 + * + * @dataProvider data_core_information_abilities + * + * @param string $ability_name Ability name. + * @param string $category Expected category slug. + */ + public function test_core_information_abilities_use_expected_category( string $ability_name, string $category ): void { + $ability = wp_get_ability( $ability_name ); + + $this->assertSame( $category, $ability->get_category() ); + $this->assertTrue( wp_has_ability_category( $category ), 'The category of the ability should be registered.' ); + } + + /** + * Tests that the core information abilities are annotated as read-only and idempotent. + * + * @ticket 66493 + * + * @dataProvider data_core_information_abilities + * + * @param string $ability_name Ability name. + */ + public function test_core_information_abilities_are_annotated_as_read_only( string $ability_name ): void { + $annotations = wp_get_ability( $ability_name )->get_meta_item( 'annotations' ); + + $this->assertTrue( $annotations['readonly'], 'The ability should be read-only.' ); + $this->assertFalse( $annotations['destructive'], 'The ability should not be destructive.' ); + $this->assertTrue( $annotations['idempotent'], 'The ability should be idempotent.' ); + } + + /** + * Data provider for the core information abilities. + * + * @return array[] + */ + public static function data_core_information_abilities(): array { + return array( + 'core/get-site-info' => array( 'core/get-site-info', 'site' ), + 'core/get-user-info' => array( 'core/get-user-info', 'user' ), + 'core/get-environment-info' => array( 'core/get-environment-info', 'site' ), + ); + } + + /** + * Tests that the roles of a user with several roles are returned in order. + * + * @ticket 66493 + */ + public function test_core_get_user_info_returns_all_roles_of_user(): void { + $user_id = self::factory()->user->create( array( 'role' => 'subscriber' ) ); + $user = wp_set_current_user( $user_id ); + $user->add_role( 'editor' ); + + $result = wp_get_ability( 'core/get-user-info' )->execute( array( 'fields' => array( 'roles' ) ) ); + + $this->assertSame( array( 'roles' => array( 'subscriber', 'editor' ) ), $result ); + } + + /** + * Tests that the roles are returned as a list when the roles of the user are not numbered in sequence. + * + * @ticket 66493 + */ + public function test_core_get_user_info_returns_roles_as_list(): void { + $user_id = self::factory()->user->create( array( 'role' => 'subscriber' ) ); + $user = wp_set_current_user( $user_id ); + + // The roles of a user can come with gaps in their numbering, for example when they are filtered. + $user->roles = array( + 2 => 'subscriber', + 5 => 'editor', + ); + + $result = wp_get_ability( 'core/get-user-info' )->execute( array( 'fields' => array( 'roles' ) ) ); + + $this->assertSame( array( 'roles' => array( 'subscriber', 'editor' ) ), $result ); + $this->assertSame( '{"roles":["subscriber","editor"]}', wp_json_encode( $result ), 'The roles should be encoded as a JSON array.' ); + } + + /** + * Tests that a user without roles gets an empty list of roles. + * + * @ticket 66493 + */ + public function test_core_get_user_info_returns_empty_roles_for_user_without_roles(): void { + $user_id = self::factory()->user->create( array( 'role' => '' ) ); + $user = wp_set_current_user( $user_id ); + + $this->assertSame( array(), $user->roles, 'The user should not have any roles.' ); + + $result = wp_get_ability( 'core/get-user-info' )->execute( array( 'fields' => array( 'roles' ) ) ); + + $this->assertSame( array( 'roles' => array() ), $result ); + } + /** * Tests that all core ability schemas only use valid JSON Schema keywords. * From d7fc1051d97fb43c712fac8cc3825d2058f36975 Mon Sep 17 00:00:00 2001 From: Md Abul Bashar Date: Fri, 6 Nov 2026 18:41:26 +0600 Subject: [PATCH 2/2] Tests: Remove overlapping core abilities tests and merge the permission denial tests. See #66493. --- .../abilities-api/wpRegisterCoreAbilities.php | 94 ++++--------------- 1 file changed, 18 insertions(+), 76 deletions(-) diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreAbilities.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreAbilities.php index 843f26b41de7d..9f4f0ee841bbc 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreAbilities.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreAbilities.php @@ -532,60 +532,38 @@ public static function data_core_ability_field_lists(): array { return $datasets; } - /** - * Tests that the `core/get-site-info` ability is available to administrators. - * - * @ticket 66493 - */ - public function test_core_get_site_info_allows_administrator(): void { - $admin_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); - wp_set_current_user( $admin_id ); - - $this->assertTrue( current_user_can( 'manage_options' ) ); - - $ability = wp_get_ability( 'core/get-site-info' ); - - $this->assertTrue( $ability->check_permissions() ); - $this->assertIsArray( $ability->execute() ); - } - /** * Tests that the `core/get-site-info` ability is denied to users who cannot manage options. * * @ticket 66493 + * + * @dataProvider data_users_who_cannot_manage_options + * + * @param string $role Role of the current user, or an empty string for a logged-out visitor. */ - public function test_core_get_site_info_requires_manage_options(): void { - $user_id = self::factory()->user->create( array( 'role' => 'subscriber' ) ); - wp_set_current_user( $user_id ); - - $this->assertFalse( current_user_can( 'manage_options' ) ); - - $ability = wp_get_ability( 'core/get-site-info' ); - - $this->assertFalse( $ability->check_permissions() ); + public function test_core_get_site_info_denies_users_who_cannot_manage_options( string $role ): void { + if ( '' !== $role ) { + wp_set_current_user( self::factory()->user->create( array( 'role' => $role ) ) ); + } else { + wp_set_current_user( 0 ); + } - $result = $ability->execute(); + $result = wp_get_ability( 'core/get-site-info' )->execute(); $this->assertWPError( $result ); $this->assertSame( 'ability_invalid_permissions', $result->get_error_code() ); } /** - * Tests that the `core/get-site-info` ability is denied to logged-out visitors. + * Data provider for the users who cannot manage options. * - * @ticket 66493 + * @return array[] */ - public function test_core_get_site_info_denies_logged_out_visitor(): void { - wp_set_current_user( 0 ); - - $ability = wp_get_ability( 'core/get-site-info' ); - - $this->assertFalse( $ability->check_permissions() ); - - $result = $ability->execute(); - - $this->assertWPError( $result ); - $this->assertSame( 'ability_invalid_permissions', $result->get_error_code() ); + public static function data_users_who_cannot_manage_options(): array { + return array( + 'subscriber' => array( 'subscriber' ), + 'logged-out user' => array( '' ), + ); } /** @@ -598,47 +576,12 @@ public function test_core_get_site_info_allows_delegated_manage_options(): void $user = wp_set_current_user( $user_id ); $user->add_cap( 'manage_options' ); - $this->assertFalse( current_user_can( 'edit_posts' ) ); - $ability = wp_get_ability( 'core/get-site-info' ); $this->assertTrue( $ability->check_permissions() ); $this->assertIsArray( $ability->execute() ); } - /** - * Tests the label and description of the core ability categories. - * - * @ticket 66493 - * - * @dataProvider data_core_ability_categories - * - * @param string $slug Category slug. - * @param string $label Expected label. - * @param string $description Expected description. - */ - public function test_core_ability_categories_are_registered( string $slug, string $label, string $description ): void { - $category = wp_get_ability_category( $slug ); - - $this->assertInstanceOf( WP_Ability_Category::class, $category ); - $this->assertSame( $slug, $category->get_slug() ); - $this->assertSame( $label, $category->get_label() ); - $this->assertSame( $description, $category->get_description() ); - } - - /** - * Data provider for the core ability categories. - * - * @return array[] - */ - public static function data_core_ability_categories(): array { - return array( - 'site' => array( 'site', 'Site', 'Abilities that retrieve or modify site information and settings.' ), - 'user' => array( 'user', 'User', 'Abilities that retrieve or modify user information and settings.' ), - 'content' => array( 'content', 'Content', 'Abilities that retrieve or manage posts and other content.' ), - ); - } - /** * Tests the category of each core information ability. * @@ -653,7 +596,6 @@ public function test_core_information_abilities_use_expected_category( string $a $ability = wp_get_ability( $ability_name ); $this->assertSame( $category, $ability->get_category() ); - $this->assertTrue( wp_has_ability_category( $category ), 'The category of the ability should be registered.' ); } /**