Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
In WTB, assertText() is case sensitive:
\Drupal\simpletest\AssertContentTrait
protected function assertTextHelper($text, $message = '', $group = 'Other', $not_exists = TRUE) {
if (!$message) {
$message = !$not_exists ? SafeMarkup::format('"@text" found', array('@text' => $text)) : SafeMarkup::format('"@text" not found', array('@text' => $text));
}
return $this->assert($not_exists == (strpos($this->getTextContent(), (string) $text) === FALSE), $message, $group);
}
Note the strpos in the actual assert().
In BTB, assertText is case sensitive via the trait
protected function assertTextHelper($text, $not_exists = TRUE) {
$args = ['@text' => $text];
$message = $not_exists ? new FormattableMarkup('"@text" not found', $args) : new FormattableMarkup('"@text" found', $args);
$raw_content = $this->getSession()->getPage()->getContent();
// Trying to simulate what the user sees, given that it removes all text
// inside the head tags, removes inline Javascript, fix all HTML entities,
// removes dangerous protocols and filtering out all HTML tags, as they are
// not visible in a normal browser.
$raw_content = preg_replace('@<head>(.+?)</head>@si', '', $raw_content);
$page_text = Xss::filter($raw_content, []);
$actual = $not_exists == (strpos($page_text, (string) $text) === FALSE);
$this->assertTrue($actual, $message);
return $actual;
}
The BC recommends calls \Behat\Mink\WebAssert
public function pageTextContains($text)
{
$actual = $this->session->getPage()->getText();
$actual = preg_replace('/\s+/u', ' ', $actual);
$regex = '/'.preg_quote($text, '/').'/ui';
$message = sprintf('The text "%s" was not found anywhere in the text of the current page.', $text);
$this->assertResponseText((bool) preg_match($regex, $actual), $message);
}
Note the 'ui' modifier on the regex, so this is case insensitive.
If this is true, what does this mean for WTB to BTB conversions or our current BC layer?
Comment | File | Size | Author |
---|---|---|---|
#3 | 2849753-03.patch | 217.27 KB | mpdonadio |
Comments
Comment #2
mpdonadioComment #3
mpdonadioHere is the patch from #2735005: Convert all Simpletest web tests to BrowserTestBase (or UnitTestBase/KernelTestBase), with
<?pre
diff --git a/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php b/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php
index 5e9caf4..b9995ce 100644
--- a/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php
+++ b/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php
@@ -74,7 +74,7 @@ protected function assertText($text) {
$this->assertSession()->responseContains($text);
}
else {
- $this->assertTextHelper($text, FALSE);
+ $this->assertSession()->pageTextContains($text);
}
}
?>
Just to see what happens.
Comment #4
mpdonadioKeep forgetting to tag issues.
Comment #6
michielnugter CreditAttribution: michielnugter as a volunteer and at Synetic commented