diff --git a/core/modules/color/tests/src/Functional/ColorTest.php b/core/modules/color/tests/src/Functional/ColorTest.php index bb7d6fa..dc44566 100644 --- a/core/modules/color/tests/src/Functional/ColorTest.php +++ b/core/modules/color/tests/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->assertSession()->assertUniqueText('Color set'); + $this->assertUniqueText('Color set'); $edit['scheme'] = ''; $edit[$test_values['palette_input']] = '#123456'; $this->drupalPostForm($settings_path, $edit, t('Save configuration')); @@ -116,9 +116,9 @@ 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)) . '|', 'Make sure the color stylesheet is included in the content. (' . $theme . ')'); $stylesheet_content = join("\n", file($stylesheet)); - $this->assertTrue(strpos($stylesheet_content, 'color: #123456') !== FALSE); + $this->assertTrue(strpos($stylesheet_content, 'color: #123456') !== FALSE, 'Make sure the color we changed is in the color stylesheet. (' . $theme . ')'); } $this->drupalGet($settings_path); @@ -130,7 +130,7 @@ function _testColor($theme, $test_values) { $stylesheets = $this->config('color.theme.' . $theme)->get('stylesheets'); foreach ($stylesheets as $stylesheet) { $stylesheet_content = join("\n", file($stylesheet)); - $this->assertTrue(strpos($stylesheet_content, 'color: ' . $test_values['scheme_color']) !== FALSE); + $this->assertTrue(strpos($stylesheet_content, 'color: ' . $test_values['scheme_color']) !== FALSE, 'Make sure the color we changed is in the color stylesheet. (' . $theme . ')'); } // Test with aggregated CSS turned on. @@ -143,7 +143,7 @@ function _testColor($theme, $test_values) { foreach ($stylesheets as $uri) { $stylesheet_content .= join("\n", file(drupal_realpath($uri))); } - $this->assertTrue(strpos($stylesheet_content, 'public://') === FALSE); + $this->assertTrue(strpos($stylesheet_content, 'public://') === FALSE, 'Make sure the color paths have been translated to local paths. (' . $theme . ')'); $config->set('css.preprocess', 0); $config->save(); } @@ -202,8 +202,8 @@ function testOverrideAndResetScheme() { $this->drupalPlaceBlock('system_branding_block', ['region' => 'header']); $this->drupalGet(''); - $this->assertNoRaw('files/color/bartik-'); - $this->assertRaw('bartik/logo.svg'); + $this->assertNoRaw('files/color/bartik-', 'Make sure the color logo is not being used.'); + $this->assertRaw('bartik/logo.svg', 'Make sure the original bartik logo exists.'); // Log in and set the color scheme to 'slate'. $this->drupalLogin($this->bigUser); @@ -213,8 +213,8 @@ function testOverrideAndResetScheme() { // Visit the homepage and ensure color changes. $this->drupalLogout(); $this->drupalGet(''); - $this->assertRaw('files/color/bartik-'); - $this->assertNoRaw('bartik/logo.svg'); + $this->assertRaw('files/color/bartik-', 'Make sure the color logo is being used.'); + $this->assertNoRaw('bartik/logo.svg', 'Make sure the original bartik logo does not exist.'); // Log in and set the color scheme back to default (delete config). $this->drupalLogin($this->bigUser); @@ -224,8 +224,8 @@ function testOverrideAndResetScheme() { // Log out and ensure there is no color and we have the original logo. $this->drupalLogout(); $this->drupalGet(''); - $this->assertNoRaw('files/color/bartik-'); - $this->assertRaw('bartik/logo.svg'); + $this->assertNoRaw('files/color/bartik-', 'Make sure the color logo is not being used.'); + $this->assertRaw('bartik/logo.svg', 'Make sure the original bartik logo exists.'); } } diff --git a/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php b/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php index 6ca9f85..700af58 100644 --- a/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php +++ b/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php @@ -204,6 +204,76 @@ protected function assertNoRaw($raw) { } /** + * 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. + * @param string $message + * (optional) A message to display with the assertion. Do not translate + * messages with t(). If left blank, a default message will be displayed. + * + * @deprecated Scheduled for removal in Drupal 9.0.0. + * Use $this->getSession()->getPage()->getText() and substr_count() instead. + */ + protected function assertUniqueText($text, $message = NULL) { + // Cast MarkupInterface objects to string. + $text = (string) $text; + + $message = $message ?: "'$text' found only once on the page"; + $page_text = $this->getSession()->getPage()->getText(); + $occurrences = substr_count($page_text, $text); + $this->assertSame(1, $occurrences, $message); + } + + /** + * 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. + * @param string $message + * (optional) A message to display with the assertion. Do not translate + * messages with t(). If left blank, a default message will be displayed. + * + * @deprecated Scheduled for removal in Drupal 9.0.0. + * Use $this->getSession()->getPage()->getText() and substr_count() instead. + */ + protected function assertNoUniqueText($text, $message = '') { + // Cast MarkupInterface objects to string. + $text = (string) $text; + + $message = $message ?: "'$text' found more than once on the page"; + $page_text = $this->getSession()->getPage()->getText(); + $occurrences = substr_count($page_text, $text); + $this->assertGreaterThan(1, $occurrences, $message); + } + + /** + * 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. + * @param string $message + * (optional) A message to display with the assertion. Do not translate + * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed + * variables in the message text, not t(). If left blank, a default message + * will be displayed. + * + * @deprecated Scheduled for removal in Drupal 9.0.0. + * Use $this->assertSession()->responseMatches() instead. + */ + protected function assertPattern($pattern, $message = '') { + $this->assertSession()->responseMatches($pattern); + } + + /** * Pass if the page title is the given string. * * @param string $expected_title diff --git a/core/tests/Drupal/Tests/WebAssert.php b/core/tests/Drupal/Tests/WebAssert.php index cc801bd..6156dda 100644 --- a/core/tests/Drupal/Tests/WebAssert.php +++ b/core/tests/Drupal/Tests/WebAssert.php @@ -422,55 +422,4 @@ public function fieldDisabled($field, TraversableElement $container = NULL) { return $node; } - /** - * 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. - */ - public 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. - */ - public 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'); - $content = $this->session->getPage()->getContent(); - $first_occurrence = strpos($content, $text); - $this->assert($first_occurrence !== FALSE, $message); - $offset = $first_occurrence + strlen($text); - $second_occurrence = strpos($content, $text, $offset); - $this->assert($be_unique === ($second_occurrence === FALSE), $message); - } - }