| | 1 | <?php |
| | 2 | |
| | 3 | /** |
| | 4 | * Admin ajax functions to be tested |
| | 5 | */ |
| | 6 | //require_once( ABSPATH . 'wp-admin/includes/ajax-actions.php' ); |
| | 7 | |
| | 8 | /** |
| | 9 | * Class Test_WP_Customize_Nav_Menus_Ajax |
| | 10 | * |
| | 11 | * @group customize |
| | 12 | */ |
| | 13 | class Test_WP_Customize_Nav_Menus_Ajax extends WP_Ajax_UnitTestCase { |
| | 14 | |
| | 15 | /** |
| | 16 | * Instance of WP_Customize_Manager which is reset for each test. |
| | 17 | * |
| | 18 | * @var WP_Customize_Manager |
| | 19 | */ |
| | 20 | public $wp_customize; |
| | 21 | |
| | 22 | |
| | 23 | /** |
| | 24 | * Set up the test fixture. |
| | 25 | */ |
| | 26 | public function setUp() { |
| | 27 | parent::setUp(); |
| | 28 | require_once ABSPATH . WPINC . '/class-wp-customize-manager.php'; |
| | 29 | wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) ); |
| | 30 | global $wp_customize; |
| | 31 | $this->wp_customize = new WP_Customize_Manager(); |
| | 32 | $wp_customize = $this->wp_customize; |
| | 33 | } |
| | 34 | |
| | 35 | /** |
| | 36 | * Tear down the test fixture. |
| | 37 | */ |
| | 38 | public function tearDown() { |
| | 39 | parent::tearDown(); |
| | 40 | } |
| | 41 | |
| | 42 | |
| | 43 | /** |
| | 44 | * Helper to keep it DRY |
| | 45 | * @param $action |
| | 46 | */ |
| | 47 | protected function make_ajax_call( $action ) { |
| | 48 | // Make the request |
| | 49 | try { |
| | 50 | $this->_handleAjax( $action ); |
| | 51 | } catch ( WPAjaxDieContinueException $e ) { |
| | 52 | unset( $e ); |
| | 53 | } |
| | 54 | } |
| | 55 | |
| | 56 | |
| | 57 | /** |
| | 58 | * Testing capabilities check for ajax_load_available_items method |
| | 59 | * |
| | 60 | * @dataProvider data_ajax_load_available_items_cap_check |
| | 61 | * |
| | 62 | * @param string $role The role we're checking caps against |
| | 63 | * @param array $expected_results |
| | 64 | */ |
| | 65 | function test_ajax_load_available_items_cap_check( $role, $expected_results ) { |
| | 66 | |
| | 67 | wp_set_current_user( $this->factory->user->create( array( 'role' => $role ) ) ); |
| | 68 | |
| | 69 | $_POST = array( |
| | 70 | 'action' => 'load-available-menu-items-customizer', |
| | 71 | 'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ), |
| | 72 | ); |
| | 73 | |
| | 74 | $this->make_ajax_call( 'load-available-menu-items-customizer' ); |
| | 75 | |
| | 76 | //get the results |
| | 77 | $response = json_decode( $this->_last_response, true ); |
| | 78 | |
| | 79 | $this->assertSame( $expected_results, $response ); |
| | 80 | |
| | 81 | } |
| | 82 | |
| | 83 | /** |
| | 84 | * dataProvider for test_ajax_load_available_items_cap_check |
| | 85 | * @return array |
| | 86 | */ |
| | 87 | function data_ajax_load_available_items_cap_check() { |
| | 88 | return array( |
| | 89 | array( |
| | 90 | 'subscriber', |
| | 91 | array( |
| | 92 | 'success' => false, |
| | 93 | 'data' => array( |
| | 94 | 'message' => 'Error: invalid user capabilities.', |
| | 95 | ), |
| | 96 | ), |
| | 97 | ), |
| | 98 | array( |
| | 99 | 'contributor', |
| | 100 | array( |
| | 101 | 'success' => false, |
| | 102 | 'data' => array( |
| | 103 | 'message' => 'Error: invalid user capabilities.', |
| | 104 | ), |
| | 105 | ), |
| | 106 | ), |
| | 107 | array( |
| | 108 | 'author', |
| | 109 | array( |
| | 110 | 'success' => false, |
| | 111 | 'data' => array( |
| | 112 | 'message' => 'Error: invalid user capabilities.', |
| | 113 | ), |
| | 114 | ), |
| | 115 | ), |
| | 116 | array( |
| | 117 | 'editor', |
| | 118 | array( |
| | 119 | 'success' => false, |
| | 120 | 'data' => array( |
| | 121 | 'message' => 'Error: invalid user capabilities.', |
| | 122 | ), |
| | 123 | ), |
| | 124 | ), |
| | 125 | array( |
| | 126 | 'administrator', |
| | 127 | array( |
| | 128 | 'success' => false, |
| | 129 | 'data' => array( |
| | 130 | 'message' => 'Missing obj_type or type param.', |
| | 131 | ), |
| | 132 | ), |
| | 133 | ), |
| | 134 | ); |
| | 135 | } |
| | 136 | |
| | 137 | |
| | 138 | /** |
| | 139 | * Testing the ajax_load_available_items method |
| | 140 | * |
| | 141 | * @dataProvider data_ajax_load_available_items_error_messages |
| | 142 | * |
| | 143 | */ |
| | 144 | function test_ajax_load_available_items_error_messages( $post_args, $expected_results ) { |
| | 145 | |
| | 146 | $_POST = array_merge( array( |
| | 147 | 'action' => 'load-available-menu-items-customizer', |
| | 148 | 'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ), |
| | 149 | ), $post_args ); |
| | 150 | |
| | 151 | |
| | 152 | // Make the request |
| | 153 | $this->make_ajax_call( 'load-available-menu-items-customizer' ); |
| | 154 | |
| | 155 | //get the results |
| | 156 | $response = json_decode( $this->_last_response, true ); |
| | 157 | |
| | 158 | $this->assertSame( $expected_results, $response ); |
| | 159 | } |
| | 160 | |
| | 161 | |
| | 162 | /** |
| | 163 | * dataProvider for test_ajax_load_available_items_error_messages |
| | 164 | * @return array |
| | 165 | * |
| | 166 | */ |
| | 167 | function data_ajax_load_available_items_error_messages() { |
| | 168 | return array( |
| | 169 | /** |
| | 170 | * Testing empty obj_type and type |
| | 171 | */ |
| | 172 | array( |
| | 173 | array( |
| | 174 | 'obj_type' => '', |
| | 175 | 'type' => '', |
| | 176 | ), |
| | 177 | |
| | 178 | array( |
| | 179 | 'success' => false, |
| | 180 | 'data' => array( |
| | 181 | 'message' => 'Missing obj_type or type param.', |
| | 182 | ), |
| | 183 | ), |
| | 184 | ), |
| | 185 | /** |
| | 186 | * Testing empty obj_type |
| | 187 | */ |
| | 188 | array( |
| | 189 | array( |
| | 190 | 'obj_type' => '', |
| | 191 | 'type' => 'post', |
| | 192 | ), |
| | 193 | |
| | 194 | array( |
| | 195 | 'success' => false, |
| | 196 | 'data' => array( |
| | 197 | 'message' => 'Missing obj_type or type param.', |
| | 198 | ), |
| | 199 | ), |
| | 200 | ), |
| | 201 | /** |
| | 202 | * Testing empty type |
| | 203 | */ |
| | 204 | array( |
| | 205 | |
| | 206 | array( |
| | 207 | 'obj_type' => '', |
| | 208 | 'type' => 'post', |
| | 209 | ), |
| | 210 | array( |
| | 211 | 'success' => false, |
| | 212 | 'data' => array( |
| | 213 | 'message' => 'Missing obj_type or type param.', |
| | 214 | ), |
| | 215 | ), |
| | 216 | ), |
| | 217 | /** |
| | 218 | * Testing incorrect obj_type option |
| | 219 | */ |
| | 220 | array( |
| | 221 | array( |
| | 222 | 'obj_type' => 'invalid', |
| | 223 | 'type' => 'post', |
| | 224 | ), |
| | 225 | |
| | 226 | array( |
| | 227 | 'success' => false, |
| | 228 | 'data' => array( |
| | 229 | 'message' => 'Invalid obj_type param: invalid', |
| | 230 | ), |
| | 231 | ), |
| | 232 | ), |
| | 233 | |
| | 234 | /** |
| | 235 | * Testing incorrect type option |
| | 236 | */ |
| | 237 | array( |
| | 238 | array( |
| | 239 | 'obj_type' => 'post_type', |
| | 240 | 'type' => 'invalid', |
| | 241 | ), |
| | 242 | |
| | 243 | array( |
| | 244 | 'success' => false, |
| | 245 | 'data' => array( |
| | 246 | 'message' => 'Unknown post type.', |
| | 247 | ), |
| | 248 | ), |
| | 249 | ), |
| | 250 | ); |
| | 251 | } |
| | 252 | |
| | 253 | |
| | 254 | /** |
| | 255 | * Testing |
| | 256 | * |
| | 257 | * @dataProvider data_ajax_load_available_items |
| | 258 | */ |
| | 259 | function test_ajax_load_available_items( $post_args, $expected_results ) { |
| | 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( $expected_results, $response['success'] ); |
| | 272 | |
| | 273 | } |
| | 274 | |
| | 275 | function data_ajax_load_available_items() { |
| | 276 | return array( |
| | 277 | array( |
| | 278 | array( |
| | 279 | 'obj_type' => 'post_type', |
| | 280 | 'type' => 'post', |
| | 281 | ), |
| | 282 | true, |
| | 283 | ), |
| | 284 | array( |
| | 285 | array( |
| | 286 | 'obj_type' => 'post_type', |
| | 287 | 'type' => 'page', |
| | 288 | ), |
| | 289 | true, |
| | 290 | ), |
| | 291 | array( |
| | 292 | array( |
| | 293 | 'obj_type' => 'post_type', |
| | 294 | 'type' => 'custom', |
| | 295 | ), |
| | 296 | false, |
| | 297 | ), |
| | 298 | array( |
| | 299 | array( |
| | 300 | 'obj_type' => 'taxonomy', |
| | 301 | 'type' => 'post_tag', |
| | 302 | ), |
| | 303 | true, |
| | 304 | ), |
| | 305 | ); |
| | 306 | } |
| | 307 | } |
| | 308 | No newline at end of file |