Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (7 years ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

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

    r41839 r42343  
    4545    public static function wpSetUpBeforeClass( $factory ) {
    4646        self::$subscriber_user_id = $factory->user->create( array( 'role' => 'subscriber' ) );
    47         self::$admin_user_id = $factory->user->create( array( 'role' => 'administrator' ) );
     47        self::$admin_user_id      = $factory->user->create( array( 'role' => 'administrator' ) );
    4848    }
    4949
     
    7171    protected function make_ajax_call( $action ) {
    7272        $this->_last_response_parsed = null;
    73         $this->_last_response = '';
     73        $this->_last_response        = '';
    7474        try {
    7575            $this->_handleAjax( $action );
     
    120120        // Unauthorized.
    121121        wp_set_current_user( self::$subscriber_user_id );
    122         $nonce = wp_create_nonce( 'save-customize_' . $wp_customize->get_stylesheet() );
     122        $nonce          = wp_create_nonce( 'save-customize_' . $wp_customize->get_stylesheet() );
    123123        $_POST['nonce'] = $_GET['nonce'] = $_REQUEST['nonce'] = $nonce;
    124         $exception = null;
     124        $exception      = null;
    125125        try {
    126126            ob_start();
     
    134134        // Not called setup_theme.
    135135        wp_set_current_user( self::$admin_user_id );
    136         $nonce = wp_create_nonce( 'save-customize_' . $wp_customize->get_stylesheet() );
     136        $nonce          = wp_create_nonce( 'save-customize_' . $wp_customize->get_stylesheet() );
    137137        $_POST['nonce'] = $_GET['nonce'] = $_REQUEST['nonce'] = $nonce;
    138138        $this->make_ajax_call( 'customize_save' );
     
    148148
    149149        // User cannot create.
    150         $nonce = wp_create_nonce( 'save-customize_' . $wp_customize->get_stylesheet() );
    151         $_POST['nonce'] = $_GET['nonce'] = $_REQUEST['nonce'] = $nonce;
    152         $post_type_obj = get_post_type_object( 'customize_changeset' );
     150        $nonce                            = wp_create_nonce( 'save-customize_' . $wp_customize->get_stylesheet() );
     151        $_POST['nonce']                   = $_GET['nonce'] = $_REQUEST['nonce'] = $nonce;
     152        $post_type_obj                    = get_post_type_object( 'customize_changeset' );
    153153        $post_type_obj->cap->create_posts = 'create_customize_changesets';
    154154        $this->make_ajax_call( 'customize_save' );
     
    166166        $this->assertFalse( $this->_last_response_parsed['success'] );
    167167        $this->assertEquals( 'changeset_already_published', $this->_last_response_parsed['data']['code'] );
    168         wp_update_post( array(
    169             'ID' => $wp_customize->changeset_post_id(),
    170             'post_status' => 'auto-draft',
    171         ) );
     168        wp_update_post(
     169            array(
     170                'ID'          => $wp_customize->changeset_post_id(),
     171                'post_status' => 'auto-draft',
     172            )
     173        );
    172174
    173175        // User cannot edit.
    174         $post_type_obj = get_post_type_object( 'customize_changeset' );
     176        $post_type_obj                 = get_post_type_object( 'customize_changeset' );
    175177        $post_type_obj->cap->edit_post = 'edit_customize_changesets';
    176178        $this->make_ajax_call( 'customize_save' );
     
    189191
    190192        // Bad customize_changeset_status.
    191         $_POST['customize_changeset_data'] = '{}';
     193        $_POST['customize_changeset_data']   = '{}';
    192194        $_POST['customize_changeset_status'] = 'unrecognized';
    193195        $this->make_ajax_call( 'customize_save' );
     
    196198
    197199        // Disallowed publish posts if not allowed.
    198         $post_type_obj = get_post_type_object( 'customize_changeset' );
    199         $post_type_obj->cap->publish_posts = 'publish_customize_changesets';
     200        $post_type_obj                       = get_post_type_object( 'customize_changeset' );
     201        $post_type_obj->cap->publish_posts   = 'publish_customize_changesets';
    200202        $_POST['customize_changeset_status'] = 'publish';
    201203        $this->make_ajax_call( 'customize_save' );
     
    210212        // Validate date.
    211213        $_POST['customize_changeset_status'] = 'draft';
    212         $_POST['customize_changeset_date'] = 'BAD DATE';
     214        $_POST['customize_changeset_date']   = 'BAD DATE';
    213215        $this->make_ajax_call( 'customize_save' );
    214216        $this->assertFalse( $this->_last_response_parsed['success'] );
     
    222224        $this->assertTrue( $this->_last_response_parsed['success'] );
    223225        $_POST['customize_changeset_status'] = 'future';
    224         $_POST['customize_changeset_date'] = '+10 minutes';
     226        $_POST['customize_changeset_date']   = '+10 minutes';
    225227        $this->make_ajax_call( 'customize_save' );
    226228        $this->assertTrue( $this->_last_response_parsed['success'] );
    227229        $this->assertEquals( 'future', get_post_status( $wp_customize->changeset_post_id() ) );
    228         wp_update_post( array(
    229             'ID' => $wp_customize->changeset_post_id(),
    230             'post_status' => 'auto-draft',
    231         ) );
     230        wp_update_post(
     231            array(
     232                'ID'          => $wp_customize->changeset_post_id(),
     233                'post_status' => 'auto-draft',
     234            )
     235        );
    232236    }
    233237
     
    241245        global $wp_customize;
    242246        wp_set_current_user( self::$admin_user_id );
    243         $wp_customize = new WP_Customize_Manager( array(
    244             'changeset_uuid' => $uuid,
    245         ) );
     247        $wp_customize = new WP_Customize_Manager(
     248            array(
     249                'changeset_uuid' => $uuid,
     250            )
     251        );
    246252        $wp_customize->register_controls();
    247         $nonce = wp_create_nonce( 'save-customize_' . $wp_customize->get_stylesheet() );
     253        $nonce          = wp_create_nonce( 'save-customize_' . $wp_customize->get_stylesheet() );
    248254        $_POST['nonce'] = $_GET['nonce'] = $_REQUEST['nonce'] = $nonce;
    249255        $wp_customize->setup_theme();
     
    261267
    262268        $_POST['customize_changeset_status'] = 'publish';
    263         $_POST['customize_changeset_title'] = 'Success Changeset';
    264         $_POST['customize_changeset_data'] = wp_json_encode( array(
    265             'blogname' => array(
    266                 'value' => 'Successful Site Title',
    267             ),
    268         ) );
     269        $_POST['customize_changeset_title']  = 'Success Changeset';
     270        $_POST['customize_changeset_data']   = wp_json_encode(
     271            array(
     272                'blogname' => array(
     273                    'value' => 'Successful Site Title',
     274                ),
     275            )
     276        );
    269277        $this->make_ajax_call( 'customize_save' );
    270278        $this->assertTrue( $this->_last_response_parsed['success'] );
     
    287295        $uuid = wp_generate_uuid4();
    288296
    289         $post_id = $this->factory()->post->create( array(
    290             'post_name' => $uuid,
    291             'post_title' => 'Original',
    292             'post_type' => 'customize_changeset',
    293             'post_status' => 'auto-draft',
    294             'post_content' => wp_json_encode( array(
    295                 'blogname' => array(
    296                     'value' => 'New Site Title',
     297        $post_id      = $this->factory()->post->create(
     298            array(
     299                'post_name'    => $uuid,
     300                'post_title'   => 'Original',
     301                'post_type'    => 'customize_changeset',
     302                'post_status'  => 'auto-draft',
     303                'post_content' => wp_json_encode(
     304                    array(
     305                        'blogname' => array(
     306                            'value' => 'New Site Title',
     307                        ),
     308                    )
    297309                ),
    298             ) ),
    299         ) );
     310            )
     311        );
    300312        $wp_customize = $this->set_up_valid_state( $uuid );
    301313
    302314        $_POST['customize_changeset_status'] = 'publish';
    303         $_POST['customize_changeset_title'] = 'Published';
     315        $_POST['customize_changeset_title']  = 'Published';
    304316        $this->make_ajax_call( 'customize_save' );
    305317        $this->assertTrue( $this->_last_response_parsed['success'] );
     
    320332     */
    321333    function test_success_save_post_date() {
    322         $uuid = wp_generate_uuid4();
    323         $post_id = $this->factory()->post->create( array(
    324             'post_name' => $uuid,
    325             'post_title' => 'Original',
    326             'post_type' => 'customize_changeset',
    327             'post_status' => 'auto-draft',
    328             'post_content' => wp_json_encode( array(
    329                 'blogname' => array(
    330                     'value' => 'New Site Title',
     334        $uuid         = wp_generate_uuid4();
     335        $post_id      = $this->factory()->post->create(
     336            array(
     337                'post_name'    => $uuid,
     338                'post_title'   => 'Original',
     339                'post_type'    => 'customize_changeset',
     340                'post_status'  => 'auto-draft',
     341                'post_content' => wp_json_encode(
     342                    array(
     343                        'blogname' => array(
     344                            'value' => 'New Site Title',
     345                        ),
     346                    )
    331347                ),
    332             ) ),
    333         ) );
     348            )
     349        );
    334350        $wp_customize = $this->set_up_valid_state( $uuid );
    335351
    336352        // Success future schedule date.
    337         $future_date = ( gmdate( 'Y' ) + 1 ) . '-01-01 00:00:00';
     353        $future_date                         = ( gmdate( 'Y' ) + 1 ) . '-01-01 00:00:00';
    338354        $_POST['customize_changeset_status'] = 'future';
    339         $_POST['customize_changeset_title'] = 'Future date';
    340         $_POST['customize_changeset_date'] = $future_date;
     355        $_POST['customize_changeset_title']  = 'Future date';
     356        $_POST['customize_changeset_date']   = $future_date;
    341357        $this->make_ajax_call( 'customize_save' );
    342358        $this->assertTrue( $this->_last_response_parsed['success'] );
     
    363379        // Success if draft with past date.
    364380        $now = current_time( 'mysql' );
    365         wp_update_post( array(
    366             'ID' => $post_id,
    367             'post_status' => 'draft',
    368             'post_date' => $now,
    369             'post_date_gmt' => get_gmt_from_date( $now ),
    370         ) );
     381        wp_update_post(
     382            array(
     383                'ID'            => $post_id,
     384                'post_status'   => 'draft',
     385                'post_date'     => $now,
     386                'post_date_gmt' => get_gmt_from_date( $now ),
     387            )
     388        );
    371389
    372390        // Fail if future request and existing date is past.
     
    378396
    379397        // Success publish changeset reset date to current.
    380         wp_update_post( array(
    381             'ID' => $post_id,
    382             'post_status' => 'future',
    383             'post_date' => $future_date,
    384             'post_date_gmt' => get_gmt_from_date( $future_date ),
    385         ) );
     398        wp_update_post(
     399            array(
     400                'ID'            => $post_id,
     401                'post_status'   => 'future',
     402                'post_date'     => $future_date,
     403                'post_date_gmt' => get_gmt_from_date( $future_date ),
     404            )
     405        );
    386406        unset( $_POST['customize_changeset_date'] );
    387407        $_POST['customize_changeset_status'] = 'publish';
     
    412432        $uuid = wp_generate_uuid4();
    413433
    414         $post_id = $this->factory()->post->create( array(
    415             'post_name' => $uuid,
    416             'post_type' => 'customize_changeset',
    417             'post_status' => 'draft',
    418             'post_content' => wp_json_encode( array(
     434        $post_id = $this->factory()->post->create(
     435            array(
     436                'post_name'    => $uuid,
     437                'post_type'    => 'customize_changeset',
     438                'post_status'  => 'draft',
     439                'post_content' => wp_json_encode(
     440                    array(
     441                        'blogname' => array(
     442                            'value' => 'New Site Title',
     443                        ),
     444                    )
     445                ),
     446            )
     447        );
     448        $this->set_up_valid_state( $uuid );
     449
     450        $this->assertFalse( wp_get_post_autosave( $post_id ) );
     451
     452        $_POST['customize_changeset_data'] = wp_json_encode(
     453            array(
    419454                'blogname' => array(
    420                     'value' => 'New Site Title',
     455                    'value' => 'Autosaved Site Title',
    421456                ),
    422             ) ),
    423         ) );
    424         $this->set_up_valid_state( $uuid );
    425 
    426         $this->assertFalse( wp_get_post_autosave( $post_id ) );
    427 
    428         $_POST['customize_changeset_data'] = wp_json_encode( array(
    429             'blogname' => array(
    430                 'value' => 'Autosaved Site Title',
    431             ),
    432         ) );
     457            )
     458        );
    433459
    434460        $_POST['customize_changeset_autosave'] = 'on';
     
    450476     */
    451477    public function test_handle_changeset_trash_request() {
    452         $uuid = wp_generate_uuid4();
     478        $uuid         = wp_generate_uuid4();
    453479        $wp_customize = $this->set_up_valid_state( $uuid );
    454480
     
    457483        $this->assertEquals( 'invalid_nonce', $this->_last_response_parsed['data']['code'] );
    458484
    459         $nonce = wp_create_nonce( 'trash_customize_changeset' );
     485        $nonce          = wp_create_nonce( 'trash_customize_changeset' );
    460486        $_POST['nonce'] = $_GET['nonce'] = $_REQUEST['nonce'] = $nonce;
    461487        $this->make_ajax_call( 'customize_trash' );
     
    465491        $wp_customize->register_controls(); // And settings too.
    466492        $wp_customize->set_post_value( 'blogname', 'HELLO' );
    467         $wp_customize->save_changeset_post( array(
    468             'status' => 'save',
    469         ) );
     493        $wp_customize->save_changeset_post(
     494            array(
     495                'status' => 'save',
     496            )
     497        );
    470498
    471499        add_filter( 'map_meta_cap', array( $this, 'return_do_not_allow' ) );
     
    475503        remove_filter( 'map_meta_cap', array( $this, 'return_do_not_allow' ) );
    476504
    477         wp_update_post( array(
    478             'ID' => $wp_customize->changeset_post_id(),
    479             'post_status' => 'trash',
    480         ) );
     505        wp_update_post(
     506            array(
     507                'ID'          => $wp_customize->changeset_post_id(),
     508                'post_status' => 'trash',
     509            )
     510        );
    481511        $this->make_ajax_call( 'customize_trash' );
    482512        $this->assertFalse( $this->_last_response_parsed['success'] );
    483513        $this->assertEquals( 'changeset_already_trashed', $this->_last_response_parsed['data']['code'] );
    484514
    485         wp_update_post( array(
    486             'ID' => $wp_customize->changeset_post_id(),
    487             'post_status' => 'draft',
    488         ) );
     515        wp_update_post(
     516            array(
     517                'ID'          => $wp_customize->changeset_post_id(),
     518                'post_status' => 'draft',
     519            )
     520        );
    489521
    490522        $wp_trash_post_count = did_action( 'wp_trash_post' );
     
    521553     */
    522554    public function test_handle_dismiss_autosave_or_lock_request() {
    523         $uuid = wp_generate_uuid4();
     555        $uuid         = wp_generate_uuid4();
    524556        $wp_customize = $this->set_up_valid_state( $uuid );
    525557
     
    528560        $this->assertEquals( 'invalid_nonce', $this->_last_response_parsed['data'] );
    529561
    530         $nonce = wp_create_nonce( 'customize_dismiss_autosave_or_lock' );
     562        $nonce          = wp_create_nonce( 'customize_dismiss_autosave_or_lock' );
    531563        $_POST['nonce'] = $_GET['nonce'] = $_REQUEST['nonce'] = $nonce;
    532564
     
    546578        $user_auto_draft_ids = array();
    547579        for ( $i = 0; $i < 3; $i++ ) {
    548             $user_auto_draft_ids[] = $this->factory()->post->create( array(
    549                 'post_name' => wp_generate_uuid4(),
    550                 'post_type' => 'customize_changeset',
    551                 'post_status' => 'auto-draft',
    552                 'post_author' => self::$admin_user_id,
    553                 'post_content' => wp_json_encode( array() ),
    554             ) );
     580            $user_auto_draft_ids[] = $this->factory()->post->create(
     581                array(
     582                    'post_name'    => wp_generate_uuid4(),
     583                    'post_type'    => 'customize_changeset',
     584                    'post_status'  => 'auto-draft',
     585                    'post_author'  => self::$admin_user_id,
     586                    'post_content' => wp_json_encode( array() ),
     587                )
     588            );
    555589        }
    556590        $other_user_auto_draft_ids = array();
    557591        for ( $i = 0; $i < 3; $i++ ) {
    558             $other_user_auto_draft_ids[] = $this->factory()->post->create( array(
    559                 'post_name' => wp_generate_uuid4(),
    560                 'post_type' => 'customize_changeset',
    561                 'post_status' => 'auto-draft',
    562                 'post_author' => $other_user_id,
    563                 'post_content' => wp_json_encode( array() ),
    564             ) );
     592            $other_user_auto_draft_ids[] = $this->factory()->post->create(
     593                array(
     594                    'post_name'    => wp_generate_uuid4(),
     595                    'post_type'    => 'customize_changeset',
     596                    'post_status'  => 'auto-draft',
     597                    'post_author'  => $other_user_id,
     598                    'post_content' => wp_json_encode( array() ),
     599                )
     600            );
    565601        }
    566602        foreach ( array_merge( $user_auto_draft_ids, $other_user_auto_draft_ids ) as $post_id ) {
     
    585621
    586622        // Save a changeset as a draft.
    587         $r = $wp_customize->save_changeset_post( array(
    588             'data' => array(
    589                 'blogname' => array(
    590                     'value' => 'Foo',
     623        $r = $wp_customize->save_changeset_post(
     624            array(
     625                'data'   => array(
     626                    'blogname' => array(
     627                        'value' => 'Foo',
     628                    ),
    591629                ),
    592             ),
    593             'status' => 'draft',
    594         ) );
     630                'status' => 'draft',
     631            )
     632        );
    595633
    596634        $_POST['dismiss_autosave'] = $_GET['dismiss_autosave'] = $_REQUEST['dismiss_autosave'] = false;
     
    610648
    611649        // Add the autosave revision.
    612         $r = $wp_customize->save_changeset_post( array(
    613             'data' => array(
    614                 'blogname' => array(
    615                     'value' => 'Bar',
     650        $r = $wp_customize->save_changeset_post(
     651            array(
     652                'data'     => array(
     653                    'blogname' => array(
     654                        'value' => 'Bar',
     655                    ),
    616656                ),
    617             ),
    618             'autosave' => true,
    619         ) );
     657                'autosave' => true,
     658            )
     659        );
    620660        $this->assertNotInstanceOf( 'WP_Error', $r );
    621661        $autosave_revision = wp_get_post_autosave( $wp_customize->changeset_post_id() );
Note: See TracChangeset for help on using the changeset viewer.