CustomizeMenus.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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. */
  10. class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
  11. /**
  12. * Instance of WP_Customize_Manager which is reset for each test.
  13. *
  14. * @var WP_Customize_Manager
  15. */
  16. public $wp_customize;
  17. /**
  18. * Page IDs.
  19. *
  20. * @var int[]
  21. */
  22. public static $pages;
  23. /**
  24. * Post IDs.
  25. *
  26. * @var int[]
  27. */
  28. public static $posts;
  29. /**
  30. * Term IDs.
  31. *
  32. * @var int[]
  33. */
  34. public static $terms;
  35. public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
  36. // Make some post objects.
  37. self::$posts = $factory->post->create_many( 5 );
  38. self::$pages = $factory->post->create_many( 5, array( 'post_type' => 'page' ) );
  39. // Some terms too.
  40. self::$terms = $factory->term->create_many( 5 );
  41. }
  42. /**
  43. * Set up the test fixture.
  44. */
  45. public function setUp() {
  46. parent::setUp();
  47. require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
  48. wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
  49. global $wp_customize;
  50. $this->wp_customize = new WP_Customize_Manager();
  51. $wp_customize = $this->wp_customize;
  52. }
  53. /**
  54. * Helper to keep it DRY
  55. *
  56. * @param string $action Action.
  57. */
  58. protected function make_ajax_call( $action ) {
  59. // Make the request.
  60. try {
  61. $this->_handleAjax( $action );
  62. } catch ( WPAjaxDieContinueException $e ) {
  63. unset( $e );
  64. }
  65. }
  66. /**
  67. * Testing capabilities check for ajax_load_available_items method
  68. *
  69. * @dataProvider data_ajax_load_available_items_cap_check
  70. *
  71. * @param string $role The role we're checking caps against.
  72. * @param array $expected_results Expected results.
  73. */
  74. function test_ajax_load_available_items_cap_check( $role, $expected_results ) {
  75. if ( 'administrator' !== $role ) {
  76. // If we're not an admin, we should get a wp_die( -1 ).
  77. $this->expectException( 'WPAjaxDieStopException' );
  78. $this->expectExceptionMessage( '-1' );
  79. }
  80. wp_set_current_user( self::factory()->user->create( array( 'role' => $role ) ) );
  81. $_POST = array(
  82. 'action' => 'load-available-menu-items-customizer',
  83. 'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
  84. );
  85. $this->make_ajax_call( 'load-available-menu-items-customizer' );
  86. // If we are an admin, we should get a proper response.
  87. if ( 'administrator' === $role ) {
  88. // Get the results.
  89. $response = json_decode( $this->_last_response, true );
  90. $this->assertSame( $expected_results, $response );
  91. }
  92. }
  93. /**
  94. * Data provider for test_ajax_load_available_items_cap_check().
  95. *
  96. * Provides various post_args to induce error messages in the that can be
  97. * compared to the expected_results.
  98. *
  99. * @since 4.3.0
  100. *
  101. * @return array {
  102. * @type array {
  103. * @string string $role The role that will test caps for.
  104. * @array array $expected_results The expected results from the Ajax call.
  105. * }
  106. * }
  107. */
  108. function data_ajax_load_available_items_cap_check() {
  109. return array(
  110. array(
  111. 'subscriber',
  112. array(),
  113. ),
  114. array(
  115. 'contributor',
  116. array(),
  117. ),
  118. array(
  119. 'author',
  120. array(),
  121. ),
  122. array(
  123. 'editor',
  124. array(),
  125. ),
  126. array(
  127. 'administrator',
  128. array(
  129. 'success' => false,
  130. 'data' => 'nav_menus_missing_type_or_object_parameter',
  131. ),
  132. ),
  133. );
  134. }
  135. /**
  136. * Testing the error messaging for ajax_load_available_items
  137. *
  138. * @dataProvider data_ajax_load_available_items_error_messages
  139. *
  140. * @param array $post_args POST args.
  141. * @param mixed $expected_results Expected results.
  142. */
  143. function test_ajax_load_available_items_error_messages( $post_args, $expected_results ) {
  144. $_POST = array_merge(
  145. array(
  146. 'action' => 'load-available-menu-items-customizer',
  147. 'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
  148. ),
  149. $post_args
  150. );
  151. // Make the request.
  152. $this->make_ajax_call( 'load-available-menu-items-customizer' );
  153. // Get the results.
  154. $response = json_decode( $this->_last_response, true );
  155. $this->assertSame( $expected_results, $response );
  156. }
  157. /**
  158. * Data provider for test_ajax_load_available_items_error_message().
  159. *
  160. * Provides various post_args to induce error messages in the that can be
  161. * compared to the expected_results.
  162. *
  163. * @since 4.3.0
  164. *
  165. * @return array {
  166. * @type array {
  167. * @array array $post_args The arguments that will merged with the $_POST array.
  168. * @array array $expected_results The expected results from the Ajax call.
  169. * }
  170. * }
  171. */
  172. function data_ajax_load_available_items_error_messages() {
  173. return array(
  174. // Testing empty obj_type and type.
  175. array(
  176. array(
  177. 'type' => '',
  178. 'object' => '',
  179. ),
  180. array(
  181. 'success' => false,
  182. 'data' => 'nav_menus_missing_type_or_object_parameter',
  183. ),
  184. ),
  185. // Testing empty obj_type.
  186. array(
  187. array(
  188. 'type' => 'post_type',
  189. 'object' => '',
  190. ),
  191. array(
  192. 'success' => false,
  193. 'data' => 'nav_menus_missing_type_or_object_parameter',
  194. ),
  195. ),
  196. // Testing empty type.
  197. array(
  198. array(
  199. 'type' => '',
  200. 'object' => 'post',
  201. ),
  202. array(
  203. 'success' => false,
  204. 'data' => 'nav_menus_missing_type_or_object_parameter',
  205. ),
  206. ),
  207. // Testing empty type of a bulk request.
  208. array(
  209. array(
  210. 'item_types' => array(
  211. array(
  212. 'type' => 'post_type',
  213. 'object' => 'post',
  214. ),
  215. array(
  216. 'type' => 'post_type',
  217. 'object' => '',
  218. ),
  219. ),
  220. ),
  221. array(
  222. 'success' => false,
  223. 'data' => 'nav_menus_missing_type_or_object_parameter',
  224. ),
  225. ),
  226. // Testing incorrect type option.
  227. array(
  228. array(
  229. 'type' => 'post_type',
  230. 'object' => 'invalid',
  231. ),
  232. array(
  233. 'success' => false,
  234. 'data' => 'nav_menus_invalid_post_type',
  235. ),
  236. ),
  237. );
  238. }
  239. /**
  240. * Testing the success status.
  241. *
  242. * @dataProvider data_ajax_load_available_items_success_status
  243. *
  244. * @param array $post_args POST args.
  245. * @param array $success_status Success status.
  246. */
  247. function test_ajax_load_available_items_success_status( $post_args, $success_status ) {
  248. $_POST = array_merge(
  249. array(
  250. 'action' => 'load-available-menu-items-customizer',
  251. 'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
  252. ),
  253. $post_args
  254. );
  255. // Make the request.
  256. $this->make_ajax_call( 'load-available-menu-items-customizer' );
  257. // Get the results.
  258. $response = json_decode( $this->_last_response, true );
  259. $this->assertSame( $success_status, $response['success'] );
  260. }
  261. /**
  262. * Data provider for test_ajax_load_available_items_success_status().
  263. *
  264. * Provides various post_args to retrieve results and compare against
  265. * the success status.
  266. *
  267. * @since 4.3.0
  268. *
  269. * @return array {
  270. * @type array {
  271. * @type array $post_args The arguments that will merged with the $_POST array.
  272. * @type bool $success_status The expected success status.
  273. * }
  274. * }
  275. */
  276. function data_ajax_load_available_items_success_status() {
  277. return array(
  278. array(
  279. array(
  280. 'type' => 'post_type',
  281. 'object' => 'post',
  282. ),
  283. true,
  284. ),
  285. array(
  286. array(
  287. 'type' => 'post_type',
  288. 'object' => 'page',
  289. ),
  290. true,
  291. ),
  292. array(
  293. array(
  294. 'type' => 'post_type',
  295. 'object' => 'custom',
  296. ),
  297. false,
  298. ),
  299. array(
  300. array(
  301. 'type' => 'taxonomy',
  302. 'object' => 'post_tag',
  303. ),
  304. true,
  305. ),
  306. // Testing a bulk request.
  307. array(
  308. array(
  309. 'item_types' => array(
  310. array(
  311. 'type' => 'post_type',
  312. 'object' => 'post',
  313. ),
  314. array(
  315. 'type' => 'post_type',
  316. 'object' => 'page',
  317. ),
  318. ),
  319. ),
  320. true,
  321. ),
  322. );
  323. }
  324. /**
  325. * Testing the array structure for a single item
  326. *
  327. * @dataProvider data_ajax_load_available_items_structure
  328. *
  329. * @param array $post_args POST args.
  330. */
  331. function test2_ajax_load_available_items_structure( $post_args ) {
  332. do_action( 'customize_register', $this->wp_customize );
  333. $expected_keys = array(
  334. 'id',
  335. 'title',
  336. 'type',
  337. 'type_label',
  338. 'object',
  339. 'object_id',
  340. 'url',
  341. );
  342. $auto_draft_post = $this->wp_customize->nav_menus->insert_auto_draft_post(
  343. array(
  344. 'post_title' => 'Test Auto Draft',
  345. 'post_type' => 'post',
  346. )
  347. );
  348. $this->wp_customize->set_post_value( 'nav_menus_created_posts', array( $auto_draft_post->ID ) );
  349. $this->wp_customize->get_setting( 'nav_menus_created_posts' )->preview();
  350. $_POST = array_merge(
  351. array(
  352. 'action' => 'load-available-menu-items-customizer',
  353. 'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
  354. ),
  355. $post_args
  356. );
  357. // Make the request.
  358. $this->make_ajax_call( 'load-available-menu-items-customizer' );
  359. // Get the results.
  360. $response = json_decode( $this->_last_response, true );
  361. $this->assertNotEmpty( current( $response['data']['items'] ) );
  362. // Get the second index to avoid the home page edge case.
  363. $first_prop = current( $response['data']['items'] );
  364. $test_item = $first_prop[1];
  365. foreach ( $expected_keys as $key ) {
  366. $this->assertArrayHasKey( $key, $test_item );
  367. $this->assertNotEmpty( $test_item[ $key ] );
  368. }
  369. // Special test for the home page.
  370. if ( 'page' === $test_item['object'] ) {
  371. $first_prop = current( $response['data']['items'] );
  372. $home = $first_prop[0];
  373. foreach ( $expected_keys as $key ) {
  374. if ( 'object_id' !== $key ) {
  375. $this->assertArrayHasKey( $key, $home );
  376. if ( 'object' !== $key ) {
  377. $this->assertNotEmpty( $home[ $key ] );
  378. }
  379. }
  380. }
  381. } elseif ( 'post' === $test_item['object'] ) {
  382. $item_ids = wp_list_pluck( $response['data']['items']['post_type:post'], 'id' );
  383. $this->assertContains( 'post-' . $auto_draft_post->ID, $item_ids );
  384. }
  385. }
  386. /**
  387. * Data provider for test_ajax_load_available_items_structure().
  388. *
  389. * Provides various post_args to return a list of items to test the array structure of.
  390. *
  391. * @since 4.3.0
  392. *
  393. * @return array {
  394. * @type array {
  395. * @type array $post_args The arguments that will merged with the $_POST array.
  396. * }
  397. * }
  398. */
  399. function data_ajax_load_available_items_structure() {
  400. return array(
  401. array(
  402. array(
  403. 'type' => 'post_type',
  404. 'object' => 'post',
  405. ),
  406. ),
  407. array(
  408. array(
  409. 'type' => 'post_type',
  410. 'object' => 'page',
  411. ),
  412. ),
  413. array(
  414. array(
  415. 'type' => 'taxonomy',
  416. 'object' => 'post_tag',
  417. ),
  418. ),
  419. );
  420. }
  421. /**
  422. * Testing the error messages for ajax_search_available_items
  423. *
  424. * @dataProvider data_ajax_search_available_items_caps_check
  425. *
  426. * @param string $role Role.
  427. * @param array $expected_results Expected results.
  428. */
  429. function test_ajax_search_available_items_caps_check( $role, $expected_results ) {
  430. if ( 'administrator' !== $role ) {
  431. // If we're not an admin, we should get a wp_die( -1 ).
  432. $this->expectException( 'WPAjaxDieStopException' );
  433. $this->expectExceptionMessage( '-1' );
  434. }
  435. wp_set_current_user( self::factory()->user->create( array( 'role' => $role ) ) );
  436. $_POST = array(
  437. 'action' => 'search-available-menu-items-customizer',
  438. 'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
  439. );
  440. $this->make_ajax_call( 'search-available-menu-items-customizer' );
  441. // If we are an admin, we should get a proper response.
  442. if ( 'administrator' === $role ) {
  443. // Get the results.
  444. $response = json_decode( $this->_last_response, true );
  445. $this->assertSame( $expected_results, $response );
  446. }
  447. }
  448. /**
  449. * Data provider for test_ajax_search_available_items_caps_check().
  450. *
  451. * Provides various post_args to induce error messages in the that can be
  452. * compared to the expected_results.
  453. *
  454. * @since 4.3.0
  455. *
  456. * @todo Make this more DRY
  457. *
  458. * @return array {
  459. * @type array {
  460. * @string string $role The role that will test caps for.
  461. * @array array $expected_results The expected results from the Ajax call.
  462. * }
  463. * }
  464. */
  465. function data_ajax_search_available_items_caps_check() {
  466. return array(
  467. array(
  468. 'subscriber',
  469. array(),
  470. ),
  471. array(
  472. 'contributor',
  473. array(),
  474. ),
  475. array(
  476. 'author',
  477. array(),
  478. ),
  479. array(
  480. 'editor',
  481. array(),
  482. ),
  483. array(
  484. 'administrator',
  485. array(
  486. 'success' => false,
  487. 'data' => 'nav_menus_missing_search_parameter',
  488. ),
  489. ),
  490. );
  491. }
  492. /**
  493. * Testing the results of various searches
  494. *
  495. * @dataProvider data_ajax_search_available_items_results
  496. *
  497. * @param array $post_args POST args.
  498. * @param array $expected_results Expected results.
  499. */
  500. function test_ajax_search_available_items_results( $post_args, $expected_results ) {
  501. do_action( 'customize_register', $this->wp_customize );
  502. self::factory()->post->create_many( 5, array( 'post_title' => 'Test Post' ) );
  503. $included_auto_draft_post = $this->wp_customize->nav_menus->insert_auto_draft_post(
  504. array(
  505. 'post_title' => 'Test Included Auto Draft',
  506. 'post_type' => 'post',
  507. )
  508. );
  509. $excluded_auto_draft_post = $this->wp_customize->nav_menus->insert_auto_draft_post(
  510. array(
  511. 'post_title' => 'Excluded Auto Draft',
  512. 'post_type' => 'post',
  513. )
  514. );
  515. $this->wp_customize->set_post_value( 'nav_menus_created_posts', array( $included_auto_draft_post->ID, $excluded_auto_draft_post->ID ) );
  516. $this->wp_customize->get_setting( 'nav_menus_created_posts' )->preview();
  517. $_POST = array_merge(
  518. array(
  519. 'action' => 'search-available-menu-items-customizer',
  520. 'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
  521. ),
  522. $post_args
  523. );
  524. $this->make_ajax_call( 'search-available-menu-items-customizer' );
  525. $response = json_decode( $this->_last_response, true );
  526. if ( isset( $post_args['search'] ) && 'test' === $post_args['search'] ) {
  527. $this->assertTrue( $response['success'] );
  528. $this->assertSame( 6, count( $response['data']['items'] ) );
  529. $item_ids = wp_list_pluck( $response['data']['items'], 'id' );
  530. $this->assertContains( 'post-' . $included_auto_draft_post->ID, $item_ids );
  531. $this->assertNotContains( 'post-' . $excluded_auto_draft_post->ID, $item_ids );
  532. } else {
  533. $this->assertSame( $expected_results, $response );
  534. }
  535. }
  536. /**
  537. * Data provider for test_ajax_search_available_items_results().
  538. *
  539. * Provides various post_args to test the results.
  540. *
  541. * @since 4.3.0
  542. *
  543. * @return array {
  544. * @type array {
  545. * @string string $post_args The args that will be passed to Ajax.
  546. * @array array $expected_results The expected results from the Ajax call.
  547. * }
  548. * }
  549. */
  550. function data_ajax_search_available_items_results() {
  551. return array(
  552. array(
  553. array(),
  554. array(
  555. 'success' => false,
  556. 'data' => 'nav_menus_missing_search_parameter',
  557. ),
  558. ),
  559. array(
  560. array(
  561. 'search' => 'all_the_things',
  562. ),
  563. array(
  564. 'success' => false,
  565. 'data' => array(
  566. 'message' => 'No results found.',
  567. ),
  568. ),
  569. ),
  570. array(
  571. array(
  572. 'search' => 'test',
  573. ),
  574. array(
  575. 'success' => true,
  576. array(),
  577. ),
  578. ),
  579. );
  580. }
  581. /**
  582. * Testing successful ajax_insert_auto_draft_post() call.
  583. *
  584. * @covers WP_Customize_Nav_Menus::ajax_insert_auto_draft_post
  585. */
  586. function test_ajax_insert_auto_draft_post_success() {
  587. $_POST = wp_slash(
  588. array(
  589. 'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
  590. 'params' => array(
  591. 'post_type' => 'post',
  592. 'post_title' => 'Hello World',
  593. ),
  594. )
  595. );
  596. $this->_last_response = '';
  597. $this->make_ajax_call( 'customize-nav-menus-insert-auto-draft' );
  598. $response = json_decode( $this->_last_response, true );
  599. $this->assertTrue( $response['success'] );
  600. $this->assertArrayHasKey( 'post_id', $response['data'] );
  601. $this->assertArrayHasKey( 'url', $response['data'] );
  602. $post = get_post( $response['data']['post_id'] );
  603. $this->assertSame( 'Hello World', $post->post_title );
  604. $this->assertSame( 'post', $post->post_type );
  605. $this->assertSame( '', $post->post_name );
  606. $this->assertSame( 'hello-world', get_post_meta( $post->ID, '_customize_draft_post_name', true ) );
  607. $this->assertSame( $this->wp_customize->changeset_uuid(), get_post_meta( $post->ID, '_customize_changeset_uuid', true ) );
  608. }
  609. /**
  610. * Testing unsuccessful ajax_insert_auto_draft_post() call.
  611. *
  612. * @covers WP_Customize_Nav_Menus::ajax_insert_auto_draft_post
  613. */
  614. function test_ajax_insert_auto_draft_failures() {
  615. // No nonce.
  616. $_POST = array();
  617. $this->_last_response = '';
  618. $this->make_ajax_call( 'customize-nav-menus-insert-auto-draft' );
  619. $response = json_decode( $this->_last_response, true );
  620. $this->assertFalse( $response['success'] );
  621. $this->assertSame( 'bad_nonce', $response['data'] );
  622. // Bad nonce.
  623. $_POST = wp_slash(
  624. array(
  625. 'customize-menus-nonce' => 'bad',
  626. )
  627. );
  628. $this->_last_response = '';
  629. $this->make_ajax_call( 'customize-nav-menus-insert-auto-draft' );
  630. $response = json_decode( $this->_last_response, true );
  631. $this->assertFalse( $response['success'] );
  632. $this->assertSame( 'bad_nonce', $response['data'] );
  633. // Bad nonce.
  634. wp_set_current_user( $this->factory()->user->create( array( 'role' => 'subscriber' ) ) );
  635. $_POST = wp_slash(
  636. array(
  637. 'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
  638. )
  639. );
  640. $this->_last_response = '';
  641. $this->make_ajax_call( 'customize-nav-menus-insert-auto-draft' );
  642. $response = json_decode( $this->_last_response, true );
  643. $this->assertFalse( $response['success'] );
  644. $this->assertSame( 'customize_not_allowed', $response['data'] );
  645. // Missing params.
  646. wp_set_current_user( $this->factory()->user->create( array( 'role' => 'administrator' ) ) );
  647. $_POST = wp_slash(
  648. array(
  649. 'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
  650. )
  651. );
  652. $this->_last_response = '';
  653. $this->make_ajax_call( 'customize-nav-menus-insert-auto-draft' );
  654. $response = json_decode( $this->_last_response, true );
  655. $this->assertFalse( $response['success'] );
  656. $this->assertSame( 'missing_params', $response['data'] );
  657. // insufficient_post_permissions.
  658. register_post_type( 'privilege', array( 'capability_type' => 'privilege' ) );
  659. $_POST = wp_slash(
  660. array(
  661. 'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
  662. 'params' => array(
  663. 'post_type' => 'privilege',
  664. ),
  665. )
  666. );
  667. $this->_last_response = '';
  668. $this->make_ajax_call( 'customize-nav-menus-insert-auto-draft' );
  669. $response = json_decode( $this->_last_response, true );
  670. $this->assertFalse( $response['success'] );
  671. $this->assertSame( 'insufficient_post_permissions', $response['data'] );
  672. // insufficient_post_permissions.
  673. $_POST = wp_slash(
  674. array(
  675. 'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
  676. 'params' => array(
  677. 'post_type' => 'non-existent',
  678. ),
  679. )
  680. );
  681. $this->_last_response = '';
  682. $this->make_ajax_call( 'customize-nav-menus-insert-auto-draft' );
  683. $response = json_decode( $this->_last_response, true );
  684. $this->assertFalse( $response['success'] );
  685. $this->assertSame( 'missing_post_type_param', $response['data'] );
  686. // missing_post_title.
  687. $_POST = wp_slash(
  688. array(
  689. 'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
  690. 'params' => array(
  691. 'post_type' => 'post',
  692. 'post_title' => ' ',
  693. ),
  694. )
  695. );
  696. $this->_last_response = '';
  697. $this->make_ajax_call( 'customize-nav-menus-insert-auto-draft' );
  698. $response = json_decode( $this->_last_response, true );
  699. $this->assertFalse( $response['success'] );
  700. $this->assertSame( 'missing_post_title', $response['data'] );
  701. // illegal_params.
  702. $_POST = wp_slash(
  703. array(
  704. 'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
  705. 'params' => array(
  706. 'post_type' => 'post',
  707. 'post_title' => 'OK',
  708. 'post_name' => 'bad',
  709. 'post_content' => 'bad',
  710. ),
  711. )
  712. );
  713. $this->_last_response = '';
  714. $this->make_ajax_call( 'customize-nav-menus-insert-auto-draft' );
  715. $response = json_decode( $this->_last_response, true );
  716. $this->assertFalse( $response['success'] );
  717. $this->assertSame( 'illegal_params', $response['data'] );
  718. }
  719. }