function BookTestTrait::checkBookNode

Same name and namespace in other branches
  1. 9 core/modules/book/tests/src/Functional/BookTestTrait.php \Drupal\Tests\book\Functional\BookTestTrait::checkBookNode()
  2. 8.9.x core/modules/book/tests/src/Functional/BookTestTrait.php \Drupal\Tests\book\Functional\BookTestTrait::checkBookNode()
  3. 11.x 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.

2 calls to BookTestTrait::checkBookNode()
BookContentModerationTest::testBookWithPendingRevisions in core/modules/book/tests/src/Functional/BookContentModerationTest.php
Tests that book drafts can not modify the book outline.
BookTest::testBook in core/modules/book/tests/src/Functional/BookTest.php
Tests book functionality through node interfaces.

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) {
  $this->drupalGet('node/' . $node->id());
  // Check outline structure.
  if ($nodes !== NULL) {
    $book_navigation = $this->getSession()
      ->getPage()
      ->find('css', sprintf('nav[aria-labelledby="book-label-%s"] ul', $this->book
      ->id()));
    $this->assertNotNull($book_navigation);
    $links = $book_navigation->findAll('css', 'a');
    $this->assertCount(count($nodes), $links);
    foreach ($nodes as $delta => $node) {
      $link = $links[$delta];
      $this->assertEquals($node->label(), $link->getText());
      $this->assertEquals($node->toUrl()
        ->toString(), $link->getAttribute('href'));
    }
  }
  // Check previous, up, and next links.
  if ($previous) {
    $previous_element = $this->assertSession()
      ->elementExists('named_exact', [
      'link',
      'Go to previous page',
    ]);
    $this->assertEquals($previous->toUrl()
      ->toString(), $previous_element->getAttribute('href'));
  }
  if ($up) {
    $parent_element = $this->assertSession()
      ->elementExists('named_exact', [
      'link',
      'Go to parent page',
    ]);
    $this->assertEquals($up->toUrl()
      ->toString(), $parent_element->getAttribute('href'));
  }
  if ($next) {
    $next_element = $this->assertSession()
      ->elementExists('named_exact', [
      'link',
      'Go to next page',
    ]);
    $this->assertEquals($next->toUrl()
      ->toString(), $next_element->getAttribute('href'));
  }
  // 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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.