WordPress.org

Make WordPress Core


Ignore:
Timestamp:
09/02/2020 12:35:36 AM (17 months ago)
Author:
SergeyBiryukov
Message:

Tests: First pass at using assertSame() instead of assertEquals() in most of the unit tests.

This ensures that not only the return values match the expected results, but also that their type is the same.

Going forward, stricter type checking by using assertSame() should generally be preferred to assertEquals() where appropriate, to make the tests more reliable.

Props johnbillion, jrf, SergeyBiryukov.
See #38266.

File:
1 edited

Legend:

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

    r48858 r48937  
    116116        $this->make_ajax_call( 'customize_save' );
    117117        $this->assertFalse( $this->_last_response_parsed['success'] );
    118         $this->assertEquals( 'unauthenticated', $this->_last_response_parsed['data'] );
     118        $this->assertSame( 'unauthenticated', $this->_last_response_parsed['data'] );
    119119
    120120        // Unauthorized.
     
    142142        $this->make_ajax_call( 'customize_save' );
    143143        $this->assertFalse( $this->_last_response_parsed['success'] );
    144         $this->assertEquals( 'not_preview', $this->_last_response_parsed['data'] );
     144        $this->assertSame( 'not_preview', $this->_last_response_parsed['data'] );
    145145
    146146        // Bad nonce.
     
    151151        $this->make_ajax_call( 'customize_save' );
    152152        $this->assertFalse( $this->_last_response_parsed['success'] );
    153         $this->assertEquals( 'invalid_nonce', $this->_last_response_parsed['data'] );
     153        $this->assertSame( 'invalid_nonce', $this->_last_response_parsed['data'] );
    154154
    155155        // User cannot create.
     
    162162        $this->make_ajax_call( 'customize_save' );
    163163        $this->assertFalse( $this->_last_response_parsed['success'] );
    164         $this->assertEquals( 'cannot_create_changeset_post', $this->_last_response_parsed['data'] );
     164        $this->assertSame( 'cannot_create_changeset_post', $this->_last_response_parsed['data'] );
    165165        $this->overridden_caps[ $post_type_obj->cap->create_posts ] = true;
    166166        $this->make_ajax_call( 'customize_save' );
     
    173173        $this->make_ajax_call( 'customize_save' );
    174174        $this->assertFalse( $this->_last_response_parsed['success'] );
    175         $this->assertEquals( 'changeset_already_published', $this->_last_response_parsed['data']['code'] );
     175        $this->assertSame( 'changeset_already_published', $this->_last_response_parsed['data']['code'] );
    176176        wp_update_post(
    177177            array(
     
    186186        $this->make_ajax_call( 'customize_save' );
    187187        $this->assertFalse( $this->_last_response_parsed['success'] );
    188         $this->assertEquals( 'cannot_edit_changeset_post', $this->_last_response_parsed['data'] );
     188        $this->assertSame( 'cannot_edit_changeset_post', $this->_last_response_parsed['data'] );
    189189        $this->overridden_caps[ $post_type_obj->cap->edit_post ] = true;
    190190        $this->make_ajax_call( 'customize_save' );
     
    196196        $this->make_ajax_call( 'customize_save' );
    197197        $this->assertFalse( $this->_last_response_parsed['success'] );
    198         $this->assertEquals( 'invalid_customize_changeset_data', $this->_last_response_parsed['data'] );
     198        $this->assertSame( 'invalid_customize_changeset_data', $this->_last_response_parsed['data'] );
    199199
    200200        // Bad customize_changeset_status.
     
    203203        $this->make_ajax_call( 'customize_save' );
    204204        $this->assertFalse( $this->_last_response_parsed['success'] );
    205         $this->assertEquals( 'bad_customize_changeset_status', $this->_last_response_parsed['data'] );
     205        $this->assertSame( 'bad_customize_changeset_status', $this->_last_response_parsed['data'] );
    206206
    207207        // Disallowed publish posts if not allowed.
     
    211211        $this->make_ajax_call( 'customize_save' );
    212212        $this->assertFalse( $this->_last_response_parsed['success'] );
    213         $this->assertEquals( 'changeset_publish_unauthorized', $this->_last_response_parsed['data'] );
     213        $this->assertSame( 'changeset_publish_unauthorized', $this->_last_response_parsed['data'] );
    214214        $_POST['customize_changeset_status'] = 'future';
    215215        $this->make_ajax_call( 'customize_save' );
    216216        $this->assertFalse( $this->_last_response_parsed['success'] );
    217         $this->assertEquals( 'changeset_publish_unauthorized', $this->_last_response_parsed['data'] );
     217        $this->assertSame( 'changeset_publish_unauthorized', $this->_last_response_parsed['data'] );
    218218        $post_type_obj->cap->publish_posts = 'customize'; // Restore.
    219219
     
    223223        $this->make_ajax_call( 'customize_save' );
    224224        $this->assertFalse( $this->_last_response_parsed['success'] );
    225         $this->assertEquals( 'bad_customize_changeset_date', $this->_last_response_parsed['data'] );
     225        $this->assertSame( 'bad_customize_changeset_date', $this->_last_response_parsed['data'] );
    226226        $_POST['customize_changeset_date'] = '2010-01-01 00:00:00';
    227227        $this->make_ajax_call( 'customize_save' );
    228228        $this->assertFalse( $this->_last_response_parsed['success'] );
    229         $this->assertEquals( 'not_future_date', $this->_last_response_parsed['data']['code'] );
     229        $this->assertSame( 'not_future_date', $this->_last_response_parsed['data']['code'] );
    230230        $_POST['customize_changeset_date'] = ( gmdate( 'Y' ) + 1 ) . '-01-01 00:00:00';
    231231        $this->make_ajax_call( 'customize_save' );
     
    235235        $this->make_ajax_call( 'customize_save' );
    236236        $this->assertTrue( $this->_last_response_parsed['success'] );
    237         $this->assertEquals( 'future', get_post_status( $wp_customize->changeset_post_id() ) );
     237        $this->assertSame( 'future', get_post_status( $wp_customize->changeset_post_id() ) );
    238238        wp_update_post(
    239239            array(
     
    289289        $this->assertInternalType( 'array', $this->_last_response_parsed['data'] );
    290290
    291         $this->assertEquals( 'publish', $this->_last_response_parsed['data']['changeset_status'] );
     291        $this->assertSame( 'publish', $this->_last_response_parsed['data']['changeset_status'] );
    292292        $this->assertArrayHasKey( 'next_changeset_uuid', $this->_last_response_parsed['data'] );
    293293        $this->assertTrue( wp_is_uuid( $this->_last_response_parsed['data']['next_changeset_uuid'], 4 ) );
    294         $this->assertEquals( 'Success Changeset', get_post( $wp_customize->changeset_post_id() )->post_title );
    295         $this->assertEquals( 'Successful Site Title', get_option( 'blogname' ) );
     294        $this->assertSame( 'Success Changeset', get_post( $wp_customize->changeset_post_id() )->post_title );
     295        $this->assertSame( 'Successful Site Title', get_option( 'blogname' ) );
    296296    }
    297297
     
    328328        $this->assertInternalType( 'array', $this->_last_response_parsed['data'] );
    329329
    330         $this->assertEquals( 'publish', $this->_last_response_parsed['data']['changeset_status'] );
     330        $this->assertSame( 'publish', $this->_last_response_parsed['data']['changeset_status'] );
    331331        $this->assertArrayHasKey( 'next_changeset_uuid', $this->_last_response_parsed['data'] );
    332332        $this->assertTrue( wp_is_uuid( $this->_last_response_parsed['data']['next_changeset_uuid'], 4 ) );
    333         $this->assertEquals( 'New Site Title', get_option( 'blogname' ) );
    334         $this->assertEquals( 'Published', get_post( $post_id )->post_title );
     333        $this->assertSame( 'New Site Title', get_option( 'blogname' ) );
     334        $this->assertSame( 'Published', get_post( $post_id )->post_title );
    335335    }
    336336
     
    369369        $this->assertArrayHasKey( 'changeset_date', $this->_last_response_parsed['data'] );
    370370        $changeset_post_schedule = get_post( $post_id );
    371         $this->assertEquals( $future_date, $changeset_post_schedule->post_date );
     371        $this->assertSame( $future_date, $changeset_post_schedule->post_date );
    372372
    373373        // Success future changeset change to draft keeping existing date.
     
    378378        $this->assertArrayNotHasKey( 'changeset_date', $this->_last_response_parsed['data'] );
    379379        $changeset_post_draft = get_post( $post_id );
    380         $this->assertEquals( $future_date, $changeset_post_draft->post_date );
     380        $this->assertSame( $future_date, $changeset_post_draft->post_date );
    381381
    382382        // Success if date is not passed with schedule changeset and stored changeset have future date.
     
    386386        $this->assertArrayHasKey( 'changeset_date', $this->_last_response_parsed['data'] );
    387387        $changeset_post_schedule = get_post( $post_id );
    388         $this->assertEquals( $future_date, $changeset_post_schedule->post_date );
     388        $this->assertSame( $future_date, $changeset_post_schedule->post_date );
    389389        // Success if draft with past date.
    390390        $now = current_time( 'mysql' );
     
    403403        $this->make_ajax_call( 'customize_save' );
    404404        $this->assertFalse( $this->_last_response_parsed['success'] );
    405         $this->assertEquals( 'not_future_date', $this->_last_response_parsed['data']['code'] );
     405        $this->assertSame( 'not_future_date', $this->_last_response_parsed['data']['code'] );
    406406
    407407        // Success publish changeset reset date to current.
     
    424424
    425425        // Check response when trying to update an already-published post.
    426         $this->assertEquals( 'trash', get_post_status( $post_id ) );
     426        $this->assertSame( 'trash', get_post_status( $post_id ) );
    427427        $_POST['customize_changeset_status'] = 'publish';
    428428        $this->make_ajax_call( 'customize_save' );
    429429        $this->assertFalse( $this->_last_response_parsed['success'] );
    430         $this->assertEquals( 'changeset_already_published', $this->_last_response_parsed['data']['code'] );
     430        $this->assertSame( 'changeset_already_published', $this->_last_response_parsed['data']['code'] );
    431431        $this->assertArrayHasKey( 'next_changeset_uuid', $this->_last_response_parsed['data'] );
    432432        $this->assertTrue( wp_is_uuid( $this->_last_response_parsed['data']['next_changeset_uuid'], 4 ) );
     
    471471        $this->make_ajax_call( 'customize_save' );
    472472        $this->assertTrue( $this->_last_response_parsed['success'] );
    473         $this->assertEquals( 'draft', $this->_last_response_parsed['data']['changeset_status'] );
     473        $this->assertSame( 'draft', $this->_last_response_parsed['data']['changeset_status'] );
    474474        $autosave_revision = wp_get_post_autosave( $post_id );
    475475        $this->assertInstanceOf( 'WP_Post', $autosave_revision );
     
    491491        $this->make_ajax_call( 'customize_trash' );
    492492        $this->assertFalse( $this->_last_response_parsed['success'] );
    493         $this->assertEquals( 'invalid_nonce', $this->_last_response_parsed['data']['code'] );
     493        $this->assertSame( 'invalid_nonce', $this->_last_response_parsed['data']['code'] );
    494494
    495495        $nonce             = wp_create_nonce( 'trash_customize_changeset' );
     
    499499        $this->make_ajax_call( 'customize_trash' );
    500500        $this->assertFalse( $this->_last_response_parsed['success'] );
    501         $this->assertEquals( 'non_existent_changeset', $this->_last_response_parsed['data']['code'] );
     501        $this->assertSame( 'non_existent_changeset', $this->_last_response_parsed['data']['code'] );
    502502
    503503        $wp_customize->register_controls(); // And settings too.
     
    512512        $this->make_ajax_call( 'customize_trash' );
    513513        $this->assertFalse( $this->_last_response_parsed['success'] );
    514         $this->assertEquals( 'changeset_trash_unauthorized', $this->_last_response_parsed['data']['code'] );
     514        $this->assertSame( 'changeset_trash_unauthorized', $this->_last_response_parsed['data']['code'] );
    515515        remove_filter( 'map_meta_cap', array( $this, 'return_do_not_allow' ) );
    516516
     
    522522        $this->make_ajax_call( 'customize_trash' );
    523523        $this->assertFalse( $this->_last_response_parsed['success'] );
    524         $this->assertEquals( 'changeset_locked', $this->_last_response_parsed['data']['code'] );
     524        $this->assertSame( 'changeset_locked', $this->_last_response_parsed['data']['code'] );
    525525        delete_post_meta( $wp_customize->changeset_post_id(), '_edit_lock' );
    526526
     
    533533        $this->make_ajax_call( 'customize_trash' );
    534534        $this->assertFalse( $this->_last_response_parsed['success'] );
    535         $this->assertEquals( 'changeset_already_trashed', $this->_last_response_parsed['data']['code'] );
     535        $this->assertSame( 'changeset_already_trashed', $this->_last_response_parsed['data']['code'] );
    536536
    537537        wp_update_post(
     
    546546        $this->make_ajax_call( 'customize_trash' );
    547547        $this->assertFalse( $this->_last_response_parsed['success'] );
    548         $this->assertEquals( 'changeset_trash_failure', $this->_last_response_parsed['data']['code'] );
     548        $this->assertSame( 'changeset_trash_failure', $this->_last_response_parsed['data']['code'] );
    549549        remove_filter( 'pre_trash_post', '__return_false' );
    550         $this->assertEquals( $wp_trash_post_count, did_action( 'wp_trash_post' ) );
     550        $this->assertSame( $wp_trash_post_count, did_action( 'wp_trash_post' ) );
    551551
    552552        $wp_trash_post_count = did_action( 'wp_trash_post' );
    553         $this->assertEquals( 'draft', get_post_status( $wp_customize->changeset_post_id() ) );
     553        $this->assertSame( 'draft', get_post_status( $wp_customize->changeset_post_id() ) );
    554554        $this->make_ajax_call( 'customize_trash' );
    555555        $this->assertTrue( $this->_last_response_parsed['success'] );
    556         $this->assertEquals( 'trash', get_post_status( $wp_customize->changeset_post_id() ) );
    557         $this->assertEquals( $wp_trash_post_count + 1, did_action( 'wp_trash_post' ) );
     556        $this->assertSame( 'trash', get_post_status( $wp_customize->changeset_post_id() ) );
     557        $this->assertSame( $wp_trash_post_count + 1, did_action( 'wp_trash_post' ) );
    558558    }
    559559
     
    583583        $this->make_ajax_call( 'customize_dismiss_autosave_or_lock' );
    584584        $this->assertFalse( $this->_last_response_parsed['success'] );
    585         $this->assertEquals( 'unauthenticated', $this->_last_response_parsed['data'] );
     585        $this->assertSame( 'unauthenticated', $this->_last_response_parsed['data'] );
    586586        wp_set_current_user( $valid_user_id );
    587587
    588588        $this->make_ajax_call( 'customize_dismiss_autosave_or_lock' );
    589589        $this->assertFalse( $this->_last_response_parsed['success'] );
    590         $this->assertEquals( 'invalid_nonce', $this->_last_response_parsed['data'] );
     590        $this->assertSame( 'invalid_nonce', $this->_last_response_parsed['data'] );
    591591
    592592        $nonce             = wp_create_nonce( 'customize_dismiss_autosave_or_lock' );
     
    600600        $this->make_ajax_call( 'customize_dismiss_autosave_or_lock' );
    601601        $this->assertFalse( $this->_last_response_parsed['success'] );
    602         $this->assertEquals( 'no_changeset_to_dismiss_lock', $this->_last_response_parsed['data'] );
     602        $this->assertSame( 'no_changeset_to_dismiss_lock', $this->_last_response_parsed['data'] );
    603603
    604604        $_POST['dismiss_autosave']    = true;
     
    607607        $this->make_ajax_call( 'customize_dismiss_autosave_or_lock' );
    608608        $this->assertFalse( $this->_last_response_parsed['success'] );
    609         $this->assertEquals( 'no_auto_draft_to_delete', $this->_last_response_parsed['data'] );
     609        $this->assertSame( 'no_auto_draft_to_delete', $this->_last_response_parsed['data'] );
    610610
    611611        $other_user_id = $this->factory()->user->create();
     
    641641        $this->make_ajax_call( 'customize_dismiss_autosave_or_lock' );
    642642        $this->assertTrue( $this->_last_response_parsed['success'] );
    643         $this->assertEquals( 'auto_draft_dismissed', $this->_last_response_parsed['data'] );
     643        $this->assertSame( 'auto_draft_dismissed', $this->_last_response_parsed['data'] );
    644644        foreach ( $user_auto_draft_ids as $post_id ) {
    645             $this->assertEquals( 'auto-draft', get_post_status( $post_id ) );
     645            $this->assertSame( 'auto-draft', get_post_status( $post_id ) );
    646646            $this->assertTrue( (bool) get_post_meta( $post_id, '_customize_restore_dismissed', true ) );
    647647        }
    648648        foreach ( $other_user_auto_draft_ids as $post_id ) {
    649             $this->assertEquals( 'auto-draft', get_post_status( $post_id ) );
     649            $this->assertSame( 'auto-draft', get_post_status( $post_id ) );
    650650            $this->assertFalse( (bool) get_post_meta( $post_id, '_customize_restore_dismissed', true ) );
    651651        }
     
    654654        $this->make_ajax_call( 'customize_dismiss_autosave_or_lock' );
    655655        $this->assertFalse( $this->_last_response_parsed['success'] );
    656         $this->assertEquals( 'no_auto_draft_to_delete', $this->_last_response_parsed['data'] );
     656        $this->assertSame( 'no_auto_draft_to_delete', $this->_last_response_parsed['data'] );
    657657
    658658        // Save a changeset as a draft.
     
    673673        $this->make_ajax_call( 'customize_dismiss_autosave_or_lock' );
    674674        $this->assertTrue( $this->_last_response_parsed['success'] );
    675         $this->assertEquals( 'changeset_lock_dismissed', $this->_last_response_parsed['data'] );
     675        $this->assertSame( 'changeset_lock_dismissed', $this->_last_response_parsed['data'] );
    676676
    677677        $_POST['dismiss_autosave']    = true;
     
    685685        $this->make_ajax_call( 'customize_dismiss_autosave_or_lock' );
    686686        $this->assertFalse( $this->_last_response_parsed['success'] );
    687         $this->assertEquals( 'no_autosave_revision_to_delete', $this->_last_response_parsed['data'] );
     687        $this->assertSame( 'no_autosave_revision_to_delete', $this->_last_response_parsed['data'] );
    688688
    689689        // Add the autosave revision.
     
    707707        $this->make_ajax_call( 'customize_dismiss_autosave_or_lock' );
    708708        $this->assertTrue( $this->_last_response_parsed['success'] );
    709         $this->assertEquals( 'autosave_revision_deleted', $this->_last_response_parsed['data'] );
     709        $this->assertSame( 'autosave_revision_deleted', $this->_last_response_parsed['data'] );
    710710        $this->assertFalse( wp_get_post_autosave( $wp_customize->changeset_post_id() ) );
    711711
     
    713713        $this->make_ajax_call( 'customize_dismiss_autosave_or_lock' );
    714714        $this->assertFalse( $this->_last_response_parsed['success'] );
    715         $this->assertEquals( 'no_autosave_revision_to_delete', $this->_last_response_parsed['data'] );
     715        $this->assertSame( 'no_autosave_revision_to_delete', $this->_last_response_parsed['data'] );
    716716    }
    717717}
Note: See TracChangeset for help on using the changeset viewer.