You are here

public function BookTestTrait::checkBookNode in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/book/tests/src/Functional/BookTestTrait.php \Drupal\Tests\book\Functional\BookTestTrait::checkBookNode()
  2. 9 core/modules/book/tests/src/Functional/BookTestTrait.php \Drupal\Tests\book\Functional\BookTestTrait::checkBookNode()

Checks the outline of sub-pages; previous, up, and next.

Also checks the printer friendly version of the outline.

Parameters

\Drupal\Core\Entity\EntityInterface $node: Node to check.

$nodes: Nodes that should be in outline.

$previous: Previous link node.

$up: Up link node.

$next: Next link node.

array $breadcrumb: The nodes that should be displayed in the breadcrumb.

File

core/modules/book/tests/src/Functional/BookTestTrait.php, line 90

Class

BookTestTrait
Provides common functionality for Book test classes.

Namespace

Drupal\Tests\book\Functional

Code

public function checkBookNode(EntityInterface $node, $nodes, $previous, $up, $next, array $breadcrumb) {

  // $number does not use drupal_static as it should not be reset
  // since it uniquely identifies each call to checkBookNode().
  static $number = 0;
  $this
    ->drupalGet('node/' . $node
    ->id());

  // Check outline structure.
  if ($nodes !== NULL) {
    $this
      ->assertSession()
      ->responseMatches($this
      ->generateOutlinePattern($nodes));
  }

  // Check previous, up, and next links.
  if ($previous) {

    /** @var \Drupal\Core\Url $url */
    $url = $previous
      ->toUrl();
    $url
      ->setOptions([
      'attributes' => [
        'rel' => [
          'prev',
        ],
        'title' => t('Go to previous page'),
      ],
    ]);
    $text = new FormattableMarkup('<b>‹</b> @label', [
      '@label' => $previous
        ->label(),
    ]);
    $this
      ->assertSession()
      ->responseContains(Link::fromTextAndUrl($text, $url)
      ->toString());
  }
  if ($up) {

    /** @var \Drupal\Core\Url $url */
    $url = $up
      ->toUrl();
    $url
      ->setOptions([
      'attributes' => [
        'title' => t('Go to parent page'),
      ],
    ]);
    $this
      ->assertSession()
      ->responseContains(Link::fromTextAndUrl('Up', $url)
      ->toString());
  }
  if ($next) {

    /** @var \Drupal\Core\Url $url */
    $url = $next
      ->toUrl();
    $url
      ->setOptions([
      'attributes' => [
        'rel' => [
          'next',
        ],
        'title' => t('Go to next page'),
      ],
    ]);
    $text = new FormattableMarkup('@label <b>›</b>', [
      '@label' => $next
        ->label(),
    ]);
    $this
      ->assertSession()
      ->responseContains(Link::fromTextAndUrl($text, $url)
      ->toString());
  }

  // Compute the expected breadcrumb.
  $expected_breadcrumb = [];
  $expected_breadcrumb[] = Url::fromRoute('<front>')
    ->toString();
  foreach ($breadcrumb as $a_node) {
    $expected_breadcrumb[] = $a_node
      ->toUrl()
      ->toString();
  }

  // Fetch links in the current breadcrumb.
  $links = $this
    ->xpath('//nav[@aria-labelledby="system-breadcrumb"]/ol/li/a');
  $got_breadcrumb = [];
  foreach ($links as $link) {
    $got_breadcrumb[] = $link
      ->getAttribute('href');
  }

  // Compare expected and got breadcrumbs.
  $this
    ->assertSame($expected_breadcrumb, $got_breadcrumb, 'The breadcrumb is correctly displayed on the page.');

  // Check printer friendly version.
  $this
    ->drupalGet('book/export/html/' . $node
    ->id());
  $this
    ->assertSession()
    ->pageTextContains($node
    ->label());
  $this
    ->assertSession()
    ->responseContains($node->body->processed);
  $number++;
}