diff --git a/core/modules/color/test/src/Functional/ColorSafePreviewTest.php b/core/modules/color/test/src/Functional/ColorSafePreviewTest.php index ba49ee8..18b0534 100644 --- a/core/modules/color/test/src/Functional/ColorSafePreviewTest.php +++ b/core/modules/color/test/src/Functional/ColorSafePreviewTest.php @@ -50,7 +50,7 @@ function testColorPreview() { $this->drupalGet($url); $this->assertText('TEST COLOR PREVIEW'); - $this->assertSession()->responseNotContains(''); + $this->assertNoRaw(''); $this->assertRaw('

TEST COLOR PREVIEW

'); } diff --git a/core/modules/color/test/src/Functional/ColorTest.php b/core/modules/color/test/src/Functional/ColorTest.php index 465c47f..a67f9de 100644 --- a/core/modules/color/test/src/Functional/ColorTest.php +++ b/core/modules/color/test/src/Functional/ColorTest.php @@ -108,7 +108,7 @@ function _testColor($theme, $test_values) { $this->drupalLogin($this->bigUser); $this->drupalGet($settings_path); $this->assertResponse(200); -// $this->assertUniqueText('Color set'); + $this->assertUniqueText('Color set'); $edit['scheme'] = ''; $edit[$test_values['palette_input']] = '#123456'; $this->drupalPostForm($settings_path, $edit, t('Save configuration')); @@ -116,7 +116,7 @@ function _testColor($theme, $test_values) { $this->drupalGet(''); $stylesheets = $this->config('color.theme.' . $theme)->get('stylesheets'); foreach ($stylesheets as $stylesheet) { - $this->assertSession()->responseMatches('|' . file_url_transform_relative(file_create_url($stylesheet)) . '|'); + $this->assertPattern('|' . file_url_transform_relative(file_create_url($stylesheet)) . '|'); $stylesheet_content = join("\n", file($stylesheet)); $this->assertTrue(strpos($stylesheet_content, 'color: #123456') !== FALSE, 'Make sure the color we changed is in the color stylesheet. (' . $theme . ')'); } @@ -186,7 +186,7 @@ function testLogoSettingOverride() { // Ensure that the overridden logo is present in Bartik, which is colorable. $this->drupalGet('admin/appearance/settings/bartik'); -// $this->assertIdentical($GLOBALS['base_path'] . 'core/misc/druplicon.png', $this->getDrupalSettings()['color']['logo']); + $this->assertIdentical($GLOBALS['base_path'] . 'core/misc/druplicon.png', $this->getDrupalSettings()['color']['logo']); } /** @@ -202,7 +202,7 @@ function testOverrideAndResetScheme() { $this->drupalPlaceBlock('system_branding_block', ['region' => 'header']); $this->drupalGet(''); - $this->assertSession()->responseNotContains('files/color/bartik-'); + $this->assertNoRaw('files/color/bartik-'); $this->assertRaw('bartik/logo.svg'); // Log in and set the color scheme to 'slate'. @@ -214,7 +214,7 @@ function testOverrideAndResetScheme() { $this->drupalLogout(); $this->drupalGet(''); $this->assertRaw('files/color/bartik-'); - $this->assertSession()->responseNotContains('bartik/logo.svg'); + $this->assertNoRaw('bartik/logo.svg'); // Log in and set the color scheme back to default (delete config). $this->drupalLogin($this->bigUser); @@ -224,7 +224,7 @@ function testOverrideAndResetScheme() { // Log out and ensure there is no color and we have the original logo. $this->drupalLogout(); $this->drupalGet(''); - $this->assertSession()->responseNotContains('files/color/bartik-'); + $this->assertNoRaw('files/color/bartik-'); $this->assertRaw('bartik/logo.svg'); } diff --git a/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php b/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php index ebd4850..44e2e6b 100644 --- a/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php +++ b/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php @@ -204,6 +204,71 @@ protected function assertNoRaw($raw) { } /** + * Triggers a pass if the Perl regex pattern is found in the raw content. + * + * @param string $pattern + * Perl regex to look for including the regex delimiters. + * + * @deprecated Scheduled for removal in Drupal 9.0.0. + * Use $this->assertSession()->responseMatches() instead. + */ + protected function assertPattern($pattern) { + $this->assertSession()->responseMatches($pattern); + } + + /** + * Passes if the text is found ONLY ONCE on the text version of the page. + * + * The text version is the equivalent of what a user would see when viewing + * through a web browser. In other words the HTML has been filtered out of + * the contents. + * + * @param string|\Drupal\Component\Render\MarkupInterface $text + * Plain text to look for. + */ + protected function assertUniqueText($text) { + $this->assertUniqueTextHelper($text, TRUE); + } + + /** + * Passes if the text is found MORE THAN ONCE on the text version of the page. + * + * The text version is the equivalent of what a user would see when viewing + * through a web browser. In other words the HTML has been filtered out of + * the contents. + * + * @param string|\Drupal\Component\Render\MarkupInterface $text + * Plain text to look for. + */ + protected function assertNoUniqueText($text) { + $this->assertUniqueTextHelper($text, FALSE); + } + + /** + * Helper for assertUniqueText and assertNoUniqueText. + * + * It is not recommended to call this function directly. + * + * @param string|\Drupal\Component\Render\MarkupInterface $text + * Plain text to look for. + * @param bool $be_unique + * (optional) TRUE if this text should be found only once, FALSE if it + * should be found more than once. Defaults to FALSE. + */ + protected function assertUniqueTextHelper($text, $be_unique = FALSE) { + // Cast MarkupInterface objects to string. + $text = (string) $text; + $message = '"' . $text . '"' . ($be_unique ? ' found only once' : ' found more than once'); + $first_occurrence = strpos($this->getTextContent(), $text); + if ($first_occurrence === FALSE) { + $this->assertTrue(FALSE, $message); + } + $offset = $first_occurrence + strlen($text); + $second_occurrence = strpos($this->getTextContent(), $text, $offset); + $this->assertTrue($be_unique == ($second_occurrence === FALSE), $message); + } + + /** * Pass if the page title is the given string. * * @param string $expected_title diff --git a/core/tests/Drupal/Tests/BrowserTestBase.php b/core/tests/Drupal/Tests/BrowserTestBase.php index 3beb32f..d2f5100 100644 --- a/core/tests/Drupal/Tests/BrowserTestBase.php +++ b/core/tests/Drupal/Tests/BrowserTestBase.php @@ -7,6 +7,7 @@ use Behat\Mink\Mink; use Behat\Mink\Session; use Drupal\Component\FileCache\FileCacheFactory; +use Drupal\Component\Serialization\Json; use Drupal\Component\Serialization\Yaml; use Drupal\Component\Utility\Html; use Drupal\Component\Utility\SafeMarkup; @@ -232,6 +233,15 @@ protected $preserveGlobalState = FALSE; /** + * The drupalSettings value from the current raw $content. + * + * drupalSettings refers to the drupalSettings JavaScript variable. + * + * @var array + */ + protected $drupalSettings; + + /** * Class name for HTML output logging. * * @var string @@ -637,7 +647,11 @@ protected function drupalGet($path, array $options = array()) { $this->prepareRequest(); $session->visit($url); $out = $session->getPage()->getContent(); - + // Store the drupalSettings JavaScript variable. + $this->drupalSettings = []; + if (preg_match('@@', $out, $matches)) { + $this->drupalSettings = Json::decode($matches[1]); + } // Ensure that any changes to variables in the other thread are picked up. $this->refreshVariables(); @@ -941,11 +955,15 @@ protected function submitForm(array $edit, $submit, $form_html_id = NULL) { // Submit form. $this->prepareRequest(); $submit_button->press(); - + // Store the drupalSettings JavaScript variable. + $out = $this->getSession()->getPage()->getContent(); + $this->drupalSettings = []; + if (preg_match('@@', $out, $matches)) { + $this->drupalSettings = Json::decode($matches[1]); + } // Ensure that any changes to variables in the other thread are picked up. $this->refreshVariables(); if ($this->htmlOutputEnabled) { - $out = $this->getSession()->getPage()->getContent(); $html_output = 'POST request to: ' . $action . '
Ending URL: ' . $this->getSession()->getCurrentUrl(); $html_output .= '
' . $out; @@ -1613,6 +1631,30 @@ public function __sleep() { } /** + * Gets the value of drupalSettings for the currently-loaded page. + * + * The drupalSettings refers to the drupalSettings JavaScript variable. + * + * @return array + * The drupalSettings value from the current page. + */ + protected function getDrupalSettings() { + return $this->drupalSettings; + } + + /** + * Sets the value of drupalSettings for the currently-loaded page. + * + * The drupalSettings refers to the drupalSettings JavaScript variable. + * + * @param array $settings + * The drupalSettings JavaScript variable. + */ + protected function setDrupalSettings($settings) { + $this->drupalSettings = $settings; + } + + /** * Logs a HTML output message in a text file. * * The link to the HTML output message will be printed by the results printer.