Skip to content

Commit

Permalink
Tests: Use assertSame() in navigation fallback tests.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
SergeyBiryukov committed May 22, 2024
1 parent 6662cb0 commit e085371
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
28 changes: 14 additions & 14 deletions tests/phpunit/tests/editor/classic-to-block-menu-converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public function test_passing_non_menu_object_to_converter_returns_wp_error( $dat

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

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

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

/**
Expand Down Expand Up @@ -104,24 +104,24 @@ public function test_can_convert_classic_menu_to_blocks() {
$second_block = $parsed_blocks[1];
$nested_block = $parsed_blocks[1]['innerBlocks'][0];

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

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

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

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

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

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

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

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

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

wp_delete_nav_menu( $menu_id );
}
Expand Down Expand Up @@ -194,11 +194,11 @@ public function test_does_not_convert_menu_items_with_non_publish_status() {

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

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

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

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

wp_delete_nav_menu( $menu_id );
}
Expand Down
30 changes: 15 additions & 15 deletions tests/phpunit/tests/editor/navigation-fallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ public function test_should_return_a_default_fallback_navigation_menu_in_absence

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

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

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

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

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

$navs_in_db = $this->get_navigations_in_database();

Expand Down Expand Up @@ -92,7 +92,7 @@ public function test_should_return_a_default_fallback_navigation_menu_with_no_bl

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

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

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

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

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

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

$navs_in_db = $this->get_navigations_in_database();

Expand Down Expand Up @@ -146,11 +146,11 @@ public function test_should_return_the_most_recently_created_navigation_menu() {

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

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

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

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

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

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

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

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

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

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

/**
Expand Down Expand Up @@ -271,7 +271,7 @@ public function test_should_fallback_to_classic_menu_with_primary_slug() {

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

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

/**
Expand Down Expand Up @@ -309,7 +309,7 @@ public function test_should_fallback_to_most_recently_created_classic_menu() {

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

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

/**
Expand Down Expand Up @@ -341,9 +341,9 @@ public function test_should_not_create_fallback_from_classic_menu_if_a_navigatio

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

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

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

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

0 comments on commit e085371

Please sign in to comment.