WordPress.org

Make WordPress Core


Ignore:
Timestamp:
09/02/2020 12:35:36 AM (14 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/oembed/controller.php

    r48135 r48937  
    176176
    177177    function test_wp_oembed_ensure_format() {
    178         $this->assertEquals( 'json', wp_oembed_ensure_format( 'json' ) );
    179         $this->assertEquals( 'xml', wp_oembed_ensure_format( 'xml' ) );
    180         $this->assertEquals( 'json', wp_oembed_ensure_format( 123 ) );
    181         $this->assertEquals( 'json', wp_oembed_ensure_format( 'random' ) );
    182         $this->assertEquals( 'json', wp_oembed_ensure_format( array() ) );
     178        $this->assertSame( 'json', wp_oembed_ensure_format( 'json' ) );
     179        $this->assertSame( 'xml', wp_oembed_ensure_format( 'xml' ) );
     180        $this->assertSame( 'json', wp_oembed_ensure_format( 123 ) );
     181        $this->assertSame( 'json', wp_oembed_ensure_format( 'random' ) );
     182        $this->assertSame( 'json', wp_oembed_ensure_format( array() ) );
    183183    }
    184184
     
    266266        $data     = $response->get_data();
    267267
    268         $this->assertEquals( 'rest_no_route', $data['code'] );
     268        $this->assertSame( 'rest_no_route', $data['code'] );
    269269    }
    270270
     
    275275        $data     = $response->get_data();
    276276
    277         $this->assertEquals( 'rest_missing_callback_param', $data['code'] );
    278         $this->assertEquals( 'url', $data['data']['params'][0] );
     277        $this->assertSame( 'rest_missing_callback_param', $data['code'] );
     278        $this->assertSame( 'url', $data['data']['params'][0] );
    279279    }
    280280
     
    286286        $data     = $response->get_data();
    287287
    288         $this->assertEquals( 'oembed_invalid_url', $data['code'] );
     288        $this->assertSame( 'oembed_invalid_url', $data['code'] );
    289289    }
    290290
     
    335335        $this->assertArrayHasKey( 'width', $data );
    336336
    337         $this->assertEquals( '1.0', $data['version'] );
    338         $this->assertEquals( get_bloginfo( 'name' ), $data['provider_name'] );
    339         $this->assertEquals( home_url(), $data['provider_url'] );
    340         $this->assertEquals( $user->display_name, $data['author_name'] );
    341         $this->assertEquals( get_author_posts_url( $user->ID, $user->user_nicename ), $data['author_url'] );
    342         $this->assertEquals( $post->post_title, $data['title'] );
    343         $this->assertEquals( 'rich', $data['type'] );
     337        $this->assertSame( '1.0', $data['version'] );
     338        $this->assertSame( get_bloginfo( 'name' ), $data['provider_name'] );
     339        $this->assertSame( home_url(), $data['provider_url'] );
     340        $this->assertSame( $user->display_name, $data['author_name'] );
     341        $this->assertSame( get_author_posts_url( $user->ID, $user->user_nicename ), $data['author_url'] );
     342        $this->assertSame( $post->post_title, $data['title'] );
     343        $this->assertSame( 'rich', $data['type'] );
    344344        $this->assertTrue( $data['width'] <= $request['maxwidth'] );
    345345    }
     
    378378        $this->assertArrayHasKey( 'width', $data );
    379379
    380         $this->assertEquals( '1.0', $data['version'] );
    381         $this->assertEquals( get_bloginfo( 'name' ), $data['provider_name'] );
    382         $this->assertEquals( home_url(), $data['provider_url'] );
    383         $this->assertEquals( get_bloginfo( 'name' ), $data['author_name'] );
    384         $this->assertEquals( home_url(), $data['author_url'] );
    385         $this->assertEquals( $post->post_title, $data['title'] );
    386         $this->assertEquals( 'rich', $data['type'] );
     380        $this->assertSame( '1.0', $data['version'] );
     381        $this->assertSame( get_bloginfo( 'name' ), $data['provider_name'] );
     382        $this->assertSame( home_url(), $data['provider_url'] );
     383        $this->assertSame( get_bloginfo( 'name' ), $data['author_name'] );
     384        $this->assertSame( home_url(), $data['author_url'] );
     385        $this->assertSame( $post->post_title, $data['title'] );
     386        $this->assertSame( 'rich', $data['type'] );
    387387        $this->assertTrue( $data['width'] <= $request['maxwidth'] );
    388388
     
    423423        $this->assertArrayHasKey( 'width', $data );
    424424
    425         $this->assertEquals( '1.0', $data['version'] );
    426         $this->assertEquals( get_bloginfo( 'name' ), $data['provider_name'] );
    427         $this->assertEquals( home_url(), $data['provider_url'] );
    428         $this->assertEquals( $user->display_name, $data['author_name'] );
    429         $this->assertEquals( get_author_posts_url( $user->ID, $user->user_nicename ), $data['author_url'] );
    430         $this->assertEquals( $post->post_title, $data['title'] );
    431         $this->assertEquals( 'rich', $data['type'] );
     425        $this->assertSame( '1.0', $data['version'] );
     426        $this->assertSame( get_bloginfo( 'name' ), $data['provider_name'] );
     427        $this->assertSame( home_url(), $data['provider_url'] );
     428        $this->assertSame( $user->display_name, $data['author_name'] );
     429        $this->assertSame( get_author_posts_url( $user->ID, $user->user_nicename ), $data['author_url'] );
     430        $this->assertSame( $post->post_title, $data['title'] );
     431        $this->assertSame( 'rich', $data['type'] );
    432432        $this->assertTrue( $data['width'] <= $request['maxwidth'] );
    433433    }
     
    509509
    510510    function test_get_oembed_endpoint_url() {
    511         $this->assertEquals( home_url() . '/index.php?rest_route=/oembed/1.0/embed', get_oembed_endpoint_url() );
    512         $this->assertEquals( home_url() . '/index.php?rest_route=/oembed/1.0/embed', get_oembed_endpoint_url( '', 'json' ) );
    513         $this->assertEquals( home_url() . '/index.php?rest_route=/oembed/1.0/embed', get_oembed_endpoint_url( '', 'xml' ) );
     511        $this->assertSame( home_url() . '/index.php?rest_route=/oembed/1.0/embed', get_oembed_endpoint_url() );
     512        $this->assertSame( home_url() . '/index.php?rest_route=/oembed/1.0/embed', get_oembed_endpoint_url( '', 'json' ) );
     513        $this->assertSame( home_url() . '/index.php?rest_route=/oembed/1.0/embed', get_oembed_endpoint_url( '', 'xml' ) );
    514514
    515515        $post_id     = $this->factory()->post->create();
     
    517517        $url_encoded = urlencode( $url );
    518518
    519         $this->assertEquals( home_url() . '/index.php?rest_route=%2Foembed%2F1.0%2Fembed&url=' . $url_encoded, get_oembed_endpoint_url( $url ) );
    520         $this->assertEquals( home_url() . '/index.php?rest_route=%2Foembed%2F1.0%2Fembed&url=' . $url_encoded . '&format=xml', get_oembed_endpoint_url( $url, 'xml' ) );
     519        $this->assertSame( home_url() . '/index.php?rest_route=%2Foembed%2F1.0%2Fembed&url=' . $url_encoded, get_oembed_endpoint_url( $url ) );
     520        $this->assertSame( home_url() . '/index.php?rest_route=%2Foembed%2F1.0%2Fembed&url=' . $url_encoded . '&format=xml', get_oembed_endpoint_url( $url, 'xml' ) );
    521521    }
    522522
     
    524524        update_option( 'permalink_structure', '/%postname%' );
    525525
    526         $this->assertEquals( home_url() . '/wp-json/oembed/1.0/embed', get_oembed_endpoint_url() );
    527         $this->assertEquals( home_url() . '/wp-json/oembed/1.0/embed', get_oembed_endpoint_url( '', 'xml' ) );
     526        $this->assertSame( home_url() . '/wp-json/oembed/1.0/embed', get_oembed_endpoint_url() );
     527        $this->assertSame( home_url() . '/wp-json/oembed/1.0/embed', get_oembed_endpoint_url( '', 'xml' ) );
    528528
    529529        $post_id     = $this->factory()->post->create();
     
    531531        $url_encoded = urlencode( $url );
    532532
    533         $this->assertEquals( home_url() . '/wp-json/oembed/1.0/embed?url=' . $url_encoded, get_oembed_endpoint_url( $url ) );
    534         $this->assertEquals( home_url() . '/wp-json/oembed/1.0/embed?url=' . $url_encoded . '&format=xml', get_oembed_endpoint_url( $url, 'xml' ) );
     533        $this->assertSame( home_url() . '/wp-json/oembed/1.0/embed?url=' . $url_encoded, get_oembed_endpoint_url( $url ) );
     534        $this->assertSame( home_url() . '/wp-json/oembed/1.0/embed?url=' . $url_encoded . '&format=xml', get_oembed_endpoint_url( $url, 'xml' ) );
    535535
    536536        update_option( 'permalink_structure', '' );
     
    542542        $response = rest_get_server()->dispatch( $request );
    543543
    544         $this->assertEquals( 400, $response->get_status() );
     544        $this->assertSame( 400, $response->get_status() );
    545545
    546546        // Test with a user that does not have edit_posts capability.
     
    550550        $response = rest_get_server()->dispatch( $request );
    551551
    552         $this->assertEquals( 403, $response->get_status() );
     552        $this->assertSame( 403, $response->get_status() );
    553553        $data = $response->get_data();
    554         $this->assertEquals( $data['code'], 'rest_forbidden' );
     554        $this->assertSame( $data['code'], 'rest_forbidden' );
    555555    }
    556556
     
    560560        $request->set_param( 'url', self::INVALID_OEMBED_URL );
    561561        $response = rest_get_server()->dispatch( $request );
    562         $this->assertEquals( 404, $response->get_status() );
     562        $this->assertSame( 404, $response->get_status() );
    563563        $data = $response->get_data();
    564         $this->assertEquals( 'oembed_invalid_url', $data['code'] );
     564        $this->assertSame( 'oembed_invalid_url', $data['code'] );
    565565    }
    566566
     
    571571        $response = rest_get_server()->dispatch( $request );
    572572
    573         $this->assertEquals( 400, $response->get_status() );
     573        $this->assertSame( 400, $response->get_status() );
    574574    }
    575575
     
    582582        $request->set_param( '_wpnonce', wp_create_nonce( 'wp_rest' ) );
    583583        $response = rest_get_server()->dispatch( $request );
    584         $this->assertEquals( 200, $response->get_status() );
    585         $this->assertEquals( 1, $this->request_count );
     584        $this->assertSame( 200, $response->get_status() );
     585        $this->assertSame( 1, $this->request_count );
    586586
    587587        // Subsequent request is cached and so it should not cause a request.
    588588        rest_get_server()->dispatch( $request );
    589         $this->assertEquals( 1, $this->request_count );
     589        $this->assertSame( 1, $this->request_count );
    590590
    591591        // Rest with another user should also be cached.
     
    597597        $request->set_param( 'maxheight', 789 );
    598598        $response = rest_get_server()->dispatch( $request );
    599         $this->assertEquals( 1, $this->request_count );
     599        $this->assertSame( 1, $this->request_count );
    600600
    601601        // Test data object.
     
    604604        $this->assertNotEmpty( $data );
    605605        $this->assertInternalType( 'object', $data );
    606         $this->assertEquals( 'YouTube', $data->provider_name );
    607         $this->assertEquals( 'https://i.ytimg.com/vi/' . self::YOUTUBE_VIDEO_ID . '/hqdefault.jpg', $data->thumbnail_url );
     606        $this->assertSame( 'YouTube', $data->provider_name );
     607        $this->assertSame( 'https://i.ytimg.com/vi/' . self::YOUTUBE_VIDEO_ID . '/hqdefault.jpg', $data->thumbnail_url );
    608608        $this->assertEquals( $data->width, $request['maxwidth'] );
    609609        $this->assertEquals( $data->height, $request['maxheight'] );
     
    623623        $request->set_param( '_wpnonce', wp_create_nonce( 'wp_rest' ) );
    624624        $response = rest_get_server()->dispatch( $request );
    625         $this->assertEquals( 200, $response->get_status() );
    626         $this->assertEquals( 2, $this->request_count );
     625        $this->assertSame( 200, $response->get_status() );
     626        $this->assertSame( 2, $this->request_count );
    627627
    628628        // Test data object.
     
    643643        $request->set_param( 'discover', false );
    644644        $response = rest_get_server()->dispatch( $request );
    645         $this->assertEquals( 404, $response->get_status() );
    646         $this->assertEquals( 0, $this->request_count );
     645        $this->assertSame( 404, $response->get_status() );
     646        $this->assertSame( 0, $this->request_count );
    647647    }
    648648
     
    654654        $request->set_param( 'url', self::INVALID_OEMBED_URL );
    655655        $response = rest_get_server()->dispatch( $request );
    656         $this->assertEquals( 404, $response->get_status() );
    657         $this->assertEquals( 1, $this->request_count );
     656        $this->assertSame( 404, $response->get_status() );
     657        $this->assertSame( 1, $this->request_count );
    658658    }
    659659
     
    666666        $response = rest_get_server()->dispatch( $request );
    667667
    668         $this->assertEquals( 400, $response->get_status() );
     668        $this->assertSame( 400, $response->get_status() );
    669669        $data = $response->get_data();
    670         $this->assertEquals( $data['code'], 'rest_invalid_param' );
     670        $this->assertSame( $data['code'], 'rest_invalid_param' );
    671671    }
    672672
     
    709709        $this->assertArrayHasKey( 'width', $data );
    710710
    711         $this->assertEquals( '1.0', $data['version'] );
    712         $this->assertEquals( get_bloginfo( 'name' ), $data['provider_name'] );
    713         $this->assertEquals( home_url(), $data['provider_url'] );
    714         $this->assertEquals( $user->display_name, $data['author_name'] );
    715         $this->assertEquals( get_author_posts_url( $user->ID, $user->user_nicename ), $data['author_url'] );
    716         $this->assertEquals( $post->post_title, $data['title'] );
    717         $this->assertEquals( 'rich', $data['type'] );
     711        $this->assertSame( '1.0', $data['version'] );
     712        $this->assertSame( get_bloginfo( 'name' ), $data['provider_name'] );
     713        $this->assertSame( home_url(), $data['provider_url'] );
     714        $this->assertSame( $user->display_name, $data['author_name'] );
     715        $this->assertSame( get_author_posts_url( $user->ID, $user->user_nicename ), $data['author_url'] );
     716        $this->assertSame( $post->post_title, $data['title'] );
     717        $this->assertSame( 'rich', $data['type'] );
    718718        $this->assertTrue( $data['width'] <= $request['maxwidth'] );
    719719    }
     
    758758        $this->assertArrayHasKey( 'width', $data );
    759759
    760         $this->assertEquals( '1.0', $data['version'] );
    761         $this->assertEquals( get_bloginfo( 'name' ), $data['provider_name'] );
    762         $this->assertEquals( home_url(), $data['provider_url'] );
    763         $this->assertEquals( get_bloginfo( 'name' ), $data['author_name'] );
    764         $this->assertEquals( home_url(), $data['author_url'] );
    765         $this->assertEquals( $post->post_title, $data['title'] );
    766         $this->assertEquals( 'rich', $data['type'] );
     760        $this->assertSame( '1.0', $data['version'] );
     761        $this->assertSame( get_bloginfo( 'name' ), $data['provider_name'] );
     762        $this->assertSame( home_url(), $data['provider_url'] );
     763        $this->assertSame( get_bloginfo( 'name' ), $data['author_name'] );
     764        $this->assertSame( home_url(), $data['author_url'] );
     765        $this->assertSame( $post->post_title, $data['title'] );
     766        $this->assertSame( 'rich', $data['type'] );
    767767        $this->assertTrue( $data['width'] <= $request['maxwidth'] );
    768768
     
    785785        $data     = $response->get_data();
    786786
    787         $this->assertEquals( 1, $this->oembed_result_filter_count );
     787        $this->assertSame( 1, $this->oembed_result_filter_count );
    788788        $this->assertInternalType( 'object', $data );
    789         $this->assertEquals( 'Untrusted', $data->provider_name );
    790         $this->assertEquals( self::UNTRUSTED_PROVIDER_URL, $data->provider_url );
    791         $this->assertEquals( 'rich', $data->type );
     789        $this->assertSame( 'Untrusted', $data->provider_name );
     790        $this->assertSame( self::UNTRUSTED_PROVIDER_URL, $data->provider_url );
     791        $this->assertSame( 'rich', $data->type );
    792792        $this->assertFalse( $data->html );
    793793    }
     
    808808        $data     = $response->get_data();
    809809
    810         $this->assertEquals( 1, $this->oembed_result_filter_count );
     810        $this->assertSame( 1, $this->oembed_result_filter_count );
    811811        $this->assertInternalType( 'object', $data );
    812812
Note: See TracChangeset for help on using the changeset viewer.