Make WordPress Core


Ignore:
Timestamp:
09/02/2020 12:35:36 AM (4 years 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/media.php

    r48516 r48937  
    4949    function test_img_caption_shortcode_added() {
    5050        global $shortcode_tags;
    51         $this->assertEquals( 'img_caption_shortcode', $shortcode_tags['caption'] );
    52         $this->assertEquals( 'img_caption_shortcode', $shortcode_tags['wp_caption'] );
     51        $this->assertSame( 'img_caption_shortcode', $shortcode_tags['caption'] );
     52        $this->assertSame( 'img_caption_shortcode', $shortcode_tags['wp_caption'] );
    5353    }
    5454
     
    6363    function test_img_caption_shortcode_with_empty_params_but_content() {
    6464        $result = img_caption_shortcode( array(), $this->caption );
    65         $this->assertEquals( $this->caption, $result );
     65        $this->assertSame( $this->caption, $result );
    6666    }
    6767
     
    7373
    7474        $result = img_caption_shortcode( array(), $this->caption );
    75         $this->assertEquals( $this->alternate_caption, $result );
     75        $this->assertSame( $this->alternate_caption, $result );
    7676    }
    7777
     
    9393            $this->caption
    9494        );
    95         $this->assertEquals( $this->caption, $result );
     95        $this->assertSame( $this->caption, $result );
    9696    }
    9797
     
    118118            $this->caption
    119119        );
    120         $this->assertEquals( $this->caption, $result );
     120        $this->assertSame( $this->caption, $result );
    121121    }
    122122
     
    129129        );
    130130
    131         $this->assertEquals( 2, preg_match_all( '/wp-caption/', $result, $_r ) );
    132         $this->assertEquals( 1, preg_match_all( '/alignnone/', $result, $_r ) );
    133         $this->assertEquals( 1, preg_match_all( "/{$this->caption}/", $result, $_r ) );
     131        $this->assertSame( 2, preg_match_all( '/wp-caption/', $result, $_r ) );
     132        $this->assertSame( 1, preg_match_all( '/alignnone/', $result, $_r ) );
     133        $this->assertSame( 1, preg_match_all( "/{$this->caption}/", $result, $_r ) );
    134134
    135135        if ( current_theme_supports( 'html5', 'caption' ) ) {
    136             $this->assertEquals( 1, preg_match_all( '/width: 20/', $result, $_r ) );
     136            $this->assertSame( 1, preg_match_all( '/width: 20/', $result, $_r ) );
    137137        } else {
    138             $this->assertEquals( 1, preg_match_all( '/width: 30/', $result, $_r ) );
     138            $this->assertSame( 1, preg_match_all( '/width: 30/', $result, $_r ) );
    139139        }
    140140    }
     
    149149            )
    150150        );
    151         $this->assertEquals( 1, preg_match_all( '/wp-caption &myAlignment/', $result, $_r ) );
    152         $this->assertEquals( 1, preg_match_all( '/id="myId"/', $result, $_r ) );
    153         $this->assertEquals( 1, preg_match_all( "/{$this->caption}/", $result, $_r ) );
     151        $this->assertSame( 1, preg_match_all( '/wp-caption &myAlignment/', $result, $_r ) );
     152        $this->assertSame( 1, preg_match_all( '/id="myId"/', $result, $_r ) );
     153        $this->assertSame( 1, preg_match_all( "/{$this->caption}/", $result, $_r ) );
    154154    }
    155155
     
    162162            )
    163163        );
    164         $this->assertEquals( 1, preg_match_all( '/wp-caption alignnone some-class another-class/', $result, $_r ) );
     164        $this->assertSame( 1, preg_match_all( '/wp-caption alignnone some-class another-class/', $result, $_r ) );
    165165
    166166    }
     
    175175        $our_preg = preg_quote( $this->html_content );
    176176
    177         $this->assertEquals( 1, preg_match_all( "~{$our_preg}~", $result, $_r ) );
     177        $this->assertSame( 1, preg_match_all( "~{$our_preg}~", $result, $_r ) );
    178178    }
    179179
     
    186186        $content_preg = preg_quote( $this->html_content );
    187187
    188         $this->assertEquals( 1, preg_match_all( "~{$img_preg}.*wp-caption-text~", $result, $_r ) );
    189         $this->assertEquals( 1, preg_match_all( "~wp-caption-text.*{$content_preg}~", $result, $_r ) );
     188        $this->assertSame( 1, preg_match_all( "~{$img_preg}.*wp-caption-text~", $result, $_r ) );
     189        $this->assertSame( 1, preg_match_all( "~wp-caption-text.*{$content_preg}~", $result, $_r ) );
    190190    }
    191191
     
    199199        $content_preg = preg_quote( $this->html_content );
    200200
    201         $this->assertEquals( 1, preg_match_all( "~{$img_preg}.*wp-caption-text~", $result, $_r ) );
    202         $this->assertEquals( 1, preg_match_all( "~wp-caption-text.*{$content_preg}~", $result, $_r ) );
     201        $this->assertSame( 1, preg_match_all( "~{$img_preg}.*wp-caption-text~", $result, $_r ) );
     202        $this->assertSame( 1, preg_match_all( "~wp-caption-text.*{$content_preg}~", $result, $_r ) );
    203203    }
    204204
     
    212212        $content_preg = preg_quote( $this->html_content );
    213213
    214         $this->assertEquals( 1, preg_match_all( "~{$img_preg}.*wp-caption-text~", $result, $_r ) );
    215         $this->assertEquals( 1, preg_match_all( "~wp-caption-text.*{$content_preg}~", $result, $_r ) );
     214        $this->assertSame( 1, preg_match_all( "~{$img_preg}.*wp-caption-text~", $result, $_r ) );
     215        $this->assertSame( 1, preg_match_all( "~wp-caption-text.*{$content_preg}~", $result, $_r ) );
    216216    }
    217217
     
    228228        );
    229229
    230         $this->assertEquals( 1, preg_match_all( '/aria-describedby="caption-myId"/', $result, $_r ) );
     230        $this->assertSame( 1, preg_match_all( '/aria-describedby="caption-myId"/', $result, $_r ) );
    231231    }
    232232
     
    246246
    247247        $result = $wp_embed->autoembed( $content );
    248         $this->assertEquals( $content, $result );
     248        $this->assertSame( $content, $result );
    249249    }
    250250
     
    268268
    269269        $result = $wp_embed->autoembed( $content );
    270         $this->assertEquals( $content, $result );
     270        $this->assertSame( $content, $result );
    271271    }
    272272
     
    334334        $wp_embed = new Test_Autoembed;
    335335
    336         $this->assertEquals( $wp_embed->autoembed( $content ), $result ? $result : $content );
     336        $this->assertSame( $wp_embed->autoembed( $content ), $result ? $result : $content );
    337337    }
    338338
     
    351351        $prepped = wp_prepare_attachment_for_js( $post );
    352352        $this->assertInternalType( 'array', $prepped );
    353         $this->assertEquals( 0, $prepped['uploadedTo'] );
    354         $this->assertEquals( '', $prepped['mime'] );
    355         $this->assertEquals( '', $prepped['type'] );
    356         $this->assertEquals( '', $prepped['subtype'] );
     353        $this->assertSame( 0, $prepped['uploadedTo'] );
     354        $this->assertSame( '', $prepped['mime'] );
     355        $this->assertSame( '', $prepped['type'] );
     356        $this->assertSame( '', $prepped['subtype'] );
    357357        // #21963, there will be a GUID always, so there will be a URL.
    358358        $this->assertNotEquals( '', $prepped['url'] );
    359         $this->assertEquals( site_url( 'wp-includes/images/media/default.png' ), $prepped['icon'] );
     359        $this->assertSame( site_url( 'wp-includes/images/media/default.png' ), $prepped['icon'] );
    360360
    361361        // Fake a mime.
    362362        $post->post_mime_type = 'image/jpeg';
    363363        $prepped              = wp_prepare_attachment_for_js( $post );
    364         $this->assertEquals( 'image/jpeg', $prepped['mime'] );
    365         $this->assertEquals( 'image', $prepped['type'] );
    366         $this->assertEquals( 'jpeg', $prepped['subtype'] );
     364        $this->assertSame( 'image/jpeg', $prepped['mime'] );
     365        $this->assertSame( 'image', $prepped['type'] );
     366        $this->assertSame( 'jpeg', $prepped['subtype'] );
    367367
    368368        // Fake a mime without a slash. See #WP22532.
    369369        $post->post_mime_type = 'image';
    370370        $prepped              = wp_prepare_attachment_for_js( $post );
    371         $this->assertEquals( 'image', $prepped['mime'] );
    372         $this->assertEquals( 'image', $prepped['type'] );
    373         $this->assertEquals( '', $prepped['subtype'] );
     371        $this->assertSame( 'image', $prepped['mime'] );
     372        $this->assertSame( 'image', $prepped['type'] );
     373        $this->assertSame( '', $prepped['subtype'] );
    374374
    375375        // Test that if author is not found, we return "(no author)" as `display_name`.
    376376        // The previously used test post contains no author, so we can reuse it.
    377         $this->assertEquals( '(no author)', $prepped['authorName'] );
     377        $this->assertSame( '(no author)', $prepped['authorName'] );
    378378
    379379        // Test that if author has HTML entities in display_name, they're decoded correctly.
     
    385385        $post->post_author  = $html_entity_author;
    386386        $prepped            = wp_prepare_attachment_for_js( $post );
    387         $this->assertEquals( 'You & Me', $prepped['authorName'] );
     387        $this->assertSame( 'You & Me', $prepped['authorName'] );
    388388    }
    389389
     
    429429
    430430        // Test if boundaries are correct.
    431         $this->assertEquals( '1TB', wp_convert_bytes_to_hr( $tb ) );
    432         $this->assertEquals( '1GB', wp_convert_bytes_to_hr( $gb ) );
    433         $this->assertEquals( '1MB', wp_convert_bytes_to_hr( $mb ) );
    434         $this->assertEquals( '1KB', wp_convert_bytes_to_hr( $kb ) );
    435 
    436         $this->assertEquals( '1 TB', size_format( $tb ) );
    437         $this->assertEquals( '1 GB', size_format( $gb ) );
    438         $this->assertEquals( '1 MB', size_format( $mb ) );
    439         $this->assertEquals( '1 KB', size_format( $kb ) );
     431        $this->assertSame( '1TB', wp_convert_bytes_to_hr( $tb ) );
     432        $this->assertSame( '1GB', wp_convert_bytes_to_hr( $gb ) );
     433        $this->assertSame( '1MB', wp_convert_bytes_to_hr( $mb ) );
     434        $this->assertSame( '1KB', wp_convert_bytes_to_hr( $kb ) );
     435
     436        $this->assertSame( '1 TB', size_format( $tb ) );
     437        $this->assertSame( '1 GB', size_format( $gb ) );
     438        $this->assertSame( '1 MB', size_format( $mb ) );
     439        $this->assertSame( '1 KB', size_format( $kb ) );
    440440
    441441        // Now some values around.
     
    453453
    454454        // Edge.
    455         $this->assertEquals( '-1B', wp_convert_bytes_to_hr( -1 ) );
    456         $this->assertEquals( '0B', wp_convert_bytes_to_hr( 0 ) );
     455        $this->assertSame( '-1B', wp_convert_bytes_to_hr( -1 ) );
     456        $this->assertSame( '0B', wp_convert_bytes_to_hr( 0 ) );
    457457    }
    458458
     
    523523        $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
    524524        $srcs    = get_post_galleries_images( $post_id );
    525         $this->assertEquals( $srcs, array( $ids1_srcs, $ids2_srcs ) );
     525        $this->assertSame( $srcs, array( $ids1_srcs, $ids2_srcs ) );
    526526    }
    527527
     
    673673        $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
    674674        $srcs    = get_post_gallery_images( $post_id );
    675         $this->assertEquals( $srcs, $ids1_srcs );
     675        $this->assertSame( $srcs, $ids1_srcs );
    676676    }
    677677
     
    722722
    723723        $matches = get_media_embedded_in_content( $content, 'audio' );
    724         $this->assertEquals( array( $audio ), $matches );
     724        $this->assertSame( array( $audio ), $matches );
    725725
    726726        $matches = get_media_embedded_in_content( $content, 'video' );
    727         $this->assertEquals( array( $video ), $matches );
     727        $this->assertSame( array( $video ), $matches );
    728728
    729729        $matches = get_media_embedded_in_content( $content, 'object' );
    730         $this->assertEquals( array( $object ), $matches );
     730        $this->assertSame( array( $object ), $matches );
    731731
    732732        $matches = get_media_embedded_in_content( $content, 'embed' );
    733         $this->assertEquals( array( $embed ), $matches );
     733        $this->assertSame( array( $embed ), $matches );
    734734
    735735        $matches = get_media_embedded_in_content( $content, 'iframe' );
    736         $this->assertEquals( array( $iframe ), $matches );
     736        $this->assertSame( array( $iframe ), $matches );
    737737
    738738        $matches = get_media_embedded_in_content( $content, $types );
    739         $this->assertEquals( $contents, $matches );
     739        $this->assertSame( $contents, $matches );
    740740    }
    741741
     
    754754
    755755        $matches1 = get_media_embedded_in_content( $content, array( 'audio', 'video' ) );
    756         $this->assertEquals( array( $audio, $video ), $matches1 );
     756        $this->assertSame( array( $audio, $video ), $matches1 );
    757757
    758758        $reversed = $video . $audio;
    759759        $matches2 = get_media_embedded_in_content( $reversed, array( 'audio', 'video' ) );
    760         $this->assertEquals( array( $video, $audio ), $matches2 );
     760        $this->assertSame( array( $video, $audio ), $matches2 );
    761761    }
    762762
     
    860860            "http://domain.tld/wp-content/uploads/2013/12/xyz.mp4</a></video></div>\n";
    861861
    862         $this->assertEquals( $expected, $content );
     862        $this->assertSame( $expected, $content );
    863863    }
    864864
     
    10171017
    10181018        $this->assertArrayHasKey( 'test-size', $sizes );
    1019         $this->assertEquals( 200, $sizes['test-size']['width'] );
    1020         $this->assertEquals( 600, $sizes['test-size']['height'] );
     1019        $this->assertSame( 200, $sizes['test-size']['width'] );
     1020        $this->assertSame( 600, $sizes['test-size']['height'] );
    10211021    }
    10221022
     
    10571057
    10581058        $image_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_path;
    1059         $this->assertEquals( $attachment_id, attachment_url_to_postid( $image_url ) );
     1059        $this->assertSame( $attachment_id, attachment_url_to_postid( $image_url ) );
    10601060    }
    10611061
     
    10751075
    10761076        $image_url = 'https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_path;
    1077         $this->assertEquals( $attachment_id, attachment_url_to_postid( $image_url ) );
     1077        $this->assertSame( $attachment_id, attachment_url_to_postid( $image_url ) );
    10781078    }
    10791079
     
    11031103
    11041104        $image_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_path_upper_case;
    1105         $this->assertEquals( $attachment_id_upper_case, attachment_url_to_postid( $image_url ) );
     1105        $this->assertSame( $attachment_id_upper_case, attachment_url_to_postid( $image_url ) );
    11061106    }
    11071107
     
    11191119        add_filter( 'upload_dir', array( $this, '_upload_dir' ) );
    11201120        $image_url = 'http://192.168.1.20.com/wp-content/uploads/' . $image_path;
    1121         $this->assertEquals( $attachment_id, attachment_url_to_postid( $image_url ) );
     1121        $this->assertSame( $attachment_id, attachment_url_to_postid( $image_url ) );
    11221122        remove_filter( 'upload_dir', array( $this, '_upload_dir' ) );
    11231123    }
     
    11341134        $post_id = attachment_url_to_postid( '' );
    11351135        $this->assertInternalType( 'int', $post_id );
    1136         $this->assertEquals( 0, $post_id );
     1136        $this->assertSame( 0, $post_id );
    11371137    }
    11381138
     
    11731173        wp_delete_attachment( $post_id );
    11741174
    1175         $this->assertEquals( 'This is a comment. / Это комментарий. / Βλέπετε ένα σχόλιο.', $post->post_excerpt );
     1175        $this->assertSame( 'This is a comment. / Это комментарий. / Βλέπετε ένα σχόλιο.', $post->post_excerpt );
    11761176    }
    11771177
     
    12121212        wp_delete_attachment( $post_id );
    12131213
    1214         $this->assertEquals( 'This is a test', $post->post_title );
     1214        $this->assertSame( 'This is a test', $post->post_title );
    12151215    }
    12161216
     
    12291229
    12301230        $result = $wp_embed->autoembed( $content );
    1231         $this->assertEquals( $content, $result );
     1231        $this->assertSame( $content, $result );
    12321232    }
    12331233
     
    12451245
    12461246        $result = $wp_embed->autoembed( $content );
    1247         $this->assertEquals( $content, $result );
     1247        $this->assertSame( $content, $result );
    12481248    }
    12491249
     
    12791279
    12801280        $result = apply_filters( 'the_content', $content );
    1281         $this->assertEqualsIgnoreEOL( $expected, $result );
     1281        $this->assertSameIgnoreEOL( $expected, $result );
    12821282    }
    12831283
     
    13081308
    13091309        $result = $wp_embed->autoembed( $content );
    1310         $this->assertEquals( $expected, $result );
     1310        $this->assertSame( $expected, $result );
    13111311
    13121312        $content = <<<EOF
     
    13221322
    13231323        $result = $wp_embed->autoembed( $content );
    1324         $this->assertEquals( $content, $result );
     1324        $this->assertSame( $content, $result );
    13251325
    13261326        remove_filter( 'embed_maybe_make_link', array( $this, 'filter_wp_embed_shortcode_custom' ), 10 );
     
    13411341        );
    13421342
    1343         $this->assertEquals( $expected, wp_get_attachment_image( self::$large_id ) );
     1343        $this->assertSame( $expected, wp_get_attachment_image( self::$large_id ) );
    13441344    }
    13451345
     
    13611361        );
    13621362
    1363         $this->assertEquals( $expected, wp_get_attachment_image( self::$large_id ) );
     1363        $this->assertSame( $expected, wp_get_attachment_image( self::$large_id ) );
    13641364
    13651365        // Cleanup.
     
    13851385        $image = wp_get_attachment_image_src( $attachment_id, 'thumbnail', false );
    13861386
    1387         $this->assertEquals( $image[0], wp_get_attachment_image_url( $attachment_id ) );
     1387        $this->assertSame( $image[0], wp_get_attachment_image_url( $attachment_id ) );
    13881388    }
    13891389
     
    14091409        $this->assertFalse( wp_get_attachment_caption( $post_id ) );
    14101410
    1411         $this->assertEquals( $caption, wp_get_attachment_caption( $attachment_id ) );
     1411        $this->assertSame( $caption, wp_get_attachment_caption( $attachment_id ) );
    14121412    }
    14131413
     
    14271427        );
    14281428
    1429         $this->assertEquals( '', wp_get_attachment_caption( $attachment_id ) );
     1429        $this->assertSame( '', wp_get_attachment_caption( $attachment_id ) );
    14301430    }
    14311431
     
    23882388        $attachment_id = wp_insert_attachment( $data, '', 0, true );
    23892389        $this->assertWPError( $attachment_id );
    2390         $this->assertEquals( 'invalid_date', $attachment_id->get_error_code() );
     2390        $this->assertSame( 'invalid_date', $attachment_id->get_error_code() );
    23912391
    23922392        $attachment_id = wp_insert_attachment( $data, '', 0 );
     
    24072407        );
    24082408
    2409         $this->assertEquals( 123, wp_get_media_creation_timestamp( $metadata ) );
     2409        $this->assertSame( 123, wp_get_media_creation_timestamp( $metadata ) );
    24102410    }
    24112411
     
    24252425        );
    24262426
    2427         $this->assertEquals( 1450978809, wp_get_media_creation_timestamp( $metadata ) );
     2427        $this->assertSame( 1450978809, wp_get_media_creation_timestamp( $metadata ) );
    24282428    }
    24292429
     
    24452445        );
    24462446
    2447         $this->assertEquals( 1450978805, wp_get_media_creation_timestamp( $metadata ) );
     2447        $this->assertSame( 1450978805, wp_get_media_creation_timestamp( $metadata ) );
    24482448    }
    24492449
     
    24632463        );
    24642464
    2465         $this->assertEquals( 1265680539, wp_get_media_creation_timestamp( $metadata ) );
     2465        $this->assertSame( 1265680539, wp_get_media_creation_timestamp( $metadata ) );
    24662466    }
    24672467
     
    24782478        $metadata = wp_read_audio_metadata( $video );
    24792479
    2480         $this->assertEquals( 1269120551, $metadata['created_timestamp'] );
     2480        $this->assertSame( 1269120551, $metadata['created_timestamp'] );
    24812481    }
    24822482
     
    24882488        $metadata = wp_read_video_metadata( $video );
    24892489
    2490         $this->assertEquals( 1269120551, $metadata['created_timestamp'] );
     2490        $this->assertSame( 1269120551, $metadata['created_timestamp'] );
    24912491    }
    24922492
     
    24982498        $metadata = wp_read_video_metadata( $video );
    24992499
    2500         $this->assertEquals( 1269120551, $metadata['created_timestamp'] );
     2500        $this->assertSame( 1269120551, $metadata['created_timestamp'] );
    25012501    }
    25022502
     
    25082508        $metadata = wp_read_video_metadata( $video );
    25092509
    2510         $this->assertEquals( 1269120551, $metadata['created_timestamp'] );
     2510        $this->assertSame( 1269120551, $metadata['created_timestamp'] );
    25112511    }
    25122512
     
    25182518        $metadata = wp_read_video_metadata( $video );
    25192519
    2520         $this->assertEquals( 1269120551, $metadata['created_timestamp'] );
     2520        $this->assertSame( 1269120551, $metadata['created_timestamp'] );
    25212521    }
    25222522
     
    29702970            )
    29712971        );
    2972         $this->assertEquals( 2, substr_count( $actual, '.jpg' ) );
     2972        $this->assertSame( 2, substr_count( $actual, '.jpg' ) );
    29732973
    29742974        // None: Does not link.
Note: See TracChangeset for help on using the changeset viewer.