You are here

MenuBlockTitleTest.php in Menu block title 8

File

tests/src/Functional/MenuBlockTitleTest.php
View source
<?php

namespace Drupal\Tests\menu_block_title\Functional;

use Drupal\Tests\BrowserTestBase;

/**
 * Class SettingsPageTest.
 *
 * @package Drupal\Tests\menu_block_title\Functional
 *
 * @group menu_block_title
 */
class MenuBlockTitleTest extends BrowserTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  protected static $modules = [
    'menu_block_title_test',
  ];

  /**
   * {@inheritdoc}
   */
  protected $profile = 'minimal';

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * Permissions for user that will be logged-in for test.
   *
   * @var array
   */
  protected static $userPermissions = [
    'access content',
  ];

  /**
   * {@inheritdoc}
   *
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  protected function setUp() : void {
    parent::setUp();
    $account = $this
      ->drupalCreateUser(static::$userPermissions);
    $this
      ->drupalLogin($account);
  }

  /**
   * Tests that the test content has been created.
   */
  public function testExistenceOfTestContent() {
    $this
      ->drupalGet('/node/3');
    $this
      ->assertSession()
      ->statusCodeEquals(200);
  }

  /**
   * Tests that the sidebar block is visible.
   */
  public function testExistenceOfMenuBlock() {
    $this
      ->drupalGet('/node/3');
    $this
      ->assertSession()
      ->elementContains('css', '#block-sidebar-nav-main', 'Menu item without children');
  }

  /**
   * Tests that viewing a node that is a child of menu item shows the parent
   * as a link as the title of the menu block.
   */
  public function testSecondLevel() {
    $href = '/node/1';
    $this
      ->drupalGet('/node/2');
    $this
      ->assertSession()
      ->elementContains('css', 'h2#block-sidebar-nav-main-menu', 'Test page title for top level nav');
    $xpath = $this
      ->assertSession()
      ->buildXPathQuery('//h2[@id="block-sidebar-nav-main-menu"]/a[contains(@href, :href)]', [
      ':href' => $href,
    ]);
    $link = $this
      ->getSession()
      ->getPage()
      ->findAll('xpath', $xpath);
    $message = strtr('Link containing href %href found.', [
      '%href' => $href,
    ]);
    $this
      ->assertSession()
      ->assert(!empty($link), $message);
  }

  /**
   * Tests that viewing a node in the 3rd level of depth shows the parent
   * as a link as the title of the menu block.
   */
  public function testThirdLevel() {
    $href = '/node/6';
    $this
      ->drupalGet('/node/7');
    $this
      ->assertSession()
      ->elementContains('css', 'h2#block-sidebar-nav-main-menu', 'Child of third item');
    $xpath = $this
      ->assertSession()
      ->buildXPathQuery('//h2[@id="block-sidebar-nav-main-menu"]/a[contains(@href, :href)]', [
      ':href' => $href,
    ]);
    $link = $this
      ->getSession()
      ->getPage()
      ->findAll('xpath', $xpath);
    $message = strtr('Link containing href %href found.', [
      '%href' => $href,
    ]);
    $this
      ->assertSession()
      ->assert(!empty($link), $message);
  }

}

Classes

Namesort descending Description
MenuBlockTitleTest Class SettingsPageTest.