More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / modules / book / tests / src / Functional / BookTestTrait.php
1 <?php
2
3 namespace Drupal\Tests\book\Functional;
4
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\Core\Entity\EntityInterface;
7
8 /**
9  * Provides common functionality for Book test classes.
10  */
11 trait BookTestTrait {
12
13   /**
14    * A book node.
15    *
16    * @var \Drupal\node\NodeInterface
17    */
18   protected $book;
19
20   /**
21    * A user with permission to create and edit books.
22    *
23    * @var \Drupal\Core\Session\AccountInterface
24    */
25   protected $bookAuthor;
26
27   /**
28    * Creates a new book with a page hierarchy.
29    *
30    * @param array $edit
31    *   (optional) Field data in an associative array. Changes the current input
32    *   fields (where possible) to the values indicated. Defaults to an empty
33    *   array.
34    *
35    * @return \Drupal\node\NodeInterface[]
36    */
37   public function createBook($edit = []) {
38     // Create new book.
39     $this->drupalLogin($this->bookAuthor);
40
41     $this->book = $this->createBookNode('new', NULL, $edit);
42     $book = $this->book;
43
44     /*
45      * Add page hierarchy to book.
46      * Book
47      *  |- Node 0
48      *   |- Node 1
49      *   |- Node 2
50      *  |- Node 3
51      *  |- Node 4
52      */
53     $nodes = [];
54     // Node 0.
55     $nodes[] = $this->createBookNode($book->id(), NULL, $edit);
56     // Node 1.
57     $nodes[] = $this->createBookNode($book->id(), $nodes[0]->book['nid'], $edit);
58     // Node 2.
59     $nodes[] = $this->createBookNode($book->id(), $nodes[0]->book['nid'], $edit);
60     // Node 3.
61     $nodes[] = $this->createBookNode($book->id(), NULL, $edit);
62     // Node 4.
63     $nodes[] = $this->createBookNode($book->id(), NULL, $edit);
64
65     $this->drupalLogout();
66
67     return $nodes;
68   }
69
70   /**
71    * Checks the outline of sub-pages; previous, up, and next.
72    *
73    * Also checks the printer friendly version of the outline.
74    *
75    * @param \Drupal\Core\Entity\EntityInterface $node
76    *   Node to check.
77    * @param $nodes
78    *   Nodes that should be in outline.
79    * @param $previous
80    *   Previous link node.
81    * @param $up
82    *   Up link node.
83    * @param $next
84    *   Next link node.
85    * @param array $breadcrumb
86    *   The nodes that should be displayed in the breadcrumb.
87    */
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().
91     static $number = 0;
92     $this->drupalGet('node/' . $node->id());
93
94     // Check outline structure.
95     if ($nodes !== NULL) {
96       $this->assertPattern($this->generateOutlinePattern($nodes), format_string('Node @number outline confirmed.', ['@number' => $number]));
97     }
98     else {
99       $this->pass(format_string('Node %number does not have outline.', ['%number' => $number]));
100     }
101
102     // Check previous, up, and next links.
103     if ($previous) {
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.');
109     }
110
111     if ($up) {
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.');
116     }
117
118     if ($next) {
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.');
124     }
125
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();
131     }
132
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');
138     }
139
140     // Compare expected and got breadcrumbs.
141     $this->assertIdentical($expected_breadcrumb, $got_breadcrumb, 'The breadcrumb is correctly displayed on the page.');
142
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.');
147
148     $number++;
149   }
150
151   /**
152    * Creates a regular expression to check for the sub-nodes in the outline.
153    *
154    * @param array $nodes
155    *   An array of nodes to check in outline.
156    *
157    * @return string
158    *   A regular expression that locates sub-nodes of the outline.
159    */
160   public function generateOutlinePattern($nodes) {
161     $outline = '';
162     foreach ($nodes as $node) {
163       $outline .= '(node\/' . $node->id() . ')(.*?)(' . $node->label() . ')(.*?)';
164     }
165
166     return '/<nav id="book-navigation-' . $this->book->id() . '"(.*?)<ul(.*?)' . $outline . '<\/ul>/s';
167   }
168
169   /**
170    * Creates a book node.
171    *
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.
176    * @param array $edit
177    *   (optional) Field data in an associative array. Changes the current input
178    *   fields (where possible) to the values indicated. Defaults to an empty
179    *   array.
180    *
181    * @return \Drupal\node\NodeInterface
182    *   The created node.
183    */
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.
188     static $number = 0;
189
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;
193
194     if ($parent !== NULL) {
195       $this->drupalPostForm('node/add/book', $edit, t('Change book (update list of parents)'));
196
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');
202     }
203     else {
204       $this->drupalPostForm('node/add/book', $edit, t('Save'));
205     }
206
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.');
210     $number++;
211
212     return $node;
213   }
214
215 }