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