Make WordPress Core

Changeset 52266


Ignore:
Timestamp:
11/29/2021 08:45:31 AM (2 years ago)
Author:
SergeyBiryukov
Message:

Tests: Replace assertEquals() with assertSame() in block template 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(), assertSameSets(), or assertSameSetsWithIndex() should generally be preferred, to make the tests more reliable.

Follow-up to [51003], [51079], [52062], [52265].

See #53364, #53363, #54335.

Location:
trunk/tests/phpunit/tests
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/block-template-utils.php

    r52265 r52266  
    11<?php
    22/**
    3  * Block_Template_Utils_Test class
     3 * Tests_Block_Template_Utils class
    44 *
    5  * @package    WordPress
     5 * @package WordPress
    66 */
    77
     
    8585
    8686        $this->assertNotWPError( $template );
    87         $this->assertEquals( get_stylesheet() . '//my_template', $template->id );
    88         $this->assertEquals( get_stylesheet(), $template->theme );
    89         $this->assertEquals( 'my_template', $template->slug );
    90         $this->assertEquals( 'publish', $template->status );
    91         $this->assertEquals( 'custom', $template->source );
    92         $this->assertEquals( 'My Template', $template->title );
    93         $this->assertEquals( 'Description of my template', $template->description );
    94         $this->assertEquals( 'wp_template', $template->type );
     87        $this->assertSame( get_stylesheet() . '//my_template', $template->id );
     88        $this->assertSame( get_stylesheet(), $template->theme );
     89        $this->assertSame( 'my_template', $template->slug );
     90        $this->assertSame( 'publish', $template->status );
     91        $this->assertSame( 'custom', $template->source );
     92        $this->assertSame( 'My Template', $template->title );
     93        $this->assertSame( 'Description of my template', $template->description );
     94        $this->assertSame( 'wp_template', $template->type );
    9595
    9696        // Test template parts.
     
    100100        );
    101101        $this->assertNotWPError( $template_part );
    102         $this->assertEquals( get_stylesheet() . '//my_template_part', $template_part->id );
    103         $this->assertEquals( get_stylesheet(), $template_part->theme );
    104         $this->assertEquals( 'my_template_part', $template_part->slug );
    105         $this->assertEquals( 'publish', $template_part->status );
    106         $this->assertEquals( 'custom', $template_part->source );
    107         $this->assertEquals( 'My Template Part', $template_part->title );
    108         $this->assertEquals( 'Description of my template part', $template_part->description );
    109         $this->assertEquals( 'wp_template_part', $template_part->type );
    110         $this->assertEquals( WP_TEMPLATE_PART_AREA_HEADER, $template_part->area );
     102        $this->assertSame( get_stylesheet() . '//my_template_part', $template_part->id );
     103        $this->assertSame( get_stylesheet(), $template_part->theme );
     104        $this->assertSame( 'my_template_part', $template_part->slug );
     105        $this->assertSame( 'publish', $template_part->status );
     106        $this->assertSame( 'custom', $template_part->source );
     107        $this->assertSame( 'My Template Part', $template_part->title );
     108        $this->assertSame( 'Description of my template part', $template_part->description );
     109        $this->assertSame( 'wp_template_part', $template_part->type );
     110        $this->assertSame( WP_TEMPLATE_PART_AREA_HEADER, $template_part->area );
    111111    }
    112112
     
    120120        );
    121121
    122         $this->assertEquals( get_stylesheet() . '//single', $template->id );
    123         $this->assertEquals( get_stylesheet(), $template->theme );
    124         $this->assertEquals( 'single', $template->slug );
    125         $this->assertEquals( 'publish', $template->status );
    126         $this->assertEquals( 'theme', $template->source );
    127         $this->assertEquals( 'Single Post', $template->title );
    128         $this->assertEquals( 'Template used to display a single blog post.', $template->description );
    129         $this->assertEquals( 'wp_template', $template->type );
     122        $this->assertSame( get_stylesheet() . '//single', $template->id );
     123        $this->assertSame( get_stylesheet(), $template->theme );
     124        $this->assertSame( 'single', $template->slug );
     125        $this->assertSame( 'publish', $template->status );
     126        $this->assertSame( 'theme', $template->source );
     127        $this->assertSame( 'Single Post', $template->title );
     128        $this->assertSame( 'Template used to display a single blog post.', $template->description );
     129        $this->assertSame( 'wp_template', $template->type );
    130130
    131131        // Test template parts.
     
    138138            'wp_template_part'
    139139        );
    140         $this->assertEquals( get_stylesheet() . '//header', $template_part->id );
    141         $this->assertEquals( get_stylesheet(), $template_part->theme );
    142         $this->assertEquals( 'header', $template_part->slug );
    143         $this->assertEquals( 'publish', $template_part->status );
    144         $this->assertEquals( 'theme', $template_part->source );
    145         $this->assertEquals( 'header', $template_part->title );
    146         $this->assertEquals( '', $template_part->description );
    147         $this->assertEquals( 'wp_template_part', $template_part->type );
    148         $this->assertEquals( WP_TEMPLATE_PART_AREA_HEADER, $template_part->area );
     140        $this->assertSame( get_stylesheet() . '//header', $template_part->id );
     141        $this->assertSame( get_stylesheet(), $template_part->theme );
     142        $this->assertSame( 'header', $template_part->slug );
     143        $this->assertSame( 'publish', $template_part->status );
     144        $this->assertSame( 'theme', $template_part->source );
     145        $this->assertSame( 'header', $template_part->title );
     146        $this->assertSame( '', $template_part->description );
     147        $this->assertSame( 'wp_template_part', $template_part->type );
     148        $this->assertSame( WP_TEMPLATE_PART_AREA_HEADER, $template_part->area );
    149149    }
    150150
     
    160160            get_stylesheet()
    161161        );
    162         $this->assertEquals( $expected, $template_content );
     162        $this->assertSame( $expected, $template_content );
    163163
    164164        $content_without_theme_attribute_nested = '<!-- wp:group --><!-- wp:template-part {"slug":"header","align":"full", "tagName":"header","className":"site-header"} /--><!-- /wp:group -->';
     
    171171            get_stylesheet()
    172172        );
    173         $this->assertEquals( $expected, $template_content );
     173        $this->assertSame( $expected, $template_content );
    174174
    175175        // Does not inject theme when there is an existing theme attribute.
     
    179179            $theme
    180180        );
    181         $this->assertEquals( $content_with_existing_theme_attribute, $template_content );
     181        $this->assertSame( $content_with_existing_theme_attribute, $template_content );
    182182
    183183        // Does not inject theme when there is no template part.
     
    187187            $theme
    188188        );
    189         $this->assertEquals( $content_with_no_template_part, $template_content );
     189        $this->assertSame( $content_with_no_template_part, $template_content );
    190190    }
    191191
     
    198198        /* $id       = get_stylesheet() . '//' . 'index';
    199199        $template = get_block_template( $id, 'wp_template' );
    200         $this->assertEquals( $id, $template->id );
    201         $this->assertEquals( get_stylesheet(), $template->theme );
    202         $this->assertEquals( 'index', $template->slug );
    203         $this->assertEquals( 'publish', $template->status );
    204         $this->assertEquals( 'theme', $template->source );
    205         $this->assertEquals( 'wp_template', $template->type );
     200        $this->assertSame( $id, $template->id );
     201        $this->assertSame( get_stylesheet(), $template->theme );
     202        $this->assertSame( 'index', $template->slug );
     203        $this->assertSame( 'publish', $template->status );
     204        $this->assertSame( 'theme', $template->source );
     205        $this->assertSame( 'wp_template', $template->type );
    206206
    207207        // Test template parts.
    208208        $id       = get_stylesheet() . '//' . 'header';
    209209        $template = get_block_template( $id, 'wp_template_part' );
    210         $this->assertEquals( $id, $template->id );
    211         $this->assertEquals( get_stylesheet(), $template->theme );
    212         $this->assertEquals( 'header', $template->slug );
    213         $this->assertEquals( 'publish', $template->status );
    214         $this->assertEquals( 'theme', $template->source );
    215         $this->assertEquals( 'wp_template_part', $template->type );
    216         $this->assertEquals( WP_TEMPLATE_PART_AREA_HEADER, $template->area );
     210        $this->assertSame( $id, $template->id );
     211        $this->assertSame( get_stylesheet(), $template->theme );
     212        $this->assertSame( 'header', $template->slug );
     213        $this->assertSame( 'publish', $template->status );
     214        $this->assertSame( 'theme', $template->source );
     215        $this->assertSame( 'wp_template_part', $template->type );
     216        $this->assertSame( WP_TEMPLATE_PART_AREA_HEADER, $template->area );
    217217        */
    218218    }
     
    224224        $id       = get_stylesheet() . '//' . 'my_template';
    225225        $template = get_block_template( $id, 'wp_template' );
    226         $this->assertEquals( $id, $template->id );
    227         $this->assertEquals( get_stylesheet(), $template->theme );
    228         $this->assertEquals( 'my_template', $template->slug );
    229         $this->assertEquals( 'publish', $template->status );
    230         $this->assertEquals( 'custom', $template->source );
    231         $this->assertEquals( 'wp_template', $template->type );
     226        $this->assertSame( $id, $template->id );
     227        $this->assertSame( get_stylesheet(), $template->theme );
     228        $this->assertSame( 'my_template', $template->slug );
     229        $this->assertSame( 'publish', $template->status );
     230        $this->assertSame( 'custom', $template->source );
     231        $this->assertSame( 'wp_template', $template->type );
    232232
    233233        // Test template parts.
    234234        $id       = get_stylesheet() . '//' . 'my_template_part';
    235235        $template = get_block_template( $id, 'wp_template_part' );
    236         $this->assertEquals( $id, $template->id );
    237         $this->assertEquals( get_stylesheet(), $template->theme );
    238         $this->assertEquals( 'my_template_part', $template->slug );
    239         $this->assertEquals( 'publish', $template->status );
    240         $this->assertEquals( 'custom', $template->source );
    241         $this->assertEquals( 'wp_template_part', $template->type );
    242         $this->assertEquals( WP_TEMPLATE_PART_AREA_HEADER, $template->area );
     236        $this->assertSame( $id, $template->id );
     237        $this->assertSame( get_stylesheet(), $template->theme );
     238        $this->assertSame( 'my_template_part', $template->slug );
     239        $this->assertSame( 'publish', $template->status );
     240        $this->assertSame( 'custom', $template->source );
     241        $this->assertSame( 'wp_template_part', $template->type );
     242        $this->assertSame( WP_TEMPLATE_PART_AREA_HEADER, $template->area );
    243243    }
    244244
     
    269269        $templates    = get_block_templates( array( 'slug__in' => array( 'my_template' ) ), 'wp_template' );
    270270        $template_ids = get_template_ids( $templates );
    271         $this->assertEquals( array( get_stylesheet() . '//' . 'my_template' ), $template_ids );
     271        $this->assertSame( array( get_stylesheet() . '//' . 'my_template' ), $template_ids );
    272272
    273273        // Filter by CPT ID.
    274274        $templates    = get_block_templates( array( 'wp_id' => self::$post->ID ), 'wp_template' );
    275275        $template_ids = get_template_ids( $templates );
    276         $this->assertEquals( array( get_stylesheet() . '//' . 'my_template' ), $template_ids );
     276        $this->assertSame( array( get_stylesheet() . '//' . 'my_template' ), $template_ids );
    277277
    278278        // Filter template part by area.
     
    280280        /*$templates    = get_block_templates( array( 'area' => WP_TEMPLATE_PART_AREA_HEADER ), 'wp_template_part' );
    281281        $template_ids = get_template_ids( $templates );
    282         $this->assertEquals(
     282        $this->assertSame(
    283283            array(
    284284                get_stylesheet() . '//' . 'my_template_part',
     
    298298        $actual                             = _flatten_blocks( $blocks );
    299299        $expected                           = array( $blocks[0], $blocks[0]['innerBlocks'][0] );
    300         $this->assertEquals( $expected, $actual );
     300        $this->assertSame( $expected, $actual );
    301301
    302302        $content_template_part_inside_group_inside_group = '<!-- wp:group --><!-- wp:group --><!-- wp:template-part {"slug":"header"} /--><!-- /wp:group --><!-- /wp:group -->';
     
    304304        $actual   = _flatten_blocks( $blocks );
    305305        $expected = array( $blocks[0], $blocks[0]['innerBlocks'][0], $blocks[0]['innerBlocks'][0]['innerBlocks'][0] );
    306         $this->assertEquals( $expected, $actual );
     306        $this->assertSame( $expected, $actual );
    307307
    308308        $content_without_inner_blocks = '<!-- wp:group /-->';
     
    310310        $actual                       = _flatten_blocks( $blocks );
    311311        $expected                     = array( $blocks[0] );
    312         $this->assertEquals( $expected, $actual );
     312        $this->assertSame( $expected, $actual );
    313313    }
    314314}
  • trunk/tests/phpunit/tests/block-template.php

    r52265 r52266  
    11<?php
    22/**
    3  * Block_Template_Test class
     3 * Tests_Block_Template class
    44 *
    55 * @package WordPress
     
    3737        );
    3838        $resolved_template_path = locate_block_template( get_stylesheet_directory() . '/page-home.php', $type, $templates );
    39         $this->assertEquals( self::$template_canvas_path, $resolved_template_path );
     39        $this->assertSame( self::$template_canvas_path, $resolved_template_path );
    4040        $this->assertStringEqualsFile( get_stylesheet_directory() . '/templates/page-home.html', $_wp_current_template_content );
    4141    }
     
    5050        );
    5151        $resolved_template_path = locate_block_template( get_stylesheet_directory() . '/page.php', $type, $templates );
    52         $this->assertEquals( self::$template_canvas_path, $resolved_template_path );
     52        $this->assertSame( self::$template_canvas_path, $resolved_template_path );
    5353        $this->assertStringEqualsFile( get_stylesheet_directory() . '/templates/page.html', $_wp_current_template_content );
    5454    }
     
    6161        );
    6262        $resolved_template_path = locate_block_template( get_stylesheet_directory() . '/index.php', $type, $templates );
    63         $this->assertEquals( self::$template_canvas_path, $resolved_template_path );
     63        $this->assertSame( self::$template_canvas_path, $resolved_template_path );
    6464        $this->assertStringEqualsFile( get_stylesheet_directory() . '/templates/index.html', $_wp_current_template_content );
    6565    }
     
    8181        );
    8282        $resolved_template_path = locate_block_template( $page_id_template_path, $type, $templates );
    83         $this->assertEquals( $page_id_template_path, $resolved_template_path );
     83        $this->assertSame( $page_id_template_path, $resolved_template_path );
    8484    }
    8585
     
    111111        );
    112112        $resolved_template_path  = locate_block_template( $page_slug_template_path, $type, $templates );
    113         $this->assertEquals( $page_slug_template_path, $resolved_template_path );
     113        $this->assertSame( $page_slug_template_path, $resolved_template_path );
    114114    }
    115115
     
    128128        );
    129129        $resolved_template_path          = locate_block_template( $parent_theme_page_template_path, $type, $templates );
    130         $this->assertEquals( self::$template_canvas_path, $resolved_template_path );
     130        $this->assertSame( self::$template_canvas_path, $resolved_template_path );
    131131        $this->assertStringEqualsFile( get_stylesheet_directory() . '/templates/page-1.html', $_wp_current_template_content );
    132132    }
Note: See TracChangeset for help on using the changeset viewer.