You appear to be a bot. Output may be restricted
Description
Test WP_Customize_Manager::save().
Usage
Tests_Ajax_CustomizeManager::test_save_failures();
Parameters
Returns
void
Source
File name: wordpress-develop-tests/phpunit/tests/ajax/CustomizeManager.php
Lines:
1 to 100 of 141
public function test_save_failures() { global $wp_customize; $wp_customize = new WP_Customize_Manager(); $wp_customize->register_controls(); add_filter( 'user_has_cap', array( $this, 'filter_user_has_cap' ) ); // Unauthenticated. wp_set_current_user( 0 ); $this->make_ajax_call( 'customize_save' ); $this->assertFalse( $this->_last_response_parsed['success'] ); $this->assertSame( 'unauthenticated', $this->_last_response_parsed['data'] ); // Unauthorized. wp_set_current_user( self::$subscriber_user_id ); $nonce = wp_create_nonce( 'save-customize_' . $wp_customize->get_stylesheet() ); $_POST['nonce'] = $nonce; $_GET['nonce'] = $nonce; $_REQUEST['nonce'] = $nonce; $exception = null; try { ob_start(); $wp_customize->setup_theme(); } catch ( WPAjaxDieContinueException $e ) { $exception = $e; } $this->assertNotEmpty( $e ); $this->assertEquals( -1, $e->getMessage() ); // Not called setup_theme. wp_set_current_user( self::$admin_user_id ); $nonce = wp_create_nonce( 'save-customize_' . $wp_customize->get_stylesheet() ); $_POST['nonce'] = $nonce; $_GET['nonce'] = $nonce; $_REQUEST['nonce'] = $nonce; $this->make_ajax_call( 'customize_save' ); $this->assertFalse( $this->_last_response_parsed['success'] ); $this->assertSame( 'not_preview', $this->_last_response_parsed['data'] ); // Bad nonce. $_POST['nonce'] = 'bad'; $_GET['nonce'] = 'bad'; $_REQUEST['nonce'] = 'bad'; $wp_customize->setup_theme(); $this->make_ajax_call( 'customize_save' ); $this->assertFalse( $this->_last_response_parsed['success'] ); $this->assertSame( 'invalid_nonce', $this->_last_response_parsed['data'] ); // User cannot create. $nonce = wp_create_nonce( 'save-customize_' . $wp_customize->get_stylesheet() ); $_POST['nonce'] = $nonce; $_GET['nonce'] = $nonce; $_REQUEST['nonce'] = $nonce; $post_type_obj = get_post_type_object( 'customize_changeset' ); $post_type_obj->cap->create_posts = 'create_customize_changesets'; $this->make_ajax_call( 'customize_save' ); $this->assertFalse( $this->_last_response_parsed['success'] ); $this->assertSame( 'cannot_create_changeset_post', $this->_last_response_parsed['data'] ); $this->overridden_caps[ $post_type_obj->cap->create_posts ] = true; $this->make_ajax_call( 'customize_save' ); $this->assertTrue( $this->_last_response_parsed['success'] ); $post_type_obj->cap->create_posts = 'customize'; // Restore. // Changeset already published. $wp_customize->set_post_value( 'blogname', 'Hello' ); $wp_customize->save_changeset_post( array( 'status' => 'publish' ) ); $this->make_ajax_call( 'customize_save' ); $this->assertFalse( $this->_last_response_parsed['success'] ); $this->assertSame( 'changeset_already_published', $this->_last_response_parsed['data']['code'] ); wp_update_post( array( 'ID' => $wp_customize->changeset_post_id(), 'post_status' => 'auto-draft', ) ); // User cannot edit. $post_type_obj = get_post_type_object( 'customize_changeset' ); $post_type_obj->cap->edit_post = 'edit_customize_changesets'; $this->make_ajax_call( 'customize_save' ); $this->assertFalse( $this->_last_response_parsed['success'] ); $this->assertSame( 'cannot_edit_changeset_post', $this->_last_response_parsed['data'] ); $this->overridden_caps[ $post_type_obj->cap->edit_post ] = true; $this->make_ajax_call( 'customize_save' ); $this->assertTrue( $this->_last_response_parsed['success'] ); $post_type_obj->cap->edit_post = 'customize'; // Restore. // Bad customize_changeset_data. $_POST['customize_changeset_data'] = '[MALFORMED]'; $this->make_ajax_call( 'customize_save' ); $this->assertFalse( $this->_last_response_parsed['success'] ); $this->assertSame( 'invalid_customize_changeset_data', $this->_last_response_parsed['data'] ); // Bad customize_changeset_status. $_POST['customize_changeset_data'] = '{}'; $_POST['customize_changeset_status'] = 'unrecognized'; $this->make_ajax_call( 'customize_save' ); $this->assertFalse( $this->_last_response_parsed['success'] );