Chris@0: 'book', Chris@0: 'title' => 'Book', Chris@0: 'book' => ['bid' => 'new'], Chris@0: ]); Chris@0: $book->save(); Chris@0: $page1 = Node::create([ Chris@0: 'type' => 'book', Chris@0: 'title' => '1st page', Chris@0: 'book' => ['bid' => $book->id(), 'pid' => $book->id(), 'weight' => 0], Chris@0: ]); Chris@0: $page1->save(); Chris@0: $page2 = Node::create([ Chris@0: 'type' => 'book', Chris@0: 'title' => '2nd page', Chris@0: 'book' => ['bid' => $book->id(), 'pid' => $book->id(), 'weight' => 1], Chris@0: ]); Chris@0: $page2->save(); Chris@0: Chris@0: // Head to admin screen and attempt to re-order. Chris@0: $this->drupalLogin($this->drupalCreateUser(['administer book outlines'])); Chris@0: $this->drupalGet('admin/structure/book/' . $book->id()); Chris@0: Chris@0: $page = $this->getSession()->getPage(); Chris@0: Chris@0: $weight_select1 = $page->findField("table[book-admin-{$page1->id()}][weight]"); Chris@0: $weight_select2 = $page->findField("table[book-admin-{$page2->id()}][weight]"); Chris@0: Chris@0: // Check that rows weight selects are hidden. Chris@0: $this->assertFalse($weight_select1->isVisible()); Chris@0: $this->assertFalse($weight_select2->isVisible()); Chris@0: Chris@0: // Check that '2nd page' row is heavier than '1st page' row. Chris@0: $this->assertGreaterThan($weight_select1->getValue(), $weight_select2->getValue()); Chris@0: Chris@0: // Check that '1st page' precedes the '2nd page'. Chris@0: $this->assertOrderInPage(['1st page', '2nd page']); Chris@0: Chris@0: // Check that the 'unsaved changes' text is not present in the message area. Chris@0: $this->assertSession()->pageTextNotContains('You have unsaved changes.'); Chris@0: Chris@0: // Drag and drop the '1st page' row over the '2nd page' row. Chris@0: // @todo: Test also the reverse, '2nd page' over '1st page', when Chris@0: // https://www.drupal.org/node/2769825 is fixed. Chris@0: // @see https://www.drupal.org/node/2769825 Chris@0: $dragged = $this->xpath("//tr[@data-drupal-selector='edit-table-book-admin-{$page1->id()}']//a[@class='tabledrag-handle']")[0]; Chris@0: $target = $this->xpath("//tr[@data-drupal-selector='edit-table-book-admin-{$page2->id()}']//a[@class='tabledrag-handle']")[0]; Chris@0: $dragged->dragTo($target); Chris@0: Chris@0: // Give javascript some time to manipulate the DOM. Chris@0: $this->assertJsCondition('jQuery(".tabledrag-changed-warning").is(":visible")'); Chris@0: Chris@0: // Check that the 'unsaved changes' text appeared in the message area. Chris@0: $this->assertSession()->pageTextContains('You have unsaved changes.'); Chris@0: Chris@0: // Check that '2nd page' page precedes the '1st page'. Chris@0: $this->assertOrderInPage(['2nd page', '1st page']); Chris@0: Chris@0: $this->submitForm([], 'Save book pages'); Chris@0: $this->assertSession()->pageTextContains(new FormattableMarkup('Updated book @book.', ['@book' => $book->getTitle()])); Chris@0: Chris@0: // Check that page reordering was done in the backend for drag-n-drop. Chris@0: $page1 = Node::load($page1->id()); Chris@0: $page2 = Node::load($page2->id()); Chris@0: $this->assertGreaterThan($page2->book['weight'], $page1->book['weight']); Chris@0: Chris@0: // Check again that '2nd page' is on top after form submit in the UI. Chris@0: $this->assertOrderInPage(['2nd page', '1st page']); Chris@0: Chris@0: // Toggle row weight selects as visible. Chris@0: $page->findButton('Show row weights')->click(); Chris@0: Chris@0: // Check that rows weight selects are visible. Chris@0: $this->assertTrue($weight_select1->isVisible()); Chris@0: $this->assertTrue($weight_select2->isVisible()); Chris@0: Chris@0: // Check that '1st page' row became heavier than '2nd page' row. Chris@0: $this->assertGreaterThan($weight_select2->getValue(), $weight_select1->getValue()); Chris@0: Chris@0: // Reverse again using the weight fields. Use the current values so the test Chris@0: // doesn't rely on knowing the values in the select boxes. Chris@0: $value1 = $weight_select1->getValue(); Chris@0: $value2 = $weight_select2->getValue(); Chris@0: $weight_select1->setValue($value2); Chris@0: $weight_select2->setValue($value1); Chris@0: Chris@0: // Toggle row weight selects back to hidden. Chris@0: $page->findButton('Hide row weights')->click(); Chris@0: Chris@0: // Check that rows weight selects are hidden again. Chris@0: $this->assertFalse($weight_select1->isVisible()); Chris@0: $this->assertFalse($weight_select2->isVisible()); Chris@0: Chris@0: $this->submitForm([], 'Save book pages'); Chris@0: $this->assertSession()->pageTextContains(new FormattableMarkup('Updated book @book.', ['@book' => $book->getTitle()])); Chris@0: Chris@0: // Check that the '1st page' is first again. Chris@0: $this->assertOrderInPage(['1st page', '2nd page']); Chris@0: Chris@0: // Check that page reordering was done in the backend for manual weight Chris@0: // field usage. Chris@0: $page1 = Node::load($page1->id()); Chris@0: $page2 = Node::load($page2->id()); Chris@0: $this->assertGreaterThan($page2->book['weight'], $page1->book['weight']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts that several pieces of markup are in a given order in the page. Chris@0: * Chris@0: * @param string[] $items Chris@0: * An ordered list of strings. Chris@0: * Chris@0: * @throws \Behat\Mink\Exception\ExpectationException Chris@0: * When any of the given string is not found. Chris@0: * Chris@0: * @todo Remove this once https://www.drupal.org/node/2817657 is committed. Chris@0: */ Chris@0: protected function assertOrderInPage(array $items) { Chris@0: $session = $this->getSession(); Chris@0: $text = $session->getPage()->getHtml(); Chris@0: $strings = []; Chris@0: foreach ($items as $item) { Chris@0: if (($pos = strpos($text, $item)) === FALSE) { Chris@0: throw new ExpectationException("Cannot find '$item' in the page", $session->getDriver()); Chris@0: } Chris@0: $strings[$pos] = $item; Chris@0: } Chris@0: ksort($strings); Chris@0: $ordered = implode(', ', array_map(function ($item) { Chris@0: return "'$item'"; Chris@0: }, $items)); Chris@0: $this->assertSame($items, array_values($strings), "Found strings, ordered as: $ordered."); Chris@0: } Chris@0: Chris@0: }