Skip to content
This repository was archived by the owner on Feb 5, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions tests/unit/abilities-api/wpAbilitiesRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Tests_Abilities_API_WpAbilitiesRegistry extends WP_UnitTestCase {
/**
* Mock abilities registry.
*
* @var WP_Abilities_Registry
* @var \WP_Abilities_Registry
*/
private $registry = null;

Expand Down Expand Up @@ -49,10 +49,10 @@ public function set_up(): void {
'description' => 'The result of adding the two numbers.',
'required' => true,
),
'execute_callback' => function ( array $input ): int {
'execute_callback' => static function ( array $input ): int {
return $input['a'] + $input['b'];
},
'permission_callback' => function (): bool {
'permission_callback' => static function (): bool {
return true;
},
'meta' => array(
Expand Down
12 changes: 7 additions & 5 deletions tests/unit/rest-api/wpRestAbilitiesInit.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Tests_REST_API_WpRestAbilitiesInit extends WP_UnitTestCase {
/**
* REST Server instance.
*
* @var WP_REST_Server
* @var \WP_REST_Server
*/
protected $server;

Expand Down Expand Up @@ -139,9 +139,11 @@ public function test_namespace_and_base_configuration(): void {
// Verify abilities endpoints are under wp/v2 namespace
$routes = $this->server->get_routes();
foreach ( array_keys( $routes ) as $route ) {
if ( strpos( $route, 'abilities' ) !== false && $route !== '/' ) {
$this->assertStringStartsWith( '/wp/v2/abilities', $route );
if ( strpos( $route, 'abilities' ) === false || $route === '/' ) {
continue;
}

$this->assertStringStartsWith( '/wp/v2/abilities', $route );
}
}

Expand All @@ -152,14 +154,14 @@ public function test_no_duplicate_routes_on_multiple_init(): void {
// First init
do_action( 'rest_api_init' );

$routes_first = $this->server->get_routes();
$routes_first = $this->server->get_routes();
$abilities_route_count_first = count( $routes_first['/wp/v2/abilities'] ?? array() );

// Second init (simulating multiple calls)
// Note: WordPress doesn't prevent duplicate registration, so we expect 2x routes
WP_REST_Abilities_Init::register_routes();

$routes_second = $this->server->get_routes();
$routes_second = $this->server->get_routes();
$abilities_route_count_second = count( $routes_second['/wp/v2/abilities'] ?? array() );

// WordPress allows duplicate route registration
Expand Down
30 changes: 17 additions & 13 deletions tests/unit/rest-api/wpRestAbilitiesListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Tests_REST_API_WpRestAbilitiesListController extends WP_UnitTestCase {
/**
* REST Server instance.
*
* @var WP_REST_Server
* @var \WP_REST_Server
*/
protected $server;

Expand Down Expand Up @@ -64,9 +64,11 @@ public function set_up(): void {
public function tear_down(): void {
// Clean up test abilities
foreach ( wp_get_abilities() as $ability ) {
if ( str_starts_with( $ability->get_name(), 'test/' ) ) {
wp_unregister_ability( $ability->get_name() );
if ( ! str_starts_with( $ability->get_name(), 'test/' ) ) {
continue;
}

wp_unregister_ability( $ability->get_name() );
}

// Reset REST server
Expand Down Expand Up @@ -100,7 +102,7 @@ private function register_test_abilities(): void {
'output_schema' => array(
'type' => 'number',
),
'execute_callback' => function ( array $input ) {
'execute_callback' => static function ( array $input ) {
switch ( $input['operation'] ) {
case 'add':
return $input['a'] + $input['b'];
Expand All @@ -114,7 +116,7 @@ private function register_test_abilities(): void {
return null;
}
},
'permission_callback' => function () {
'permission_callback' => static function () {
return current_user_can( 'read' );
},
'meta' => array(
Expand Down Expand Up @@ -147,7 +149,7 @@ private function register_test_abilities(): void {
'wp_version' => array( 'type' => 'string' ),
),
),
'execute_callback' => function ( array $input ) {
'execute_callback' => static function ( array $input ) {
$info = array(
'php_version' => phpversion(),
'wp_version' => get_bloginfo( 'version' ),
Expand All @@ -157,7 +159,7 @@ private function register_test_abilities(): void {
}
return $info;
},
'permission_callback' => function () {
'permission_callback' => static function () {
return current_user_can( 'read' );
},
'meta' => array(
Expand All @@ -174,7 +176,7 @@ private function register_test_abilities(): void {
array(
'label' => "Test Ability {$i}",
'description' => "Test ability number {$i}",
'execute_callback' => function () use ( $i ) {
'execute_callback' => static function () use ( $i ) {
return "Result from ability {$i}";
},
'permission_callback' => '__return_true',
Expand Down Expand Up @@ -409,7 +411,7 @@ public function test_ability_name_with_valid_special_characters(): void {
array(
'label' => 'Test Hyphen Ability',
'description' => 'Test ability with hyphen',
'execute_callback' => function ( $input ) {
'execute_callback' => static function ( $input ) {
return array( 'success' => true );
},
'permission_callback' => '__return_true',
Expand Down Expand Up @@ -501,10 +503,12 @@ public function test_invalid_pagination_parameters( array $params ): void {
// Should either use defaults or return error
$this->assertContains( $response->get_status(), array( 200, 400 ) );

if ( $response->get_status() === 200 ) {
// Check that reasonable defaults were used
$data = $response->get_data();
$this->assertIsArray( $data );
if ( $response->get_status() !== 200 ) {
return;
}

// Check that reasonable defaults were used
$data = $response->get_data();
$this->assertIsArray( $data );
}
}
Loading