public function MenuLinksTest::testMenuLinkOnEntityDelete in Drupal 10
Same name and namespace in other branches
- 8 core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php \Drupal\Tests\menu_link_content\Kernel\MenuLinksTest::testMenuLinkOnEntityDelete()
- 9 core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php \Drupal\Tests\menu_link_content\Kernel\MenuLinksTest::testMenuLinkOnEntityDelete()
Tests that menu link pointing to entities get removed on entity remove.
File
- core/
modules/ menu_link_content/ tests/ src/ Kernel/ MenuLinksTest.php, line 163
Class
- MenuLinksTest
- Tests handling of menu links hierarchies.
Namespace
Drupal\Tests\menu_link_content\KernelCode
public function testMenuLinkOnEntityDelete() {
// Create user.
$user = User::create([
'name' => 'username',
]);
$user
->save();
// Create External test entity.
$external_entity = EntityTestExternal::create();
$external_entity
->save();
// Ensure an external entity can be deleted.
$external_entity
->delete();
// Create "canonical" menu link pointing to the user.
$menu_link_content = MenuLinkContent::create([
'title' => 'username profile',
'menu_name' => 'menu_test',
'link' => [
[
'uri' => 'entity:user/' . $user
->id(),
],
],
'bundle' => 'menu_test',
]);
$menu_link_content
->save();
// Create "collection" menu link pointing to the user listing page.
$menu_link_content_collection = MenuLinkContent::create([
'title' => 'users listing',
'menu_name' => 'menu_test',
'link' => [
[
'uri' => 'internal:/' . $user
->toUrl('collection')
->getInternalPath(),
],
],
'bundle' => 'menu_test',
]);
$menu_link_content_collection
->save();
// Check is menu links present in the menu.
$menu_tree_condition = (new MenuTreeParameters())
->addCondition('route_name', 'entity.user.canonical');
$this
->assertCount(1, \Drupal::menuTree()
->load('menu_test', $menu_tree_condition));
$menu_tree_condition_collection = (new MenuTreeParameters())
->addCondition('route_name', 'entity.user.collection');
$this
->assertCount(1, \Drupal::menuTree()
->load('menu_test', $menu_tree_condition_collection));
// Delete the user.
$user
->delete();
// The "canonical" menu item has to be deleted.
$this
->assertCount(0, \Drupal::menuTree()
->load('menu_test', $menu_tree_condition));
// The "collection" menu item should still present in the menu.
$this
->assertCount(1, \Drupal::menuTree()
->load('menu_test', $menu_tree_condition_collection));
}