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