3 namespace Drupal\Tests\book\Functional;
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\Core\Entity\EntityInterface;
9 * Provides common functionality for Book test classes.
16 * @var \Drupal\node\NodeInterface
21 * A user with permission to create and edit books.
23 * @var \Drupal\Core\Session\AccountInterface
25 protected $bookAuthor;
28 * Creates a new book with a page hierarchy.
31 * (optional) Field data in an associative array. Changes the current input
32 * fields (where possible) to the values indicated. Defaults to an empty
35 * @return \Drupal\node\NodeInterface[]
37 public function createBook($edit = []) {
39 $this->drupalLogin($this->bookAuthor);
41 $this->book = $this->createBookNode('new', NULL, $edit);
45 * Add page hierarchy to book.
55 $nodes[] = $this->createBookNode($book->id(), NULL, $edit);
57 $nodes[] = $this->createBookNode($book->id(), $nodes[0]->book['nid'], $edit);
59 $nodes[] = $this->createBookNode($book->id(), $nodes[0]->book['nid'], $edit);
61 $nodes[] = $this->createBookNode($book->id(), NULL, $edit);
63 $nodes[] = $this->createBookNode($book->id(), NULL, $edit);
65 $this->drupalLogout();
71 * Checks the outline of sub-pages; previous, up, and next.
73 * Also checks the printer friendly version of the outline.
75 * @param \Drupal\Core\Entity\EntityInterface $node
78 * Nodes that should be in outline.
85 * @param array $breadcrumb
86 * The nodes that should be displayed in the breadcrumb.
88 public function checkBookNode(EntityInterface $node, $nodes, $previous, $up, $next, array $breadcrumb) {
89 // $number does not use drupal_static as it should not be reset
90 // since it uniquely identifies each call to checkBookNode().
92 $this->drupalGet('node/' . $node->id());
94 // Check outline structure.
95 if ($nodes !== NULL) {
96 $this->assertPattern($this->generateOutlinePattern($nodes), format_string('Node @number outline confirmed.', ['@number' => $number]));
99 $this->pass(format_string('Node %number does not have outline.', ['%number' => $number]));
102 // Check previous, up, and next links.
104 /** @var \Drupal\Core\Url $url */
105 $url = $previous->urlInfo();
106 $url->setOptions(['attributes' => ['rel' => ['prev'], 'title' => t('Go to previous page')]]);
107 $text = SafeMarkup::format('<b>‹</b> @label', ['@label' => $previous->label()]);
108 $this->assertRaw(\Drupal::l($text, $url), 'Previous page link found.');
112 /** @var \Drupal\Core\Url $url */
113 $url = $up->urlInfo();
114 $url->setOptions(['attributes' => ['title' => t('Go to parent page')]]);
115 $this->assertRaw(\Drupal::l('Up', $url), 'Up page link found.');
119 /** @var \Drupal\Core\Url $url */
120 $url = $next->urlInfo();
121 $url->setOptions(['attributes' => ['rel' => ['next'], 'title' => t('Go to next page')]]);
122 $text = SafeMarkup::format('@label <b>›</b>', ['@label' => $next->label()]);
123 $this->assertRaw(\Drupal::l($text, $url), 'Next page link found.');
126 // Compute the expected breadcrumb.
127 $expected_breadcrumb = [];
128 $expected_breadcrumb[] = \Drupal::url('<front>');
129 foreach ($breadcrumb as $a_node) {
130 $expected_breadcrumb[] = $a_node->url();
133 // Fetch links in the current breadcrumb.
134 $links = $this->xpath('//nav[@class="breadcrumb"]/ol/li/a');
135 $got_breadcrumb = [];
136 foreach ($links as $link) {
137 $got_breadcrumb[] = $link->getAttribute('href');
140 // Compare expected and got breadcrumbs.
141 $this->assertIdentical($expected_breadcrumb, $got_breadcrumb, 'The breadcrumb is correctly displayed on the page.');
143 // Check printer friendly version.
144 $this->drupalGet('book/export/html/' . $node->id());
145 $this->assertText($node->label(), 'Printer friendly title found.');
146 $this->assertRaw($node->body->processed, 'Printer friendly body found.');
152 * Creates a regular expression to check for the sub-nodes in the outline.
154 * @param array $nodes
155 * An array of nodes to check in outline.
158 * A regular expression that locates sub-nodes of the outline.
160 public function generateOutlinePattern($nodes) {
162 foreach ($nodes as $node) {
163 $outline .= '(node\/' . $node->id() . ')(.*?)(' . $node->label() . ')(.*?)';
166 return '/<nav id="book-navigation-' . $this->book->id() . '"(.*?)<ul(.*?)' . $outline . '<\/ul>/s';
170 * Creates a book node.
172 * @param int|string $book_nid
173 * A book node ID or set to 'new' to create a new book.
174 * @param int|null $parent
175 * (optional) Parent book reference ID. Defaults to NULL.
177 * (optional) Field data in an associative array. Changes the current input
178 * fields (where possible) to the values indicated. Defaults to an empty
181 * @return \Drupal\node\NodeInterface
184 public function createBookNode($book_nid, $parent = NULL, $edit = []) {
185 // $number does not use drupal_static as it should not be reset
186 // since it uniquely identifies each call to createBookNode().
187 // Used to ensure that when sorted nodes stay in same order.
190 $edit['title[0][value]'] = str_pad($number, 2, '0', STR_PAD_LEFT) . ' - SimpleTest test node ' . $this->randomMachineName(10);
191 $edit['body[0][value]'] = 'SimpleTest test body ' . $this->randomMachineName(32) . ' ' . $this->randomMachineName(32);
192 $edit['book[bid]'] = $book_nid;
194 if ($parent !== NULL) {
195 $this->drupalPostForm('node/add/book', $edit, t('Change book (update list of parents)'));
197 $edit['book[pid]'] = $parent;
198 $this->drupalPostForm(NULL, $edit, t('Save'));
199 // Make sure the parent was flagged as having children.
200 $parent_node = \Drupal::entityManager()->getStorage('node')->loadUnchanged($parent);
201 $this->assertFalse(empty($parent_node->book['has_children']), 'Parent node is marked as having children');
204 $this->drupalPostForm('node/add/book', $edit, t('Save'));
207 // Check to make sure the book node was created.
208 $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
209 $this->assertNotNull(($node === FALSE ? NULL : $node), 'Book node found in database.');