Make WordPress Core


Ignore:
Timestamp:
12/02/2016 12:01:51 AM (8 years ago)
Author:
westonruter
Message:

Customize: Reject a changeset update when a non-future date is provided and also ensure that a published changeset always gets set to the current date/time.

  • Also moves checks from customize_save Ajax handler to the underlying WP_Customize_Manager::save_changeset_post() call which plugins may invoke directly.
  • Ensures that customize_save_response filter is always passed an array, with error code available as code.

Props utkarshpatel, westonruter, sayedwp.
See #30937.
Fixes #38943.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/ajax/CustomizeManager.php

    r38810 r39409  
    165165        $this->make_ajax_call( 'customize_save' );
    166166        $this->assertFalse( $this->_last_response_parsed['success'] );
    167         $this->assertEquals( 'changeset_already_published', $this->_last_response_parsed['data'] );
     167        $this->assertEquals( 'changeset_already_published', $this->_last_response_parsed['data']['code'] );
    168168        wp_update_post( array( 'ID' => $wp_customize->changeset_post_id(), 'post_status' => 'auto-draft' ) );
    169169
     
    214214        $this->make_ajax_call( 'customize_save' );
    215215        $this->assertFalse( $this->_last_response_parsed['success'] );
    216         $this->assertEquals( 'not_future_date', $this->_last_response_parsed['data'] );
     216        $this->assertEquals( 'not_future_date', $this->_last_response_parsed['data']['code'] );
    217217        $_POST['customize_changeset_date'] = ( gmdate( 'Y' ) + 1 ) . '-01-01 00:00:00';
    218218        $this->make_ajax_call( 'customize_save' );
     
    308308        $this->assertEquals( 'Published', get_post( $post_id )->post_title );
    309309    }
     310
     311    /**
     312     * Test WP_Customize_Manager::save().
     313     *
     314     * @ticket 38943
     315     * @covers WP_Customize_Manager::save()
     316     */
     317    function test_success_save_post_date() {
     318        $uuid = wp_generate_uuid4();
     319        $post_id = $this->factory()->post->create( array(
     320            'post_name' => $uuid,
     321            'post_title' => 'Original',
     322            'post_type' => 'customize_changeset',
     323            'post_status' => 'auto-draft',
     324            'post_content' => wp_json_encode( array(
     325                'blogname' => array(
     326                    'value' => 'New Site Title',
     327                ),
     328            ) ),
     329        ) );
     330        $wp_customize = $this->set_up_valid_state( $uuid );
     331
     332        // Success future schedule date.
     333        $future_date = ( gmdate( 'Y' ) + 1 ) . '-01-01 00:00:00';
     334        $_POST['customize_changeset_status'] = 'future';
     335        $_POST['customize_changeset_title'] = 'Future date';
     336        $_POST['customize_changeset_date'] = $future_date;
     337        $this->make_ajax_call( 'customize_save' );
     338        $this->assertTrue( $this->_last_response_parsed['success'] );
     339        $changeset_post_schedule = get_post( $post_id );
     340        $this->assertEquals( $future_date, $changeset_post_schedule->post_date );
     341
     342        // Success future changeset change to draft keeping existing date.
     343        unset( $_POST['customize_changeset_date'] );
     344        $_POST['customize_changeset_status'] = 'draft';
     345        $this->make_ajax_call( 'customize_save' );
     346        $this->assertTrue( $this->_last_response_parsed['success'] );
     347        $changeset_post_draft = get_post( $post_id );
     348        $this->assertEquals( $future_date, $changeset_post_draft->post_date );
     349
     350        // Success if date is not passed with schedule changeset and stored changeset have future date.
     351        $_POST['customize_changeset_status'] = 'future';
     352        $this->make_ajax_call( 'customize_save' );
     353        $this->assertTrue( $this->_last_response_parsed['success'] );
     354        $changeset_post_schedule = get_post( $post_id );
     355        $this->assertEquals( $future_date, $changeset_post_schedule->post_date );
     356        // Success if draft with past date.
     357        $now = current_time( 'mysql' );
     358        wp_update_post( array(
     359            'ID' => $post_id,
     360            'post_status' => 'draft',
     361            'post_date' => $now,
     362            'post_date_gmt' => get_gmt_from_date( $now ),
     363        ) );
     364
     365        // Fail if future request and existing date is past.
     366        $_POST['customize_changeset_status'] = 'future';
     367        unset( $_POST['customize_changeset_date'] );
     368        $this->make_ajax_call( 'customize_save' );
     369        $this->assertFalse( $this->_last_response_parsed['success'] );
     370        $this->assertEquals( 'not_future_date', $this->_last_response_parsed['data']['code'] );
     371
     372        // Success publish changeset reset date to current.
     373        wp_update_post( array(
     374            'ID' => $post_id,
     375            'post_status' => 'future',
     376            'post_date' => $future_date,
     377            'post_date_gmt' => get_gmt_from_date( $future_date ),
     378        ) );
     379        unset( $_POST['customize_changeset_date'] );
     380        $_POST['customize_changeset_status'] = 'publish';
     381        $this->make_ajax_call( 'customize_save' );
     382        $this->assertTrue( $this->_last_response_parsed['success'] );
     383        $changeset_post_publish = get_post( $post_id );
     384        $this->assertNotEquals( $future_date, $changeset_post_publish->post_date );
     385    }
    310386}
Note: See TracChangeset for help on using the changeset viewer.