diff --git a/core/modules/book/tests/src/Functional/BookTest.php b/core/modules/book/tests/src/Functional/BookTest.php index a213588..354b887 100644 --- a/core/modules/book/tests/src/Functional/BookTest.php +++ b/core/modules/book/tests/src/Functional/BookTest.php @@ -571,34 +571,6 @@ function testBookDelete() { } /** - * Tests re-ordering of books. - */ - public function testBookOrdering() { - // Create new book. - $this->createBook(); - $book = $this->book; - - $this->drupalLogin($this->adminUser); - $node1 = $this->createBookNode($book->id()); - $node2 = $this->createBookNode($book->id()); - $pid = $node1->book['nid']; - - // Head to admin screen and attempt to re-order. - $this->drupalGet('admin/structure/book/' . $book->id()); - $edit = array( - "table[book-admin-{$node1->id()}][weight]" => 1, - "table[book-admin-{$node2->id()}][weight]" => 2, - // Put node 2 under node 1. - "table[book-admin-{$node2->id()}][pid]" => $pid, - ); - $this->drupalPostForm(NULL, $edit, t('Save book pages')); - // Verify weight was updated. - $this->assertFieldByName("table[book-admin-{$node1->id()}][weight]", 1); - $this->assertFieldByName("table[book-admin-{$node2->id()}][weight]", 2); - $this->assertFieldByName("table[book-admin-{$node2->id()}][pid]", $pid); - } - - /** * Tests outline of a book. */ public function testBookOutline() { diff --git a/core/modules/book/tests/src/FunctionalJavascript/BookJavascriptTest.php b/core/modules/book/tests/src/FunctionalJavascript/BookJavascriptTest.php new file mode 100644 index 0000000..2ab7d3a --- /dev/null +++ b/core/modules/book/tests/src/FunctionalJavascript/BookJavascriptTest.php @@ -0,0 +1,137 @@ + 'book', + 'title' => 'Book', + 'book' => ['bid' => 'new'], + ]); + $book->save(); + $page1 = Node::create([ + 'type' => 'book', + 'title' => '1st page', + 'book' => ['bid' => $book->id(), 'pid' => $book->id(), 'weight' => 0], + ]); + $page1->save(); + $page2 = Node::create([ + 'type' => 'book', + 'title' => '2nd page', + 'book' => ['bid' => $book->id(), 'pid' => $book->id(), 'weight' => 1], + ]); + $page2->save(); + + // Head to admin screen and attempt to re-order. + $this->drupalLogin($this->drupalCreateUser(['administer book outlines'])); + $this->drupalGet('admin/structure/book/' . $book->id()); + + $session = $this->getSession(); + $page = $session->getPage(); + + $weight_select1 = $page->findField("table[book-admin-{$page1->id()}][weight]"); + $weight_select2 = $page->findField("table[book-admin-{$page2->id()}][weight]"); + + // Check that rows weight selects are hidden. + $this->assertFalse($weight_select1->isVisible()); + $this->assertFalse($weight_select2->isVisible()); + + // Check that '2nd page' row is heavier than '1st page' row. + $this->assertTrue($weight_select2->getValue() > $weight_select1->getValue()); + + // Check that '1st page' precedes the '2nd page'. + $this->assertFirstPrecedesSecond('1st page', '2nd'); + + // Check that the 'unsaved changes' text is not present in the message area. + $this->assertSession()->pageTextNotContains('You have unsaved changes.'); + + // Drag and drop the '1st page' row over the '2nd page' row. + // @todo: Investigate why if trying the reverse (2nd over 1st), the 2nd is + // indented instead of moved upward. This might be a bug in tabledrag.js. + $dragged = $this->xpath("//tr[@data-drupal-selector='edit-table-book-admin-{$page1->id()}']//a[@class='tabledrag-handle']")[0]; + $target = $this->xpath("//tr[@data-drupal-selector='edit-table-book-admin-{$page2->id()}']//a[@class='tabledrag-handle']")[0]; + $dragged->dragTo($target); + + // Give javascript some time to manipulate the DOM. + $this->getSession()->wait(500); + + // Check that the 'unsaved changes' text appeared in the message area. + $this->assertSession()->pageTextContains('You have unsaved changes.'); + + // Check that '2nd page' page precedes the '1st page'. + $this->assertFirstPrecedesSecond('2nd page', '1st page'); + + // Toggle row weight selects as visible. + $page->findButton(t('Show row weights'))->click(); + + // Check that rows weight selects are visible. + $this->assertTrue($weight_select1->isVisible()); + $this->assertTrue($weight_select2->isVisible()); + + // Check that '1st page' row became heavier than '1st page' row. + $this->assertTrue($weight_select1->getValue() > $weight_select2->getValue()); + + // Toggle row weight selects back to hidden. + $page->findButton(t('Hide row weights'))->click(); + + // Check that rows weight selects are hidden again. + $this->assertFalse($weight_select1->isVisible()); + $this->assertFalse($weight_select2->isVisible()); + + $this->submitForm([], t('Save book pages')); + $this->assertSession()->pageTextContains(t('Updated book @book.', ['@book' => $book->getTitle()])); + + // Check again that '2nd page' is on top after form submit. + $this->assertFirstPrecedesSecond('2nd page', '1st page'); + + // Check that page reordering was done in the backend. + $page1 = Node::load($page1->id()); + $page2 = Node::load($page2->id()); + $this->assertTrue($page1->book['weight'] > $page2->book['weight']); + } + + /** + * Asserts that the first piece of markup precedes the second. + * + * @param string $first + * The first string to be checked. + * @param string $second + * The second string to be checked. + * + * @throws \Behat\Mink\Exception\ExpectationException + * When any of the strings is not found. + */ + protected function assertFirstPrecedesSecond($first, $second) { + $session = $this->getSession(); + $text = $session->getPage()->getHtml(); + if (($pos1 = strpos($text, $first)) === FALSE) { + throw new ExpectationException("Cannot find '$first'", $session->getDriver()); + } + if (($pos2 = strpos($text, $second)) === FALSE) { + throw new ExpectationException("Cannot find '$second'", $session->getDriver()); + } + $this->assertTrue($pos2 > $pos1, "'$first' precedes '$second'"); + } + +}