Make WordPress Core

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

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

Updated/cleaned up ajax unit tests

  • 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      customize
     10 */
     11class Test_WP_Customize_Nav_Menus_Ajax 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        /**
     22         * Set up the test fixture.
     23         */
     24        public function setUp() {
     25                parent::setUp();
     26                require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
     27                wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
     28                global $wp_customize;
     29                $this->wp_customize = new WP_Customize_Manager();
     30                $wp_customize = $this->wp_customize;
     31        }
     32
     33        /**
     34         * Tear down the test fixture.
     35         */
     36        public function tearDown() {
     37                parent::tearDown();
     38        }
     39
     40
     41        /**
     42         * Helper to keep it DRY
     43         * @param $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        /**
     56         * Testing capabilities check for ajax_load_available_items method
     57         *
     58         * @dataProvider data_ajax_load_available_items_cap_check
     59         *
     60         * @param string $role The role we're checking caps against
     61         * @param array $expected_results
     62         */
     63        function test_ajax_load_available_items_cap_check( $role, $expected_results ) {
     64
     65                wp_set_current_user( $this->factory->user->create( array( 'role' => $role ) ) );
     66
     67                $_POST = array(
     68                        'action'                => 'load-available-menu-items-customizer',
     69                        'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
     70                );
     71
     72                $this->make_ajax_call( 'load-available-menu-items-customizer' );
     73
     74                //get the results
     75                $response = json_decode( $this->_last_response, true );
     76
     77                $this->assertSame( $expected_results, $response );
     78
     79        }
     80
     81        /**
     82         *
     83         * Data provider.
     84         *
     85         * Provides various post_args to induce error messages in the that can be
     86         * compared to the expected_results.
     87         *
     88         * @since 4.3.0
     89         *
     90         * @see test_ajax_load_available_items_cap_check
     91         *
     92         * @return array {
     93         *     @string string $role             The role that will test caps for.
     94         *     @array  array  $expected_results The expected results from the ajax call.
     95         * }
     96         *
     97         */
     98        function data_ajax_load_available_items_cap_check() {
     99                return array(
     100                        array(
     101                                'subscriber',
     102                                array(
     103                                        'success' => false,
     104                                        'data' => array(
     105                                                'message' => 'Error: invalid user capabilities.',
     106                                        ),
     107                                ),
     108                        ),
     109                        array(
     110                                'contributor',
     111                                array(
     112                                        'success' => false,
     113                                        'data' => array(
     114                                                'message' => 'Error: invalid user capabilities.',
     115                                        ),
     116                                ),
     117                        ),
     118                        array(
     119                                'author',
     120                                array(
     121                                        'success' => false,
     122                                        'data' => array(
     123                                                'message' => 'Error: invalid user capabilities.',
     124                                        ),
     125                                ),
     126                        ),
     127                        array(
     128                                'editor',
     129                                array(
     130                                        'success' => false,
     131                                        'data' => array(
     132                                                'message' => 'Error: invalid user capabilities.',
     133                                        ),
     134                                ),
     135                        ),
     136                        array(
     137                                'administrator',
     138                                array(
     139                                        'success' => false,
     140                                        'data' => array(
     141                                                'message' => 'Missing obj_type or type param.',
     142                                        ),
     143                                ),
     144                        ),
     145                );
     146        }
     147
     148
     149        /**
     150         * Testing the error messaging for ajax_load_available_items
     151         *
     152         * @dataProvider data_ajax_load_available_items_error_messages
     153         *
     154         */
     155        function test_ajax_load_available_items_error_messages( $post_args, $expected_results ) {
     156
     157                $_POST = array_merge( array(
     158                        'action'                => 'load-available-menu-items-customizer',
     159                        'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
     160                ), $post_args );
     161
     162
     163                // Make the request
     164                $this->make_ajax_call( 'load-available-menu-items-customizer' );
     165
     166                //get the results
     167                $response = json_decode( $this->_last_response, true );
     168
     169                $this->assertSame( $expected_results, $response );
     170        }
     171
     172
     173        /**
     174         *
     175         * Data provider.
     176         *
     177         * Provides various post_args to induce error messages in the that can be
     178         * compared to the expected_results.
     179         *
     180         * @since 4.3.0
     181         *
     182         * @see test_ajax_load_available_items_error_messages
     183         *
     184         * @return array {
     185         *     @array array $post_args        The arguments that will merged with the $_POST array.
     186         *     @array array $expected_results The expected results from the ajax call.
     187         * }
     188         *
     189         */
     190        function data_ajax_load_available_items_error_messages() {
     191                return array(
     192                        /**
     193                         * Testing empty obj_type and type
     194                         */
     195                        array(
     196                                array(
     197                                        'obj_type'  => '',
     198                                        'type'      => '',
     199                                ),
     200
     201                                array(
     202                                        'success' => false,
     203                                        'data' => array(
     204                                                'message' => 'Missing obj_type or type param.',
     205                                        ),
     206                                ),
     207                        ),
     208                        /**
     209                         * Testing empty obj_type
     210                         */
     211                        array(
     212                                array(
     213                                        'obj_type'  => '',
     214                                        'type'      => 'post',
     215                                ),
     216
     217                                array(
     218                                        'success' => false,
     219                                        'data' => array(
     220                                                'message' => 'Missing obj_type or type param.',
     221                                        ),
     222                                ),
     223                        ),
     224                        /**
     225                         * Testing empty type
     226                         */
     227                        array(
     228
     229                                array(
     230                                        'obj_type'  => '',
     231                                        'type'      => 'post',
     232                                ),
     233                                array(
     234                                        'success' => false,
     235                                        'data' => array(
     236                                                'message' => 'Missing obj_type or type param.',
     237                                        ),
     238                                ),
     239                        ),
     240                        /**
     241                         * Testing incorrect obj_type option
     242                         */
     243                        array(
     244                                array(
     245                                        'obj_type'  => 'invalid',
     246                                        'type'      => 'post',
     247                                ),
     248
     249                                array(
     250                                        'success' => false,
     251                                        'data' => array(
     252                                                'message' => 'Invalid obj_type param: invalid',
     253                                        ),
     254                                ),
     255                        ),
     256
     257                        /**
     258                         * Testing incorrect type option
     259                         */
     260                        array(
     261                                array(
     262                                        'obj_type'  => 'post_type',
     263                                        'type'      => 'invalid',
     264                                ),
     265
     266                                array(
     267                                        'success' => false,
     268                                        'data' => array(
     269                                                'message' => 'Unknown post type.',
     270                                        ),
     271                                ),
     272                        ),
     273                );
     274        }
     275
     276
     277        /**
     278         * Testing the success status
     279         *
     280         * @dataProvider data_ajax_load_available_items_sucess_status
     281         */
     282        function test_ajax_load_available_items_sucess_status( $post_args, $success_status ) {
     283
     284                $_POST = array_merge( array(
     285                        'action'                => 'load-available-menu-items-customizer',
     286                        'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
     287                ), $post_args );
     288
     289                // Make the request
     290                $this->make_ajax_call( 'load-available-menu-items-customizer' );
     291
     292                //get the results
     293                $response = json_decode( $this->_last_response, true );
     294                $this->assertSame( $success_status, $response['success'] );
     295
     296        }
     297
     298        /**
     299         *
     300         * Data provider.
     301         *
     302         * Provides various post_args to retrieve results and compare against
     303         * the success status.
     304         *
     305         * @since 4.3.0
     306         *
     307         * @see test_ajax_load_available_items_sucess_status
     308         *
     309         * @return array {
     310         *     @type array $post_args      The arguments that will merged with the $_POST array.
     311         *     @type bool  $success_status The expected success status.
     312         * }
     313         *
     314         */
     315        function data_ajax_load_available_items_sucess_status() {
     316                return array(
     317                        array(
     318                                array(
     319                                        'obj_type'  => 'post_type',
     320                                        'type'      => 'post',
     321                                ),
     322                                true,
     323                        ),
     324                        array(
     325                                array(
     326                                        'obj_type'  => 'post_type',
     327                                        'type'      => 'page',
     328                                ),
     329                                true,
     330                        ),
     331                        array(
     332                                array(
     333                                        'obj_type'  => 'post_type',
     334                                        'type'      => 'custom',
     335                                ),
     336                                false,
     337                        ),
     338                        array(
     339                                array(
     340                                        'obj_type'  => 'taxonomy',
     341                                        'type'      => 'post_tag',
     342                                ),
     343                                true,
     344                        ),
     345                );
     346        }
     347}
     348 No newline at end of file
  • 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        /**
    6353         * Test the search_available_items_ajax method.
    6454         *