Skip to content

Commit 1dfb754

Browse files
committed
Tests: Use assertSame() in WP_Customize_Nav_Menu_Item_Setting tests.
This ensures that not only the return values match the expected results, but also that their type is the same. Where a loose comparison is intentional, inline comments now explain why: post meta returns IDs as strings, and some objects are compared by value. Developed in: #13451 Props r1k0, mukesh27. See #64895. git-svn-id: https://develop.svn.wordpress.org/trunk@63604 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 01248ee commit 1dfb754

3 files changed

Lines changed: 27 additions & 2 deletions

File tree

‎tests/phpunit/tests/customize/nav-menu-item-setting.php‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public function test_value_type_post_type() {
190190
$value = $setting->value();
191191
$this->assertSame( $menu_item->title, $value['title'] );
192192
$this->assertSame( $menu_item->type, $value['type'] );
193-
$this->assertEquals( $menu_item->object_id, $value['object_id'] );
193+
$this->assertSame( (int) $menu_item->object_id, $value['object_id'] );
194194
$this->assertSame( $menu_id, $value['nav_menu_term_id'] );
195195
$this->assertSame( 'Hello World', $value['original_title'] );
196196

@@ -273,7 +273,7 @@ public function test_value_type_taxonomy() {
273273
$value = $setting->value();
274274
$this->assertSame( $menu_item->title, $value['title'] );
275275
$this->assertSame( $menu_item->type, $value['type'] );
276-
$this->assertEquals( $menu_item->object_id, $value['object_id'] );
276+
$this->assertSame( (int) $menu_item->object_id, $value['object_id'] );
277277
$this->assertSame( $menu_id, $value['nav_menu_term_id'] );
278278
$this->assertSame( 'Salutations', $value['original_title'] );
279279
}
@@ -632,8 +632,17 @@ public function test_sanitize() {
632632
$post = get_post( $nav_menu_item_id );
633633
$nav_menu_item = wp_setup_nav_menu_item( clone $post );
634634

635+
/*
636+
* Keep assertEquals() because sanitize() returns object_id as an integer
637+
* while wp_setup_nav_menu_item() retrieves it as a string from post meta.
638+
*/
635639
$this->assertEquals( $expected_sanitized['object_id'], $nav_menu_item->object_id );
636640
$this->assertSame( $expected_sanitized['object'], $nav_menu_item->object );
641+
642+
/*
643+
* Keep assertEquals() because sanitize() returns menu_item_parent as an integer,
644+
* while wp_setup_nav_menu_item() retrieves it as a string from post meta.
645+
*/
637646
$this->assertEquals( $expected_sanitized['menu_item_parent'], $nav_menu_item->menu_item_parent );
638647
$this->assertSame( $expected_sanitized['position'], $post->menu_order );
639648
$this->assertSame( $expected_sanitized['type'], $nav_menu_item->type );
@@ -697,6 +706,11 @@ public function test_save_updated() {
697706
$updated_item = $menu_items[ $i ];
698707
$post_value['post_status'] = $post_value['status'];
699708
unset( $post_value['status'] );
709+
710+
/*
711+
* Keep assertEquals() because object_id is an integer in $post_value
712+
* but is returned as a string from post meta by wp_setup_nav_menu_item().
713+
*/
700714
foreach ( $post_value as $key => $value ) {
701715
$this->assertEquals( $value, $updated_item->$key, "Key $key mismatch" );
702716
}
@@ -770,6 +784,11 @@ public function test_save_inserted() {
770784
unset( $post_value['status'] );
771785
$post_value['menu_order'] = $post_value['position'];
772786
unset( $post_value['position'] );
787+
788+
/*
789+
* Keep assertEquals() because object_id is an integer in $post_value
790+
* but is returned as a string from post meta by wp_setup_nav_menu_item().
791+
*/
773792
foreach ( $post_value as $key => $value ) {
774793
$this->assertEquals( $value, $last_item->$key, "Mismatch for $key property." );
775794
}

‎tests/phpunit/tests/customize/nav-menu-setting.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ public function test_preview_updated() {
232232
$this->assertSameSets( $value, wp_array_slice_assoc( $term, array_keys( $value ) ) );
233233

234234
$menu_object = wp_get_nav_menu_object( $menu_id );
235+
236+
// Keep assertEquals() because the objects are intentionally compared by value.
235237
$this->assertEquals( (object) $term, $menu_object );
236238
$this->assertSame( $post_value['name'], $menu_object->name );
237239

@@ -277,6 +279,8 @@ public function test_preview_inserted() {
277279
$this->assertSame( $menu_id, $term['term_taxonomy_id'] );
278280

279281
$menu_object = wp_get_nav_menu_object( $menu_id );
282+
283+
// Keep assertEquals() because the objects are intentionally compared by value.
280284
$this->assertEquals( (object) $term, $menu_object );
281285
$this->assertSame( $post_value['name'], $menu_object->name );
282286

‎tests/phpunit/tests/customize/widgets.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,8 @@ public function test_sanitize_widget_instance_raw_instance() {
515515
$this->assertSame( '', $sanitized_for_js['title'] );
516516
$this->assertTrue( $sanitized_for_js['is_widget_customizer_js_value'] );
517517
$this->assertArrayHasKey( 'instance_hash_key', $sanitized_for_js );
518+
519+
// Keep assertEquals() because the objects are intentionally compared by value.
518520
$this->assertEquals( (object) $block_instance, $sanitized_for_js['raw_instance'] );
519521

520522
$unsanitized_from_js = $this->manager->widgets->sanitize_widget_instance( $sanitized_for_js );

0 commit comments

Comments
 (0)