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/widgets/text-widget.php

    r48858 r48937  
    4949    function test_construct() {
    5050        $widget = new WP_Widget_Text();
    51         $this->assertEquals( 'text', $widget->id_base );
    52         $this->assertEquals( 'widget_text', $widget->widget_options['classname'] );
     51        $this->assertSame( 'text', $widget->id_base );
     52        $this->assertSame( 'widget_text', $widget->widget_options['classname'] );
    5353        $this->assertTrue( $widget->widget_options['customize_selective_refresh'] );
    54         $this->assertEquals( 400, $widget->control_options['width'] );
    55         $this->assertEquals( 350, $widget->control_options['height'] );
     54        $this->assertSame( 400, $widget->control_options['width'] );
     55        $this->assertSame( 350, $widget->control_options['height'] );
    5656    }
    5757
     
    6666        $widget->_register();
    6767
    68         $this->assertEquals( 10, has_action( 'admin_print_scripts-widgets.php', array( $widget, 'enqueue_admin_scripts' ) ) );
    69         $this->assertEquals( 10, has_action( 'admin_footer-widgets.php', array( 'WP_Widget_Text', 'render_control_template_scripts' ) ) );
     68        $this->assertSame( 10, has_action( 'admin_print_scripts-widgets.php', array( $widget, 'enqueue_admin_scripts' ) ) );
     69        $this->assertSame( 10, has_action( 'admin_footer-widgets.php', array( 'WP_Widget_Text', 'render_control_template_scripts' ) ) );
    7070        $this->assertContains( 'wp.textWidgets.idBases.push( "text" );', wp_scripts()->registered['text-widgets']->extra['after'] );
    7171        $this->assertFalse( has_action( 'wp_enqueue_scripts', array( $widget, 'enqueue_preview_scripts' ) ) );
     
    9898        $widget = new WP_Widget_Text();
    9999        $widget->_register();
    100         $this->assertEquals( 10, has_action( 'wp_enqueue_scripts', array( $widget, 'enqueue_preview_scripts' ) ) );
     100        $this->assertSame( 10, has_action( 'wp_enqueue_scripts', array( $widget, 'enqueue_preview_scripts' ) ) );
    101101    }
    102102
     
    174174        $this->assertContains( '<br />', $output );
    175175        $this->assertNotEmpty( $this->widget_text_args );
    176         $this->assertEquals( $instance['text'], $this->widget_text_args[0] );
    177         $this->assertEquals( $instance, $this->widget_text_args[1] );
    178         $this->assertEquals( $widget, $this->widget_text_args[2] );
     176        $this->assertSame( $instance['text'], $this->widget_text_args[0] );
     177        $this->assertSame( $instance, $this->widget_text_args[1] );
     178        $this->assertSame( $widget, $this->widget_text_args[2] );
    179179        $this->assertEmpty( $this->widget_text_content_args );
    180180        $this->assertContains( '[filter:widget_text]', $output );
     
    201201        $this->assertContains( '<br />', $output );
    202202        $this->assertCount( 3, $this->widget_text_args );
    203         $this->assertEquals( $expected_instance['text'], $this->widget_text_args[0] );
    204         $this->assertEquals( $expected_instance, $this->widget_text_args[1] );
    205         $this->assertEquals( $widget, $this->widget_text_args[2] );
     203        $this->assertSame( $expected_instance['text'], $this->widget_text_args[0] );
     204        $this->assertSame( $expected_instance, $this->widget_text_args[1] );
     205        $this->assertSame( $widget, $this->widget_text_args[2] );
    206206        $this->assertCount( 3, $this->widget_text_content_args );
    207         $this->assertEquals( $expected_instance['text'] . '[filter:widget_text]', $this->widget_text_content_args[0] );
    208         $this->assertEquals( $expected_instance, $this->widget_text_content_args[1] );
    209         $this->assertEquals( $widget, $this->widget_text_content_args[2] );
     207        $this->assertSame( $expected_instance['text'] . '[filter:widget_text]', $this->widget_text_content_args[0] );
     208        $this->assertSame( $expected_instance, $this->widget_text_content_args[1] );
     209        $this->assertSame( $widget, $this->widget_text_content_args[2] );
    210210        $this->assertContains( wpautop( $expected_instance['text'] . '[filter:widget_text][filter:widget_text_content]' ), $output );
    211211
     
    225225        $this->assertContains( '<br />', $output );
    226226        $this->assertCount( 3, $this->widget_text_args );
    227         $this->assertEquals( $expected_instance['text'], $this->widget_text_args[0] );
    228         $this->assertEquals( $expected_instance, $this->widget_text_args[1] );
    229         $this->assertEquals( $widget, $this->widget_text_args[2] );
     227        $this->assertSame( $expected_instance['text'], $this->widget_text_args[0] );
     228        $this->assertSame( $expected_instance, $this->widget_text_args[1] );
     229        $this->assertSame( $widget, $this->widget_text_args[2] );
    230230        $this->assertCount( 3, $this->widget_text_content_args );
    231         $this->assertEquals( $expected_instance['text'] . '[filter:widget_text]', $this->widget_text_content_args[0] );
    232         $this->assertEquals( $expected_instance, $this->widget_text_content_args[1] );
    233         $this->assertEquals( $widget, $this->widget_text_content_args[2] );
     231        $this->assertSame( $expected_instance['text'] . '[filter:widget_text]', $this->widget_text_content_args[0] );
     232        $this->assertSame( $expected_instance, $this->widget_text_content_args[1] );
     233        $this->assertSame( $widget, $this->widget_text_content_args[2] );
    234234        $this->assertContains( wpautop( $expected_instance['text'] . '[filter:widget_text][filter:widget_text_content]' ), $output );
    235235
     
    249249        $this->assertContains( '<br />', $output );
    250250        $this->assertCount( 3, $this->widget_text_args );
    251         $this->assertEquals( $expected_instance['text'], $this->widget_text_args[0] );
    252         $this->assertEquals( $expected_instance, $this->widget_text_args[1] );
    253         $this->assertEquals( $widget, $this->widget_text_args[2] );
     251        $this->assertSame( $expected_instance['text'], $this->widget_text_args[0] );
     252        $this->assertSame( $expected_instance, $this->widget_text_args[1] );
     253        $this->assertSame( $widget, $this->widget_text_args[2] );
    254254        $this->assertNull( $this->widget_text_content_args );
    255255        $this->assertContains( wpautop( $expected_instance['text'] . '[filter:widget_text]' ), $output );
     
    270270        $this->assertNotContains( '<br />', $output );
    271271        $this->assertCount( 3, $this->widget_text_args );
    272         $this->assertEquals( $expected_instance['text'], $this->widget_text_args[0] );
    273         $this->assertEquals( $expected_instance, $this->widget_text_args[1] );
    274         $this->assertEquals( $widget, $this->widget_text_args[2] );
     272        $this->assertSame( $expected_instance['text'], $this->widget_text_args[0] );
     273        $this->assertSame( $expected_instance, $this->widget_text_args[1] );
     274        $this->assertSame( $widget, $this->widget_text_args[2] );
    275275        $this->assertNull( $this->widget_text_content_args );
    276276        $this->assertContains( $expected_instance['text'] . '[filter:widget_text]', $output );
     
    345345        $widget->widget( $args, $instance );
    346346        $output = ob_get_clean();
    347         $this->assertEquals( 1, $this->shortcode_render_count );
     347        $this->assertSame( 1, $this->shortcode_render_count );
    348348        $this->assertNotContains( '[example]', $output, 'Expected shortcode to be processed in legacy widget with plugin adding filter' );
    349349        $this->assertContains( $this->example_shortcode_content, $output, 'Shortcode was applied without wpautop corrupting it.' );
     
    363363        $widget->widget( $args, $instance );
    364364        $output = ob_get_clean();
    365         $this->assertEquals( 1, $this->shortcode_render_count );
     365        $this->assertSame( 1, $this->shortcode_render_count );
    366366        $this->assertNotContains( '[example]', $output, 'Expected shortcode to be processed in legacy widget with plugin adding filter' );
    367367        $this->assertContains( $this->example_shortcode_content, $output, 'Shortcode was applied without wpautop corrupting it.' );
     
    375375        $widget->widget( $args, $instance );
    376376        $output = ob_get_clean();
    377         $this->assertEquals( 1, $this->shortcode_render_count );
     377        $this->assertSame( 1, $this->shortcode_render_count );
    378378        $this->assertNotContains( '[example]', $output, 'Expected shortcode to be processed in legacy widget with plugin adding filter' );
    379379        $this->assertContains( wpautop( $this->example_shortcode_content ), $output, 'Shortcode was applied *with* wpautop() applying to shortcode output since plugin used legacy filter.' );
     
    391391        // Visual Text Widget with only core-added widget_text_content filter for do_shortcode().
    392392        $this->assertFalse( has_filter( 'widget_text', 'do_shortcode' ) );
    393         $this->assertEquals( 11, has_filter( 'widget_text_content', 'do_shortcode' ), 'Expected core to have set do_shortcode as widget_text_content filter.' );
     393        $this->assertSame( 11, has_filter( 'widget_text_content', 'do_shortcode' ), 'Expected core to have set do_shortcode as widget_text_content filter.' );
    394394        $this->shortcode_render_count = 0;
    395395        ob_start();
    396396        $widget->widget( $args, $instance );
    397397        $output = ob_get_clean();
    398         $this->assertEquals( 1, $this->shortcode_render_count );
     398        $this->assertSame( 1, $this->shortcode_render_count );
    399399        $this->assertContains( $this->example_shortcode_content, $output, 'Shortcode was applied without wpautop corrupting it.' );
    400400        $this->assertNotContains( '<p>' . $this->example_shortcode_content . '</p>', $output, 'Expected shortcode_unautop() to have run.' );
    401401        $this->assertFalse( has_filter( 'widget_text', 'do_shortcode' ), 'The widget_text filter still lacks do_shortcode handler.' );
    402         $this->assertEquals( 11, has_filter( 'widget_text_content', 'do_shortcode' ), 'The widget_text_content filter still has do_shortcode handler.' );
     402        $this->assertSame( 11, has_filter( 'widget_text_content', 'do_shortcode' ), 'The widget_text_content filter still has do_shortcode handler.' );
    403403        $this->assertNull( $this->post_during_shortcode );
    404404
     
    409409        $widget->widget( $args, $instance );
    410410        $output = ob_get_clean();
    411         $this->assertEquals( 1, $this->shortcode_render_count );
     411        $this->assertSame( 1, $this->shortcode_render_count );
    412412        $this->assertContains( $this->example_shortcode_content, $output, 'Shortcode was applied without wpautop corrupting it.' );
    413413        $this->assertNotContains( '<p>' . $this->example_shortcode_content . '</p>', $output, 'Expected shortcode_unautop() to have run.' );
    414         $this->assertEquals( 10, has_filter( 'widget_text', 'do_shortcode' ), 'Expected do_shortcode to be restored to widget_text.' );
     414        $this->assertSame( 10, has_filter( 'widget_text', 'do_shortcode' ), 'Expected do_shortcode to be restored to widget_text.' );
    415415        $this->assertNull( $this->post_during_shortcode );
    416416        $this->assertNull( $this->post_during_shortcode );
     
    424424        $widget->widget( $args, $instance );
    425425        $output = ob_get_clean();
    426         $this->assertEquals( 0, $this->shortcode_render_count );
     426        $this->assertSame( 0, $this->shortcode_render_count );
    427427        $this->assertContains( '[example]', $output );
    428428        $this->assertNotContains( $this->example_shortcode_content, $output );
     
    731731        );
    732732        $result   = $widget->update( $instance, array() );
    733         $this->assertEquals( $expected, $result );
     733        $this->assertSame( $expected, $result );
    734734        $this->assertTrue( ! empty( $expected['filter'] ), 'Expected filter prop to be truthy, to handle case where 4.8 is downgraded to 4.7.' );
    735735
     
    739739        $expected['text'] = $instance['text'];
    740740        $result           = $widget->update( $instance, array() );
    741         $this->assertEquals( $expected, $result, 'KSES should apply as expected.' );
     741        $this->assertSame( $expected, $result, 'KSES should apply as expected.' );
    742742        remove_filter( 'map_meta_cap', array( $this, 'grant_unfiltered_html_cap' ) );
    743743
     
    747747        $expected['text'] = wp_kses_post( $instance['text'] );
    748748        $result           = $widget->update( $instance, array() );
    749         $this->assertEquals( $expected, $result, 'KSES should not apply since user can unfiltered_html.' );
     749        $this->assertSame( $expected, $result, 'KSES should not apply since user can unfiltered_html.' );
    750750        remove_filter( 'map_meta_cap', array( $this, 'revoke_unfiltered_html_cap' ), 10 );
    751751    }
     
    766766        );
    767767        $result   = $widget->update( $instance, array() );
    768         $this->assertEquals( $instance, $result, 'Updating a widget without visual prop and explicit filter=false leaves visual prop absent' );
     768        $this->assertSame( $instance, $result, 'Updating a widget without visual prop and explicit filter=false leaves visual prop absent' );
    769769
    770770        // --
     
    775775        );
    776776        $result   = $widget->update( $instance, array() );
    777         $this->assertEquals( $instance, $result, 'Updating a widget without visual prop and explicit filter=true leaves legacy prop absent.' );
     777        $this->assertSame( $instance, $result, 'Updating a widget without visual prop and explicit filter=true leaves legacy prop absent.' );
    778778
    779779        // --
     
    796796        );
    797797        $result       = $widget->update( $instance, $old_instance );
    798         $this->assertEquals( $expected, $result, 'Updating a pre-existing widget with visual mode forces filter to be true.' );
     798        $this->assertSame( $expected, $result, 'Updating a pre-existing widget with visual mode forces filter to be true.' );
    799799
    800800        // --
     
    817817            )
    818818        );
    819         $this->assertEquals( $expected, $result, 'Updating a pre-existing visual widget retains visual mode when updated.' );
     819        $this->assertSame( $expected, $result, 'Updating a pre-existing visual widget retains visual mode when updated.' );
    820820
    821821        // --
     
    838838            )
    839839        );
    840         $this->assertEquals( $expected, $result, 'Updating a pre-existing visual widget retains visual=true and supplies missing filter=true.' );
     840        $this->assertSame( $expected, $result, 'Updating a pre-existing visual widget retains visual=true and supplies missing filter=true.' );
    841841
    842842        // --
     
    891891            )
    892892        );
    893         $this->assertEquals( $expected, $result, 'Updating a widget that previously had legacy form results in filter allowed to be false.' );
     893        $this->assertSame( $expected, $result, 'Updating a widget that previously had legacy form results in filter allowed to be false.' );
    894894
    895895        // --
     
    907907            )
    908908        );
    909         $this->assertEquals( $expected, $result, 'Updating a widget that had \'content\' as its filter value persists non-legacy mode. This only existed in WP 4.8.0.' );
     909        $this->assertSame( $expected, $result, 'Updating a widget that had \'content\' as its filter value persists non-legacy mode. This only existed in WP 4.8.0.' );
    910910
    911911        // --
     
    944944            )
    945945        );
    946         $this->assertEquals( $expected, $result, 'Updating a widget with filter=content (from WP 4.8.0) upgrades to filter=true&visual=true.' );
     946        $this->assertSame( $expected, $result, 'Updating a widget with filter=content (from WP 4.8.0) upgrades to filter=true&visual=true.' );
    947947    }
    948948
Note: See TracChangeset for help on using the changeset viewer.