3 namespace Drupal\Tests\book\FunctionalJavascript;
5 use Behat\Mink\Exception\ExpectationException;
6 use Drupal\Component\Render\FormattableMarkup;
7 use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
8 use Drupal\node\Entity\Node;
11 * Tests Book javascript functionality.
15 class BookJavascriptTest extends JavascriptTestBase {
20 public static $modules = ['book'];
23 * Tests re-ordering of books.
25 public function testBookOrdering() {
26 $book = Node::create([
29 'book' => ['bid' => 'new'],
32 $page1 = Node::create([
34 'title' => '1st page',
35 'book' => ['bid' => $book->id(), 'pid' => $book->id(), 'weight' => 0],
38 $page2 = Node::create([
40 'title' => '2nd page',
41 'book' => ['bid' => $book->id(), 'pid' => $book->id(), 'weight' => 1],
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());
49 $page = $this->getSession()->getPage();
51 $weight_select1 = $page->findField("table[book-admin-{$page1->id()}][weight]");
52 $weight_select2 = $page->findField("table[book-admin-{$page2->id()}][weight]");
54 // Check that rows weight selects are hidden.
55 $this->assertFalse($weight_select1->isVisible());
56 $this->assertFalse($weight_select2->isVisible());
58 // Check that '2nd page' row is heavier than '1st page' row.
59 $this->assertGreaterThan($weight_select1->getValue(), $weight_select2->getValue());
61 // Check that '1st page' precedes the '2nd page'.
62 $this->assertOrderInPage(['1st page', '2nd page']);
64 // Check that the 'unsaved changes' text is not present in the message area.
65 $this->assertSession()->pageTextNotContains('You have unsaved changes.');
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);
75 // Give javascript some time to manipulate the DOM.
76 $this->assertJsCondition('jQuery(".tabledrag-changed-warning").is(":visible")');
78 // Check that the 'unsaved changes' text appeared in the message area.
79 $this->assertSession()->pageTextContains('You have unsaved changes.');
81 // Check that '2nd page' page precedes the '1st page'.
82 $this->assertOrderInPage(['2nd page', '1st page']);
84 $this->submitForm([], 'Save book pages');
85 $this->assertSession()->pageTextContains(new FormattableMarkup('Updated book @book.', ['@book' => $book->getTitle()]));
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']);
92 // Check again that '2nd page' is on top after form submit in the UI.
93 $this->assertOrderInPage(['2nd page', '1st page']);
95 // Toggle row weight selects as visible.
96 $page->findButton('Show row weights')->click();
98 // Check that rows weight selects are visible.
99 $this->assertTrue($weight_select1->isVisible());
100 $this->assertTrue($weight_select2->isVisible());
102 // Check that '1st page' row became heavier than '2nd page' row.
103 $this->assertGreaterThan($weight_select2->getValue(), $weight_select1->getValue());
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);
112 // Toggle row weight selects back to hidden.
113 $page->findButton('Hide row weights')->click();
115 // Check that rows weight selects are hidden again.
116 $this->assertFalse($weight_select1->isVisible());
117 $this->assertFalse($weight_select2->isVisible());
119 $this->submitForm([], 'Save book pages');
120 $this->assertSession()->pageTextContains(new FormattableMarkup('Updated book @book.', ['@book' => $book->getTitle()]));
122 // Check that the '1st page' is first again.
123 $this->assertOrderInPage(['1st page', '2nd page']);
125 // Check that page reordering was done in the backend for manual weight
127 $page1 = Node::load($page1->id());
128 $page2 = Node::load($page2->id());
129 $this->assertGreaterThan($page2->book['weight'], $page1->book['weight']);
133 * Asserts that several pieces of markup are in a given order in the page.
135 * @param string[] $items
136 * An ordered list of strings.
138 * @throws \Behat\Mink\Exception\ExpectationException
139 * When any of the given string is not found.
141 * @todo Remove this once https://www.drupal.org/node/2817657 is committed.
143 protected function assertOrderInPage(array $items) {
144 $session = $this->getSession();
145 $text = $session->getPage()->getHtml();
147 foreach ($items as $item) {
148 if (($pos = strpos($text, $item)) === FALSE) {
149 throw new ExpectationException("Cannot find '$item' in the page", $session->getDriver());
151 $strings[$pos] = $item;
154 $ordered = implode(', ', array_map(function ($item) {
157 $this->assertSame($items, array_values($strings), "Found strings, ordered as: $ordered.");