|
| 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