mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-07-01 07:40:07 +00:00
Editor: add navigation fallback.
Creates a fallback menu for the Navigation block including an API endpoint to retrieve it. Props get_dave, spacedmonkey, kebbet, flixos90, mikeschroder, ramonopoly, audrasjb. Fixes 58557. git-svn-id: https://develop.svn.wordpress.org/trunk@56052 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
221
tests/phpunit/tests/editor/classic-to-block-menu-converter.php
Normal file
221
tests/phpunit/tests/editor/classic-to-block-menu-converter.php
Normal file
@@ -0,0 +1,221 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests WP_Classic_To_Block_Menu_Converter_Test
|
||||
*
|
||||
* @package WordPress
|
||||
*/
|
||||
|
||||
/**
|
||||
* Tests for the WP_Classic_To_Block_Menu_Converter_Test class.
|
||||
*/
|
||||
class WP_Classic_To_Block_Menu_Converter_Test extends WP_UnitTestCase {
|
||||
|
||||
/**
|
||||
* @ticket 58557
|
||||
* @covers WP_Classic_To_Block_Menu_Converter::get_fallback
|
||||
*/
|
||||
public function test_class_exists() {
|
||||
$this->assertTrue( class_exists( 'WP_Classic_To_Block_Menu_Converter' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 58557
|
||||
* @covers WP_Classic_To_Block_Menu_Converter::convert
|
||||
* @dataProvider provider_test_passing_non_menu_object_to_converter_returns_wp_error
|
||||
*/
|
||||
public function test_passing_non_menu_object_to_converter_returns_wp_error( $data ) {
|
||||
|
||||
$result = WP_Classic_To_Block_Menu_Converter::convert( $data );
|
||||
|
||||
$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->assertEquals( 'The menu provided is not a valid menu.', $result->get_error_message(), 'Error message should communicate invalidity of menu argument.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 58557
|
||||
* @covers WP_Classic_To_Block_Menu_Converter::convert
|
||||
*/
|
||||
public function provider_test_passing_non_menu_object_to_converter_returns_wp_error() {
|
||||
return array(
|
||||
array( 1 ),
|
||||
array( -1 ),
|
||||
array( '1' ),
|
||||
array( 'not a menu object' ),
|
||||
array( true ),
|
||||
array( false ),
|
||||
array( array() ),
|
||||
array( new stdClass() ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 58557
|
||||
* @covers WP_Classic_To_Block_Menu_Converter::convert
|
||||
*/
|
||||
public function test_can_convert_classic_menu_to_blocks() {
|
||||
|
||||
$menu_id = wp_create_nav_menu( 'Classic Menu' );
|
||||
|
||||
wp_update_nav_menu_item(
|
||||
$menu_id,
|
||||
0,
|
||||
array(
|
||||
'menu-item-title' => 'Classic Menu Item 1',
|
||||
'menu-item-url' => '/classic-menu-item-1',
|
||||
'menu-item-status' => 'publish',
|
||||
)
|
||||
);
|
||||
|
||||
$second_menu_item_id = wp_update_nav_menu_item(
|
||||
$menu_id,
|
||||
0,
|
||||
array(
|
||||
'menu-item-title' => 'Classic Menu Item 2',
|
||||
'menu-item-url' => '/classic-menu-item-2',
|
||||
'menu-item-status' => 'publish',
|
||||
)
|
||||
);
|
||||
|
||||
wp_update_nav_menu_item(
|
||||
$menu_id,
|
||||
0,
|
||||
array(
|
||||
'menu-item-title' => 'Nested Menu Item 1',
|
||||
'menu-item-url' => '/nested-menu-item-1',
|
||||
'menu-item-status' => 'publish',
|
||||
'menu-item-parent-id' => $second_menu_item_id,
|
||||
)
|
||||
);
|
||||
|
||||
$classic_nav_menu = wp_get_nav_menu_object( $menu_id );
|
||||
|
||||
$blocks = WP_Classic_To_Block_Menu_Converter::convert( $classic_nav_menu );
|
||||
|
||||
$this->assertNotEmpty( $blocks );
|
||||
|
||||
$parsed_blocks = parse_blocks( $blocks );
|
||||
|
||||
$first_block = $parsed_blocks[0];
|
||||
$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->assertEquals( '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.' );
|
||||
|
||||
// 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->assertEquals( '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->assertEquals( '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->assertEquals( '/nested-menu-item-1', $nested_block['attrs']['url'], 'Nested block URL should match.' );
|
||||
|
||||
wp_delete_nav_menu( $menu_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 58557
|
||||
* @covers WP_Classic_To_Block_Menu_Converter::convert
|
||||
*/
|
||||
public function test_does_not_convert_menu_items_with_non_publish_status() {
|
||||
|
||||
$menu_id = wp_create_nav_menu( 'Classic Menu' );
|
||||
|
||||
wp_update_nav_menu_item(
|
||||
$menu_id,
|
||||
0,
|
||||
array(
|
||||
'menu-item-title' => 'Classic Menu Item 1',
|
||||
'menu-item-url' => '/classic-menu-item-1',
|
||||
'menu-item-status' => 'publish',
|
||||
)
|
||||
);
|
||||
|
||||
wp_update_nav_menu_item(
|
||||
$menu_id,
|
||||
0,
|
||||
array(
|
||||
'menu-item-status' => 'draft',
|
||||
'menu-item-title' => 'Draft Menu Item',
|
||||
'menu-item-url' => '/draft-menu-item',
|
||||
)
|
||||
);
|
||||
|
||||
wp_update_nav_menu_item(
|
||||
$menu_id,
|
||||
0,
|
||||
array(
|
||||
'menu-item-status' => 'private',
|
||||
'menu-item-title' => 'Private Item',
|
||||
'menu-item-url' => '/private-menu-item',
|
||||
)
|
||||
);
|
||||
|
||||
wp_update_nav_menu_item(
|
||||
$menu_id,
|
||||
0,
|
||||
array(
|
||||
'menu-item-status' => 'pending',
|
||||
'menu-item-title' => 'Pending Menu Item',
|
||||
'menu-item-url' => '/pending-menu-item',
|
||||
)
|
||||
);
|
||||
|
||||
wp_update_nav_menu_item(
|
||||
$menu_id,
|
||||
0,
|
||||
array(
|
||||
'menu-item-status' => 'future',
|
||||
'menu-item-title' => 'Future Menu Item',
|
||||
'menu-item-url' => '/future-menu-item',
|
||||
)
|
||||
);
|
||||
|
||||
$classic_nav_menu = wp_get_nav_menu_object( $menu_id );
|
||||
|
||||
$blocks = WP_Classic_To_Block_Menu_Converter::convert( $classic_nav_menu );
|
||||
|
||||
$this->assertNotEmpty( $blocks );
|
||||
|
||||
$parsed_blocks = parse_blocks( $blocks );
|
||||
|
||||
$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->assertEquals( '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.' );
|
||||
|
||||
wp_delete_nav_menu( $menu_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 58557
|
||||
* @covers WP_Classic_To_Block_Menu_Converter::convert
|
||||
*/
|
||||
public function test_returns_empty_array_for_menus_with_no_items() {
|
||||
$menu_id = wp_create_nav_menu( 'Empty Menu' );
|
||||
|
||||
$classic_nav_menu = wp_get_nav_menu_object( $menu_id );
|
||||
|
||||
$blocks = WP_Classic_To_Block_Menu_Converter::convert( $classic_nav_menu );
|
||||
|
||||
$this->assertEmpty( $blocks, 'Result should be empty.' );
|
||||
|
||||
$this->assertIsArray( $blocks, 'Result should be empty array.' );
|
||||
|
||||
wp_delete_nav_menu( $menu_id );
|
||||
}
|
||||
}
|
||||
348
tests/phpunit/tests/editor/navigation-fallback.php
Normal file
348
tests/phpunit/tests/editor/navigation-fallback.php
Normal file
@@ -0,0 +1,348 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests WP_Navigation_Fallback
|
||||
*
|
||||
* @package WordPress
|
||||
*/
|
||||
|
||||
/**
|
||||
* Tests for the WP_Navigation_Fallback class.
|
||||
*/
|
||||
class WP_Navigation_Fallback_Test extends WP_UnitTestCase {
|
||||
|
||||
protected static $admin_user;
|
||||
protected static $editor_user;
|
||||
|
||||
public static function wpSetUpBeforeClass( $factory ) {
|
||||
self::$admin_user = $factory->user->create( array( 'role' => 'administrator' ) );
|
||||
|
||||
self::$editor_user = $factory->user->create( array( 'role' => 'editor' ) );
|
||||
}
|
||||
|
||||
public function set_up() {
|
||||
parent::set_up();
|
||||
|
||||
wp_set_current_user( self::$admin_user );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 58557
|
||||
* @covers WP_REST_Navigation_Fallback_Controller
|
||||
*/
|
||||
public function test_it_exists() {
|
||||
$this->assertTrue( class_exists( 'WP_Navigation_Fallback' ), 'WP_Navigation_Fallback class should exist.' );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @ticket 58557
|
||||
* @covers WP_REST_Navigation_Fallback_Controller::get_fallback
|
||||
*/
|
||||
public function test_should_return_a_default_fallback_navigation_menu_in_absence_of_other_fallbacks() {
|
||||
$data = WP_Navigation_Fallback::get_fallback();
|
||||
|
||||
$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->assertEquals( '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->assertEquals( '<!-- wp:page-list /-->', $data->post_content );
|
||||
|
||||
$navs_in_db = $this->get_navigations_in_database();
|
||||
|
||||
$this->assertCount( 1, $navs_in_db, 'The fallback Navigation post should be the only one in the database.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 58557
|
||||
* @covers WP_REST_Navigation_Fallback_Controller::get_fallback
|
||||
*/
|
||||
public function test_should_return_a_default_fallback_navigation_menu_with_no_blocks_if_page_list_block_is_not_registered() {
|
||||
|
||||
$original_page_list_block = WP_Block_Type_Registry::get_instance()->get_registered( 'core/page-list' );
|
||||
|
||||
unregister_block_type( 'core/page-list' );
|
||||
|
||||
$data = WP_Navigation_Fallback::get_fallback();
|
||||
|
||||
$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->assertEmpty( $data->post_content, 'Menu should be empty.' );
|
||||
|
||||
register_block_type( 'core/page-list', $original_page_list_block );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 58557
|
||||
* @covers WP_REST_Navigation_Fallback_Controller::get_fallback
|
||||
*/
|
||||
public function test_should_handle_consecutive_invocations() {
|
||||
// Invoke the method multiple times to ensure that it doesn't create a new fallback menu on each invocation.
|
||||
WP_Navigation_Fallback::get_fallback();
|
||||
WP_Navigation_Fallback::get_fallback();
|
||||
|
||||
// Assert on the final invocation.
|
||||
$data = WP_Navigation_Fallback::get_fallback();
|
||||
|
||||
$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' );
|
||||
|
||||
$navs_in_db = $this->get_navigations_in_database();
|
||||
|
||||
$this->assertCount( 1, $navs_in_db, 'The fallback Navigation post should be the only one in the database.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 58557
|
||||
* @covers WP_REST_Navigation_Fallback_Controller::get_fallback
|
||||
*/
|
||||
public function test_should_return_the_most_recently_created_navigation_menu() {
|
||||
|
||||
self::factory()->post->create_and_get(
|
||||
array(
|
||||
'post_type' => 'wp_navigation',
|
||||
'post_title' => 'Existing Navigation Menu 1',
|
||||
'post_content' => '<!-- wp:page-list /-->',
|
||||
)
|
||||
);
|
||||
|
||||
$most_recently_published_nav = self::factory()->post->create_and_get(
|
||||
array(
|
||||
'post_type' => 'wp_navigation',
|
||||
'post_title' => 'Existing Navigation Menu 2',
|
||||
'post_content' => '<!-- wp:navigation-link {"label":"Hello world","type":"post","id":1,"url":"/hello-world","kind":"post-type"} /-->',
|
||||
)
|
||||
);
|
||||
|
||||
$data = WP_Navigation_Fallback::get_fallback();
|
||||
|
||||
$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->assertEquals( $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.' );
|
||||
|
||||
// Check that no new Navigation menu was created.
|
||||
$navs_in_db = $this->get_navigations_in_database();
|
||||
|
||||
$this->assertCount( 2, $navs_in_db, 'Only the existing Navigation menus should be present in the database.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 58557
|
||||
* @covers WP_REST_Navigation_Fallback_Controller::get_fallback
|
||||
*/
|
||||
public function test_should_return_fallback_navigation_from_existing_classic_menu_if_no_navigation_menus_exist() {
|
||||
$menu_id = wp_create_nav_menu( 'Existing Classic Menu' );
|
||||
|
||||
wp_update_nav_menu_item(
|
||||
$menu_id,
|
||||
0,
|
||||
array(
|
||||
'menu-item-title' => 'Classic Menu Item 1',
|
||||
'menu-item-url' => '/classic-menu-item-1',
|
||||
'menu-item-status' => 'publish',
|
||||
)
|
||||
);
|
||||
|
||||
$data = WP_Navigation_Fallback::get_fallback();
|
||||
|
||||
$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.' );
|
||||
|
||||
// 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.' );
|
||||
|
||||
// Assert that fallback post_content contains the expected menu item title.
|
||||
$this->assertStringContainsString( '"label":"Classic Menu Item 1"', $data->post_content, 'The fallback Navigation Menu should contain menu item with a label matching the title of the menu item from the Classic Menu.' );
|
||||
|
||||
// Assert that fallback post_content contains the expected menu item url.
|
||||
$this->assertStringContainsString( '"url":"/classic-menu-item-1"', $data->post_content, 'The fallback Navigation Menu should contain menu item with a url matching the slug of the menu item from the Classic Menu.' );
|
||||
|
||||
// Check that only a single Navigation fallback was created.
|
||||
$navs_in_db = $this->get_navigations_in_database();
|
||||
$this->assertCount( 1, $navs_in_db, 'A single Navigation menu should be present in the database.' );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 58557
|
||||
* @covers WP_REST_Navigation_Fallback_Controller::get_fallback
|
||||
*/
|
||||
public function test_should_prioritise_fallback_to_classic_menu_in_primary_location() {
|
||||
$pl_menu_id = wp_create_nav_menu( 'Classic Menu in Primary Location' );
|
||||
|
||||
wp_update_nav_menu_item(
|
||||
$pl_menu_id,
|
||||
0,
|
||||
array(
|
||||
'menu-item-title' => 'PL Classic Menu Item',
|
||||
'menu-item-url' => '/pl-classic-menu-item',
|
||||
'menu-item-status' => 'publish',
|
||||
)
|
||||
);
|
||||
|
||||
$another_menu_id = wp_create_nav_menu( 'Another Classic Menu' );
|
||||
|
||||
wp_update_nav_menu_item(
|
||||
$another_menu_id,
|
||||
0,
|
||||
array(
|
||||
'menu-item-title' => 'Another Classic Menu Item',
|
||||
'menu-item-url' => '/another-classic-menu-item',
|
||||
'menu-item-status' => 'publish',
|
||||
)
|
||||
);
|
||||
|
||||
$locations = get_nav_menu_locations();
|
||||
$locations['primary'] = $pl_menu_id;
|
||||
$locations['header'] = $another_menu_id;
|
||||
set_theme_mod( 'nav_menu_locations', $locations );
|
||||
|
||||
$data = WP_Navigation_Fallback::get_fallback();
|
||||
|
||||
$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.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 58557
|
||||
* @covers WP_REST_Navigation_Fallback_Controller::get_fallback
|
||||
*/
|
||||
public function test_should_fallback_to_classic_menu_with_primary_slug() {
|
||||
|
||||
// Creates a classic menu with the slug "primary".
|
||||
$primary_menu_id = wp_create_nav_menu( 'Primary' );
|
||||
|
||||
wp_update_nav_menu_item(
|
||||
$primary_menu_id,
|
||||
0,
|
||||
array(
|
||||
'menu-item-title' => 'Classic Menu Item',
|
||||
'menu-item-url' => '/classic-menu-item',
|
||||
'menu-item-status' => 'publish',
|
||||
)
|
||||
);
|
||||
|
||||
$another_menu_id = wp_create_nav_menu( 'Another Classic Menu' );
|
||||
|
||||
wp_update_nav_menu_item(
|
||||
$another_menu_id,
|
||||
0,
|
||||
array(
|
||||
'menu-item-title' => 'Another Classic Menu Item',
|
||||
'menu-item-url' => '/another-classic-menu-item',
|
||||
'menu-item-status' => 'publish',
|
||||
)
|
||||
);
|
||||
|
||||
$data = WP_Navigation_Fallback::get_fallback();
|
||||
|
||||
$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".' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 58557
|
||||
* @covers WP_REST_Navigation_Fallback_Controller::get_fallback
|
||||
*/
|
||||
public function test_should_fallback_to_most_recently_created_classic_menu() {
|
||||
|
||||
// Creates a classic menu with the slug "primary".
|
||||
$primary_menu_id = wp_create_nav_menu( 'Older Classic Menu' );
|
||||
|
||||
wp_update_nav_menu_item(
|
||||
$primary_menu_id,
|
||||
0,
|
||||
array(
|
||||
'menu-item-title' => 'Classic Menu Item',
|
||||
'menu-item-url' => '/classic-menu-item',
|
||||
'menu-item-status' => 'publish',
|
||||
)
|
||||
);
|
||||
|
||||
$most_recent_menu_id = wp_create_nav_menu( 'Most Recent Classic Menu' );
|
||||
|
||||
wp_update_nav_menu_item(
|
||||
$most_recent_menu_id,
|
||||
0,
|
||||
array(
|
||||
'menu-item-title' => 'Another Classic Menu Item',
|
||||
'menu-item-url' => '/another-classic-menu-item',
|
||||
'menu-item-status' => 'publish',
|
||||
)
|
||||
);
|
||||
|
||||
$data = WP_Navigation_Fallback::get_fallback();
|
||||
|
||||
$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.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 58557
|
||||
* @covers WP_REST_Navigation_Fallback_Controller::get_fallback
|
||||
*/
|
||||
public function test_should_not_create_fallback_from_classic_menu_if_a_navigation_menu_already_exists() {
|
||||
$menu_id = wp_create_nav_menu( 'Existing Classic Menu' );
|
||||
|
||||
wp_update_nav_menu_item(
|
||||
$menu_id,
|
||||
0,
|
||||
array(
|
||||
'menu-item-title' => 'Classic Menu Item 1',
|
||||
'menu-item-url' => '/classic-menu-item-1',
|
||||
'menu-item-status' => 'publish',
|
||||
)
|
||||
);
|
||||
|
||||
$existing_navigation_menu = self::factory()->post->create_and_get(
|
||||
array(
|
||||
'post_type' => 'wp_navigation',
|
||||
'post_title' => 'Existing Navigation Menu 1',
|
||||
'post_content' => '<!-- wp:page-list /-->',
|
||||
)
|
||||
);
|
||||
|
||||
$data = WP_Navigation_Fallback::get_fallback();
|
||||
|
||||
$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->assertNotEquals( '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();
|
||||
|
||||
$this->assertCount( 1, $navs_in_db, 'Only the existing Navigation menus should be present in the database.' );
|
||||
|
||||
}
|
||||
|
||||
private function get_navigations_in_database() {
|
||||
$navs_in_db = new WP_Query(
|
||||
array(
|
||||
'post_type' => 'wp_navigation',
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => -1,
|
||||
'orderby' => 'date',
|
||||
'order' => 'DESC',
|
||||
)
|
||||
);
|
||||
|
||||
return $navs_in_db->posts ? $navs_in_db->posts : array();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
<?php
|
||||
/**
|
||||
* Unit tests covering WP_REST_Navigation_Fallback_Controller functionality.
|
||||
*
|
||||
* Note: that these tests are designed to provide high level coverage only. The majority of the tests
|
||||
* are made directly against the WP_Navigation_Fallback class as this:
|
||||
*
|
||||
* - is where the bulk of the logic is.
|
||||
* - is also consumed by the Navigation block's server side rendering.
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage REST API
|
||||
*
|
||||
* @covers WP_REST_Navigation_Fallback_Controller
|
||||
*/
|
||||
|
||||
/**
|
||||
* @group restapi
|
||||
* @group navigation
|
||||
*/
|
||||
class WP_REST_Navigation_Fallback_Controller_Test extends WP_Test_REST_Controller_Testcase {
|
||||
|
||||
protected static $admin_user;
|
||||
protected static $editor_user;
|
||||
|
||||
public static function wpSetUpBeforeClass( $factory ) {
|
||||
self::$admin_user = $factory->user->create( array( 'role' => 'administrator' ) );
|
||||
|
||||
self::$editor_user = $factory->user->create( array( 'role' => 'editor' ) );
|
||||
}
|
||||
|
||||
public function set_up() {
|
||||
parent::set_up();
|
||||
|
||||
wp_set_current_user( self::$admin_user );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 58557
|
||||
* @covers WP_REST_Navigation_Fallback_Controller::register_routes
|
||||
*
|
||||
* @since 6.3.0 Added Navigation Fallbacks endpoint.
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = rest_get_server()->get_routes();
|
||||
|
||||
$this->assertArrayHasKey( '/wp-block-editor/v1/navigation-fallback', $routes, 'Fallback route should be registered.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 58557
|
||||
* @covers WP_REST_Navigation_Fallback_Controller
|
||||
*
|
||||
* @since 6.3.0 Added Navigation Fallbacks endpoint.
|
||||
*/
|
||||
public function test_should_not_return_menus_for_users_without_permissions() {
|
||||
|
||||
wp_set_current_user( self::$editor_user );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wp-block-editor/v1/navigation-fallback' );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 403, $response->get_status(), 'Response should indicate user does not have permission.' );
|
||||
|
||||
$this->assertEquals( 'rest_cannot_create', $data['code'], 'Response should indicate user cannot create.' );
|
||||
|
||||
$this->assertEquals( 'Sorry, you are not allowed to create Navigation Menus as this user.', $data['message'], 'Response should indicate failed request status.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 58557
|
||||
* @covers WP_REST_Navigation_Fallback_Controller
|
||||
*
|
||||
* @since 6.3.0 Added Navigation Fallbacks endpoint.
|
||||
*/
|
||||
public function test_get_item() {
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wp-block-editor/v1/navigation-fallback' );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status(), 'Status should indicate successful request.' );
|
||||
|
||||
$this->assertIsArray( $data, 'Response should be of correct type.' );
|
||||
|
||||
$this->assertArrayHasKey( 'id', $data, 'Response should contain expected fields.' );
|
||||
|
||||
$this->assertEquals( 'wp_navigation', get_post_type( $data['id'] ), '"id" field should represent a post of type "wp_navigation"' );
|
||||
|
||||
// Check that only a single Navigation fallback was created.
|
||||
$navs_in_db = $this->get_navigations_in_database();
|
||||
|
||||
$this->assertCount( 1, $navs_in_db, 'Only a single Navigation menu should be present in the database.' );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 58557
|
||||
* @covers WP_REST_Navigation_Fallback_Controller
|
||||
*
|
||||
* @since 6.3.0 Added Navigation Fallbacks endpoint.
|
||||
*/
|
||||
public function test_get_item_schema() {
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wp-block-editor/v1/navigation-fallback' );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status(), 'Status should indicate successful request.' );
|
||||
|
||||
$this->assertArrayHasKey( 'schema', $data, '"schema" key should exist in response.' );
|
||||
|
||||
$schema = $data['schema'];
|
||||
|
||||
$this->assertEquals( 'object', $schema['type'], 'The schema type should match the expected type.' );
|
||||
|
||||
$this->assertArrayHasKey( 'id', $schema['properties'], 'Schema should have an "id" property.' );
|
||||
$this->assertEquals( 'integer', $schema['properties']['id']['type'], 'Schema "id" property should be an integer.' );
|
||||
$this->assertTrue( $schema['properties']['id']['readonly'], 'Schema "id" property should be readonly.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 58557
|
||||
* @covers WP_REST_Navigation_Fallback_Controller
|
||||
*
|
||||
* @since 6.3.0 Added Navigation Fallbacks endpoint.
|
||||
*/
|
||||
public function test_adds_links() {
|
||||
$request = new WP_REST_Request( 'GET', '/wp-block-editor/v1/navigation-fallback' );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$navigation_post_id = $data['id'];
|
||||
|
||||
$links = $response->get_links();
|
||||
|
||||
$this->assertNotEmpty( $links, 'Response should contain links.' );
|
||||
|
||||
$this->assertArrayHasKey( 'self', $links, 'Response should contain a "self" link.' );
|
||||
|
||||
$this->assertStringContainsString( 'wp/v2/navigation/' . $navigation_post_id, $links['self'][0]['href'], 'Self link should reference the correct Navigation Menu post resource url.' );
|
||||
|
||||
$this->assertTrue( $links['self'][0]['attributes']['embeddable'], 'Self link should be embeddable.' );
|
||||
}
|
||||
|
||||
private function get_navigations_in_database() {
|
||||
$navs_in_db = new WP_Query(
|
||||
array(
|
||||
'post_type' => 'wp_navigation',
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => -1,
|
||||
'orderby' => 'date',
|
||||
'order' => 'DESC',
|
||||
)
|
||||
);
|
||||
|
||||
return $navs_in_db->posts ? $navs_in_db->posts : array();
|
||||
}
|
||||
|
||||
/**
|
||||
* @doesNotPerformAssertions
|
||||
*/
|
||||
public function test_prepare_item() {
|
||||
// Covered by the core test.
|
||||
}
|
||||
|
||||
/**
|
||||
* @doesNotPerformAssertions
|
||||
*/
|
||||
public function test_context_param() {
|
||||
// Covered by the core test.
|
||||
}
|
||||
|
||||
/**
|
||||
* @doesNotPerformAssertions
|
||||
*/
|
||||
public function test_get_items() {
|
||||
// Covered by the core test.
|
||||
}
|
||||
|
||||
/**
|
||||
* @doesNotPerformAssertions
|
||||
*/
|
||||
public function test_create_item() {
|
||||
// Controller does not implement create_item().
|
||||
}
|
||||
|
||||
/**
|
||||
* @doesNotPerformAssertions
|
||||
*/
|
||||
public function test_update_item() {
|
||||
// Controller does not implement update_item().
|
||||
}
|
||||
|
||||
/**
|
||||
* @doesNotPerformAssertions
|
||||
*/
|
||||
public function test_delete_item() {
|
||||
// Controller does not implement delete_item().
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user