Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@
},
"bundled": true,
"commands": [
"ability",
"ability category",
"ability category exists",
"ability category get",
"ability category list",
"ability execute",
"ability exists",
"ability get",
"ability list",
"comment",
"comment approve",
"comment count",
Expand Down
23 changes: 23 additions & 0 deletions entity-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,26 @@
},
)
);

WP_CLI::add_command(
'ability',
'Ability_Command',
array(
'before_invoke' => function () {
if ( Utils\wp_version_compare( '6.9-beta1', '<' ) ) {
WP_CLI::error( 'Requires WordPress 6.9 or greater.' );
}
},
)
);
WP_CLI::add_command(
'ability category',
'Ability_Category_Command',
array(
'before_invoke' => function () {
if ( Utils\wp_version_compare( '6.9-beta1', '<' ) ) {
WP_CLI::error( 'Requires WordPress 6.9 or greater.' );
}
},
)
);
101 changes: 101 additions & 0 deletions features/ability-category.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
Feature: Manage WordPress ability categories

Background:
Given a WP install

@less-than-wp-6.9
Scenario: Ability category commands require WordPress 6.9+
When I try `wp ability category list`
Then STDERR should contain:
"""
Error: Requires WordPress 6.9 or greater.
"""
And the return code should be 1

@require-wp-6.9
Scenario: List ability categories
When I run `wp ability category list --format=count`
Then save STDOUT as {COUNT}

Given a wp-content/mu-plugins/test-ability-categories.php file:
"""
<?php
add_action( 'wp_abilities_api_categories_init', function() {
wp_register_ability_category( 'test-category-1', array(
'label' => 'First test category',
'description' => 'First test category',
) );

wp_register_ability_category( 'test-category-2', array(
'label' => 'Second test category',
'description' => 'Second test category',
) );
} );
"""

When I run `wp ability category list --format=count`
Then STDOUT should not contain:
"""
{COUNT}
"""

When I run `wp ability category list --fields=slug,description --format=csv`
Then STDOUT should contain:
"""
test-category-1,"First test category"
"""
And STDOUT should contain:
"""
test-category-2,"Second test category"
"""

@require-wp-6.9
Scenario: Get a specific ability category
Given a wp-content/mu-plugins/test-ability-categories.php file:
"""
<?php
add_action( 'wp_abilities_api_categories_init', function() {
wp_register_ability_category( 'content', array(
'label' => 'Content category',
'description' => 'Content category',
) );
} );
"""

When I try `wp ability category get invalid_category`
Then STDERR should contain:
"""
Error: Ability category invalid_category doesn't exist.
"""
And the return code should be 1

When I run `wp ability category get content --field=description`
Then STDOUT should be:
"""
Content category
"""

When I run `wp ability category get content --field=slug`
Then STDOUT should be:
"""
content
"""

@require-wp-6.9
Scenario: Check if an ability category exists
Given a wp-content/mu-plugins/test-ability-categories.php file:
"""
<?php
add_action( 'wp_abilities_api_categories_init', function() {
wp_register_ability_category( 'existing-category', array(
'label' => 'This category exists',
'description' => 'This category exists',
) );
} );
"""

When I try `wp ability category exists existing-category`
Then the return code should be 0

When I try `wp ability category exists non_existent_category`
Then the return code should be 1
214 changes: 214 additions & 0 deletions features/ability.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
Feature: Manage WordPress abilities

Background:
Given a WP install

@less-than-wp-6.9
Scenario: Ability commands require WordPress 6.9+
When I try `wp ability list`
Then STDERR should contain:
"""
Error: Requires WordPress 6.9 or greater.
"""
And the return code should be 1

@require-wp-6.9
Scenario: List abilities
When I run `wp ability list --format=count`
Then save STDOUT as {ABILITIES_COUNT}

Given a wp-content/mu-plugins/test-abilities.php file:
"""
<?php
add_action( 'wp_abilities_api_init', function() {
wp_register_ability( 'my-plugin/test-ability-1', array(
'label' => 'Test Ability 1',
'category' => 'site',
'description' => 'Test ability one',
'permission_callback' => '__return_true',
'execute_callback' => function( $input ) {
return array( 'result' => 'success', 'input' => $input );
},
'input_schema' => array(
'type' => 'object',
'properties' => array(
'id' => array( 'type' => 'integer' ),
),
),
'output_schema' => array(
'type' => 'object',
),
) );

wp_register_ability( 'my-plugin/test-ability-2', array(
'label' => 'Test Ability 2',
'category' => 'user',
'description' => 'Test ability two',
'permission_callback' => '__return_true',
'execute_callback' => function( $input ) {
return array( 'result' => 'done' );
},
'input_schema' => array( 'type' => 'object' ),
'output_schema' => array( 'type' => 'object' ),
) );
} );
"""

When I run `wp ability list --format=count`
Then STDOUT should not contain:
"""
{ABILITIES_COUNT}
"""

When I run `wp ability list --fields=name,category,description --format=csv`
Then STDOUT should contain:
"""
my-plugin/test-ability-1,site,"Test ability one"
"""
And STDOUT should contain:
"""
my-plugin/test-ability-2,user,"Test ability two"
"""

@require-wp-6.9
Scenario: Get a specific ability
Given a wp-content/mu-plugins/test-abilities.php file:
"""
<?php
add_action( 'wp_abilities_api_init', function() {
wp_register_ability( 'my-plugin/get-test-post', array(
'label' => 'Get Test Post',
'category' => 'site',
'description' => 'Gets a test post',
'permission_callback' => '__return_true',
'execute_callback' => function( $input ) {
return array( 'id' => $input['id'], 'title' => 'Test Post' );
},
'input_schema' => array(
'type' => 'object',
'properties' => array(
'id' => array( 'type' => 'integer' ),
),
),
'output_schema' => array(
'type' => 'object',
),
) );
} );
"""

When I try `wp ability get invalid_ability`
Then STDERR should contain:
"""
Error: Ability invalid_ability doesn't exist.
"""
And the return code should be 1

When I run `wp ability get my-plugin/get-test-post --field=category`
Then STDOUT should be:
"""
site
"""

When I run `wp ability get my-plugin/get-test-post --field=description`
Then STDOUT should be:
"""
Gets a test post
"""

@require-wp-6.9
Scenario: Check if an ability exists
Given a wp-content/mu-plugins/test-abilities.php file:
"""
<?php
add_action( 'wp_abilities_api_init', function() {
wp_register_ability( 'my-plugin/test-exists', array(
'label' => 'Test Exists',
'category' => 'site',
'description' => 'Test exists',
'permission_callback' => '__return_true',
'execute_callback' => function( $input ) {
return array( 'result' => 'ok' );
},
'input_schema' => array( 'type' => 'object' ),
'output_schema' => array( 'type' => 'object' ),
) );
} );
"""

When I try `wp ability exists my-plugin/test-exists`
Then the return code should be 0

When I try `wp ability exists non-existent-ability`
Then the return code should be 1

@require-wp-6.9
Scenario: Execute an ability with JSON input
Given a wp-content/mu-plugins/test-abilities.php file:
"""
<?php
add_action( 'wp_abilities_api_init', function() {
wp_register_ability( 'my-plugin/echo-input', array(
'label' => 'Echo Input',
'category' => 'site',
'description' => 'Echoes input',
'permission_callback' => '__return_true',
'execute_callback' => function( $input ) {
return array( 'echoed' => $input );
},
'input_schema' => array( 'type' => 'object' ),
'output_schema' => array( 'type' => 'object' ),
) );
} );
"""

When I try `wp ability execute non-existent-ability '{"test": "data"}'`
Then STDERR should contain:
"""
Error: Ability non-existent-ability doesn't exist.
"""
And the return code should be 1

When I run `wp ability execute my-plugin/echo-input '{"message": "hello"}'`
Then STDOUT should contain:
"""
"echoed"
"""
And STDOUT should contain:
"""
"message"
"""
And STDOUT should contain:
"""
"hello"
"""

@require-wp-6.9
Scenario: Execute an ability with input from STDIN
Given a wp-content/mu-plugins/test-abilities.php file:
"""
<?php
add_action( 'wp_abilities_api_init', function() {
wp_register_ability( 'my-plugin/process-input', array(
'label' => 'Process Input',
'category' => 'site',
'description' => 'Processes input',
'permission_callback' => '__return_true',
'execute_callback' => function( $input ) {
return array( 'processed' => true, 'data' => $input );
},
'input_schema' => array( 'type' => 'object' ),
'output_schema' => array( 'type' => 'object' ),
) );
} );
"""

When I run `echo '{"value": 42}' | wp ability execute my-plugin/process-input`
Then STDOUT should contain:
"""
"processed": true
"""
And STDOUT should contain:
"""
"value": 42
"""
1 change: 1 addition & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<exclude-pattern>*/src/Site(_Meta|_Option)?_Command\.php$</exclude-pattern>
<exclude-pattern>*/src/Term(_Meta)?_Command\.php$</exclude-pattern>
<exclude-pattern>*/src/User(_Application_Password|_Meta|_Session|_Term)?_Command\.php$</exclude-pattern>
<exclude-pattern>*/src/Ability(_Category)?_Command\.php$</exclude-pattern>
</rule>

<!-- Whitelisting to provide backward compatibility to classes possibly extending this class. -->
Expand Down
Loading
Loading