Make WordPress Core

Changeset 65307


Ignore:
Timestamp:
11/06/2026 12:57:48 PM (26 hours ago)
Author:
gziolo
Message:

Tests: Extend coverage for core abilities

Cover core/get-site-info permissions, the categories and annotations of the core information abilities, and the roles returned by core/get-user-info.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/15528.

Props hmbashar, gziolo.
Fixes #66493.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/abilities-api/wpRegisterCoreAbilities.php

    r64729 r65307  
    531531
    532532                return $datasets;
     533        }
     534
     535        /**
     536         * Tests that the `core/get-site-info` ability is denied to users who cannot manage options.
     537         *
     538         * @ticket 66493
     539         *
     540         * @dataProvider data_users_who_cannot_manage_options
     541         *
     542         * @param string $role Role of the current user, or an empty string for a logged-out visitor.
     543         */
     544        public function test_core_get_site_info_denies_users_who_cannot_manage_options( string $role ): void {
     545                if ( '' !== $role ) {
     546                        wp_set_current_user( self::factory()->user->create( array( 'role' => $role ) ) );
     547                } else {
     548                        wp_set_current_user( 0 );
     549                }
     550
     551                $result = wp_get_ability( 'core/get-site-info' )->execute();
     552
     553                $this->assertWPError( $result );
     554                $this->assertSame( 'ability_invalid_permissions', $result->get_error_code() );
     555        }
     556
     557        /**
     558         * Data provider for the users who cannot manage options.
     559         *
     560         * @return array[]
     561         */
     562        public static function data_users_who_cannot_manage_options(): array {
     563                return array(
     564                        'subscriber'      => array( 'subscriber' ),
     565                        'logged-out user' => array( '' ),
     566                );
     567        }
     568
     569        /**
     570         * Tests that managing options is enough to use the `core/get-site-info` ability.
     571         *
     572         * @ticket 66493
     573         */
     574        public function test_core_get_site_info_allows_delegated_manage_options(): void {
     575                $user_id = self::factory()->user->create( array( 'role' => 'subscriber' ) );
     576                $user    = wp_set_current_user( $user_id );
     577                $user->add_cap( 'manage_options' );
     578
     579                $ability = wp_get_ability( 'core/get-site-info' );
     580
     581                $this->assertTrue( $ability->check_permissions() );
     582                $this->assertIsArray( $ability->execute() );
     583        }
     584
     585        /**
     586         * Tests the category of each core information ability.
     587         *
     588         * @ticket 66493
     589         *
     590         * @dataProvider data_core_information_abilities
     591         *
     592         * @param string $ability_name Ability name.
     593         * @param string $category     Expected category slug.
     594         */
     595        public function test_core_information_abilities_use_expected_category( string $ability_name, string $category ): void {
     596                $ability = wp_get_ability( $ability_name );
     597
     598                $this->assertSame( $category, $ability->get_category() );
     599        }
     600
     601        /**
     602         * Tests that the core information abilities are annotated as read-only and idempotent.
     603         *
     604         * @ticket 66493
     605         *
     606         * @dataProvider data_core_information_abilities
     607         *
     608         * @param string $ability_name Ability name.
     609         */
     610        public function test_core_information_abilities_are_annotated_as_read_only( string $ability_name ): void {
     611                $annotations = wp_get_ability( $ability_name )->get_meta_item( 'annotations' );
     612
     613                $this->assertTrue( $annotations['readonly'], 'The ability should be read-only.' );
     614                $this->assertFalse( $annotations['destructive'], 'The ability should not be destructive.' );
     615                $this->assertTrue( $annotations['idempotent'], 'The ability should be idempotent.' );
     616        }
     617
     618        /**
     619         * Data provider for the core information abilities.
     620         *
     621         * @return array[]
     622         */
     623        public static function data_core_information_abilities(): array {
     624                return array(
     625                        'core/get-site-info'        => array( 'core/get-site-info', 'site' ),
     626                        'core/get-user-info'        => array( 'core/get-user-info', 'user' ),
     627                        'core/get-environment-info' => array( 'core/get-environment-info', 'site' ),
     628                );
     629        }
     630
     631        /**
     632         * Tests that the roles of a user with several roles are returned in order.
     633         *
     634         * @ticket 66493
     635         */
     636        public function test_core_get_user_info_returns_all_roles_of_user(): void {
     637                $user_id = self::factory()->user->create( array( 'role' => 'subscriber' ) );
     638                $user    = wp_set_current_user( $user_id );
     639                $user->add_role( 'editor' );
     640
     641                $result = wp_get_ability( 'core/get-user-info' )->execute( array( 'fields' => array( 'roles' ) ) );
     642
     643                $this->assertSame( array( 'roles' => array( 'subscriber', 'editor' ) ), $result );
     644        }
     645
     646        /**
     647         * Tests that the roles are returned as a list when the roles of the user are not numbered in sequence.
     648         *
     649         * @ticket 66493
     650         */
     651        public function test_core_get_user_info_returns_roles_as_list(): void {
     652                $user_id = self::factory()->user->create( array( 'role' => 'subscriber' ) );
     653                $user    = wp_set_current_user( $user_id );
     654
     655                // The roles of a user can come with gaps in their numbering, for example when they are filtered.
     656                $user->roles = array(
     657                        2 => 'subscriber',
     658                        5 => 'editor',
     659                );
     660
     661                $result = wp_get_ability( 'core/get-user-info' )->execute( array( 'fields' => array( 'roles' ) ) );
     662
     663                $this->assertSame( array( 'roles' => array( 'subscriber', 'editor' ) ), $result );
     664                $this->assertSame( '{"roles":["subscriber","editor"]}', wp_json_encode( $result ), 'The roles should be encoded as a JSON array.' );
     665        }
     666
     667        /**
     668         * Tests that a user without roles gets an empty list of roles.
     669         *
     670         * @ticket 66493
     671         */
     672        public function test_core_get_user_info_returns_empty_roles_for_user_without_roles(): void {
     673                $user_id = self::factory()->user->create( array( 'role' => '' ) );
     674                $user    = wp_set_current_user( $user_id );
     675
     676                $this->assertSame( array(), $user->roles, 'The user should not have any roles.' );
     677
     678                $result = wp_get_ability( 'core/get-user-info' )->execute( array( 'fields' => array( 'roles' ) ) );
     679
     680                $this->assertSame( array( 'roles' => array() ), $result );
    533681        }
    534682
Note: See TracChangeset for help on using the changeset viewer.