|
| 1 | +<?php |
| 2 | + |
| 3 | +declare( strict_types=1 ); |
| 4 | + |
| 5 | +/** |
| 6 | + * Tests for the core/get-settings ability via REST API. |
| 7 | + * |
| 8 | + * @covers WP_Settings_Abilities |
| 9 | + * |
| 10 | + * @group abilities-api |
| 11 | + * @group rest-api |
| 12 | + */ |
| 13 | +class Tests_REST_API_WpRestAbilitiesSettingsController extends WP_UnitTestCase { |
| 14 | + |
| 15 | + /** |
| 16 | + * REST Server instance. |
| 17 | + * |
| 18 | + * @var WP_REST_Server |
| 19 | + */ |
| 20 | + protected $server; |
| 21 | + |
| 22 | + /** |
| 23 | + * Administrator user ID. |
| 24 | + * |
| 25 | + * @var int |
| 26 | + */ |
| 27 | + protected static $admin_id; |
| 28 | + |
| 29 | + /** |
| 30 | + * Subscriber user ID. |
| 31 | + * |
| 32 | + * @var int |
| 33 | + */ |
| 34 | + protected static $subscriber_id; |
| 35 | + |
| 36 | + /** |
| 37 | + * Set up before class. |
| 38 | + */ |
| 39 | + public static function set_up_before_class(): void { |
| 40 | + parent::set_up_before_class(); |
| 41 | + |
| 42 | + self::$admin_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); |
| 43 | + self::$subscriber_id = self::factory()->user->create( array( 'role' => 'subscriber' ) ); |
| 44 | + |
| 45 | + // Register initial settings first so abilities can build schemas. |
| 46 | + register_initial_settings(); |
| 47 | + |
| 48 | + // Ensure core abilities are registered for these tests. |
| 49 | + remove_action( 'wp_abilities_api_categories_init', '_unhook_core_ability_categories_registration', 1 ); |
| 50 | + remove_action( 'wp_abilities_api_init', '_unhook_core_abilities_registration', 1 ); |
| 51 | + |
| 52 | + add_action( 'wp_abilities_api_categories_init', 'wp_register_core_ability_categories' ); |
| 53 | + add_action( 'wp_abilities_api_init', 'wp_register_core_abilities' ); |
| 54 | + do_action( 'wp_abilities_api_categories_init' ); |
| 55 | + do_action( 'wp_abilities_api_init' ); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Tear down after class. |
| 60 | + */ |
| 61 | + public static function tear_down_after_class(): void { |
| 62 | + // Re-add the unhook functions for subsequent tests. |
| 63 | + add_action( 'wp_abilities_api_categories_init', '_unhook_core_ability_categories_registration', 1 ); |
| 64 | + add_action( 'wp_abilities_api_init', '_unhook_core_abilities_registration', 1 ); |
| 65 | + |
| 66 | + // Remove the core abilities and their categories. |
| 67 | + foreach ( wp_get_abilities() as $ability ) { |
| 68 | + wp_unregister_ability( $ability->get_name() ); |
| 69 | + } |
| 70 | + foreach ( wp_get_ability_categories() as $ability_category ) { |
| 71 | + wp_unregister_ability_category( $ability_category->get_slug() ); |
| 72 | + } |
| 73 | + |
| 74 | + parent::tear_down_after_class(); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Set up before each test. |
| 79 | + */ |
| 80 | + public function set_up(): void { |
| 81 | + parent::set_up(); |
| 82 | + |
| 83 | + global $wp_rest_server; |
| 84 | + $wp_rest_server = new WP_REST_Server(); |
| 85 | + $this->server = $wp_rest_server; |
| 86 | + |
| 87 | + do_action( 'rest_api_init' ); |
| 88 | + |
| 89 | + wp_set_current_user( self::$admin_id ); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Tear down after each test. |
| 94 | + */ |
| 95 | + public function tear_down(): void { |
| 96 | + global $wp_rest_server; |
| 97 | + $wp_rest_server = null; |
| 98 | + |
| 99 | + parent::tear_down(); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Tests that unauthenticated users cannot access the get-settings ability. |
| 104 | + * |
| 105 | + * @ticket 62635 |
| 106 | + */ |
| 107 | + public function test_core_get_settings_requires_authentication(): void { |
| 108 | + wp_set_current_user( 0 ); |
| 109 | + |
| 110 | + $request = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities/core/get-settings/run' ); |
| 111 | + $response = $this->server->dispatch( $request ); |
| 112 | + |
| 113 | + $this->assertSame( 401, $response->get_status() ); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Tests that subscribers cannot access the get-settings ability. |
| 118 | + * |
| 119 | + * @ticket 62635 |
| 120 | + */ |
| 121 | + public function test_core_get_settings_requires_manage_options_capability(): void { |
| 122 | + wp_set_current_user( self::$subscriber_id ); |
| 123 | + |
| 124 | + $request = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities/core/get-settings/run' ); |
| 125 | + $response = $this->server->dispatch( $request ); |
| 126 | + |
| 127 | + $this->assertSame( 403, $response->get_status() ); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Tests that administrators can access the get-settings ability. |
| 132 | + * |
| 133 | + * @ticket 62635 |
| 134 | + */ |
| 135 | + public function test_core_get_settings_allows_administrators(): void { |
| 136 | + $request = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities/core/get-settings/run' ); |
| 137 | + $response = $this->server->dispatch( $request ); |
| 138 | + |
| 139 | + $this->assertSame( 200, $response->get_status() ); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Tests that the get-settings ability returns settings grouped by registration group. |
| 144 | + * |
| 145 | + * @ticket 62635 |
| 146 | + */ |
| 147 | + public function test_core_get_settings_returns_grouped_settings(): void { |
| 148 | + $request = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities/core/get-settings/run' ); |
| 149 | + $response = $this->server->dispatch( $request ); |
| 150 | + |
| 151 | + $this->assertSame( 200, $response->get_status() ); |
| 152 | + |
| 153 | + $data = $response->get_data(); |
| 154 | + |
| 155 | + $this->assertIsArray( $data ); |
| 156 | + $this->assertArrayHasKey( 'general', $data ); |
| 157 | + $this->assertArrayHasKey( 'blogname', $data['general'] ); |
| 158 | + $this->assertArrayHasKey( 'blogdescription', $data['general'] ); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Tests that the get-settings ability can filter by group. |
| 163 | + * |
| 164 | + * @ticket 62635 |
| 165 | + */ |
| 166 | + public function test_core_get_settings_filters_by_group(): void { |
| 167 | + $request = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities/core/get-settings/run' ); |
| 168 | + $request->set_query_params( |
| 169 | + array( |
| 170 | + 'input' => array( |
| 171 | + 'group' => 'general', |
| 172 | + ), |
| 173 | + ) |
| 174 | + ); |
| 175 | + $response = $this->server->dispatch( $request ); |
| 176 | + |
| 177 | + $this->assertSame( 200, $response->get_status() ); |
| 178 | + |
| 179 | + $data = $response->get_data(); |
| 180 | + |
| 181 | + $this->assertIsArray( $data ); |
| 182 | + $this->assertCount( 1, $data ); |
| 183 | + $this->assertArrayHasKey( 'general', $data ); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Tests that the get-settings ability can filter by specific slugs. |
| 188 | + * |
| 189 | + * @ticket 62635 |
| 190 | + */ |
| 191 | + public function test_core_get_settings_filters_by_slugs(): void { |
| 192 | + $request = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities/core/get-settings/run' ); |
| 193 | + $request->set_query_params( |
| 194 | + array( |
| 195 | + 'input' => array( |
| 196 | + 'slugs' => array( 'blogname', 'blogdescription' ), |
| 197 | + ), |
| 198 | + ) |
| 199 | + ); |
| 200 | + $response = $this->server->dispatch( $request ); |
| 201 | + |
| 202 | + $this->assertSame( 200, $response->get_status() ); |
| 203 | + |
| 204 | + $data = $response->get_data(); |
| 205 | + |
| 206 | + $this->assertIsArray( $data ); |
| 207 | + $this->assertArrayHasKey( 'general', $data ); |
| 208 | + $this->assertCount( 2, $data['general'] ); |
| 209 | + $this->assertArrayHasKey( 'blogname', $data['general'] ); |
| 210 | + $this->assertArrayHasKey( 'blogdescription', $data['general'] ); |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * Tests that settings without show_in_abilities are excluded. |
| 215 | + * |
| 216 | + * @ticket 62635 |
| 217 | + */ |
| 218 | + public function test_core_get_settings_excludes_settings_without_show_in_abilities(): void { |
| 219 | + register_setting( |
| 220 | + 'general', |
| 221 | + 'test_setting_excluded', |
| 222 | + array( |
| 223 | + 'type' => 'string', |
| 224 | + 'default' => 'test_value', |
| 225 | + 'show_in_abilities' => false, |
| 226 | + ) |
| 227 | + ); |
| 228 | + update_option( 'test_setting_excluded', 'test_value' ); |
| 229 | + |
| 230 | + $request = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities/core/get-settings/run' ); |
| 231 | + $response = $this->server->dispatch( $request ); |
| 232 | + |
| 233 | + $this->assertSame( 200, $response->get_status() ); |
| 234 | + |
| 235 | + $data = $response->get_data(); |
| 236 | + |
| 237 | + $this->assertArrayNotHasKey( 'test_setting_excluded', $data['general'] ?? array() ); |
| 238 | + |
| 239 | + unregister_setting( 'general', 'test_setting_excluded' ); |
| 240 | + delete_option( 'test_setting_excluded' ); |
| 241 | + } |
| 242 | + |
| 243 | + /** |
| 244 | + * Tests that core settings with show_in_abilities are included. |
| 245 | + * |
| 246 | + * @ticket 62635 |
| 247 | + */ |
| 248 | + public function test_core_get_settings_includes_settings_with_show_in_abilities(): void { |
| 249 | + $request = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities/core/get-settings/run' ); |
| 250 | + $response = $this->server->dispatch( $request ); |
| 251 | + |
| 252 | + $this->assertSame( 200, $response->get_status() ); |
| 253 | + |
| 254 | + $data = $response->get_data(); |
| 255 | + |
| 256 | + // blogname has show_in_abilities => true in register_initial_settings(). |
| 257 | + $this->assertArrayHasKey( 'general', $data ); |
| 258 | + $this->assertArrayHasKey( 'blogname', $data['general'] ); |
| 259 | + |
| 260 | + // use_smilies has show_in_abilities => true. |
| 261 | + $this->assertArrayHasKey( 'writing', $data ); |
| 262 | + $this->assertArrayHasKey( 'use_smilies', $data['writing'] ); |
| 263 | + } |
| 264 | + |
| 265 | + /** |
| 266 | + * Tests that boolean settings are cast to actual booleans. |
| 267 | + * |
| 268 | + * @ticket 62635 |
| 269 | + */ |
| 270 | + public function test_core_get_settings_casts_boolean_values(): void { |
| 271 | + $request = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities/core/get-settings/run' ); |
| 272 | + $request->set_query_params( |
| 273 | + array( |
| 274 | + 'input' => array( |
| 275 | + 'slugs' => array( 'use_smilies' ), |
| 276 | + ), |
| 277 | + ) |
| 278 | + ); |
| 279 | + $response = $this->server->dispatch( $request ); |
| 280 | + |
| 281 | + $this->assertSame( 200, $response->get_status() ); |
| 282 | + |
| 283 | + $data = $response->get_data(); |
| 284 | + |
| 285 | + $this->assertArrayHasKey( 'writing', $data ); |
| 286 | + $this->assertArrayHasKey( 'use_smilies', $data['writing'] ); |
| 287 | + $this->assertIsBool( $data['writing']['use_smilies'] ); |
| 288 | + } |
| 289 | + |
| 290 | + /** |
| 291 | + * Tests that integer settings are cast to actual integers. |
| 292 | + * |
| 293 | + * @ticket 62635 |
| 294 | + */ |
| 295 | + public function test_core_get_settings_casts_integer_values(): void { |
| 296 | + $request = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities/core/get-settings/run' ); |
| 297 | + $request->set_query_params( |
| 298 | + array( |
| 299 | + 'input' => array( |
| 300 | + 'slugs' => array( 'start_of_week' ), |
| 301 | + ), |
| 302 | + ) |
| 303 | + ); |
| 304 | + $response = $this->server->dispatch( $request ); |
| 305 | + |
| 306 | + $this->assertSame( 200, $response->get_status() ); |
| 307 | + |
| 308 | + $data = $response->get_data(); |
| 309 | + |
| 310 | + $this->assertArrayHasKey( 'general', $data ); |
| 311 | + $this->assertArrayHasKey( 'start_of_week', $data['general'] ); |
| 312 | + $this->assertIsInt( $data['general']['start_of_week'] ); |
| 313 | + } |
| 314 | + |
| 315 | + /** |
| 316 | + * Tests that the get-settings ability requires GET method (read-only). |
| 317 | + * |
| 318 | + * @ticket 62635 |
| 319 | + */ |
| 320 | + public function test_core_get_settings_requires_get_method(): void { |
| 321 | + $request = new WP_REST_Request( 'POST', '/wp-abilities/v1/abilities/core/get-settings/run' ); |
| 322 | + $request->set_header( 'Content-Type', 'application/json' ); |
| 323 | + $request->set_body( wp_json_encode( array( 'input' => array() ) ) ); |
| 324 | + $response = $this->server->dispatch( $request ); |
| 325 | + |
| 326 | + $this->assertSame( 405, $response->get_status() ); |
| 327 | + |
| 328 | + $data = $response->get_data(); |
| 329 | + $this->assertSame( 'rest_ability_invalid_method', $data['code'] ); |
| 330 | + } |
| 331 | + |
| 332 | + /** |
| 333 | + * Tests that the get-settings ability returns correct values. |
| 334 | + * |
| 335 | + * @ticket 62635 |
| 336 | + */ |
| 337 | + public function test_core_get_settings_returns_correct_values(): void { |
| 338 | + update_option( 'blogname', 'Test Site Name' ); |
| 339 | + |
| 340 | + $request = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities/core/get-settings/run' ); |
| 341 | + $request->set_query_params( |
| 342 | + array( |
| 343 | + 'input' => array( |
| 344 | + 'slugs' => array( 'blogname' ), |
| 345 | + ), |
| 346 | + ) |
| 347 | + ); |
| 348 | + $response = $this->server->dispatch( $request ); |
| 349 | + |
| 350 | + $this->assertSame( 200, $response->get_status() ); |
| 351 | + |
| 352 | + $data = $response->get_data(); |
| 353 | + |
| 354 | + $this->assertSame( 'Test Site Name', $data['general']['blogname'] ); |
| 355 | + } |
| 356 | +} |
0 commit comments