Skip to content

Commit e085371

Browse files
Tests: Use assertSame() in navigation fallback 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. Follow-up to [56052]. See #60705. git-svn-id: https://develop.svn.wordpress.org/trunk@58183 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 6662cb0 commit e085371

2 files changed

Lines changed: 29 additions & 29 deletions

File tree

tests/phpunit/tests/editor/classic-to-block-menu-converter.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ public function test_passing_non_menu_object_to_converter_returns_wp_error( $dat
3131

3232
$this->assertTrue( is_wp_error( $result ), 'Should be a WP_Error instance' );
3333

34-
$this->assertEquals( 'invalid_menu', $result->get_error_code(), 'Error code should indicate invalidity of menu argument.' );
34+
$this->assertSame( 'invalid_menu', $result->get_error_code(), 'Error code should indicate invalidity of menu argument.' );
3535

36-
$this->assertEquals( 'The menu provided is not a valid menu.', $result->get_error_message(), 'Error message should communicate invalidity of menu argument.' );
36+
$this->assertSame( 'The menu provided is not a valid menu.', $result->get_error_message(), 'Error message should communicate invalidity of menu argument.' );
3737
}
3838

3939
/**
@@ -104,24 +104,24 @@ public function test_can_convert_classic_menu_to_blocks() {
104104
$second_block = $parsed_blocks[1];
105105
$nested_block = $parsed_blocks[1]['innerBlocks'][0];
106106

107-
$this->assertEquals( 'core/navigation-link', $first_block['blockName'], 'First block name should be "core/navigation-link"' );
107+
$this->assertSame( 'core/navigation-link', $first_block['blockName'], 'First block name should be "core/navigation-link"' );
108108

109-
$this->assertEquals( 'Classic Menu Item 1', $first_block['attrs']['label'], 'First block label should match.' );
109+
$this->assertSame( 'Classic Menu Item 1', $first_block['attrs']['label'], 'First block label should match.' );
110110

111-
$this->assertEquals( '/classic-menu-item-1', $first_block['attrs']['url'], 'First block URL should match.' );
111+
$this->assertSame( '/classic-menu-item-1', $first_block['attrs']['url'], 'First block URL should match.' );
112112

113113
// Assert parent of nested menu item is a submenu block.
114-
$this->assertEquals( 'core/navigation-submenu', $second_block['blockName'], 'Second block name should be "core/navigation-submenu"' );
114+
$this->assertSame( 'core/navigation-submenu', $second_block['blockName'], 'Second block name should be "core/navigation-submenu"' );
115115

116-
$this->assertEquals( 'Classic Menu Item 2', $second_block['attrs']['label'], 'Second block label should match.' );
116+
$this->assertSame( 'Classic Menu Item 2', $second_block['attrs']['label'], 'Second block label should match.' );
117117

118-
$this->assertEquals( '/classic-menu-item-2', $second_block['attrs']['url'], 'Second block URL should match.' );
118+
$this->assertSame( '/classic-menu-item-2', $second_block['attrs']['url'], 'Second block URL should match.' );
119119

120-
$this->assertEquals( 'core/navigation-link', $nested_block['blockName'], 'Nested block name should be "core/navigation-link"' );
120+
$this->assertSame( 'core/navigation-link', $nested_block['blockName'], 'Nested block name should be "core/navigation-link"' );
121121

122-
$this->assertEquals( 'Nested Menu Item 1', $nested_block['attrs']['label'], 'Nested block label should match.' );
122+
$this->assertSame( 'Nested Menu Item 1', $nested_block['attrs']['label'], 'Nested block label should match.' );
123123

124-
$this->assertEquals( '/nested-menu-item-1', $nested_block['attrs']['url'], 'Nested block URL should match.' );
124+
$this->assertSame( '/nested-menu-item-1', $nested_block['attrs']['url'], 'Nested block URL should match.' );
125125

126126
wp_delete_nav_menu( $menu_id );
127127
}
@@ -194,11 +194,11 @@ public function test_does_not_convert_menu_items_with_non_publish_status() {
194194

195195
$this->assertCount( 1, $parsed_blocks, 'Should only be one block in the array.' );
196196

197-
$this->assertEquals( 'core/navigation-link', $parsed_blocks[0]['blockName'], 'First block name should be "core/navigation-link"' );
197+
$this->assertSame( 'core/navigation-link', $parsed_blocks[0]['blockName'], 'First block name should be "core/navigation-link"' );
198198

199-
$this->assertEquals( 'Classic Menu Item 1', $parsed_blocks[0]['attrs']['label'], 'First block label should match.' );
199+
$this->assertSame( 'Classic Menu Item 1', $parsed_blocks[0]['attrs']['label'], 'First block label should match.' );
200200

201-
$this->assertEquals( '/classic-menu-item-1', $parsed_blocks[0]['attrs']['url'], 'First block URL should match.' );
201+
$this->assertSame( '/classic-menu-item-1', $parsed_blocks[0]['attrs']['url'], 'First block URL should match.' );
202202

203203
wp_delete_nav_menu( $menu_id );
204204
}

tests/phpunit/tests/editor/navigation-fallback.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ public function test_should_return_a_default_fallback_navigation_menu_in_absence
4545

4646
$this->assertInstanceOf( 'WP_Post', $data, 'Response should be of the correct type.' );
4747

48-
$this->assertEquals( 'wp_navigation', $data->post_type, 'Fallback menu type should be `wp_navigation`' );
48+
$this->assertSame( 'wp_navigation', $data->post_type, 'Fallback menu type should be `wp_navigation`' );
4949

50-
$this->assertEquals( 'Navigation', $data->post_title, 'Fallback menu title should be the default fallback title' );
50+
$this->assertSame( 'Navigation', $data->post_title, 'Fallback menu title should be the default fallback title' );
5151

52-
$this->assertEquals( 'navigation', $data->post_name, 'Fallback menu slug (post_name) should be the default slug' );
52+
$this->assertSame( 'navigation', $data->post_name, 'Fallback menu slug (post_name) should be the default slug' );
5353

54-
$this->assertEquals( '<!-- wp:page-list /-->', $data->post_content );
54+
$this->assertSame( '<!-- wp:page-list /-->', $data->post_content );
5555

5656
$navs_in_db = $this->get_navigations_in_database();
5757

@@ -92,7 +92,7 @@ public function test_should_return_a_default_fallback_navigation_menu_with_no_bl
9292

9393
$this->assertInstanceOf( 'WP_Post', $data, 'Response should be of the correct type.' );
9494

95-
$this->assertNotEquals( '<!-- wp:page-list /-->', $data->post_content, 'Navigation Menu should not contain a Page List block.' );
95+
$this->assertNotSame( '<!-- wp:page-list /-->', $data->post_content, 'Navigation Menu should not contain a Page List block.' );
9696

9797
$this->assertEmpty( $data->post_content, 'Menu should be empty.' );
9898

@@ -113,7 +113,7 @@ public function test_should_handle_consecutive_invocations() {
113113

114114
$this->assertInstanceOf( 'WP_Post', $data, 'Response should be of the correct type.' );
115115

116-
$this->assertEquals( 'Navigation', $data->post_title, 'Fallback menu title should be the default title' );
116+
$this->assertSame( 'Navigation', $data->post_title, 'Fallback menu title should be the default title' );
117117

118118
$navs_in_db = $this->get_navigations_in_database();
119119

@@ -146,11 +146,11 @@ public function test_should_return_the_most_recently_created_navigation_menu() {
146146

147147
$this->assertInstanceOf( 'WP_Post', $data, 'Response should be of the correct type.' );
148148

149-
$this->assertEquals( $most_recently_published_nav->post_title, $data->post_title, 'Fallback menu title should be the same as the most recently created menu.' );
149+
$this->assertSame( $most_recently_published_nav->post_title, $data->post_title, 'Fallback menu title should be the same as the most recently created menu.' );
150150

151-
$this->assertEquals( $most_recently_published_nav->post_name, $data->post_name, 'Post name should be the same as the most recently created menu.' );
151+
$this->assertSame( $most_recently_published_nav->post_name, $data->post_name, 'Post name should be the same as the most recently created menu.' );
152152

153-
$this->assertEquals( $most_recently_published_nav->post_content, $data->post_content, 'Post content should be the same as the most recently created menu.' );
153+
$this->assertSame( $most_recently_published_nav->post_content, $data->post_content, 'Post content should be the same as the most recently created menu.' );
154154

155155
// Check that no new Navigation menu was created.
156156
$navs_in_db = $this->get_navigations_in_database();
@@ -179,7 +179,7 @@ public function test_should_return_fallback_navigation_from_existing_classic_men
179179

180180
$this->assertInstanceOf( 'WP_Post', $data, 'Response should be of the correct type.' );
181181

182-
$this->assertEquals( 'Existing Classic Menu', $data->post_title, 'Fallback menu title should be the same as the classic menu.' );
182+
$this->assertSame( 'Existing Classic Menu', $data->post_title, 'Fallback menu title should be the same as the classic menu.' );
183183

184184
// Assert that the fallback contains a navigation-link block.
185185
$this->assertStringContainsString( '<!-- wp:navigation-link', $data->post_content, 'The fallback Navigation Menu should contain a `core/navigation-link` block.' );
@@ -233,7 +233,7 @@ public function test_should_prioritise_fallback_to_classic_menu_in_primary_locat
233233

234234
$this->assertInstanceOf( 'WP_Post', $data, 'Response should be of the correct type.' );
235235

236-
$this->assertEquals( 'Classic Menu in Primary Location', $data->post_title, 'Fallback menu title should match the menu in the "primary" location.' );
236+
$this->assertSame( 'Classic Menu in Primary Location', $data->post_title, 'Fallback menu title should match the menu in the "primary" location.' );
237237
}
238238

239239
/**
@@ -271,7 +271,7 @@ public function test_should_fallback_to_classic_menu_with_primary_slug() {
271271

272272
$this->assertInstanceOf( 'WP_Post', $data, 'Response should be of the correct type.' );
273273

274-
$this->assertEquals( 'Primary', $data->post_title, 'Fallback menu title should match the menu with the slug "primary".' );
274+
$this->assertSame( 'Primary', $data->post_title, 'Fallback menu title should match the menu with the slug "primary".' );
275275
}
276276

277277
/**
@@ -309,7 +309,7 @@ public function test_should_fallback_to_most_recently_created_classic_menu() {
309309

310310
$this->assertInstanceOf( 'WP_Post', $data, 'Response should be of the correct type.' );
311311

312-
$this->assertEquals( 'Most Recent Classic Menu', $data->post_title, 'Fallback menu title should match the menu that was created most recently.' );
312+
$this->assertSame( 'Most Recent Classic Menu', $data->post_title, 'Fallback menu title should match the menu that was created most recently.' );
313313
}
314314

315315
/**
@@ -341,9 +341,9 @@ public function test_should_not_create_fallback_from_classic_menu_if_a_navigatio
341341

342342
$this->assertInstanceOf( 'WP_Post', $data, 'Response should be of the correct type.' );
343343

344-
$this->assertEquals( $existing_navigation_menu->post_title, $data->post_title, 'Fallback menu title should be the same as the existing Navigation menu.' );
344+
$this->assertSame( $existing_navigation_menu->post_title, $data->post_title, 'Fallback menu title should be the same as the existing Navigation menu.' );
345345

346-
$this->assertNotEquals( 'Existing Classic Menu', $data->post_title, 'Fallback menu title should not be the same as the Classic Menu.' );
346+
$this->assertNotSame( 'Existing Classic Menu', $data->post_title, 'Fallback menu title should not be the same as the Classic Menu.' );
347347

348348
// Check that only a single Navigation fallback was created.
349349
$navs_in_db = $this->get_navigations_in_database();

0 commit comments

Comments
 (0)