X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fbook%2Ftests%2Fsrc%2FFunctional%2FBookTestTrait.php;fp=web%2Fcore%2Fmodules%2Fbook%2Ftests%2Fsrc%2FFunctional%2FBookTestTrait.php;h=177673841d3efe64d098387a247a9d4ce659013a;hp=0000000000000000000000000000000000000000;hb=9917807b03b64faf00f6a1f29dcb6eafc454efa5;hpb=aea91e65e895364e460983b890e295aa5d5540a5 diff --git a/web/core/modules/book/tests/src/Functional/BookTestTrait.php b/web/core/modules/book/tests/src/Functional/BookTestTrait.php new file mode 100644 index 000000000..177673841 --- /dev/null +++ b/web/core/modules/book/tests/src/Functional/BookTestTrait.php @@ -0,0 +1,215 @@ +drupalLogin($this->bookAuthor); + + $this->book = $this->createBookNode('new', NULL, $edit); + $book = $this->book; + + /* + * Add page hierarchy to book. + * Book + * |- Node 0 + * |- Node 1 + * |- Node 2 + * |- Node 3 + * |- Node 4 + */ + $nodes = []; + // Node 0. + $nodes[] = $this->createBookNode($book->id(), NULL, $edit); + // Node 1. + $nodes[] = $this->createBookNode($book->id(), $nodes[0]->book['nid'], $edit); + // Node 2. + $nodes[] = $this->createBookNode($book->id(), $nodes[0]->book['nid'], $edit); + // Node 3. + $nodes[] = $this->createBookNode($book->id(), NULL, $edit); + // Node 4. + $nodes[] = $this->createBookNode($book->id(), NULL, $edit); + + $this->drupalLogout(); + + return $nodes; + } + + /** + * Checks the outline of sub-pages; previous, up, and next. + * + * Also checks the printer friendly version of the outline. + * + * @param \Drupal\Core\Entity\EntityInterface $node + * Node to check. + * @param $nodes + * Nodes that should be in outline. + * @param $previous + * Previous link node. + * @param $up + * Up link node. + * @param $next + * Next link node. + * @param array $breadcrumb + * The nodes that should be displayed in the breadcrumb. + */ + 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->assertPattern($this->generateOutlinePattern($nodes), format_string('Node @number outline confirmed.', ['@number' => $number])); + } + else { + $this->pass(format_string('Node %number does not have outline.', ['%number' => $number])); + } + + // Check previous, up, and next links. + if ($previous) { + /** @var \Drupal\Core\Url $url */ + $url = $previous->urlInfo(); + $url->setOptions(['attributes' => ['rel' => ['prev'], 'title' => t('Go to previous page')]]); + $text = SafeMarkup::format('‹ @label', ['@label' => $previous->label()]); + $this->assertRaw(\Drupal::l($text, $url), 'Previous page link found.'); + } + + if ($up) { + /** @var \Drupal\Core\Url $url */ + $url = $up->urlInfo(); + $url->setOptions(['attributes' => ['title' => t('Go to parent page')]]); + $this->assertRaw(\Drupal::l('Up', $url), 'Up page link found.'); + } + + if ($next) { + /** @var \Drupal\Core\Url $url */ + $url = $next->urlInfo(); + $url->setOptions(['attributes' => ['rel' => ['next'], 'title' => t('Go to next page')]]); + $text = SafeMarkup::format('@label ›', ['@label' => $next->label()]); + $this->assertRaw(\Drupal::l($text, $url), 'Next page link found.'); + } + + // Compute the expected breadcrumb. + $expected_breadcrumb = []; + $expected_breadcrumb[] = \Drupal::url(''); + foreach ($breadcrumb as $a_node) { + $expected_breadcrumb[] = $a_node->url(); + } + + // Fetch links in the current breadcrumb. + $links = $this->xpath('//nav[@class="breadcrumb"]/ol/li/a'); + $got_breadcrumb = []; + foreach ($links as $link) { + $got_breadcrumb[] = $link->getAttribute('href'); + } + + // Compare expected and got breadcrumbs. + $this->assertIdentical($expected_breadcrumb, $got_breadcrumb, 'The breadcrumb is correctly displayed on the page.'); + + // Check printer friendly version. + $this->drupalGet('book/export/html/' . $node->id()); + $this->assertText($node->label(), 'Printer friendly title found.'); + $this->assertRaw($node->body->processed, 'Printer friendly body found.'); + + $number++; + } + + /** + * Creates a regular expression to check for the sub-nodes in the outline. + * + * @param array $nodes + * An array of nodes to check in outline. + * + * @return string + * A regular expression that locates sub-nodes of the outline. + */ + public function generateOutlinePattern($nodes) { + $outline = ''; + foreach ($nodes as $node) { + $outline .= '(node\/' . $node->id() . ')(.*?)(' . $node->label() . ')(.*?)'; + } + + return '/