You are here

public function MenuLinkFieldKernelTest::testMenuTreeSave in Menu Link (Field) 8

File

tests/src/Kernel/MenuLinkFieldKernelTest.php, line 70

Class

MenuLinkFieldKernelTest
Tests the menu link field.

Namespace

Drupal\Tests\menu_link\Kernel

Code

public function testMenuTreeSave() : void {
  $entity_test_mul = EntityTestMul::create([
    'type' => 'entity_test_mul',
    'name' => 'test',
    'field_menu_link' => [
      'menu_name' => 'test_menu',
      'title' => 'test title',
      'description' => 'test description',
    ],
  ]);
  $entity_test_mul
    ->save();

  /** @var \Drupal\Core\Menu\MenuLinkTree $menu_tree */
  $menu_tree = \Drupal::service('menu.link_tree');
  $parameters = new MenuTreeParameters();
  $parameters
    ->addCondition('title', 'test title');
  $result = $menu_tree
    ->load('test_menu', $parameters);
  self::assertCount(1, $result);
  $menu_link = reset($result);
  self::assertEquals(1, $menu_link->depth);
  self::assertFalse($menu_link->hasChildren);
  self::assertEquals('test title', $menu_link->link
    ->getTitle());
  self::assertEquals('', $menu_link->link
    ->getParent());
  self::assertEquals('test description', $menu_link->link
    ->getDescription());
  self::assertEquals('test_menu', $menu_link->link
    ->getMenuName());
  self::assertTrue($menu_link->link
    ->getUrlObject()
    ->isRouted());
  self::assertEquals('entity.entity_test_mul.canonical', $menu_link->link
    ->getUrlObject()
    ->getRouteName());
  self::assertEquals([
    'entity_test_mul' => $entity_test_mul
      ->id(),
  ], $menu_link->link
    ->getUrlObject()
    ->getRouteParameters());

  // Add another entity as a child of the first one.
  $entity_test_mul2 = EntityTestMul::create([
    'type' => 'entity_test_mul',
    'name' => 'test',
    'field_menu_link' => [
      'menu_name' => 'test_menu',
      'parent' => $menu_link->link
        ->getPluginId(),
      'title' => 'test title 2',
      'description' => 'test description 2',
    ],
  ]);
  $entity_test_mul2
    ->save();
  $parameters = new MenuTreeParameters();
  $parameters
    ->addCondition('title', 'test title 2');
  $result = $menu_tree
    ->load('test_menu', $parameters);
  self::assertCount(1, $result);
  $menu_link2 = reset($result);
  self::assertEquals(2, $menu_link2->depth);
  self::assertFalse($menu_link2->hasChildren);
  self::assertEquals('test title 2', $menu_link2->link
    ->getTitle());
  self::assertEquals($menu_link->link
    ->getPluginId(), $menu_link2->link
    ->getParent());
  self::assertEquals('test description 2', $menu_link2->link
    ->getDescription());
  self::assertEquals('test_menu', $menu_link2->link
    ->getMenuName());
  self::assertTrue($menu_link2->link
    ->getUrlObject()
    ->isRouted());
  self::assertEquals('entity.entity_test_mul.canonical', $menu_link2->link
    ->getUrlObject()
    ->getRouteName());
  self::assertEquals([
    'entity_test_mul' => $entity_test_mul2
      ->id(),
  ], $menu_link2->link
    ->getUrlObject()
    ->getRouteParameters());
}