Chris@0: drupalLogin($this->bookAuthor);
Chris@0:
Chris@0: $this->book = $this->createBookNode('new', NULL, $edit);
Chris@0: $book = $this->book;
Chris@0:
Chris@0: /*
Chris@0: * Add page hierarchy to book.
Chris@0: * Book
Chris@0: * |- Node 0
Chris@0: * |- Node 1
Chris@0: * |- Node 2
Chris@0: * |- Node 3
Chris@0: * |- Node 4
Chris@0: */
Chris@0: $nodes = [];
Chris@0: // Node 0.
Chris@0: $nodes[] = $this->createBookNode($book->id(), NULL, $edit);
Chris@0: // Node 1.
Chris@0: $nodes[] = $this->createBookNode($book->id(), $nodes[0]->book['nid'], $edit);
Chris@0: // Node 2.
Chris@0: $nodes[] = $this->createBookNode($book->id(), $nodes[0]->book['nid'], $edit);
Chris@0: // Node 3.
Chris@0: $nodes[] = $this->createBookNode($book->id(), NULL, $edit);
Chris@0: // Node 4.
Chris@0: $nodes[] = $this->createBookNode($book->id(), NULL, $edit);
Chris@0:
Chris@0: $this->drupalLogout();
Chris@0:
Chris@0: return $nodes;
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Checks the outline of sub-pages; previous, up, and next.
Chris@0: *
Chris@0: * Also checks the printer friendly version of the outline.
Chris@0: *
Chris@0: * @param \Drupal\Core\Entity\EntityInterface $node
Chris@0: * Node to check.
Chris@0: * @param $nodes
Chris@0: * Nodes that should be in outline.
Chris@0: * @param $previous
Chris@0: * Previous link node.
Chris@0: * @param $up
Chris@0: * Up link node.
Chris@0: * @param $next
Chris@0: * Next link node.
Chris@0: * @param array $breadcrumb
Chris@0: * The nodes that should be displayed in the breadcrumb.
Chris@0: */
Chris@0: public function checkBookNode(EntityInterface $node, $nodes, $previous, $up, $next, array $breadcrumb) {
Chris@0: // $number does not use drupal_static as it should not be reset
Chris@0: // since it uniquely identifies each call to checkBookNode().
Chris@0: static $number = 0;
Chris@0: $this->drupalGet('node/' . $node->id());
Chris@0:
Chris@0: // Check outline structure.
Chris@0: if ($nodes !== NULL) {
Chris@0: $this->assertPattern($this->generateOutlinePattern($nodes), format_string('Node @number outline confirmed.', ['@number' => $number]));
Chris@0: }
Chris@0: else {
Chris@0: $this->pass(format_string('Node %number does not have outline.', ['%number' => $number]));
Chris@0: }
Chris@0:
Chris@0: // Check previous, up, and next links.
Chris@0: if ($previous) {
Chris@0: /** @var \Drupal\Core\Url $url */
Chris@18: $url = $previous->toUrl();
Chris@0: $url->setOptions(['attributes' => ['rel' => ['prev'], 'title' => t('Go to previous page')]]);
Chris@17: $text = new FormattableMarkup('‹ @label', ['@label' => $previous->label()]);
Chris@0: $this->assertRaw(\Drupal::l($text, $url), 'Previous page link found.');
Chris@0: }
Chris@0:
Chris@0: if ($up) {
Chris@0: /** @var \Drupal\Core\Url $url */
Chris@18: $url = $up->toUrl();
Chris@0: $url->setOptions(['attributes' => ['title' => t('Go to parent page')]]);
Chris@0: $this->assertRaw(\Drupal::l('Up', $url), 'Up page link found.');
Chris@0: }
Chris@0:
Chris@0: if ($next) {
Chris@0: /** @var \Drupal\Core\Url $url */
Chris@18: $url = $next->toUrl();
Chris@0: $url->setOptions(['attributes' => ['rel' => ['next'], 'title' => t('Go to next page')]]);
Chris@17: $text = new FormattableMarkup('@label ›', ['@label' => $next->label()]);
Chris@0: $this->assertRaw(\Drupal::l($text, $url), 'Next page link found.');
Chris@0: }
Chris@0:
Chris@0: // Compute the expected breadcrumb.
Chris@0: $expected_breadcrumb = [];
Chris@18: $expected_breadcrumb[] = Url::fromRoute('')->toString();
Chris@0: foreach ($breadcrumb as $a_node) {
Chris@18: $expected_breadcrumb[] = $a_node->toUrl()->toString();
Chris@0: }
Chris@0:
Chris@0: // Fetch links in the current breadcrumb.
Chris@0: $links = $this->xpath('//nav[@class="breadcrumb"]/ol/li/a');
Chris@0: $got_breadcrumb = [];
Chris@0: foreach ($links as $link) {
Chris@0: $got_breadcrumb[] = $link->getAttribute('href');
Chris@0: }
Chris@0:
Chris@0: // Compare expected and got breadcrumbs.
Chris@0: $this->assertIdentical($expected_breadcrumb, $got_breadcrumb, 'The breadcrumb is correctly displayed on the page.');
Chris@0:
Chris@0: // Check printer friendly version.
Chris@0: $this->drupalGet('book/export/html/' . $node->id());
Chris@0: $this->assertText($node->label(), 'Printer friendly title found.');
Chris@0: $this->assertRaw($node->body->processed, 'Printer friendly body found.');
Chris@0:
Chris@0: $number++;
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Creates a regular expression to check for the sub-nodes in the outline.
Chris@0: *
Chris@0: * @param array $nodes
Chris@0: * An array of nodes to check in outline.
Chris@0: *
Chris@0: * @return string
Chris@0: * A regular expression that locates sub-nodes of the outline.
Chris@0: */
Chris@0: public function generateOutlinePattern($nodes) {
Chris@0: $outline = '';
Chris@0: foreach ($nodes as $node) {
Chris@0: $outline .= '(node\/' . $node->id() . ')(.*?)(' . $node->label() . ')(.*?)';
Chris@0: }
Chris@0:
Chris@0: return '/