diff --git a/core/modules/book/tests/src/FunctionalJavascript/BookJavascriptTest.php b/core/modules/book/tests/src/FunctionalJavascript/BookJavascriptTest.php index af32ea6..b324e72 100644 --- a/core/modules/book/tests/src/FunctionalJavascript/BookJavascriptTest.php +++ b/core/modules/book/tests/src/FunctionalJavascript/BookJavascriptTest.php @@ -59,7 +59,7 @@ public function testBookOrdering() { $this->assertGreaterThan($weight_select1->getValue(), $weight_select2->getValue()); // Check that '1st page' precedes the '2nd page'. - $this->assertOrderInPage(['1st page', '2nd page']); + $this->assertSession()->orderInPage(['1st page', '2nd page']); // Check that the 'unsaved changes' text is not present in the message area. $this->assertSession()->pageTextNotContains('You have unsaved changes.'); @@ -79,7 +79,7 @@ public function testBookOrdering() { $this->assertSession()->pageTextContains('You have unsaved changes.'); // Check that '2nd page' page precedes the '1st page'. - $this->assertOrderInPage(['2nd page', '1st page']); + $this->assertSession()->orderInPage(['2nd page', '1st page']); // Toggle row weight selects as visible. $page->findButton('Show row weights')->click(); @@ -102,7 +102,7 @@ public function testBookOrdering() { $this->assertSession()->pageTextContains(new FormattableMarkup('Updated book @book.', ['@book' => $book->getTitle()])); // Check again that '2nd page' is on top after form submit. - $this->assertOrderInPage(['2nd page', '1st page']); + $this->assertSession()->orderInPage(['2nd page', '1st page']); // Check that page reordering was done in the backend. $page1 = Node::load($page1->id()); @@ -110,30 +110,4 @@ public function testBookOrdering() { $this->assertGreaterThan($page2->book['weight'], $page1->book['weight']); } - /** - * Asserts that several pieces of markup are in a given order in the page. - * - * @param string[] $items - * An ordered list of strings. - * - * @throws \Behat\Mink\Exception\ExpectationException - * When any of the given string is not found. - */ - protected function assertOrderInPage(array $items) { - $session = $this->getSession(); - $text = $session->getPage()->getHtml(); - $strings = []; - foreach ($items as $item) { - if (($pos = strpos($text, $item)) === FALSE) { - throw new ExpectationException("Cannot find '$item' in the page", $session->getDriver()); - } - $strings[$pos] = $item; - } - ksort($strings); - $ordered = implode(', ', array_map(function ($item) { - return "'$item'"; - }, $items)); - $this->assertSame($items, array_values($strings), "Found strings, ordered as: $ordered."); - } - } diff --git a/core/tests/Drupal/Tests/WebAssert.php b/core/tests/Drupal/Tests/WebAssert.php index 6156dda..f967e4d 100644 --- a/core/tests/Drupal/Tests/WebAssert.php +++ b/core/tests/Drupal/Tests/WebAssert.php @@ -422,4 +422,31 @@ public function fieldDisabled($field, TraversableElement $container = NULL) { return $node; } + /** + * Asserts that several pieces of markup are in a given order in the page. + * + * @param string[] $items + * An ordered list of strings. + * + * @throws \Behat\Mink\Exception\ExpectationException + * When any of the given string is not found. + */ + public function orderInPage(array $items) { + $text = $this->session->getPage()->getHtml(); + $strings = []; + foreach ($items as $item) { + if (($pos = strpos($text, $item)) === FALSE) { + throw new ExpectationException("Cannot find '$item' in the page", $this->session->getDriver()); + } + $strings[$pos] = $item; + } + ksort($strings); + $ordered = implode(', ', array_map(function ($item) { + return "'$item'"; + }, $items)); + if ($items !== array_values($strings)) { + throw new ExpectationException("Strings were not correctly ordered as: $ordered.", $this->session->getDriver()); + } + } + }