Make WordPress Core


Ignore:
Timestamp:
02/09/2026 04:59:52 PM (2 months ago)
Author:
jorgefilipecosta
Message:

Abilities API: Allow nested namespace ability names (2-4 segments).

Expand ability name validation from exactly 2 segments (namespace/ability) to 2-4 segments, enabling names like my-plugin/resource/find and my-plugin/resource/sub/find.
This allows plugins to organize abilities into logical resource groups. The validation regex changes from /^[a-z0-9-]+\/[a-z0-9-]+$/ to /^[a-z0-9-]+(?:\/[a-z0-9-]+){1,3}$/, which accepts the first segment plus 1-3 additional slash-delimited segments.
Updates the validation regex, error messages, docblocks, and adds corresponding unit and REST API tests.

Props jorgefilipecosta, justlevine, jorbin.
Fixes #64596.

File:
1 edited

Legend:

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

    r61390 r61602  
    138138
    139139    /**
     140     * Should accept ability name with 3 segments (2 slashes).
     141     *
     142     * @ticket 64098
     143     *
     144     * @covers WP_Abilities_Registry::register
     145     */
     146    public function test_register_valid_name_with_three_segments() {
     147        $result = $this->registry->register( 'test/sub/add-numbers', self::$test_ability_args );
     148        $this->assertInstanceOf( WP_Ability::class, $result );
     149        $this->assertSame( 'test/sub/add-numbers', $result->get_name() );
     150    }
     151
     152    /**
     153     * Should accept ability name with 4 segments (3 slashes).
     154     *
     155     * @ticket 64098
     156     *
     157     * @covers WP_Abilities_Registry::register
     158     */
     159    public function test_register_valid_name_with_four_segments() {
     160        $result = $this->registry->register( 'test/sub/deep/add-numbers', self::$test_ability_args );
     161        $this->assertInstanceOf( WP_Ability::class, $result );
     162        $this->assertSame( 'test/sub/deep/add-numbers', $result->get_name() );
     163    }
     164
     165    /**
     166     * Should reject ability name with 5 segments (exceeds maximum of 4).
     167     *
     168     * @ticket 64098
     169     *
     170     * @covers WP_Abilities_Registry::register
     171     *
     172     * @expectedIncorrectUsage WP_Abilities_Registry::register
     173     */
     174    public function test_register_invalid_name_with_five_segments() {
     175        $result = $this->registry->register( 'test/a/b/c/too-deep', self::$test_ability_args );
     176        $this->assertNull( $result );
     177    }
     178
     179    /**
     180     * Should reject ability name with empty segments (double slashes).
     181     *
     182     * @ticket 64098
     183     *
     184     * @covers WP_Abilities_Registry::register
     185     *
     186     * @expectedIncorrectUsage WP_Abilities_Registry::register
     187     */
     188    public function test_register_invalid_name_with_empty_segment() {
     189        $result = $this->registry->register( 'test//add-numbers', self::$test_ability_args );
     190        $this->assertNull( $result );
     191    }
     192
     193    /**
     194     * Should reject ability name with trailing slash.
     195     *
     196     * @ticket 64098
     197     *
     198     * @covers WP_Abilities_Registry::register
     199     *
     200     * @expectedIncorrectUsage WP_Abilities_Registry::register
     201     */
     202    public function test_register_invalid_name_with_trailing_slash() {
     203        $result = $this->registry->register( 'test/add-numbers/', self::$test_ability_args );
     204        $this->assertNull( $result );
     205    }
     206
     207    /**
    140208     * Should reject ability registration without a label.
    141209     *
Note: See TracChangeset for help on using the changeset viewer.