Make WordPress Core

Ticket #32687: 32687-ajax-tests.3.diff

File 32687-ajax-tests.3.diff, 13.9 KB (added by welcher, 11 years ago)

Completed Tests for the 2 ajax methods in nav-menus.php

  • tests/phpunit/tests/customize/nav-menus-ajax.php

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

     
    4848                $this->assertInstanceOf( 'WP_Customize_Manager', $menus->manager );
    4949        }
    5050
    51         /**
    52          * Test the test_load_available_items_ajax method.
    53          *
    54          * @see WP_Customize_Nav_Menus::load_available_items_ajax()
    55          */
    56         function test_load_available_items_ajax() {
    5751
    58                 $this->markTestIncomplete( 'This test has not been implemented.' );
    59 
    60         }
    61 
    6252        /**
    63          * Test the search_available_items_ajax method.
    64          *
    65          * @see WP_Customize_Nav_Menus::search_available_items_ajax()
    66          */
    67         function test_search_available_items_ajax() {
    68 
    69                 $this->markTestIncomplete( 'This test has not been implemented.' );
    70 
    71         }
    72 
    73         /**
    7453         * Test the search_available_items_query method.
    7554         *
    7655         * @see WP_Customize_Nav_Menus::search_available_items_query()