Issue #3175718 by mondrake, alexpott, jungle, longwave: Random fails due to drupal-settings-json being counted as page text

merge-requests/826/head
mondrake 2021-06-21 20:32:27 +00:00 committed by larowlan
parent 0211e1ebee
commit 49eb106bca
5 changed files with 90 additions and 2 deletions

View File

@ -313,7 +313,7 @@ class UpdatePathTestBaseFilledTest extends UpdatePathTestBaseTest {
$this->drupalGet('admin/config/system/actions');
$this->assertSession()->pageTextContains('Test action');
$this->drupalGet('admin/config/system/actions/configure/test_action');
$this->assertSession()->pageTextContains('test_action');
$this->assertSession()->fieldValueEquals('id', 'test_action');
$this->assertRaw('drupal.org');
// Make sure our ban still exists.

View File

@ -207,7 +207,7 @@ class TourTest extends TourTestBasic {
$this->assertCount(1, $elements, 'Found code tip was weighted last and had "End tour".');
// Test hook_tour_alter().
$this->assertSession()->pageTextContains('Altered by hook_tour_tips_alter');
$this->assertSession()->responseContains('Altered by hook_tour_tips_alter');
// Navigate to tour-test-3 and verify the tour_test_1 tip is found with
// appropriate classes.

View File

@ -57,6 +57,9 @@ class BrowserTestBaseTest extends BrowserTestBase {
$text = $this->getTextContent();
$this->assertStringContainsString('Test page text.', $text);
$this->assertStringNotContainsString('</html>', $text);
// Ensure Drupal Javascript settings are not part of the page text.
$this->assertArrayHasKey('currentPathIsAdmin', $this->getDrupalSettings()['path']);
$this->assertStringNotContainsString('currentPathIsAdmin', $text);
// Response includes cache tags that we can assert.
$this->assertSession()->responseHeaderExists('X-Drupal-Cache-Tags');

View File

@ -0,0 +1,84 @@
<?php
namespace Drupal\Tests;
use Behat\Mink\Driver\BrowserKitDriver;
use Behat\Mink\Element\TraversableElement;
/**
* Document element.
*
* This is largely a copy of \Behat\Mink\Element\DocumentElement. This fixes the
* ::getText() method to remove script tags inside the body element.
*
* @see \Behat\Mink\Element\DocumentElement
* @internal
*/
class DocumentElement extends TraversableElement {
/**
* Returns XPath for handled element.
*
* @return string
*/
public function getXpath() {
return '//html';
}
/**
* Returns document content.
*
* @return string
*/
public function getContent() {
return trim($this->getDriver()->getContent());
}
/**
* Check whether document has specified content.
*
* @param string $content
*
* @return bool
*/
public function hasContent($content) {
return $this->has('named', ['content', $content]);
}
/**
* {@inheritdoc}
*/
public function getText() {
if ($this->getDriver() instanceof BrowserKitDriver) {
// Work around https://github.com/minkphp/MinkBrowserKitDriver/issues/153.
// To simulate what the user sees, it removes:
// - all text inside the head tags
// - Drupal settings json.
$raw_content = preg_replace([
'@<head>(.+?)</head>@si',
'@<script type="application/json" data-drupal-selector="drupal-settings-json">([^<]*)</script>@',
], '', $this->getContent());
// Filter out all HTML tags, as they are not visible in a normal browser.
$text = strip_tags($raw_content);
// To preserve BC and match \Behat\Mink\Element\Element::getText() include
// the page title.
$title_element = $this->find('css', 'title');
if ($title_element) {
$text = $title_element->getText() . ' ' . $text;
}
// To match what the user sees and \Behat\Mink\Element\Element::getText()
// decode HTML entities.
$text = html_entity_decode($text, ENT_QUOTES);
// To match \Behat\Mink\Element\Element::getText() remove new lines and
// normalize spaces.
$text = str_replace("\n", ' ', $text);
$text = preg_replace('/ {2,}/', ' ', $text);
return trim($text);
}
// If using a real browser fallback to the \Behat\Mink\Element\Element
// implementation.
return parent::getText();
}
}

View File

@ -154,6 +154,7 @@ function drupal_phpunit_populate_class_loader() {
// Do class loader population.
$loader = drupal_phpunit_populate_class_loader();
class_alias('\Drupal\Tests\DocumentElement', '\Behat\Mink\Element\DocumentElement', TRUE);
ClassWriter::mutateTestBase($loader);