You are here

public function MenuFirstchildTest::testBasicFunc in Menu Firstchild 2.x

Test Basic Functionality.

File

tests/src/Functional/MenuFirstchildTest.php, line 112

Class

MenuFirstchildTest
Test the Member Discounts api.

Namespace

Drupal\Tests\menu_firstchild\Functional

Code

public function testBasicFunc() {
  $session = $this
    ->assertSession();

  // Goto First node created.
  $first_url = Url::fromRoute('entity.node.canonical', [
    'node' => $this->nodes[0]
      ->id(),
  ])
    ->toString();
  $node_f_url = "/node/" . $this->nodes[0]
    ->id();
  $this
    ->drupalGet($node_f_url);
  $session
    ->statusCodeEquals(200, "Status 200");

  // Goto Second node created.
  $second_url = Url::fromRoute('entity.node.canonical', [
    'node' => $this->nodes[1]
      ->id(),
  ])
    ->toString();
  $node_s_url = "/node/" . $this->nodes[1]
    ->id();
  $this
    ->drupalGet($node_s_url);
  $session
    ->statusCodeEquals(200, "Status 200");

  // Login Admin User.
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->drupalPlaceBlock('local_actions_block', [
    'label' => 'Primary admin actions',
    'region' => 'content',
    'theme' => 'seven',
  ]);
  $this
    ->drupalGet("admin/structure/block/list/seven");

  // Add Menu Items.
  $this->menu = $this
    ->addCustomMenu();
  $menu_name = $this->menu
    ->id();

  // Test the 'Add link' local action.
  $this
    ->drupalGet(Url::fromRoute('entity.menu.edit_form', [
    'menu' => $menu_name,
  ]));

  // Build Parent Link.
  $this
    ->clickLink($this
    ->t('Add link'));
  $this
    ->drupalPostForm(NULL, [
    'link[0][uri]' => 'route:<none>',
    'title[0][value]' => "MFC Parent",
    'menu_firstchild_enabled' => TRUE,
    'expanded' => TRUE,
    'description[0][value]' => "MFC Parent",
    'weight[0][value]' => 10,
  ], $this
    ->t('Save'));
  $this
    ->assertUrl(Url::fromRoute('entity.menu.edit_form', [
    'menu' => $menu_name,
  ]));
  $menu_links = \Drupal::entityTypeManager()
    ->getStorage('menu_link_content')
    ->loadByProperties([
    'title' => "MFC Parent",
  ]);
  $parent_menu_link = reset($menu_links);
  $this
    ->assertInstanceOf(MenuLinkContent::class, $parent_menu_link);
  $this
    ->assertMenuLink([
    'menu_name' => $menu_name,
    'children' => [],
  ], $parent_menu_link
    ->getPluginId());

  // Build First Child link.
  $this
    ->clickLink($this
    ->t('Add link'));
  $this
    ->drupalPostForm(NULL, [
    'link[0][uri]' => '/node/' . $this->nodes[0]
      ->id(),
    'title[0][value]' => "First Child",
    'description[0][value]' => "First Child",
    'menu_parent' => $menu_name . ':' . $parent_menu_link
      ->getPluginId(),
    'weight[0][value]' => 0,
  ], $this
    ->t('Save'));
  $this
    ->assertUrl(Url::fromRoute('entity.menu.edit_form', [
    'menu' => $menu_name,
  ]));
  $menu_links = \Drupal::entityTypeManager()
    ->getStorage('menu_link_content')
    ->loadByProperties([
    'title' => "First Child",
  ]);
  $c1_menu_link = reset($menu_links);
  $this
    ->assertInstanceOf(MenuLinkContent::class, $c1_menu_link);
  $this
    ->assertMenuLink([
    'menu_name' => $menu_name,
    'parent' => $parent_menu_link
      ->getPluginId(),
  ], $c1_menu_link
    ->getPluginId());

  // Build Second Child link.
  $this
    ->clickLink($this
    ->t('Add link'));
  $this
    ->drupalPostForm(NULL, [
    'link[0][uri]' => '/node/' . $this->nodes[1]
      ->id(),
    'title[0][value]' => "Second Child",
    'description[0][value]' => "Second Child",
    'menu_parent' => $menu_name . ':' . $parent_menu_link
      ->getPluginId(),
    'weight[0][value]' => 10,
  ], $this
    ->t('Save'));
  $this
    ->assertUrl(Url::fromRoute('entity.menu.edit_form', [
    'menu' => $menu_name,
  ]));
  $menu_links = \Drupal::entityTypeManager()
    ->getStorage('menu_link_content')
    ->loadByProperties([
    'title' => "Second Child",
  ]);
  $c2_menu_link = reset($menu_links);
  $this
    ->assertInstanceOf(MenuLinkContent::class, $c2_menu_link);
  $this
    ->assertMenuLink([
    'menu_name' => $menu_name,
    'parent' => $parent_menu_link
      ->getPluginId(),
  ], $c2_menu_link
    ->getPluginId());

  // Go to Homepage.
  $this
    ->drupalGet('<front>');

  // Check for Parent links.
  $this
    ->assertLink($this
    ->t('MFC Parent'));
  $links = $this
    ->xpath('//a[contains(@href, :href)][@title = :label]', [
    ':href' => $first_url,
    ':label' => "MFC Parent",
  ]);
  $this
    ->assert(!empty($links), "MFC Parent Link Does not exist: " . $first_url . " :: " . count($links));

  // Check for Child 1 links /w hierarchy.
  $this
    ->assertLink($this
    ->t('First Child'));
  $links = $this
    ->xpath('//a[contains(@href, :href1)][@title = :label1]/following-sibling::ul[contains(@class, "menu")]/*/a[contains(@href, :href2)][@title = :label2]', [
    ':href1' => $first_url,
    ':label1' => "MFC Parent",
    ':href2' => $first_url,
    ':label2' => "First Child",
  ]);
  $this
    ->assert(!empty($links), "First Child Link Does not exist");

  // Check for Child 2 links /w hierarchy.
  $this
    ->assertLink($this
    ->t('Second Child'));
  $links = $this
    ->xpath('//a[contains(@href, :href1)][@title = :label1]/following-sibling::ul[contains(@class, "menu")]/*/a[contains(@href, :href2)][@title = :label2]', [
    ':href1' => $first_url,
    ':label1' => "MFC Parent",
    ':href2' => $second_url,
    ':label2' => "Second Child",
  ]);
  $this
    ->assert(!empty($links), "Second Child Link Does not exist");

  // Install test module, and clear cache.
  \Drupal::service('module_installer')
    ->install([
    'menu_firstchild_test',
  ]);
  \Drupal::service('cache.menu')
    ->invalidateAll();

  // Refresh Homepage.
  $this
    ->drupalGet('<front>');
  $links = $this
    ->xpath('//a[contains(@class, :class)][@title = :label]', [
    ':class' => "custom-class-to-test-for",
    ':label' => "MFC Parent",
  ]);
}