You are here

public function MenuLinkFormatterTest::testMenuLinkFormatters in Menu Link (Field) 8

Tests the menu_link and menu_link_breadcrumb field formatters.

File

tests/src/Kernel/MenuLinkFormatterTest.php, line 79

Class

MenuLinkFormatterTest
Tests the menu link field formatters.

Namespace

Drupal\Tests\menu_link\Kernel

Code

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

  /** @var \Drupal\Core\Menu\MenuLinkTree $menu_tree */
  $menu_tree = \Drupal::service('menu.link_tree');
  $parameters = new MenuTreeParameters();
  $parameters
    ->addCondition('title', 'test title 1');
  $entity_test_mul
    ->save();
  $result = $menu_tree
    ->load('test_menu', $parameters);
  $menu_link = reset($result);

  // 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();

  // Test that the menu_link formatter outputs plain text.
  $content = $this
    ->renderLink($entity_test_mul2, 'menu_link', [
    'link_to_target' => FALSE,
  ]);
  $this
    ->assertRaw('<div>test title 2</div>', $content);

  // Test that the menu_link formatter outputs a link.
  $content2 = $this
    ->renderLink($entity_test_mul2, 'menu_link', [
    'link_to_target' => TRUE,
  ]);
  $this
    ->assertRaw('<div><a href="/entity_test_mul/manage/2" title="test description 2" hreflang="en">test title 2</a></div>', $content2);

  // Test that the breadcrumb formatter outputs links.
  $content3 = $this
    ->renderLink($entity_test_mul2, 'menu_link_breadcrumb', [
    'link_to_target' => TRUE,
  ]);
  $this
    ->assertRaw('<a href="/entity_test_mul/manage/1">test title 1</a>', $content3);
  $this
    ->assertRaw('<a href="/entity_test_mul/manage/2">test title 2</a>', $content3);

  // Test that the breadcrumb formatter outputs plain text.
  $content4 = $this
    ->renderLink($entity_test_mul2, 'menu_link_breadcrumb', [
    'link_to_target' => FALSE,
  ]);
  $this
    ->assertRaw('test title 1', $content4);
  $this
    ->assertRaw('test title 2', $content4);
  $this
    ->assertNoRaw('<a href="/entity_test_mul/manage/1">test title 1</a>', $content3);
  $this
    ->assertNoRaw('<a href="/entity_test_mul/manage/2">test title 2</a>', $content3);

  // Test that we skip the last element when parents_only is enabled.
  $content5 = $this
    ->renderLink($entity_test_mul2, 'menu_link_breadcrumb', [
    'link_to_target' => FALSE,
    'parents_only' => TRUE,
  ]);
  $this
    ->assertRaw('test title 1', $content5);
  $this
    ->assertNoRaw('test title 2', $content5);
}