More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / modules / book / tests / src / FunctionalJavascript / BookJavascriptTest.php
1 <?php
2
3 namespace Drupal\Tests\book\FunctionalJavascript;
4
5 use Behat\Mink\Exception\ExpectationException;
6 use Drupal\Component\Render\FormattableMarkup;
7 use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
8 use Drupal\node\Entity\Node;
9
10 /**
11  * Tests Book javascript functionality.
12  *
13  * @group book
14  */
15 class BookJavascriptTest extends JavascriptTestBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   public static $modules = ['book'];
21
22   /**
23    * Tests re-ordering of books.
24    */
25   public function testBookOrdering() {
26     $book = Node::create([
27       'type' => 'book',
28       'title' => 'Book',
29       'book' => ['bid' => 'new'],
30     ]);
31     $book->save();
32     $page1 = Node::create([
33       'type' => 'book',
34       'title' => '1st page',
35       'book' => ['bid' => $book->id(), 'pid' => $book->id(), 'weight' => 0],
36     ]);
37     $page1->save();
38     $page2 = Node::create([
39       'type' => 'book',
40       'title' => '2nd page',
41       'book' => ['bid' => $book->id(), 'pid' => $book->id(), 'weight' => 1],
42     ]);
43     $page2->save();
44
45     // Head to admin screen and attempt to re-order.
46     $this->drupalLogin($this->drupalCreateUser(['administer book outlines']));
47     $this->drupalGet('admin/structure/book/' . $book->id());
48
49     $page = $this->getSession()->getPage();
50
51     $weight_select1 = $page->findField("table[book-admin-{$page1->id()}][weight]");
52     $weight_select2 = $page->findField("table[book-admin-{$page2->id()}][weight]");
53
54     // Check that rows weight selects are hidden.
55     $this->assertFalse($weight_select1->isVisible());
56     $this->assertFalse($weight_select2->isVisible());
57
58     // Check that '2nd page' row is heavier than '1st page' row.
59     $this->assertGreaterThan($weight_select1->getValue(), $weight_select2->getValue());
60
61     // Check that '1st page' precedes the '2nd page'.
62     $this->assertOrderInPage(['1st page', '2nd page']);
63
64     // Check that the 'unsaved changes' text is not present in the message area.
65     $this->assertSession()->pageTextNotContains('You have unsaved changes.');
66
67     // Drag and drop the '1st page' row over the '2nd page' row.
68     // @todo: Test also the reverse, '2nd page' over '1st page', when
69     //   https://www.drupal.org/node/2769825 is fixed.
70     // @see https://www.drupal.org/node/2769825
71     $dragged = $this->xpath("//tr[@data-drupal-selector='edit-table-book-admin-{$page1->id()}']//a[@class='tabledrag-handle']")[0];
72     $target = $this->xpath("//tr[@data-drupal-selector='edit-table-book-admin-{$page2->id()}']//a[@class='tabledrag-handle']")[0];
73     $dragged->dragTo($target);
74
75     // Give javascript some time to manipulate the DOM.
76     $this->assertJsCondition('jQuery(".tabledrag-changed-warning").is(":visible")');
77
78     // Check that the 'unsaved changes' text appeared in the message area.
79     $this->assertSession()->pageTextContains('You have unsaved changes.');
80
81     // Check that '2nd page' page precedes the '1st page'.
82     $this->assertOrderInPage(['2nd page', '1st page']);
83
84     $this->submitForm([], 'Save book pages');
85     $this->assertSession()->pageTextContains(new FormattableMarkup('Updated book @book.', ['@book' => $book->getTitle()]));
86
87     // Check that page reordering was done in the backend for drag-n-drop.
88     $page1 = Node::load($page1->id());
89     $page2 = Node::load($page2->id());
90     $this->assertGreaterThan($page2->book['weight'], $page1->book['weight']);
91
92     // Check again that '2nd page' is on top after form submit in the UI.
93     $this->assertOrderInPage(['2nd page', '1st page']);
94
95     // Toggle row weight selects as visible.
96     $page->findButton('Show row weights')->click();
97
98     // Check that rows weight selects are visible.
99     $this->assertTrue($weight_select1->isVisible());
100     $this->assertTrue($weight_select2->isVisible());
101
102     // Check that '1st page' row became heavier than '2nd page' row.
103     $this->assertGreaterThan($weight_select2->getValue(), $weight_select1->getValue());
104
105     // Reverse again using the weight fields. Use the current values so the test
106     // doesn't rely on knowing the values in the select boxes.
107     $value1 = $weight_select1->getValue();
108     $value2 = $weight_select2->getValue();
109     $weight_select1->setValue($value2);
110     $weight_select2->setValue($value1);
111
112     // Toggle row weight selects back to hidden.
113     $page->findButton('Hide row weights')->click();
114
115     // Check that rows weight selects are hidden again.
116     $this->assertFalse($weight_select1->isVisible());
117     $this->assertFalse($weight_select2->isVisible());
118
119     $this->submitForm([], 'Save book pages');
120     $this->assertSession()->pageTextContains(new FormattableMarkup('Updated book @book.', ['@book' => $book->getTitle()]));
121
122     // Check that the '1st page' is first again.
123     $this->assertOrderInPage(['1st page', '2nd page']);
124
125     // Check that page reordering was done in the backend for manual weight
126     // field usage.
127     $page1 = Node::load($page1->id());
128     $page2 = Node::load($page2->id());
129     $this->assertGreaterThan($page2->book['weight'], $page1->book['weight']);
130   }
131
132   /**
133    * Asserts that several pieces of markup are in a given order in the page.
134    *
135    * @param string[] $items
136    *   An ordered list of strings.
137    *
138    * @throws \Behat\Mink\Exception\ExpectationException
139    *   When any of the given string is not found.
140    *
141    * @todo Remove this once https://www.drupal.org/node/2817657 is committed.
142    */
143   protected function assertOrderInPage(array $items) {
144     $session = $this->getSession();
145     $text = $session->getPage()->getHtml();
146     $strings = [];
147     foreach ($items as $item) {
148       if (($pos = strpos($text, $item)) === FALSE) {
149         throw new ExpectationException("Cannot find '$item' in the page", $session->getDriver());
150       }
151       $strings[$pos] = $item;
152     }
153     ksort($strings);
154     $ordered = implode(', ', array_map(function ($item) {
155       return "'$item'";
156     }, $items));
157     $this->assertSame($items, array_values($strings), "Found strings, ordered as: $ordered.");
158   }
159
160 }