Skip to content

Commit 783e899

Browse files
Copilotswissspidy
andcommitted
Add Ability and Ability Category commands with tests
Co-authored-by: swissspidy <[email protected]>
1 parent ce264e7 commit 783e899

File tree

6 files changed

+968
-0
lines changed

6 files changed

+968
-0
lines changed

composer.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@
3838
},
3939
"bundled": true,
4040
"commands": [
41+
"ability",
42+
"ability category",
43+
"ability category exists",
44+
"ability category get",
45+
"ability category list",
46+
"ability execute",
47+
"ability exists",
48+
"ability get",
49+
"ability list",
4150
"comment",
4251
"comment approve",
4352
"comment count",

entity-command.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,26 @@
9292
},
9393
)
9494
);
95+
96+
WP_CLI::add_command(
97+
'ability',
98+
'Ability_Command',
99+
array(
100+
'before_invoke' => function () {
101+
if ( Utils\wp_version_compare( '6.9', '<' ) ) {
102+
WP_CLI::error( 'Requires WordPress 6.9 or greater.' );
103+
}
104+
},
105+
)
106+
);
107+
WP_CLI::add_command(
108+
'ability category',
109+
'Ability_Category_Command',
110+
array(
111+
'before_invoke' => function () {
112+
if ( Utils\wp_version_compare( '6.9', '<' ) ) {
113+
WP_CLI::error( 'Requires WordPress 6.9 or greater.' );
114+
}
115+
},
116+
)
117+
);

features/ability-category.feature

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
Feature: Manage WordPress ability categories
2+
3+
Background:
4+
Given a WP install
5+
6+
@require-wp-6.9
7+
Scenario: Ability category commands require WordPress 6.9+
8+
When I try `wp core download --version=6.8 --force`
9+
And I run `wp core version`
10+
Then STDOUT should contain:
11+
"""
12+
6.8
13+
"""
14+
15+
When I try `wp ability category list`
16+
Then STDERR should contain:
17+
"""
18+
Error: Requires WordPress 6.9 or greater.
19+
"""
20+
And the return code should be 1
21+
22+
@require-wp-6.9
23+
Scenario: List ability categories
24+
Given a wp-content/mu-plugins/test-ability-categories.php file:
25+
"""
26+
<?php
27+
add_action( 'init', function() {
28+
if ( ! function_exists( 'wp_register_ability_category' ) ) {
29+
return;
30+
}
31+
32+
wp_register_ability_category( 'test_category_1', array(
33+
'description' => 'First test category',
34+
) );
35+
36+
wp_register_ability_category( 'test_category_2', array(
37+
'description' => 'Second test category',
38+
) );
39+
} );
40+
"""
41+
42+
When I run `wp ability category list --format=count`
43+
Then STDOUT should contain:
44+
"""
45+
2
46+
"""
47+
48+
When I run `wp ability category list --fields=name,description --format=csv`
49+
Then STDOUT should contain:
50+
"""
51+
test_category_1,"First test category"
52+
"""
53+
And STDOUT should contain:
54+
"""
55+
test_category_2,"Second test category"
56+
"""
57+
58+
@require-wp-6.9
59+
Scenario: Get a specific ability category
60+
Given a wp-content/mu-plugins/test-ability-categories.php file:
61+
"""
62+
<?php
63+
add_action( 'init', function() {
64+
if ( ! function_exists( 'wp_register_ability_category' ) ) {
65+
return;
66+
}
67+
68+
wp_register_ability_category( 'content_ops', array(
69+
'description' => 'Content operations category',
70+
) );
71+
} );
72+
"""
73+
74+
When I try `wp ability category get invalid_category`
75+
Then STDERR should contain:
76+
"""
77+
Error: Ability category invalid_category doesn't exist.
78+
"""
79+
And the return code should be 1
80+
81+
When I run `wp ability category get content_ops --field=description`
82+
Then STDOUT should be:
83+
"""
84+
Content operations category
85+
"""
86+
87+
When I run `wp ability category get content_ops --field=name`
88+
Then STDOUT should be:
89+
"""
90+
content_ops
91+
"""
92+
93+
@require-wp-6.9
94+
Scenario: Check if an ability category exists
95+
Given a wp-content/mu-plugins/test-ability-categories.php file:
96+
"""
97+
<?php
98+
add_action( 'init', function() {
99+
if ( ! function_exists( 'wp_register_ability_category' ) ) {
100+
return;
101+
}
102+
103+
wp_register_ability_category( 'existing_category', array(
104+
'description' => 'This category exists',
105+
) );
106+
} );
107+
"""
108+
109+
When I try `wp ability category exists existing_category`
110+
Then the return code should be 0
111+
112+
When I try `wp ability category exists non_existent_category`
113+
Then the return code should be 1

features/ability.feature

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
Feature: Manage WordPress abilities
2+
3+
Background:
4+
Given a WP install
5+
6+
@require-wp-6.9
7+
Scenario: Ability commands require WordPress 6.9+
8+
When I try `wp core download --version=6.8 --force`
9+
And I run `wp core version`
10+
Then STDOUT should contain:
11+
"""
12+
6.8
13+
"""
14+
15+
When I try `wp ability list`
16+
Then STDERR should contain:
17+
"""
18+
Error: Requires WordPress 6.9 or greater.
19+
"""
20+
And the return code should be 1
21+
22+
@require-wp-6.9
23+
Scenario: List abilities
24+
Given a wp-content/mu-plugins/test-abilities.php file:
25+
"""
26+
<?php
27+
add_action( 'init', function() {
28+
if ( ! function_exists( 'wp_register_ability' ) ) {
29+
return;
30+
}
31+
32+
wp_register_ability( 'test_ability_1', array(
33+
'category' => 'content',
34+
'description' => 'Test ability one',
35+
'callback' => function( $input ) {
36+
return array( 'result' => 'success', 'input' => $input );
37+
},
38+
'input_schema' => array(
39+
'type' => 'object',
40+
'properties' => array(
41+
'id' => array( 'type' => 'integer' ),
42+
),
43+
),
44+
'output_schema' => array(
45+
'type' => 'object',
46+
),
47+
) );
48+
49+
wp_register_ability( 'test_ability_2', array(
50+
'category' => 'users',
51+
'description' => 'Test ability two',
52+
'callback' => function( $input ) {
53+
return array( 'result' => 'done' );
54+
},
55+
'input_schema' => array( 'type' => 'object' ),
56+
'output_schema' => array( 'type' => 'object' ),
57+
) );
58+
} );
59+
"""
60+
61+
When I run `wp ability list --format=count`
62+
Then STDOUT should contain:
63+
"""
64+
2
65+
"""
66+
67+
When I run `wp ability list --fields=name,category,description --format=csv`
68+
Then STDOUT should contain:
69+
"""
70+
test_ability_1,content,"Test ability one"
71+
"""
72+
And STDOUT should contain:
73+
"""
74+
test_ability_2,users,"Test ability two"
75+
"""
76+
77+
When I run `wp ability list --category=content --format=count`
78+
Then STDOUT should contain:
79+
"""
80+
1
81+
"""
82+
83+
@require-wp-6.9
84+
Scenario: Get a specific ability
85+
Given a wp-content/mu-plugins/test-abilities.php file:
86+
"""
87+
<?php
88+
add_action( 'init', function() {
89+
if ( ! function_exists( 'wp_register_ability' ) ) {
90+
return;
91+
}
92+
93+
wp_register_ability( 'get_test_post', array(
94+
'category' => 'content',
95+
'description' => 'Gets a test post',
96+
'callback' => function( $input ) {
97+
return array( 'id' => $input['id'], 'title' => 'Test Post' );
98+
},
99+
'input_schema' => array(
100+
'type' => 'object',
101+
'properties' => array(
102+
'id' => array( 'type' => 'integer' ),
103+
),
104+
),
105+
'output_schema' => array(
106+
'type' => 'object',
107+
),
108+
) );
109+
} );
110+
"""
111+
112+
When I try `wp ability get invalid_ability`
113+
Then STDERR should contain:
114+
"""
115+
Error: Ability invalid_ability doesn't exist.
116+
"""
117+
And the return code should be 1
118+
119+
When I run `wp ability get get_test_post --field=category`
120+
Then STDOUT should be:
121+
"""
122+
content
123+
"""
124+
125+
When I run `wp ability get get_test_post --field=description`
126+
Then STDOUT should be:
127+
"""
128+
Gets a test post
129+
"""
130+
131+
@require-wp-6.9
132+
Scenario: Check if an ability exists
133+
Given a wp-content/mu-plugins/test-abilities.php file:
134+
"""
135+
<?php
136+
add_action( 'init', function() {
137+
if ( ! function_exists( 'wp_register_ability' ) ) {
138+
return;
139+
}
140+
141+
wp_register_ability( 'test_exists', array(
142+
'category' => 'content',
143+
'description' => 'Test exists',
144+
'callback' => function( $input ) {
145+
return array( 'result' => 'ok' );
146+
},
147+
'input_schema' => array( 'type' => 'object' ),
148+
'output_schema' => array( 'type' => 'object' ),
149+
) );
150+
} );
151+
"""
152+
153+
When I try `wp ability exists test_exists`
154+
Then the return code should be 0
155+
156+
When I try `wp ability exists non_existent_ability`
157+
Then the return code should be 1
158+
159+
@require-wp-6.9
160+
Scenario: Execute an ability with JSON input
161+
Given a wp-content/mu-plugins/test-abilities.php file:
162+
"""
163+
<?php
164+
add_action( 'init', function() {
165+
if ( ! function_exists( 'wp_register_ability' ) ) {
166+
return;
167+
}
168+
169+
wp_register_ability( 'echo_input', array(
170+
'category' => 'testing',
171+
'description' => 'Echoes input',
172+
'callback' => function( $input ) {
173+
return array( 'echoed' => $input );
174+
},
175+
'input_schema' => array( 'type' => 'object' ),
176+
'output_schema' => array( 'type' => 'object' ),
177+
) );
178+
} );
179+
"""
180+
181+
When I try `wp ability execute non_existent_ability '{"test": "data"}'`
182+
Then STDERR should contain:
183+
"""
184+
Error: Ability non_existent_ability doesn't exist.
185+
"""
186+
And the return code should be 1
187+
188+
When I run `wp ability execute echo_input '{"message": "hello"}'`
189+
Then STDOUT should contain:
190+
"""
191+
"echoed"
192+
"""
193+
And STDOUT should contain:
194+
"""
195+
"message"
196+
"""
197+
And STDOUT should contain:
198+
"""
199+
"hello"
200+
"""
201+
And STDOUT should contain:
202+
"""
203+
Success: Ability executed successfully.
204+
"""
205+
206+
@require-wp-6.9
207+
Scenario: Execute an ability with input from STDIN
208+
Given a wp-content/mu-plugins/test-abilities.php file:
209+
"""
210+
<?php
211+
add_action( 'init', function() {
212+
if ( ! function_exists( 'wp_register_ability' ) ) {
213+
return;
214+
}
215+
216+
wp_register_ability( 'process_input', array(
217+
'category' => 'testing',
218+
'description' => 'Processes input',
219+
'callback' => function( $input ) {
220+
return array( 'processed' => true, 'data' => $input );
221+
},
222+
'input_schema' => array( 'type' => 'object' ),
223+
'output_schema' => array( 'type' => 'object' ),
224+
) );
225+
} );
226+
"""
227+
228+
When I run `echo '{"value": 42}' | wp ability execute process_input`
229+
Then STDOUT should contain:
230+
"""
231+
"processed": true
232+
"""
233+
And STDOUT should contain:
234+
"""
235+
"value": 42
236+
"""
237+
And STDOUT should contain:
238+
"""
239+
Success: Ability executed successfully.
240+
"""

0 commit comments

Comments
 (0)