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/nav-menu-item-setting.php

    r48858 r48937  
    6969
    7070        $setting = new WP_Customize_Nav_Menu_Item_Setting( $this->wp_customize, 'nav_menu_item[123]' );
    71         $this->assertEquals( 'nav_menu_item', $setting->type );
    72         $this->assertEquals( 123, $setting->post_id );
     71        $this->assertSame( 'nav_menu_item', $setting->type );
     72        $this->assertSame( 123, $setting->post_id );
    7373        $this->assertNull( $setting->previous_post_id );
    7474        $this->assertNull( $setting->update_status );
     
    9494            '_invalid'         => false,
    9595        );
    96         $this->assertEquals( $default, $setting->default );
     96        $this->assertSame( $default, $setting->default );
    9797
    9898        $exception = null;
     
    137137        );
    138138        $setting = new WP_Customize_Nav_Menu_Item_Setting( $this->wp_customize, 'nav_menu_item[-5]', compact( 'default' ) );
    139         $this->assertEquals( -5, $setting->post_id );
     139        $this->assertSame( -5, $setting->post_id );
    140140        $this->assertNull( $setting->previous_post_id );
    141         $this->assertEquals( $default, $setting->default );
     141        $this->assertSame( $default, $setting->default );
    142142    }
    143143
     
    168168        $post      = get_post( $item_id );
    169169        $menu_item = wp_setup_nav_menu_item( $post );
    170         $this->assertEquals( $item_title, $menu_item->title );
     170        $this->assertSame( $item_title, $menu_item->title );
    171171
    172172        $setting_id = "nav_menu_item[$item_id]";
     
    174174
    175175        $value = $setting->value();
    176         $this->assertEquals( $menu_item->title, $value['title'] );
    177         $this->assertEquals( $menu_item->type, $value['type'] );
     176        $this->assertSame( $menu_item->title, $value['title'] );
     177        $this->assertSame( $menu_item->type, $value['type'] );
    178178        $this->assertEquals( $menu_item->object_id, $value['object_id'] );
    179         $this->assertEquals( $menu_id, $value['nav_menu_term_id'] );
    180         $this->assertEquals( 'Hello World', $value['original_title'] );
     179        $this->assertSame( $menu_id, $value['nav_menu_term_id'] );
     180        $this->assertSame( 'Hello World', $value['original_title'] );
    181181
    182182        $other_menu_id = wp_create_nav_menu( 'Menu2' );
     
    189189        );
    190190        $value = $setting->value();
    191         $this->assertEquals( 'Hola', $value['title'] );
    192         $this->assertEquals( $other_menu_id, $value['nav_menu_term_id'] );
     191        $this->assertSame( 'Hola', $value['title'] );
     192        $this->assertSame( $other_menu_id, $value['nav_menu_term_id'] );
    193193    }
    194194
     
    221221
    222222        $value = $setting->value();
    223         $this->assertEquals( '', $value['title'] );
    224         $this->assertEquals( $original_title, $value['original_title'] );
     223        $this->assertSame( '', $value['title'] );
     224        $this->assertSame( $original_title, $value['original_title'] );
    225225    }
    226226
     
    251251        $post      = get_post( $item_id );
    252252        $menu_item = wp_setup_nav_menu_item( $post );
    253         $this->assertEquals( $item_title, $menu_item->title );
     253        $this->assertSame( $item_title, $menu_item->title );
    254254
    255255        $setting_id = "nav_menu_item[$item_id]";
     
    257257
    258258        $value = $setting->value();
    259         $this->assertEquals( $menu_item->title, $value['title'] );
    260         $this->assertEquals( $menu_item->type, $value['type'] );
     259        $this->assertSame( $menu_item->title, $value['title'] );
     260        $this->assertSame( $menu_item->type, $value['type'] );
    261261        $this->assertEquals( $menu_item->object_id, $value['object_id'] );
    262         $this->assertEquals( $menu_id, $value['nav_menu_term_id'] );
    263         $this->assertEquals( 'Salutations', $value['original_title'] );
     262        $this->assertSame( $menu_id, $value['nav_menu_term_id'] );
     263        $this->assertSame( 'Salutations', $value['original_title'] );
    264264    }
    265265
     
    292292
    293293        $value = $setting->value();
    294         $this->assertEquals( $menu_item->type_label, 'Custom Label' );
    295         $this->assertEquals( $menu_item->type_label, $value['type_label'] );
     294        $this->assertSame( $menu_item->type_label, 'Custom Label' );
     295        $this->assertSame( $menu_item->type_label, $value['type_label'] );
    296296    }
    297297
     
    317317        $menu->preview();
    318318        $value = $menu->value();
    319         $this->assertEquals( $post_value, $value );
     319        $this->assertSame( $post_value, $value );
    320320
    321321        $post_id = self::factory()->post->create( array( 'post_title' => 'Hello World' ) );
     
    338338        $setting    = new WP_Customize_Nav_Menu_Item_Setting( $this->wp_customize, $setting_id );
    339339        $value      = $setting->value();
    340         $this->assertEquals( 0, $value['nav_menu_term_id'] );
     340        $this->assertSame( 0, $value['nav_menu_term_id'] );
    341341    }
    342342
     
    392392        unset( $post_value['status'] );
    393393        foreach ( $post_value as $key => $value ) {
    394             $this->assertEquals( $value, $updated_item->$key, "Key $key mismatch" );
     394            $this->assertSame( $value, $updated_item->$key, "Key $key mismatch" );
    395395        }
    396396    }
     
    445445
    446446        $last_item = array_pop( $preview_items );
    447         $this->assertEquals( $new_item_id, $last_item->db_id );
     447        $this->assertSame( $new_item_id, $last_item->db_id );
    448448        $post_value['post_status'] = $post_value['status'];
    449449        unset( $post_value['status'] );
     
    451451        unset( $post_value['position'] );
    452452        foreach ( $post_value as $key => $value ) {
    453             $this->assertEquals( $value, $last_item->$key, "Mismatch for $key property." );
     453            $this->assertSame( $value, $last_item->$key, "Mismatch for $key property." );
    454454        }
    455455    }
     
    533533            $url_setting = $setting->sanitize( array( 'url' => $valid_url ) );
    534534            $this->assertInternalType( 'array', $url_setting );
    535             $this->assertEquals( $valid_url, $url_setting['url'] );
     535            $this->assertSame( $valid_url, $url_setting['url'] );
    536536        }
    537537
     
    544544            $url_setting = $setting->sanitize( array( 'url' => $invalid_url ) );
    545545            $this->assertInstanceOf( 'WP_Error', $url_setting );
    546             $this->assertEquals( 'invalid_url', $url_setting->get_error_code() );
     546            $this->assertSame( 'invalid_url', $url_setting->get_error_code() );
    547547        }
    548548
     
    588588
    589589        foreach ( $expected_sanitized as $key => $value ) {
    590             $this->assertEquals( $value, $sanitized[ $key ], "Expected $key to be sanitized." );
     590            $this->assertSame( $value, $sanitized[ $key ], "Expected $key to be sanitized." );
    591591        }
    592592
     
    617617
    618618        $this->assertEquals( $expected_sanitized['object_id'], $nav_menu_item->object_id );
    619         $this->assertEquals( $expected_sanitized['object'], $nav_menu_item->object );
     619        $this->assertSame( $expected_sanitized['object'], $nav_menu_item->object );
    620620        $this->assertEquals( $expected_sanitized['menu_item_parent'], $nav_menu_item->menu_item_parent );
    621         $this->assertEquals( $expected_sanitized['position'], $post->menu_order );
    622         $this->assertEquals( $expected_sanitized['type'], $nav_menu_item->type );
    623         $this->assertEquals( $expected_sanitized['title'], $post->post_title );
    624         $this->assertEquals( $expected_sanitized['url'], $nav_menu_item->url );
    625         $this->assertEquals( $expected_sanitized['description'], $post->post_content );
    626         $this->assertEquals( $expected_sanitized['attr_title'], $post->post_excerpt );
    627         $this->assertEquals( $expected_sanitized['target'], $nav_menu_item->target );
    628         $this->assertEquals( $expected_sanitized['classes'], implode( ' ', $nav_menu_item->classes ) );
    629         $this->assertEquals( $expected_sanitized['xfn'], $nav_menu_item->xfn );
    630         $this->assertEquals( $expected_sanitized['status'], $post->post_status );
     621        $this->assertSame( $expected_sanitized['position'], $post->menu_order );
     622        $this->assertSame( $expected_sanitized['type'], $nav_menu_item->type );
     623        $this->assertSame( $expected_sanitized['title'], $post->post_title );
     624        $this->assertSame( $expected_sanitized['url'], $nav_menu_item->url );
     625        $this->assertSame( $expected_sanitized['description'], $post->post_content );
     626        $this->assertSame( $expected_sanitized['attr_title'], $post->post_excerpt );
     627        $this->assertSame( $expected_sanitized['target'], $nav_menu_item->target );
     628        $this->assertSame( $expected_sanitized['classes'], implode( ' ', $nav_menu_item->classes ) );
     629        $this->assertSame( $expected_sanitized['xfn'], $nav_menu_item->xfn );
     630        $this->assertSame( $expected_sanitized['status'], $post->post_status );
    631631    }
    632632
     
    694694        $this->assertArrayHasKey( 'status', $update_result );
    695695
    696         $this->assertEquals( $item_id, $update_result['post_id'] );
     696        $this->assertSame( $item_id, $update_result['post_id'] );
    697697        $this->assertNull( $update_result['previous_post_id'] );
    698698        $this->assertNull( $update_result['error'] );
    699         $this->assertEquals( 'updated', $update_result['status'] );
     699        $this->assertSame( 'updated', $update_result['status'] );
    700700    }
    701701
     
    749749
    750750        $last_item = array_pop( $preview_items );
    751         $this->assertEquals( $setting->post_id, $last_item->db_id );
     751        $this->assertSame( $setting->post_id, $last_item->db_id );
    752752        $post_value['post_status'] = $post_value['status'];
    753753        unset( $post_value['status'] );
     
    767767        $this->assertArrayHasKey( 'status', $update_result );
    768768
    769         $this->assertEquals( $setting->post_id, $update_result['post_id'] );
    770         $this->assertEquals( $new_item_id, $update_result['previous_post_id'] );
     769        $this->assertSame( $setting->post_id, $update_result['post_id'] );
     770        $this->assertSame( $new_item_id, $update_result['previous_post_id'] );
    771771        $this->assertNull( $update_result['error'] );
    772         $this->assertEquals( 'inserted', $update_result['status'] );
     772        $this->assertSame( 'inserted', $update_result['status'] );
    773773    }
    774774
     
    821821        $this->assertArrayHasKey( 'status', $update_result );
    822822
    823         $this->assertEquals( $delete_item_id, $update_result['post_id'] );
     823        $this->assertSame( $delete_item_id, $update_result['post_id'] );
    824824        $this->assertNull( $update_result['previous_post_id'] );
    825825        $this->assertNull( $update_result['error'] );
    826         $this->assertEquals( 'deleted', $update_result['status'] );
     826        $this->assertSame( 'deleted', $update_result['status'] );
    827827    }
    828828
     
    919919        $this->assertArrayHasKey( 'type_label', $item_value );
    920920        $nav_menu_item = $setting->value_as_wp_post_nav_menu_item();
    921         $this->assertEquals( 'Custom Link', $nav_menu_item->type_label );
    922         $this->assertEquals( $item_value['type_label'], $nav_menu_item->type_label );
     921        $this->assertSame( 'Custom Link', $nav_menu_item->type_label );
     922        $this->assertSame( $item_value['type_label'], $nav_menu_item->type_label );
    923923        add_filter( 'wp_setup_nav_menu_item', array( $this, 'filter_type_label' ) );
    924924        $nav_menu_item = $setting->value_as_wp_post_nav_menu_item();
    925         $this->assertEquals( 'Custom Label', $nav_menu_item->type_label );
     925        $this->assertSame( 'Custom Label', $nav_menu_item->type_label );
    926926
    927927        $this->assertObjectNotHasAttribute( 'nav_menu_term_id', $nav_menu_item );
    928928        $this->assertObjectNotHasAttribute( 'status', $nav_menu_item );
    929         $this->assertEquals( 'publish', $nav_menu_item->post_status );
    930         $this->assertEquals( 'nav_menu_item', $nav_menu_item->post_type );
     929        $this->assertSame( 'publish', $nav_menu_item->post_status );
     930        $this->assertSame( 'nav_menu_item', $nav_menu_item->post_type );
    931931        $this->assertObjectNotHasAttribute( 'position', $nav_menu_item );
    932         $this->assertEquals( $post_value['position'], $nav_menu_item->menu_order );
    933         $this->assertEquals( $post_value['title'], $nav_menu_item->post_title );
    934         $this->assertEquals( 123, $nav_menu_item->ID );
    935         $this->assertEquals( 123, $nav_menu_item->db_id );
    936         $this->assertEquals( wp_get_current_user()->ID, $nav_menu_item->post_author );
     932        $this->assertSame( $post_value['position'], $nav_menu_item->menu_order );
     933        $this->assertSame( $post_value['title'], $nav_menu_item->post_title );
     934        $this->assertSame( 123, $nav_menu_item->ID );
     935        $this->assertSame( 123, $nav_menu_item->db_id );
     936        $this->assertSame( wp_get_current_user()->ID, $nav_menu_item->post_author );
    937937        $this->assertObjectHasAttribute( 'type_label', $nav_menu_item );
    938938        $expected = apply_filters( 'nav_menu_attr_title', wp_unslash( apply_filters( 'excerpt_save_pre', wp_slash( $post_value['attr_title'] ) ) ) );
    939         $this->assertEquals( $expected, $nav_menu_item->attr_title );
    940         $this->assertEquals( 'Attempted \o/ o’o markup', $nav_menu_item->description );
     939        $this->assertSame( $expected, $nav_menu_item->attr_title );
     940        $this->assertSame( 'Attempted \o/ o’o markup', $nav_menu_item->description );
    941941    }
    942942
     
    974974        $setting->preview();
    975975        $nav_menu_item = $setting->value_as_wp_post_nav_menu_item();
    976         $this->assertEquals( get_term_link( $term_id ), $nav_menu_item->url );
     976        $this->assertSame( get_term_link( $term_id ), $nav_menu_item->url );
    977977
    978978        // Post.
     
    993993        $setting->preview();
    994994        $nav_menu_item = $setting->value_as_wp_post_nav_menu_item();
    995         $this->assertEquals( get_permalink( $post_id ), $nav_menu_item->url );
     995        $this->assertSame( get_permalink( $post_id ), $nav_menu_item->url );
    996996
    997997        // Post type archive.
     
    10111011        $setting->preview();
    10121012        $nav_menu_item = $setting->value_as_wp_post_nav_menu_item();
    1013         $this->assertEquals( get_post_type_archive_link( 'press_release' ), $nav_menu_item->url );
     1013        $this->assertSame( get_post_type_archive_link( 'press_release' ), $nav_menu_item->url );
    10141014    }
    10151015
     
    10651065        );
    10661066        $item_value       = $setting->value();
    1067         $this->assertEquals( $original_post_title, $item_value['original_title'] );
    1068         $this->assertEquals( '', $item_value['title'] );
     1067        $this->assertSame( $original_post_title, $item_value['original_title'] );
     1068        $this->assertSame( '', $item_value['title'] );
    10691069        $item = $setting->value_as_wp_post_nav_menu_item();
    10701070        $this->assertObjectHasAttribute( 'type_label', $item );
    1071         $this->assertEquals( $original_post_title, $item->original_title );
    1072         $this->assertEquals( $original_post_title, $item->title );
     1071        $this->assertSame( $original_post_title, $item->original_title );
     1072        $this->assertSame( $original_post_title, $item->title );
    10731073        $this->assertArrayHasKey( 'type_label', $item_value );
    1074         $this->assertEquals( get_post_type_object( 'press_release' )->labels->singular_name, $item_value['type_label'] );
    1075         $this->assertEquals( $item->type_label, $item_value['type_label'] );
     1074        $this->assertSame( get_post_type_object( 'press_release' )->labels->singular_name, $item_value['type_label'] );
     1075        $this->assertSame( $item->type_label, $item_value['type_label'] );
    10761076
    10771077        // Post: staged nav menu item.
     
    10921092        $setting->preview();
    10931093        $item_value = $setting->value();
    1094         $this->assertEquals( $original_post_title, $item_value['original_title'] );
    1095         $this->assertEquals( '', $item_value['title'] );
     1094        $this->assertSame( $original_post_title, $item_value['original_title'] );
     1095        $this->assertSame( '', $item_value['title'] );
    10961096        $item = $setting->value_as_wp_post_nav_menu_item();
    10971097        $this->assertObjectHasAttribute( 'type_label', $item );
    1098         $this->assertEquals( $original_post_title, $item->original_title );
    1099         $this->assertEquals( $original_post_title, $item->title );
     1098        $this->assertSame( $original_post_title, $item->original_title );
     1099        $this->assertSame( $original_post_title, $item->title );
    11001100        $this->assertArrayHasKey( 'type_label', $item_value );
    1101         $this->assertEquals( get_post_type_object( 'press_release' )->labels->singular_name, $item_value['type_label'] );
    1102         $this->assertEquals( $item->type_label, $item_value['type_label'] );
     1101        $this->assertSame( get_post_type_object( 'press_release' )->labels->singular_name, $item_value['type_label'] );
     1102        $this->assertSame( $item->type_label, $item_value['type_label'] );
    11031103
    11041104        // Term: existing nav menu item.
     
    11191119        );
    11201120        $item_value       = $setting->value();
    1121         $this->assertEquals( $original_term_title, $item_value['original_title'] );
    1122         $this->assertEquals( '', $item_value['title'] );
     1121        $this->assertSame( $original_term_title, $item_value['original_title'] );
     1122        $this->assertSame( '', $item_value['title'] );
    11231123        $item = $setting->value_as_wp_post_nav_menu_item();
    11241124        $this->assertObjectHasAttribute( 'type_label', $item );
    1125         $this->assertEquals( $original_term_title, $item->original_title );
    1126         $this->assertEquals( $original_term_title, $item->title );
     1125        $this->assertSame( $original_term_title, $item->original_title );
     1126        $this->assertSame( $original_term_title, $item->title );
    11271127        $this->assertArrayHasKey( 'type_label', $item_value );
    1128         $this->assertEquals( get_taxonomy( 'category' )->labels->singular_name, $item_value['type_label'] );
    1129         $this->assertEquals( $item->type_label, $item_value['type_label'] );
     1128        $this->assertSame( get_taxonomy( 'category' )->labels->singular_name, $item_value['type_label'] );
     1129        $this->assertSame( $item->type_label, $item_value['type_label'] );
    11301130
    11311131        // Term: staged nav menu item.
     
    11461146        $setting->preview();
    11471147        $item_value = $setting->value();
    1148         $this->assertEquals( $original_term_title, $item_value['original_title'] );
    1149         $this->assertEquals( '', $item_value['title'] );
     1148        $this->assertSame( $original_term_title, $item_value['original_title'] );
     1149        $this->assertSame( '', $item_value['title'] );
    11501150        $item = $setting->value_as_wp_post_nav_menu_item();
    11511151        $this->assertObjectHasAttribute( 'type_label', $item );
    1152         $this->assertEquals( $original_term_title, $item->original_title );
    1153         $this->assertEquals( $original_term_title, $item->title );
     1152        $this->assertSame( $original_term_title, $item->original_title );
     1153        $this->assertSame( $original_term_title, $item->title );
    11541154        $this->assertArrayHasKey( 'type_label', $item_value );
    1155         $this->assertEquals( get_taxonomy( 'category' )->labels->singular_name, $item_value['type_label'] );
    1156         $this->assertEquals( $item->type_label, $item_value['type_label'] );
     1155        $this->assertSame( get_taxonomy( 'category' )->labels->singular_name, $item_value['type_label'] );
     1156        $this->assertSame( $item->type_label, $item_value['type_label'] );
    11571157
    11581158        // Post Type Archive: existing nav menu item.
     
    11721172        );
    11731173        $item_value       = $setting->value();
    1174         $this->assertEquals( get_post_type_object( 'press_release' )->labels->archives, $item_value['original_title'] );
    1175         $this->assertEquals( '', $item_value['title'] );
     1174        $this->assertSame( get_post_type_object( 'press_release' )->labels->archives, $item_value['original_title'] );
     1175        $this->assertSame( '', $item_value['title'] );
    11761176        $item = $setting->value_as_wp_post_nav_menu_item();
    11771177        $this->assertObjectHasAttribute( 'type_label', $item );
    1178         $this->assertEquals( get_post_type_object( 'press_release' )->labels->archives, $item->original_title );
    1179         $this->assertEquals( get_post_type_object( 'press_release' )->labels->archives, $item->title );
     1178        $this->assertSame( get_post_type_object( 'press_release' )->labels->archives, $item->original_title );
     1179        $this->assertSame( get_post_type_object( 'press_release' )->labels->archives, $item->title );
    11801180        $this->assertArrayHasKey( 'type_label', $item_value );
    1181         $this->assertEquals( __( 'Post Type Archive' ), $item_value['type_label'] );
    1182         $this->assertEquals( $item->type_label, $item_value['type_label'] );
     1181        $this->assertSame( __( 'Post Type Archive' ), $item_value['type_label'] );
     1182        $this->assertSame( $item->type_label, $item_value['type_label'] );
    11831183
    11841184        // Post Type Archive: staged nav menu item.
     
    11981198        $setting->preview();
    11991199        $item_value = $setting->value();
    1200         $this->assertEquals( get_post_type_object( 'press_release' )->labels->archives, $item_value['original_title'] );
    1201         $this->assertEquals( '', $item_value['title'] );
     1200        $this->assertSame( get_post_type_object( 'press_release' )->labels->archives, $item_value['original_title'] );
     1201        $this->assertSame( '', $item_value['title'] );
    12021202        $item = $setting->value_as_wp_post_nav_menu_item();
    12031203        $this->assertObjectHasAttribute( 'type_label', $item );
    1204         $this->assertEquals( get_post_type_object( 'press_release' )->labels->archives, $item->original_title );
    1205         $this->assertEquals( get_post_type_object( 'press_release' )->labels->archives, $item->title );
     1204        $this->assertSame( get_post_type_object( 'press_release' )->labels->archives, $item->original_title );
     1205        $this->assertSame( get_post_type_object( 'press_release' )->labels->archives, $item->title );
    12061206        $this->assertArrayHasKey( 'type_label', $item_value );
    1207         $this->assertEquals( __( 'Post Type Archive' ), $item_value['type_label'] );
    1208         $this->assertEquals( $item->type_label, $item_value['type_label'] );
     1207        $this->assertSame( __( 'Post Type Archive' ), $item_value['type_label'] );
     1208        $this->assertSame( $item->type_label, $item_value['type_label'] );
    12091209    }
    12101210
     
    12391239
    12401240        $nav_menu_item = $setting->value_as_wp_post_nav_menu_item();
    1241         $this->assertEquals( $original_title, $nav_menu_item->title );
     1241        $this->assertSame( $original_title, $nav_menu_item->title );
    12421242    }
    12431243}
Note: See TracChangeset for help on using the changeset viewer.