| | 1 | <?php |
| | 2 | /** |
| | 3 | * Testing ajax customize menus functionality |
| | 4 | * |
| | 5 | * @package WordPress |
| | 6 | * @subpackage UnitTests |
| | 7 | * @since 4.3.0 |
| | 8 | * @group ajax |
| | 9 | * @runTestsInSeparateProcesses |
| | 10 | */ |
| | 11 | class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase { |
| | 12 | |
| | 13 | /** |
| | 14 | * Instance of WP_Customize_Manager which is reset for each test. |
| | 15 | * |
| | 16 | * @var WP_Customize_Manager |
| | 17 | */ |
| | 18 | public $wp_customize; |
| | 19 | |
| | 20 | /** |
| | 21 | * Set up the test fixture. |
| | 22 | */ |
| | 23 | public function setUp() { |
| | 24 | parent::setUp(); |
| | 25 | require_once ABSPATH . WPINC . '/class-wp-customize-manager.php'; |
| | 26 | wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) ); |
| | 27 | global $wp_customize; |
| | 28 | $this->wp_customize = new WP_Customize_Manager(); |
| | 29 | $wp_customize = $this->wp_customize; |
| | 30 | } |
| | 31 | |
| | 32 | /** |
| | 33 | * Tear down the test fixture. |
| | 34 | */ |
| | 35 | public function tearDown() { |
| | 36 | wp_set_current_user( 0 ); |
| | 37 | parent::tearDown(); |
| | 38 | } |
| | 39 | |
| | 40 | /** |
| | 41 | * Helper to keep it DRY |
| | 42 | * @param $action |
| | 43 | */ |
| | 44 | protected function make_ajax_call( $action ) { |
| | 45 | // Make the request. |
| | 46 | try { |
| | 47 | $this->_handleAjax( $action ); |
| | 48 | } catch ( WPAjaxDieContinueException $e ) { |
| | 49 | unset( $e ); |
| | 50 | } |
| | 51 | } |
| | 52 | |
| | 53 | /** |
| | 54 | * Testing capabilities check for ajax_load_available_items method |
| | 55 | * |
| | 56 | * @dataProvider data_ajax_load_available_items_cap_check |
| | 57 | * |
| | 58 | * @param string $role The role we're checking caps against |
| | 59 | * @param array $expected_results |
| | 60 | */ |
| | 61 | function test_ajax_load_available_items_cap_check( $role, $expected_results ) { |
| | 62 | |
| | 63 | if ( 'administrator' != $role ) { |
| | 64 | // If we're not an admin, we should get a wp_die(-1). |
| | 65 | $this->setExpectedException( 'WPAjaxDieStopException' ); |
| | 66 | } |
| | 67 | |
| | 68 | wp_set_current_user( $this->factory->user->create( array( 'role' => $role ) ) ); |
| | 69 | |
| | 70 | $_POST = array( |
| | 71 | 'action' => 'load-available-menu-items-customizer', |
| | 72 | 'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ), |
| | 73 | ); |
| | 74 | |
| | 75 | $this->make_ajax_call( 'load-available-menu-items-customizer' ); |
| | 76 | |
| | 77 | // If we are an admin, we should get a proper response. |
| | 78 | if ( 'administrator' === $role ) { |
| | 79 | // Get the results. |
| | 80 | $response = json_decode( $this->_last_response, true ); |
| | 81 | |
| | 82 | $this->assertSame( $expected_results, $response ); |
| | 83 | } |
| | 84 | |
| | 85 | } |
| | 86 | |
| | 87 | /** |
| | 88 | * |
| | 89 | * Data provider. |
| | 90 | * |
| | 91 | * Provides various post_args to induce error messages in the that can be |
| | 92 | * compared to the expected_results. |
| | 93 | * |
| | 94 | * @since 4.3.0 |
| | 95 | * |
| | 96 | * @return array { |
| | 97 | * @type array { |
| | 98 | * @string string $role The role that will test caps for. |
| | 99 | * @array array $expected_results The expected results from the ajax call. |
| | 100 | * } |
| | 101 | * } |
| | 102 | * |
| | 103 | */ |
| | 104 | function data_ajax_load_available_items_cap_check() { |
| | 105 | return array( |
| | 106 | array( |
| | 107 | 'subscriber', |
| | 108 | array(), |
| | 109 | ), |
| | 110 | array( |
| | 111 | 'contributor', |
| | 112 | array(), |
| | 113 | ), |
| | 114 | array( |
| | 115 | 'author', |
| | 116 | array(), |
| | 117 | ), |
| | 118 | array( |
| | 119 | 'editor', |
| | 120 | array(), |
| | 121 | ), |
| | 122 | array( |
| | 123 | 'administrator', |
| | 124 | array( |
| | 125 | 'success' => false, |
| | 126 | 'data' => 'nav_menus_missing_obj_type_or_type_parameter', |
| | 127 | ), |
| | 128 | ), |
| | 129 | ); |
| | 130 | } |
| | 131 | |
| | 132 | /** |
| | 133 | * Testing the error messaging for ajax_load_available_items |
| | 134 | * |
| | 135 | * @dataProvider data_ajax_load_available_items_error_messages |
| | 136 | * |
| | 137 | */ |
| | 138 | function test_ajax_load_available_items_error_messages( $post_args, $expected_results ) { |
| | 139 | |
| | 140 | $_POST = array_merge( array( |
| | 141 | 'action' => 'load-available-menu-items-customizer', |
| | 142 | 'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ), |
| | 143 | ), $post_args ); |
| | 144 | |
| | 145 | // Make the request. |
| | 146 | $this->make_ajax_call( 'load-available-menu-items-customizer' ); |
| | 147 | |
| | 148 | // Get the results. |
| | 149 | $response = json_decode( $this->_last_response, true ); |
| | 150 | |
| | 151 | $this->assertSame( $expected_results, $response ); |
| | 152 | } |
| | 153 | |
| | 154 | /** |
| | 155 | * |
| | 156 | * Data provider. |
| | 157 | * |
| | 158 | * Provides various post_args to induce error messages in the that can be |
| | 159 | * compared to the expected_results. |
| | 160 | * |
| | 161 | * @since 4.3.0 |
| | 162 | * |
| | 163 | * @return array { |
| | 164 | * @type array { |
| | 165 | * @array array $post_args The arguments that will merged with the $_POST array. |
| | 166 | * @array array $expected_results The expected results from the ajax call. |
| | 167 | * } |
| | 168 | * } |
| | 169 | * |
| | 170 | */ |
| | 171 | function data_ajax_load_available_items_error_messages() { |
| | 172 | return array( |
| | 173 | // Testing empty obj_type and type. |
| | 174 | array( |
| | 175 | array( |
| | 176 | 'obj_type' => '', |
| | 177 | 'type' => '', |
| | 178 | ), |
| | 179 | array( |
| | 180 | 'success' => false, |
| | 181 | 'data' => 'nav_menus_missing_obj_type_or_type_parameter', |
| | 182 | ), |
| | 183 | ), |
| | 184 | // Testing empty obj_type. |
| | 185 | array( |
| | 186 | array( |
| | 187 | 'obj_type' => '', |
| | 188 | 'type' => 'post', |
| | 189 | ), |
| | 190 | array( |
| | 191 | 'success' => false, |
| | 192 | 'data' => 'nav_menus_missing_obj_type_or_type_parameter', |
| | 193 | ), |
| | 194 | ), |
| | 195 | // Testing empty type. |
| | 196 | array( |
| | 197 | array( |
| | 198 | 'obj_type' => '', |
| | 199 | 'type' => 'post', |
| | 200 | ), |
| | 201 | array( |
| | 202 | 'success' => false, |
| | 203 | 'data' => 'nav_menus_missing_obj_type_or_type_parameter', |
| | 204 | ), |
| | 205 | ), |
| | 206 | // Testing incorrect obj_type option. |
| | 207 | array( |
| | 208 | array( |
| | 209 | 'obj_type' => 'invalid', |
| | 210 | 'type' => 'post', |
| | 211 | ), |
| | 212 | array( |
| | 213 | 'success' => false, |
| | 214 | 'data' => 'nav_menus_invalid_obj_type', |
| | 215 | ), |
| | 216 | ), |
| | 217 | // Testing incorrect type option. |
| | 218 | array( |
| | 219 | array( |
| | 220 | 'obj_type' => 'post_type', |
| | 221 | 'type' => 'invalid', |
| | 222 | ), |
| | 223 | array( |
| | 224 | 'success' => false, |
| | 225 | 'data' => 'nav_menus_invalid_post_type', |
| | 226 | ), |
| | 227 | ), |
| | 228 | ); |
| | 229 | } |
| | 230 | |
| | 231 | /** |
| | 232 | * Testing the success status |
| | 233 | * |
| | 234 | * @dataProvider data_ajax_load_available_items_sucess_status |
| | 235 | */ |
| | 236 | function test_ajax_load_available_items_sucess_status( $post_args, $success_status ) { |
| | 237 | |
| | 238 | $_POST = array_merge( array( |
| | 239 | 'action' => 'load-available-menu-items-customizer', |
| | 240 | 'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ), |
| | 241 | ), $post_args ); |
| | 242 | |
| | 243 | // Make the request. |
| | 244 | $this->make_ajax_call( 'load-available-menu-items-customizer' ); |
| | 245 | |
| | 246 | // Get the results. |
| | 247 | $response = json_decode( $this->_last_response, true ); |
| | 248 | $this->assertSame( $success_status, $response['success'] ); |
| | 249 | |
| | 250 | } |
| | 251 | |
| | 252 | /** |
| | 253 | * |
| | 254 | * Data provider. |
| | 255 | * |
| | 256 | * Provides various post_args to retrieve results and compare against |
| | 257 | * the success status. |
| | 258 | * |
| | 259 | * @since 4.3.0 |
| | 260 | * |
| | 261 | * @return array { |
| | 262 | * @type array { |
| | 263 | * @type array $post_args The arguments that will merged with the $_POST array. |
| | 264 | * @type bool $success_status The expected success status. |
| | 265 | * } |
| | 266 | * } |
| | 267 | * |
| | 268 | */ |
| | 269 | function data_ajax_load_available_items_sucess_status() { |
| | 270 | return array( |
| | 271 | array( |
| | 272 | array( |
| | 273 | 'obj_type' => 'post_type', |
| | 274 | 'type' => 'post', |
| | 275 | ), |
| | 276 | true, |
| | 277 | ), |
| | 278 | array( |
| | 279 | array( |
| | 280 | 'obj_type' => 'post_type', |
| | 281 | 'type' => 'page', |
| | 282 | ), |
| | 283 | true, |
| | 284 | ), |
| | 285 | array( |
| | 286 | array( |
| | 287 | 'obj_type' => 'post_type', |
| | 288 | 'type' => 'custom', |
| | 289 | ), |
| | 290 | false, |
| | 291 | ), |
| | 292 | array( |
| | 293 | array( |
| | 294 | 'obj_type' => 'taxonomy', |
| | 295 | 'type' => 'post_tag', |
| | 296 | ), |
| | 297 | true, |
| | 298 | ), |
| | 299 | ); |
| | 300 | } |
| | 301 | |
| | 302 | /** |
| | 303 | * Testing the array structure for a single item |
| | 304 | * |
| | 305 | * @dataProvider data_ajax_load_available_items_structure |
| | 306 | * |
| | 307 | */ |
| | 308 | function test2_ajax_load_available_items_structure( $post_args ) { |
| | 309 | |
| | 310 | $expected_keys = array( |
| | 311 | 'id', |
| | 312 | 'title', |
| | 313 | 'type', |
| | 314 | 'type_label', |
| | 315 | 'object', |
| | 316 | 'object_id', |
| | 317 | 'url', |
| | 318 | ); |
| | 319 | |
| | 320 | // Create some terms and pages. |
| | 321 | $this->factory->term->create_many( 5 ); |
| | 322 | $this->factory->post->create_many( 5, array( 'post_type' => 'page' ) ); |
| | 323 | |
| | 324 | $_POST = array_merge( array( |
| | 325 | 'action' => 'load-available-menu-items-customizer', |
| | 326 | 'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ), |
| | 327 | ), $post_args ); |
| | 328 | |
| | 329 | // Make the request. |
| | 330 | $this->make_ajax_call( 'load-available-menu-items-customizer' ); |
| | 331 | |
| | 332 | // Get the results. |
| | 333 | $response = json_decode( $this->_last_response, true ); |
| | 334 | |
| | 335 | $this->assertNotEmpty( $response['data']['items'] ); |
| | 336 | |
| | 337 | // Get the second index to avoid the home page edge case. |
| | 338 | $test_item = $response['data']['items'][1]; |
| | 339 | |
| | 340 | foreach ( $expected_keys as $key ) { |
| | 341 | $this->assertArrayHasKey( $key, $test_item ); |
| | 342 | $this->assertNotEmpty( $test_item[ $key ] ); |
| | 343 | } |
| | 344 | |
| | 345 | // Special test for the home page. |
| | 346 | if ( 'page' === $test_item['object'] ) { |
| | 347 | $home = $response['data']['items'][0]; |
| | 348 | foreach ( $expected_keys as $key ) { |
| | 349 | if ( 'object_id' !== $key ){ |
| | 350 | $this->assertArrayHasKey( $key, $home ); |
| | 351 | if ( 'object' !== $key ) { |
| | 352 | $this->assertNotEmpty( $home[ $key ] ); |
| | 353 | } |
| | 354 | } |
| | 355 | } |
| | 356 | } |
| | 357 | } |
| | 358 | |
| | 359 | /** |
| | 360 | * |
| | 361 | * Data provider. |
| | 362 | * |
| | 363 | * Provides various post_args to return a list of items to test the array structure of. |
| | 364 | * |
| | 365 | * @since 4.3.0 |
| | 366 | * |
| | 367 | * @return array { |
| | 368 | * @type array { |
| | 369 | * @type array $post_args The arguments that will merged with the $_POST array. |
| | 370 | * } |
| | 371 | * } |
| | 372 | * |
| | 373 | */ |
| | 374 | function data_ajax_load_available_items_structure() { |
| | 375 | return array( |
| | 376 | array( |
| | 377 | array( |
| | 378 | 'obj_type' => 'post_type', |
| | 379 | 'type' => 'post', |
| | 380 | ), |
| | 381 | ), |
| | 382 | array( |
| | 383 | array( |
| | 384 | 'obj_type' => 'post_type', |
| | 385 | 'type' => 'page', |
| | 386 | ), |
| | 387 | ), |
| | 388 | array( |
| | 389 | array( |
| | 390 | 'obj_type' => 'taxonomy', |
| | 391 | 'type' => 'post_tag', |
| | 392 | ), |
| | 393 | ), |
| | 394 | ); |
| | 395 | } |
| | 396 | |
| | 397 | /** |
| | 398 | * Testing the error messages for ajax_search_available_items |
| | 399 | * |
| | 400 | * @dataProvider data_ajax_search_available_items_caps_check |
| | 401 | */ |
| | 402 | function test_ajax_search_available_items_caps_check( $role, $expected_results ) { |
| | 403 | |
| | 404 | if ( 'administrator' != $role ) { |
| | 405 | // If we're not an admin, we should get a wp_die(-1). |
| | 406 | $this->setExpectedException( 'WPAjaxDieStopException' ); |
| | 407 | } |
| | 408 | |
| | 409 | wp_set_current_user( $this->factory->user->create( array( 'role' => $role ) ) ); |
| | 410 | |
| | 411 | $_POST = array( |
| | 412 | 'action' => 'search-available-menu-items-customizer', |
| | 413 | 'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ), |
| | 414 | ); |
| | 415 | |
| | 416 | $this->make_ajax_call( 'search-available-menu-items-customizer' ); |
| | 417 | |
| | 418 | // If we are an admin, we should get a proper response. |
| | 419 | if ( 'administrator' === $role ) { |
| | 420 | // Get the results. |
| | 421 | $response = json_decode( $this->_last_response, true ); |
| | 422 | |
| | 423 | $this->assertSame( $expected_results, $response ); |
| | 424 | } |
| | 425 | } |
| | 426 | |
| | 427 | /** |
| | 428 | * |
| | 429 | * Data provider. |
| | 430 | * |
| | 431 | * Provides various post_args to induce error messages in the that can be |
| | 432 | * compared to the expected_results. |
| | 433 | * |
| | 434 | * @since 4.3.0 |
| | 435 | * |
| | 436 | * @todo Make this more DRY |
| | 437 | * |
| | 438 | * @return array { |
| | 439 | * @type array { |
| | 440 | * @string string $role The role that will test caps for. |
| | 441 | * @array array $expected_results The expected results from the ajax call. |
| | 442 | * } |
| | 443 | * } |
| | 444 | * |
| | 445 | */ |
| | 446 | function data_ajax_search_available_items_caps_check() { |
| | 447 | return array( |
| | 448 | array( |
| | 449 | 'subscriber', |
| | 450 | array(), |
| | 451 | ), |
| | 452 | array( |
| | 453 | 'contributor', |
| | 454 | array(), |
| | 455 | ), |
| | 456 | array( |
| | 457 | 'author', |
| | 458 | array(), |
| | 459 | ), |
| | 460 | array( |
| | 461 | 'editor', |
| | 462 | array(), |
| | 463 | ), |
| | 464 | array( |
| | 465 | 'administrator', |
| | 466 | array( |
| | 467 | 'success' => false, |
| | 468 | 'data' => 'nav_menus_missing_search_parameter', |
| | 469 | ), |
| | 470 | ), |
| | 471 | ); |
| | 472 | } |
| | 473 | |
| | 474 | /** |
| | 475 | * Testing the results of various searches |
| | 476 | * |
| | 477 | * @dataProvider data_ajax_search_available_items_results |
| | 478 | */ |
| | 479 | function test_ajax_search_available_items_results( $post_args, $expected_results ) { |
| | 480 | |
| | 481 | $this->factory->post->create_many( 5, array( 'post_title' => 'Test Post' ) ); |
| | 482 | |
| | 483 | $_POST = array_merge( array( |
| | 484 | 'action' => 'search-available-menu-items-customizer', |
| | 485 | 'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ), |
| | 486 | ), $post_args ); |
| | 487 | |
| | 488 | $this->make_ajax_call( 'search-available-menu-items-customizer' ); |
| | 489 | |
| | 490 | $response = json_decode( $this->_last_response, true ); |
| | 491 | |
| | 492 | if ( isset( $post_args['search'] ) && 'test' === $post_args['search'] ) { |
| | 493 | $this->assertsame( true, $response['success'] ); |
| | 494 | $this->assertSame( 5, count( $response['data']['items'] ) ); |
| | 495 | } else { |
| | 496 | $this->assertSame( $expected_results, $response ); |
| | 497 | } |
| | 498 | |
| | 499 | } |
| | 500 | |
| | 501 | /** |
| | 502 | * |
| | 503 | * Data provider. |
| | 504 | * |
| | 505 | * Provides various post_args to test the results. |
| | 506 | * |
| | 507 | * @since 4.3.0 |
| | 508 | * |
| | 509 | * @return array { |
| | 510 | * @type array { |
| | 511 | * @string string $post_args The args that will be passed to ajax. |
| | 512 | * @array array $expected_results The expected results from the ajax call. |
| | 513 | * } |
| | 514 | * } |
| | 515 | * |
| | 516 | */ |
| | 517 | function data_ajax_search_available_items_results() { |
| | 518 | return array( |
| | 519 | array( |
| | 520 | array(), |
| | 521 | array( |
| | 522 | 'success' => false, |
| | 523 | 'data' => 'nav_menus_missing_search_parameter', |
| | 524 | ), |
| | 525 | ), |
| | 526 | array( |
| | 527 | array( |
| | 528 | 'search' => 'all_the_things', |
| | 529 | ), |
| | 530 | array( |
| | 531 | 'success' => false, |
| | 532 | 'data' => array( |
| | 533 | 'message' => 'No menu items found.', |
| | 534 | ), |
| | 535 | ), |
| | 536 | ), |
| | 537 | array( |
| | 538 | array( |
| | 539 | 'search' => 'test', |
| | 540 | ), |
| | 541 | array( |
| | 542 | 'success' => true, |
| | 543 | array(), |
| | 544 | ), |
| | 545 | ), |
| | 546 | ); |
| | 547 | } |
| | 548 | } |