diff --git a/core/modules/book/tests/src/FunctionalJavascript/BookJavascriptTest.php b/core/modules/book/tests/src/FunctionalJavascript/BookJavascriptTest.php index 0b01bb6..1af143c 100644 --- a/core/modules/book/tests/src/FunctionalJavascript/BookJavascriptTest.php +++ b/core/modules/book/tests/src/FunctionalJavascript/BookJavascriptTest.php @@ -3,6 +3,7 @@ namespace Drupal\Tests\book\FunctionalJavascript; use Behat\Mink\Exception\ExpectationException; +use Drupal\Component\Render\FormattableMarkup; use Drupal\FunctionalJavascriptTests\JavascriptTestBase; use Drupal\node\Entity\Node; @@ -47,8 +48,7 @@ public function testBookOrdering() { $this->drupalLogin($this->drupalCreateUser(['administer book outlines'])); $this->drupalGet('admin/structure/book/' . $book->id()); - $session = $this->getSession(); - $page = $session->getPage(); + $page = $this->getSession()->getPage(); $weight_select1 = $page->findField("table[book-admin-{$page1->id()}][weight]"); $weight_select2 = $page->findField("table[book-admin-{$page2->id()}][weight]"); @@ -58,10 +58,10 @@ public function testBookOrdering() { $this->assertFalse($weight_select2->isVisible()); // Check that '2nd page' row is heavier than '1st page' row. - $this->assertTrue($weight_select2->getValue() > $weight_select1->getValue()); + $this->assertGreaterThan($weight_select1->getValue(), $weight_select2->getValue()); // Check that '1st page' precedes the '2nd page'. - $this->assertFirstPrecedesSecond('1st page', '2nd'); + $this->assertOrderInPage(['1st page', '2nd page']); // Check that the 'unsaved changes' text is not present in the message area. $this->assertSession()->pageTextNotContains('You have unsaved changes.'); @@ -81,30 +81,30 @@ public function testBookOrdering() { $this->assertSession()->pageTextContains('You have unsaved changes.'); // Check that '2nd page' page precedes the '1st page'. - $this->assertFirstPrecedesSecond('2nd page', '1st page'); + $this->assertOrderInPage(['2nd page', '1st page']); // Toggle row weight selects as visible. - $page->findButton(t('Show row weights'))->click(); + $page->findButton('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()); + // Check that '1st page' row became heavier than '2nd page' row. + $this->assertGreaterThan($weight_select2->getValue(), $weight_select1->getValue()); // Toggle row weight selects back to hidden. - $page->findButton(t('Hide row weights'))->click(); + $page->findButton('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()])); + $this->submitForm([], 'Save book pages'); + $this->assertSession()->pageTextContains(new FormattableMarkup('Updated book @book.', ['@book' => $book->getTitle()])); // Check again that '2nd page' is on top after form submit. - $this->assertFirstPrecedesSecond('2nd page', '1st page'); + $this->assertOrderInPage(['2nd page', '1st page']); // Check that page reordering was done in the backend. $page1 = Node::load($page1->id()); @@ -113,26 +113,29 @@ public function testBookOrdering() { } /** - * Asserts that the first piece of markup precedes the second. + * Asserts that several pieces of markup are in a given order in the page. * - * @param string $first - * The first string to be checked. - * @param string $second - * The second string to be checked. + * @param string[] $items + * An ordered list of strings. * * @throws \Behat\Mink\Exception\ExpectationException - * When any of the strings is not found. + * When any of the given string is not found. */ - protected function assertFirstPrecedesSecond($first, $second) { + protected function assertOrderInPage(array $items) { $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()); + $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; } - $this->assertGreaterThan($pos1, $pos2, "'$first' precedes '$second'"); + ksort($strings); + $ordered = implode(', ', array_map(function ($item) { + return "'$item'"; + }, $items)); + $this->assertSame($items, array_values($strings), "Found strings, ordered as: $ordered."); } }