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

    r47198 r48937  
    3939        $widget_categories = get_option( 'widget_categories' );
    4040        $this->assertArrayHasKey( 2, $widget_categories );
    41         $this->assertEquals( '', $widget_categories[2]['title'] );
     41        $this->assertSame( '', $widget_categories[2]['title'] );
    4242
    4343        $this->backup_registered_sidebars = $GLOBALS['wp_registered_sidebars'];
     
    9191    function test_construct() {
    9292        $this->assertInstanceOf( 'WP_Customize_Widgets', $this->manager->widgets );
    93         $this->assertEquals( $this->manager, $this->manager->widgets->manager );
     93        $this->assertSame( $this->manager, $this->manager->widgets->manager );
    9494    }
    9595
     
    106106        register_sidebar( array( 'id' => $sidebar_id ) );
    107107        $this->manager->widgets->customize_register();
    108         $this->assertEquals( array_fill_keys( array( 'wp_inactive_widgets', $sidebar_id ), array() ), wp_get_sidebars_widgets() );
     108        $this->assertSame( array_fill_keys( array( 'wp_inactive_widgets', $sidebar_id ), array() ), wp_get_sidebars_widgets() );
    109109    }
    110110
     
    122122        $selective_refreshable_widgets = $this->manager->widgets->get_selective_refreshable_widgets();
    123123        $this->assertInternalType( 'array', $selective_refreshable_widgets );
    124         $this->assertEquals( count( $wp_widget_factory->widgets ), count( $selective_refreshable_widgets ) );
     124        $this->assertSame( count( $wp_widget_factory->widgets ), count( $selective_refreshable_widgets ) );
    125125        $this->assertArrayHasKey( 'text', $selective_refreshable_widgets );
    126126        $this->assertTrue( $selective_refreshable_widgets['text'] );
     
    207207        $setting = $this->manager->get_setting( 'widget_categories[2]' );
    208208        $this->assertNotEmpty( $setting, 'Expected setting for pre-existing widget category-2, being customized.' );
    209         $this->assertEquals( $expected_transport, $setting->transport );
     209        $this->assertSame( $expected_transport, $setting->transport );
    210210
    211211        $setting = $this->manager->get_setting( 'widget_search[2]' );
    212212        $this->assertNotEmpty( $setting, 'Expected setting for pre-existing widget search-2, not being customized.' );
    213         $this->assertEquals( $expected_transport, $setting->transport );
     213        $this->assertSame( $expected_transport, $setting->transport );
    214214
    215215        $setting = $this->manager->get_setting( 'widget_search[3]' );
    216216        $this->assertNotEmpty( $setting, 'Expected dynamic setting for non-existing widget search-3, being customized.' );
    217         $this->assertEquals( $expected_transport, $setting->transport );
     217        $this->assertSame( $expected_transport, $setting->transport );
    218218
    219219        $widget_categories = get_option( 'widget_categories' );
    220         $this->assertEquals( $raw_widget_customized['widget_categories[2]'], $widget_categories[2], 'Expected $wp_customize->get_setting(widget_categories[2])->preview() to have been called.' );
     220        $this->assertSame( $raw_widget_customized['widget_categories[2]'], $widget_categories[2], 'Expected $wp_customize->get_setting(widget_categories[2])->preview() to have been called.' );
    221221    }
    222222
     
    268268        $args         = $this->manager->widgets->get_setting_args( 'widget_foo[2]' );
    269269        foreach ( $default_args as $key => $default_value ) {
    270             $this->assertEquals( $default_value, $args[ $key ] );
    271         }
    272         $this->assertEquals( 'WIDGET_FOO[2]', $args['uppercase_id_set_by_filter'] );
     270            $this->assertSame( $default_value, $args[ $key ] );
     271        }
     272        $this->assertSame( 'WIDGET_FOO[2]', $args['uppercase_id_set_by_filter'] );
    273273
    274274        $default_args = array(
     
    282282        $args         = $this->manager->widgets->get_setting_args( 'widget_search[2]' );
    283283        foreach ( $default_args as $key => $default_value ) {
    284             $this->assertEquals( $default_value, $args[ $key ] );
     284            $this->assertSame( $default_value, $args[ $key ] );
    285285        }
    286286
    287287        remove_theme_support( 'customize-selective-refresh-widgets' );
    288288        $args = $this->manager->widgets->get_setting_args( 'widget_search[2]' );
    289         $this->assertEquals( 'refresh', $args['transport'] );
     289        $this->assertSame( 'refresh', $args['transport'] );
    290290        add_theme_support( 'customize-selective-refresh-widgets' );
    291291
     
    300300        $args          = $this->manager->widgets->get_setting_args( 'widget_bar[3]', $override_args );
    301301        foreach ( $override_args as $key => $override_value ) {
    302             $this->assertEquals( $override_value, $args[ $key ] );
    303         }
    304         $this->assertEquals( 'WIDGET_BAR[3]', $args['uppercase_id_set_by_filter'] );
     302            $this->assertSame( $override_value, $args[ $key ] );
     303        }
     304        $this->assertSame( 'WIDGET_BAR[3]', $args['uppercase_id_set_by_filter'] );
    305305
    306306        $default_args = array(
     
    314314        $args         = $this->manager->widgets->get_setting_args( 'sidebars_widgets[sidebar-1]' );
    315315        foreach ( $default_args as $key => $default_value ) {
    316             $this->assertEquals( $default_value, $args[ $key ] );
    317         }
    318         $this->assertEquals( 'SIDEBARS_WIDGETS[SIDEBAR-1]', $args['uppercase_id_set_by_filter'] );
     316            $this->assertSame( $default_value, $args[ $key ] );
     317        }
     318        $this->assertSame( 'SIDEBARS_WIDGETS[SIDEBAR-1]', $args['uppercase_id_set_by_filter'] );
    319319
    320320        $override_args = array(
     
    328328        $args          = $this->manager->widgets->get_setting_args( 'sidebars_widgets[sidebar-2]', $override_args );
    329329        foreach ( $override_args as $key => $override_value ) {
    330             $this->assertEquals( $override_value, $args[ $key ] );
    331         }
    332         $this->assertEquals( 'SIDEBARS_WIDGETS[SIDEBAR-2]', $args['uppercase_id_set_by_filter'] );
     330            $this->assertSame( $override_value, $args[ $key ] );
     331        }
     332        $this->assertSame( 'SIDEBARS_WIDGETS[SIDEBAR-2]', $args['uppercase_id_set_by_filter'] );
    333333    }
    334334
     
    354354        $this->assertArrayHasKey( 'encoded_serialized_instance', $sanitized_for_js );
    355355        $this->assertTrue( is_serialized( base64_decode( $sanitized_for_js['encoded_serialized_instance'] ), true ) );
    356         $this->assertEquals( $new_categories_instance['title'], $sanitized_for_js['title'] );
     356        $this->assertSame( $new_categories_instance['title'], $sanitized_for_js['title'] );
    357357        $this->assertTrue( $sanitized_for_js['is_widget_customizer_js_value'] );
    358358        $this->assertArrayHasKey( 'instance_hash_key', $sanitized_for_js );
     
    363363
    364364        $unsanitized_from_js = $this->manager->widgets->sanitize_widget_instance( $sanitized_for_js );
    365         $this->assertEquals( $unsanitized_from_js, $new_categories_instance );
     365        $this->assertSame( $unsanitized_from_js, $new_categories_instance );
    366366    }
    367367
     
    426426        $params  = $control->json();
    427427
    428         $this->assertEquals( 'widget_form', $params['type'] );
     428        $this->assertSame( 'widget_form', $params['type'] );
    429429        $this->assertRegExp( '#^<li[^>]+>\s*</li>$#', $params['content'] );
    430430        $this->assertRegExp( '#^<div[^>]*class=\'widget\'[^>]*#s', $params['widget_control'] );
     
    432432        $this->assertNotContains( '<input class="widefat"', $params['widget_control'] );
    433433        $this->assertContains( '<input class="widefat"', $params['widget_content'] );
    434         $this->assertEquals( 'search-2', $params['widget_id'] );
    435         $this->assertEquals( 'search', $params['widget_id_base'] );
     434        $this->assertSame( 'search-2', $params['widget_id'] );
     435        $this->assertSame( 'search', $params['widget_id_base'] );
    436436        $this->assertArrayHasKey( 'sidebar_id', $params );
    437437        $this->assertArrayHasKey( 'width', $params );
     
    497497        $this->assertArrayHasKey( 'instance', $result );
    498498        $this->assertArrayHasKey( 'form', $result );
    499         $this->assertEquals( $instance, $result['instance'] );
     499        $this->assertSame( $instance, $result['instance'] );
    500500        $this->assertContains( sprintf( 'value="%s"', esc_attr( $instance['title'] ) ), $result['form'] );
    501501
     
    508508        $this->assertArrayHasKey( 'instance_hash_key', $post_value );
    509509        $this->assertArrayHasKey( 'is_widget_customizer_js_value', $post_value );
    510         $this->assertEquals( $post_value, $this->manager->widgets->sanitize_widget_js_instance( $instance ) );
     510        $this->assertSame( $post_value, $this->manager->widgets->sanitize_widget_js_instance( $instance ) );
    511511    }
    512512
     
    521521        $args = apply_filters( 'customize_dynamic_partial_args', false, 'widget[search-2]' );
    522522        $this->assertInternalType( 'array', $args );
    523         $this->assertEquals( 'widget', $args['type'] );
    524         $this->assertEquals( array( $this->manager->widgets, 'render_widget_partial' ), $args['render_callback'] );
     523        $this->assertSame( 'widget', $args['type'] );
     524        $this->assertSame( array( $this->manager->widgets, 'render_widget_partial' ), $args['render_callback'] );
    525525        $this->assertTrue( $args['container_inclusive'] );
    526526
    527527        $args = apply_filters( 'customize_dynamic_partial_args', array( 'fallback_refresh' => false ), 'widget[search-2]' );
    528528        $this->assertInternalType( 'array', $args );
    529         $this->assertEquals( 'widget', $args['type'] );
    530         $this->assertEquals( array( $this->manager->widgets, 'render_widget_partial' ), $args['render_callback'] );
     529        $this->assertSame( 'widget', $args['type'] );
     530        $this->assertSame( array( $this->manager->widgets, 'render_widget_partial' ), $args['render_callback'] );
    531531        $this->assertTrue( $args['container_inclusive'] );
    532532        $this->assertFalse( $args['fallback_refresh'] );
     
    545545        add_theme_support( 'customize-selective-refresh-widgets' );
    546546        $this->manager->widgets->selective_refresh_init();
    547         $this->assertEquals( 10, has_action( 'dynamic_sidebar_before', array( $this->manager->widgets, 'start_dynamic_sidebar' ) ) );
    548         $this->assertEquals( 10, has_action( 'dynamic_sidebar_after', array( $this->manager->widgets, 'end_dynamic_sidebar' ) ) );
    549         $this->assertEquals( 10, has_filter( 'dynamic_sidebar_params', array( $this->manager->widgets, 'filter_dynamic_sidebar_params' ) ) );
    550         $this->assertEquals( 10, has_filter( 'wp_kses_allowed_html', array( $this->manager->widgets, 'filter_wp_kses_allowed_data_attributes' ) ) );
     547        $this->assertSame( 10, has_action( 'dynamic_sidebar_before', array( $this->manager->widgets, 'start_dynamic_sidebar' ) ) );
     548        $this->assertSame( 10, has_action( 'dynamic_sidebar_after', array( $this->manager->widgets, 'end_dynamic_sidebar' ) ) );
     549        $this->assertSame( 10, has_filter( 'dynamic_sidebar_params', array( $this->manager->widgets, 'filter_dynamic_sidebar_params' ) ) );
     550        $this->assertSame( 10, has_filter( 'wp_kses_allowed_html', array( $this->manager->widgets, 'filter_wp_kses_allowed_data_attributes' ) ) );
    551551    }
    552552
     
    604604            array(),
    605605        );
    606         $this->assertEquals( $params, $this->manager->widgets->filter_dynamic_sidebar_params( $params ), 'Expected short-circuit if not called after dynamic_sidebar_before.' );
     606        $this->assertSame( $params, $this->manager->widgets->filter_dynamic_sidebar_params( $params ), 'Expected short-circuit if not called after dynamic_sidebar_before.' );
    607607
    608608        ob_start();
    609609        do_action( 'dynamic_sidebar_before', 'foo' );
    610610        $output = ob_get_clean();
    611         $this->assertEquals( '<!--dynamic_sidebar_before:foo:1-->', trim( $output ) );
     611        $this->assertSame( '<!--dynamic_sidebar_before:foo:1-->', trim( $output ) );
    612612
    613613        $bad_params = $params;
    614614        unset( $bad_params[0]['id'] );
    615         $this->assertEquals( $bad_params, $this->manager->widgets->filter_dynamic_sidebar_params( $bad_params ) );
     615        $this->assertSame( $bad_params, $this->manager->widgets->filter_dynamic_sidebar_params( $bad_params ) );
    616616
    617617        $bad_params          = $params;
    618618        $bad_params[0]['id'] = 'non-existing';
    619         $this->assertEquals( $bad_params, $this->manager->widgets->filter_dynamic_sidebar_params( $bad_params ) );
     619        $this->assertSame( $bad_params, $this->manager->widgets->filter_dynamic_sidebar_params( $bad_params ) );
    620620
    621621        $bad_params                     = $params;
    622622        $bad_params[0]['before_widget'] = '   <oops>';
    623         $this->assertEquals( $bad_params, $this->manager->widgets->filter_dynamic_sidebar_params( $bad_params ) );
     623        $this->assertSame( $bad_params, $this->manager->widgets->filter_dynamic_sidebar_params( $bad_params ) );
    624624
    625625        $filtered_params = $this->manager->widgets->filter_dynamic_sidebar_params( $params );
     
    628628        do_action( 'dynamic_sidebar_after', 'foo' );
    629629        $output = ob_get_clean();
    630         $this->assertEquals( '<!--dynamic_sidebar_after:foo:1-->', trim( $output ) );
     630        $this->assertSame( '<!--dynamic_sidebar_after:foo:1-->', trim( $output ) );
    631631
    632632        $output = wp_kses_post( $filtered_params[0]['before_widget'] );
     
    649649        $this->assertNotEmpty( $partials );
    650650        $partial = array_shift( $partials );
    651         $this->assertEquals( $partial_id, $partial->id );
     651        $this->assertSame( $partial_id, $partial->id );
    652652
    653653        $this->assertFalse( $this->manager->widgets->render_widget_partial( $partial, array() ) );
     
    656656        $output = $this->manager->widgets->render_widget_partial( $partial, array( 'sidebar_id' => 'sidebar-1' ) );
    657657
    658         $this->assertEquals( 1, substr_count( $output, 'data-customize-partial-id' ) );
    659         $this->assertEquals( 1, substr_count( $output, 'data-customize-partial-type="widget"' ) );
     658        $this->assertSame( 1, substr_count( $output, 'data-customize-partial-id' ) );
     659        $this->assertSame( 1, substr_count( $output, 'data-customize-partial-type="widget"' ) );
    660660        $this->assertContains( ' id="search-2"', $output );
    661661    }
Note: See TracChangeset for help on using the changeset viewer.