From d52fc7488aed8ff8191fa08c2ec9539e20abb1d6 Mon Sep 17 00:00:00 2001 From: webchick <drupal@webchick.net> Date: Thu, 6 Aug 2015 11:15:21 -0700 Subject: [PATCH] Issue #2546582 by alexpott, dawehner: AssertContentTrait::getTextContent() includes the @import css statement, that is nuts --- .../simpletest/src/AssertContentTrait.php | 5 +- .../tests/src/Unit/AssertContentTraitTest.php | 54 +++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 core/modules/simpletest/tests/src/Unit/AssertContentTraitTest.php diff --git a/core/modules/simpletest/src/AssertContentTrait.php b/core/modules/simpletest/src/AssertContentTrait.php index a425f4f954c6..b0efbe5cc4a5 100644 --- a/core/modules/simpletest/src/AssertContentTrait.php +++ b/core/modules/simpletest/src/AssertContentTrait.php @@ -76,7 +76,10 @@ protected function setRawContent($content) { */ protected function getTextContent() { if (!isset($this->plainTextContent)) { - $this->plainTextContent = Xss::filter($this->getRawContent(), array()); + $raw_content = $this->getRawContent(); + // Strip everything between the HEAD tags. + $raw_content = preg_replace('@<head>(.+?)</head>@si', '', $raw_content); + $this->plainTextContent = Xss::filter($raw_content, array()); } return $this->plainTextContent; } diff --git a/core/modules/simpletest/tests/src/Unit/AssertContentTraitTest.php b/core/modules/simpletest/tests/src/Unit/AssertContentTraitTest.php new file mode 100644 index 000000000000..505a3b91ff42 --- /dev/null +++ b/core/modules/simpletest/tests/src/Unit/AssertContentTraitTest.php @@ -0,0 +1,54 @@ +<?php + +/** + * @file + * Contains \Drupal\Tests\simpletest\Unit\AssertContentTraitTest. + */ + +namespace Drupal\Tests\simpletest\Unit; + +use Drupal\simpletest\AssertContentTrait; +use Drupal\Tests\UnitTestCase; + +/** + * @coversDefaultClass \Drupal\simpletest\AssertContentTrait + * @group simpletest + */ +class AssertContentTraitTest extends UnitTestCase { + + /** + * @covers ::getTextContent + */ + public function testGetTextContent() { + $test = new TestClass(); + $raw_content = <<<EOT + +<Head> +<style> +@import url("foo.css"); +</style> +</head> +<body> +bar +</body> +EOT; + $test->_setRawContent($raw_content); + $this->assertNotContains('foo', $test->_getTextContent()); + $this->assertNotContains('<body>', $test->_getTextContent()); + $this->assertContains('bar', $test->_getTextContent()); + } + +} + +class TestClass { + use AssertContentTrait; + + public function _setRawContent($content) { + $this->setRawContent($content); + } + + public function _getTextContent() { + return $this->getTextContent(); + } + +} -- GitLab