Issue #2551335 by alexpott, Wim Leers: Remove unnecessary SafeMarkup usage in tests and clear the static cache before every run

8.0.x
Nathaniel Catchpole 2015-08-18 10:32:20 +01:00
parent 20164699ba
commit 3d44e9ae80
21 changed files with 125 additions and 124 deletions

View File

@ -7,7 +7,6 @@
namespace Drupal\Tests\filter\Unit; namespace Drupal\Tests\filter\Unit;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
/** /**
@ -160,10 +159,7 @@ class FilterUninstallValidatorTest extends UnitTestCase {
]); ]);
$expected = [ $expected = [
SafeMarkup::format('Provides a filter plugin that is in use in the following filter formats: %formats', ['%formats' => implode(', ', [ 'Provides a filter plugin that is in use in the following filter formats: <em class="placeholder">Filter Format 1 Label, Filter Format 2 Label</em>'
'Filter Format 1 Label',
'Filter Format 2 Label',
])]),
]; ];
$reasons = $this->filterUninstallValidator->validate($this->randomMachineName()); $reasons = $this->filterUninstallValidator->validate($this->randomMachineName());
$this->assertSame($expected, $reasons); $this->assertSame($expected, $reasons);

View File

@ -7,7 +7,6 @@
namespace Drupal\Tests\forum\Unit; namespace Drupal\Tests\forum\Unit;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
/** /**
@ -127,10 +126,7 @@ class ForumUninstallValidatorTest extends UnitTestCase {
$module = 'forum'; $module = 'forum';
$expected = [ $expected = [
'To uninstall Forum, first delete all <em>Forum</em> content', 'To uninstall Forum, first delete all <em>Forum</em> content',
SafeMarkup::format('To uninstall Forum, first delete all <a href="@url">%vocabulary</a> terms', [ 'To uninstall Forum, first delete all <a href="/path/to/vocabulary/overview"><em class="placeholder">Vocabulary label</em></a> terms',
'@url' => '/path/to/vocabulary/overview',
'%vocabulary' => 'Vocabulary label',
]),
]; ];
$reasons = $this->forumUninstallValidator->validate($module); $reasons = $this->forumUninstallValidator->validate($module);
$this->assertSame($expected, $reasons); $this->assertSame($expected, $reasons);
@ -164,9 +160,7 @@ class ForumUninstallValidatorTest extends UnitTestCase {
$module = 'forum'; $module = 'forum';
$expected = [ $expected = [
'To uninstall Forum, first delete all <em>Forum</em> content', 'To uninstall Forum, first delete all <em>Forum</em> content',
SafeMarkup::format('To uninstall Forum, first delete all %vocabulary terms', [ 'To uninstall Forum, first delete all <em class="placeholder">Vocabulary label</em> terms',
'%vocabulary' => 'Vocabulary label',
]),
]; ];
$reasons = $this->forumUninstallValidator->validate($module); $reasons = $this->forumUninstallValidator->validate($module);
$this->assertSame($expected, $reasons); $this->assertSame($expected, $reasons);
@ -200,10 +194,7 @@ class ForumUninstallValidatorTest extends UnitTestCase {
$module = 'forum'; $module = 'forum';
$expected = [ $expected = [
SafeMarkup::format('To uninstall Forum, first delete all <a href="@url">%vocabulary</a> terms', [ 'To uninstall Forum, first delete all <a href="/path/to/vocabulary/overview"><em class="placeholder">Vocabulary label</em></a> terms',
'@url' => '/path/to/vocabulary/overview',
'%vocabulary' => 'Vocabulary label',
]),
]; ];
$reasons = $this->forumUninstallValidator->validate($module); $reasons = $this->forumUninstallValidator->validate($module);
$this->assertSame($expected, $reasons); $this->assertSame($expected, $reasons);
@ -236,9 +227,7 @@ class ForumUninstallValidatorTest extends UnitTestCase {
$module = 'forum'; $module = 'forum';
$expected = [ $expected = [
SafeMarkup::format('To uninstall Forum, first delete all %vocabulary terms', [ 'To uninstall Forum, first delete all <em class="placeholder">Vocabulary label</em> terms',
'%vocabulary' => 'Vocabulary label',
]),
]; ];
$reasons = $this->forumUninstallValidator->validate($module); $reasons = $this->forumUninstallValidator->validate($module);
$this->assertSame($expected, $reasons); $this->assertSame($expected, $reasons);

View File

@ -7,7 +7,6 @@
namespace Drupal\Tests\user\Unit; namespace Drupal\Tests\user\Unit;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Access\AccessResult; use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\Context\CacheContextsManager; use Drupal\Core\Cache\Context\CacheContextsManager;
use Drupal\Core\DependencyInjection\Container; use Drupal\Core\DependencyInjection\Container;
@ -133,13 +132,8 @@ class UserAccessControlHandlerTest extends UnitTestCase {
->will($this->returnValue($this->{$target})); ->will($this->returnValue($this->{$target}));
foreach (array('view' => $view, 'edit' => $edit) as $operation => $result) { foreach (array('view' => $view, 'edit' => $edit) as $operation => $result) {
$message = SafeMarkup::format("User @field field access returns @result with operation '@op' for @account accessing @target", array( $result_text = !isset($result) ? 'null' : ($result ? 'true' : 'false');
'@field' => $field, $message = "User '$field' field access returns '$result_text' with operation '$operation' for '$viewer' accessing '$target'";
'@result' => !isset($result) ? 'null' : ($result ? 'true' : 'false'),
'@op' => $operation,
'@account' => $viewer,
'@target' => $target,
));
$this->assertSame($result, $this->accessControlHandler->fieldAccess($operation, $field_definition, $this->{$viewer}, $this->items), $message); $this->assertSame($result, $this->accessControlHandler->fieldAccess($operation, $field_definition, $this->{$viewer}, $this->items), $message);
} }
} }

View File

@ -930,13 +930,10 @@ class TestEntityType extends EntityType {
} }
namespace { namespace {
use Drupal\Component\Utility\SafeMarkup;
if (!function_exists('t')) { if (!function_exists('t')) {
function t($string, array $args = []) { function t($string, array $args = []) {
return SafeMarkup::format($string, $args); return strtr($string, $args);
} }
} }
} }

View File

@ -7,7 +7,6 @@
namespace Drupal\Tests\views\Unit\Plugin\field; namespace Drupal\Tests\views\Unit\Plugin\field;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
use Drupal\views\Entity\View; use Drupal\views\Entity\View;
use Drupal\views\Plugin\views\field\Counter; use Drupal\views\Plugin\views\field\Counter;
@ -125,15 +124,9 @@ class CounterTest extends UnitTestCase {
$expected = $i + 1; $expected = $i + 1;
$counter = $counter_handler->getValue($this->testData[$i]); $counter = $counter_handler->getValue($this->testData[$i]);
$this->assertEquals($expected, $counter, SafeMarkup::format('The expected number (@expected) patches with the rendered number (@counter) failed', array( $this->assertEquals($expected, $counter, 'The expected number matches with the counter number');
'@expected' => $expected,
'@counter' => $counter
)));
$counter = $this->renderCounter($counter_handler, $this->testData[$i]); $counter = $this->renderCounter($counter_handler, $this->testData[$i]);
$this->assertEquals($expected, $counter, SafeMarkup::format('The expected number (@expected) patches with the rendered number (@counter) failed', array( $this->assertEquals($expected, $counter, 'The expected number matches with the rendered number');
'@expected' => $expected,
'@counter' => $counter
)));
} }
/** /**
@ -157,15 +150,9 @@ class CounterTest extends UnitTestCase {
$expected = $rand_start + $i; $expected = $rand_start + $i;
$counter = $counter_handler->getValue($this->testData[$i]); $counter = $counter_handler->getValue($this->testData[$i]);
$this->assertEquals($expected, $counter, SafeMarkup::format('The expected number (@expected) patches with the rendered number (@counter) failed', array( $this->assertEquals($expected, $counter, 'The expected number matches with the counter number');
'@expected' => $expected,
'@counter' => $counter
)));
$counter = $this->renderCounter($counter_handler, $this->testData[$i]); $counter = $this->renderCounter($counter_handler, $this->testData[$i]);
$this->assertEquals($expected, $counter, SafeMarkup::format('The expected number (@expected) patches with the rendered number (@counter) failed', array( $this->assertEquals($expected, $counter, 'The expected number matches with the rendered number');
'@expected' => $expected,
'@counter' => $counter
)));
} }
/** /**
@ -192,15 +179,9 @@ class CounterTest extends UnitTestCase {
$expected = $offset + $rand_start + $i; $expected = $offset + $rand_start + $i;
$counter = $counter_handler->getValue($this->testData[$i]); $counter = $counter_handler->getValue($this->testData[$i]);
$this->assertEquals($expected, $counter, SafeMarkup::format('The expected number (@expected) patches with the rendered number (@counter) failed', array( $this->assertEquals($expected, $counter, 'The expected number matches with the counter number');
'@expected' => $expected,
'@counter' => $counter
)));
$counter = $this->renderCounter($counter_handler, $this->testData[$i]); $counter = $this->renderCounter($counter_handler, $this->testData[$i]);
$this->assertEquals($expected, $counter, SafeMarkup::format('The expected number (@expected) patches with the rendered number (@counter) failed', array( $this->assertEquals($expected, $counter, 'The expected number matches with the rendered number');
'@expected' => $expected,
'@counter' => $counter
)));
} }
/** /**
@ -231,15 +212,9 @@ class CounterTest extends UnitTestCase {
$expected = $items_per_page + $offset + $rand_start + $i; $expected = $items_per_page + $offset + $rand_start + $i;
$counter = $counter_handler->getValue($this->testData[$i]); $counter = $counter_handler->getValue($this->testData[$i]);
$this->assertEquals($expected, $counter, SafeMarkup::format('The expected number (@expected) patches with the rendered number (@counter) failed', array( $this->assertEquals($expected, $counter, 'The expected number matches with the counter number');
'@expected' => $expected,
'@counter' => $counter
)));
$counter = $this->renderCounter($counter_handler, $this->testData[$i]); $counter = $this->renderCounter($counter_handler, $this->testData[$i]);
$this->assertEquals($expected, $counter, SafeMarkup::format('The expected number (@expected) patches with the rendered number (@counter) failed', array( $this->assertEquals($expected, $counter, 'The expected number matches with the rendered number');
'@expected' => $expected,
'@counter' => $counter
)));
} }
/** /**

View File

@ -7,7 +7,6 @@
namespace Drupal\Tests\views\Unit; namespace Drupal\Tests\views\Unit;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
use Drupal\views\ViewsDataHelper; use Drupal\views\ViewsDataHelper;
use Drupal\views\Tests\ViewTestData; use Drupal\views\Tests\ViewTestData;
@ -106,7 +105,7 @@ class ViewsDataHelperTest extends UnitTestCase {
array_walk($expected_keys, function(&$item) { array_walk($expected_keys, function(&$item) {
$item = "views_test_data.$item"; $item = "views_test_data.$item";
}); });
$this->assertEquals($expected_keys, array_keys($fields), SafeMarkup::format('Handlers of type @handler_type are not listed as expected.', array('@handler_type' => $handler_type))); $this->assertEquals($expected_keys, array_keys($fields), "Handlers of type $handler_type are not listed as expected");
} }
// Check for subtype filtering, so header and footer. // Check for subtype filtering, so header and footer.
@ -117,7 +116,7 @@ class ViewsDataHelperTest extends UnitTestCase {
array_walk($expected_keys, function(&$item) { array_walk($expected_keys, function(&$item) {
$item = "views_test_data.$item"; $item = "views_test_data.$item";
}); });
$this->assertEquals($expected_keys, array_keys($fields), SafeMarkup::format('Sub_type @sub_type is not filtered as expected.', array('@sub_type' => $sub_type))); $this->assertEquals($expected_keys, array_keys($fields), "Sub_type $sub_type is not filtered as expected.");
} }
} }

View File

@ -44,7 +44,9 @@
</testsuite> </testsuite>
</testsuites> </testsuites>
<listeners> <listeners>
<listener class="\Drupal\Tests\Standards\DrupalStandardsListener"> <listener class="\Drupal\Tests\Listeners\DrupalStandardsListener">
</listener>
<listener class="\Drupal\Tests\Listeners\SafeMarkupSideEffects">
</listener> </listener>
</listeners> </listeners>
<!-- Filter for coverage reports. --> <!-- Filter for coverage reports. -->

View File

@ -8,7 +8,6 @@
namespace Drupal\Tests\Component\Utility; namespace Drupal\Tests\Component\Utility;
use Drupal\Component\Utility\Random; use Drupal\Component\Utility\Random;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
/** /**
@ -39,7 +38,7 @@ class RandomTest extends UnitTestCase {
$random = new Random(); $random = new Random();
for ($i = 0; $i <= 50; $i++) { for ($i = 0; $i <= 50; $i++) {
$str = $random->string(1, TRUE); $str = $random->string(1, TRUE);
$this->assertFalse(isset($strings[$str]), SafeMarkup::format('Generated duplicate random string !string', array('!string' => $str))); $this->assertFalse(isset($strings[$str]), 'Generated duplicate random string ' . $str);
$strings[$str] = TRUE; $strings[$str] = TRUE;
} }
} }
@ -54,7 +53,7 @@ class RandomTest extends UnitTestCase {
$random = new Random(); $random = new Random();
for ($i = 0; $i <= 10; $i++) { for ($i = 0; $i <= 10; $i++) {
$str = $random->name(1, TRUE); $str = $random->name(1, TRUE);
$this->assertFalse(isset($names[$str]), SafeMarkup::format('Generated duplicate random name !name', array('!name' => $str))); $this->assertFalse(isset($names[$str]), 'Generated duplicate random name ' . $str);
$names[$str] = TRUE; $names[$str] = TRUE;
} }
} }

View File

@ -8,6 +8,7 @@
namespace Drupal\Tests\Component\Utility; namespace Drupal\Tests\Component\Utility;
use Drupal\Component\Utility\SafeMarkup; use Drupal\Component\Utility\SafeMarkup;
use Drupal\Component\Utility\SafeStringInterface;
use Drupal\Component\Utility\Xss; use Drupal\Component\Utility\Xss;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
@ -183,11 +184,11 @@ class SafeMarkupTest extends UnitTestCase {
function providerFormat() { function providerFormat() {
$tests[] = array('Simple text', array(), 'Simple text', 'SafeMarkup::format leaves simple text alone.', TRUE); $tests[] = array('Simple text', array(), 'Simple text', 'SafeMarkup::format leaves simple text alone.', TRUE);
$tests[] = array('Escaped text: @value', array('@value' => '<script>'), 'Escaped text: &lt;script&gt;', 'SafeMarkup::format replaces and escapes string.', TRUE); $tests[] = array('Escaped text: @value', array('@value' => '<script>'), 'Escaped text: &lt;script&gt;', 'SafeMarkup::format replaces and escapes string.', TRUE);
$tests[] = array('Escaped text: @value', array('@value' => SafeMarkup::set('<span>Safe HTML</span>')), 'Escaped text: <span>Safe HTML</span>', 'SafeMarkup::format does not escape an already safe string.', TRUE); $tests[] = array('Escaped text: @value', array('@value' => SafeMarkupTestSafeString::create('<span>Safe HTML</span>')), 'Escaped text: <span>Safe HTML</span>', 'SafeMarkup::format does not escape an already safe string.', TRUE);
$tests[] = array('Placeholder text: %value', array('%value' => '<script>'), 'Placeholder text: <em class="placeholder">&lt;script&gt;</em>', 'SafeMarkup::format replaces, escapes and themes string.', TRUE); $tests[] = array('Placeholder text: %value', array('%value' => '<script>'), 'Placeholder text: <em class="placeholder">&lt;script&gt;</em>', 'SafeMarkup::format replaces, escapes and themes string.', TRUE);
$tests[] = array('Placeholder text: %value', array('%value' => SafeMarkup::set('<span>Safe HTML</span>')), 'Placeholder text: <em class="placeholder"><span>Safe HTML</span></em>', 'SafeMarkup::format does not escape an already safe string themed as a placeholder.', TRUE); $tests[] = array('Placeholder text: %value', array('%value' => SafeMarkupTestSafeString::create('<span>Safe HTML</span>')), 'Placeholder text: <em class="placeholder"><span>Safe HTML</span></em>', 'SafeMarkup::format does not escape an already safe string themed as a placeholder.', TRUE);
$tests[] = array('Verbatim text: !value', array('!value' => '<script>'), 'Verbatim text: <script>', 'SafeMarkup::format replaces verbatim string as-is.', FALSE); $tests[] = array('Verbatim text: !value', array('!value' => '<script>'), 'Verbatim text: <script>', 'SafeMarkup::format replaces verbatim string as-is.', FALSE);
$tests[] = array('Verbatim text: !value', array('!value' => SafeMarkup::set('<span>Safe HTML</span>')), 'Verbatim text: <span>Safe HTML</span>', 'SafeMarkup::format replaces verbatim string as-is.', TRUE); $tests[] = array('Verbatim text: !value', array('!value' => SafeMarkupTestSafeString::create('<span>Safe HTML</span>')), 'Verbatim text: <span>Safe HTML</span>', 'SafeMarkup::format replaces verbatim string as-is.', TRUE);
return $tests; return $tests;
} }
@ -259,7 +260,7 @@ class SafeMarkupTest extends UnitTestCase {
// Subject unsafe. // Subject unsafe.
$tests[] = [ $tests[] = [
'<placeholder>', '<placeholder>',
SafeMarkup::set('foo'), SafeMarkupTestSafeString::create('foo'),
'<placeholder>bazqux', '<placeholder>bazqux',
'foobazqux', 'foobazqux',
FALSE, FALSE,
@ -268,8 +269,8 @@ class SafeMarkupTest extends UnitTestCase {
// All safe. // All safe.
$tests[] = [ $tests[] = [
'<placeholder>', '<placeholder>',
SafeMarkup::set('foo'), SafeMarkupTestSafeString::create('foo'),
SafeMarkup::set('<placeholder>barbaz'), SafeMarkupTestSafeString::create('<placeholder>barbaz'),
'foobarbaz', 'foobarbaz',
TRUE, TRUE,
]; ];
@ -279,7 +280,7 @@ class SafeMarkupTest extends UnitTestCase {
$tests[] = [ $tests[] = [
'<placeholder>', '<placeholder>',
'fubar', 'fubar',
SafeMarkup::set('<placeholder>barbaz'), SafeMarkupTestSafeString::create('<placeholder>barbaz'),
'fubarbarbaz', 'fubarbarbaz',
FALSE, FALSE,
]; ];
@ -287,8 +288,8 @@ class SafeMarkupTest extends UnitTestCase {
// Array with all safe. // Array with all safe.
$tests[] = [ $tests[] = [
['<placeholder1>', '<placeholder2>', '<placeholder3>'], ['<placeholder1>', '<placeholder2>', '<placeholder3>'],
[SafeMarkup::set('foo'), SafeMarkup::set('bar'), SafeMarkup::set('baz')], [SafeMarkupTestSafeString::create('foo'), SafeMarkupTestSafeString::create('bar'), SafeMarkupTestSafeString::create('baz')],
SafeMarkup::set('<placeholder1><placeholder2><placeholder3>'), SafeMarkupTestSafeString::create('<placeholder1><placeholder2><placeholder3>'),
'foobarbaz', 'foobarbaz',
TRUE, TRUE,
]; ];
@ -296,8 +297,8 @@ class SafeMarkupTest extends UnitTestCase {
// Array with unsafe replacement. // Array with unsafe replacement.
$tests[] = [ $tests[] = [
['<placeholder1>', '<placeholder2>', '<placeholder3>',], ['<placeholder1>', '<placeholder2>', '<placeholder3>',],
[SafeMarkup::set('bar'), SafeMarkup::set('baz'), 'qux'], [SafeMarkupTestSafeString::create('bar'), SafeMarkupTestSafeString::create('baz'), 'qux'],
SafeMarkup::set('<placeholder1><placeholder2><placeholder3>'), SafeMarkupTestSafeString::create('<placeholder1><placeholder2><placeholder3>'),
'barbazqux', 'barbazqux',
FALSE, FALSE,
]; ];
@ -320,3 +321,27 @@ class SafeMarkupTestString {
} }
} }
/**
* Marks text as safe.
*
* SafeMarkupTestSafeString is used to mark text as safe because
* SafeMarkup::set() is a global static that affects all tests.
*/
class SafeMarkupTestSafeString implements SafeStringInterface {
protected $string;
public function __construct($string) {
$this->string = $string;
}
public function __toString() {
return $this->string;
}
public static function create($string) {
$safe_string = new static($string);
return $safe_string;
}
}

View File

@ -8,7 +8,6 @@
namespace Drupal\Tests\Component\Utility; namespace Drupal\Tests\Component\Utility;
use Drupal\Component\Utility\UrlHelper; use Drupal\Component\Utility\UrlHelper;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
/** /**
@ -94,7 +93,7 @@ class UrlHelperTest extends UnitTestCase {
public function testValidAbsolute($url, $scheme) { public function testValidAbsolute($url, $scheme) {
$test_url = $scheme . '://' . $url; $test_url = $scheme . '://' . $url;
$valid_url = UrlHelper::isValid($test_url, TRUE); $valid_url = UrlHelper::isValid($test_url, TRUE);
$this->assertTrue($valid_url, SafeMarkup::format('@url is a valid URL.', array('@url' => $test_url))); $this->assertTrue($valid_url, $test_url . ' is a valid URL.');
} }
/** /**
@ -125,7 +124,7 @@ class UrlHelperTest extends UnitTestCase {
public function testInvalidAbsolute($url, $scheme) { public function testInvalidAbsolute($url, $scheme) {
$test_url = $scheme . '://' . $url; $test_url = $scheme . '://' . $url;
$valid_url = UrlHelper::isValid($test_url, TRUE); $valid_url = UrlHelper::isValid($test_url, TRUE);
$this->assertFalse($valid_url, SafeMarkup::format('@url is NOT a valid URL.', array('@url' => $test_url))); $this->assertFalse($valid_url, $test_url . ' is NOT a valid URL.');
} }
/** /**
@ -159,7 +158,7 @@ class UrlHelperTest extends UnitTestCase {
public function testValidRelative($url, $prefix) { public function testValidRelative($url, $prefix) {
$test_url = $prefix . $url; $test_url = $prefix . $url;
$valid_url = UrlHelper::isValid($test_url); $valid_url = UrlHelper::isValid($test_url);
$this->assertTrue($valid_url, SafeMarkup::format('@url is a valid URL.', array('@url' => $test_url))); $this->assertTrue($valid_url, $test_url . ' is a valid URL.');
} }
/** /**
@ -190,7 +189,7 @@ class UrlHelperTest extends UnitTestCase {
public function testInvalidRelative($url, $prefix) { public function testInvalidRelative($url, $prefix) {
$test_url = $prefix . $url; $test_url = $prefix . $url;
$valid_url = UrlHelper::isValid($test_url); $valid_url = UrlHelper::isValid($test_url);
$this->assertFalse($valid_url, SafeMarkup::format('@url is NOT a valid URL.', array('@url' => $test_url))); $this->assertFalse($valid_url, $test_url . ' is NOT a valid URL.');
} }
/** /**

View File

@ -7,7 +7,6 @@
namespace Drupal\Tests\Core\Controller; namespace Drupal\Tests\Core\Controller;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Controller\TitleResolver; use Drupal\Core\Controller\TitleResolver;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\ParameterBag; use Symfony\Component\HttpFoundation\ParameterBag;
@ -151,7 +150,7 @@ class TitleCallback {
* Returns the example string. * Returns the example string.
*/ */
public function example($value) { public function example($value) {
return SafeMarkup::format('test @value', array('@value' => $value)); return 'test ' . $value;
} }
} }

View File

@ -7,7 +7,6 @@
namespace Drupal\Tests\Core\Extension; namespace Drupal\Tests\Core\Extension;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
/** /**
@ -72,7 +71,7 @@ class RequiredModuleUninstallValidatorTest extends UnitTestCase {
->method('getModuleInfoByModule') ->method('getModuleInfoByModule')
->willReturn(['required' => TRUE, 'name' => $module]); ->willReturn(['required' => TRUE, 'name' => $module]);
$expected = [SafeMarkup::format('The @module module is required', ['@module' => $module])]; $expected = ["The $module module is required"];
$reasons = $this->uninstallValidator->validate($module); $reasons = $this->uninstallValidator->validate($module);
$this->assertSame($expected, $reasons); $this->assertSame($expected, $reasons);
} }

View File

@ -427,9 +427,9 @@ class FormCacheTest extends UnitTestCase {
* @covers ::setCache * @covers ::setCache
*/ */
public function testSetCacheWithSafeStrings() { public function testSetCacheWithSafeStrings() {
// A call to SafeMarkup::set() is appropriate in this test as a way to add a // A call to SafeMarkup::format() is appropriate in this test as a way to
// string to the safe list in the simplest way possible. Normally, avoid it. // add a string to the safe list in the simplest way possible.
SafeMarkup::set('a_safe_string'); SafeMarkup::format('@value', ['@value' => 'a_safe_string']);
$form_build_id = 'the_form_build_id'; $form_build_id = 'the_form_build_id';
$form = [ $form = [
'#form_id' => 'the_form_id' '#form_id' => 'the_form_id'

View File

@ -7,7 +7,6 @@
namespace Drupal\Tests\Core\Form; namespace Drupal\Tests\Core\Form;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Form\FormState; use Drupal\Core\Form\FormState;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
@ -468,7 +467,7 @@ class FormValidatorTest extends UnitTestCase {
'#maxlength' => 7, '#maxlength' => 7,
'#value' => $this->randomMachineName(8), '#value' => $this->randomMachineName(8),
), ),
SafeMarkup::format('!name cannot be longer than %max characters but is currently %length characters long.', array('!name' => 'Test', '%max' => '7', '%length' => 8)), 'Test cannot be longer than <em class="placeholder">7</em> characters but is currently <em class="placeholder">8</em> characters long.',
FALSE, FALSE,
), ),
); );

View File

@ -7,7 +7,6 @@
namespace Drupal\Tests\Core\Path; namespace Drupal\Tests\Core\Path;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Path\PathMatcher; use Drupal\Core\Path\PathMatcher;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
@ -49,12 +48,7 @@ class PathMatcherTest extends UnitTestCase {
public function testMatchPath($patterns, $paths) { public function testMatchPath($patterns, $paths) {
foreach ($paths as $path => $expected_result) { foreach ($paths as $path => $expected_result) {
$actual_result = $this->pathMatcher->matchPath($path, $patterns); $actual_result = $this->pathMatcher->matchPath($path, $patterns);
$this->assertEquals($actual_result, $expected_result, SafeMarkup::format('Tried matching the path <code>@path</code> to the pattern <pre>@patterns</pre> - expected @expected, got @actual.', array( $this->assertEquals($actual_result, $expected_result, "Tried matching the path '$path' to the pattern '$patterns'.");
'@path' => $path,
'@patterns' => $patterns,
'@expected' => var_export($expected_result, TRUE),
'@actual' => var_export($actual_result, TRUE),
)));
} }
} }

View File

@ -8,9 +8,9 @@
namespace Drupal\Tests\Core\Render; namespace Drupal\Tests\Core\Render;
use Drupal\Component\Utility\Html; use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Render\Element; use Drupal\Core\Render\Element;
use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\Cache;
use Drupal\Core\Render\SafeString;
/** /**
* @coversDefaultClass \Drupal\Core\Render\Renderer * @coversDefaultClass \Drupal\Core\Render\Renderer
@ -69,11 +69,10 @@ class RendererPlaceholdersTest extends RendererTestBase {
$token_render_array['#cache']['keys'] = $cache_keys; $token_render_array['#cache']['keys'] = $cache_keys;
} }
$token = hash('crc32b', serialize($token_render_array)); $token = hash('crc32b', serialize($token_render_array));
return SafeMarkup::format('<drupal-render-placeholder callback="@callback" arguments="@arguments" token="@token"></drupal-render-placeholder>', [ // \Drupal\Core\Render\SafeString::create() is necessary as the render
'@callback' => 'Drupal\Tests\Core\Render\PlaceholdersTest::callback', // system would mangle this markup. As this is exactly what happens at
'@arguments' => '0=' . $args[0], // runtime this is a valid use-case.
'@token' => $token, return SafeString::create('<drupal-render-placeholder callback="Drupal\Tests\Core\Render\PlaceholdersTest::callback" arguments="' . '0=' . $args[0] . '" token="' . $token . '"></drupal-render-placeholder>');
]);
}; };
$extract_placeholder_render_array = function ($placeholder_render_array) { $extract_placeholder_render_array = function ($placeholder_render_array) {
@ -151,7 +150,7 @@ class RendererPlaceholdersTest extends RendererTestBase {
'foo' => 'bar', 'foo' => 'bar',
], ],
'placeholders' => [ 'placeholders' => [
$generate_placeholder_markup() => [ (string) $generate_placeholder_markup() => [
'#lazy_builder' => ['Drupal\Tests\Core\Render\PlaceholdersTest::callback', $args], '#lazy_builder' => ['Drupal\Tests\Core\Render\PlaceholdersTest::callback', $args],
], ],
], ],
@ -318,8 +317,8 @@ class RendererPlaceholdersTest extends RendererTestBase {
// - manually created // - manually created
// - uncacheable // - uncacheable
$x = $base_element_b; $x = $base_element_b;
$expected_placeholder_render_array = $x['#attached']['placeholders'][$generate_placeholder_markup()]; $expected_placeholder_render_array = $x['#attached']['placeholders'][(string) $generate_placeholder_markup()];
unset($x['#attached']['placeholders'][$generate_placeholder_markup()]['#cache']); unset($x['#attached']['placeholders'][(string) $generate_placeholder_markup()]['#cache']);
$cases[] = [ $cases[] = [
$x, $x,
$args, $args,
@ -333,6 +332,7 @@ class RendererPlaceholdersTest extends RendererTestBase {
// - cacheable // - cacheable
$x = $base_element_b; $x = $base_element_b;
$x['#markup'] = $placeholder_markup = $generate_placeholder_markup($keys); $x['#markup'] = $placeholder_markup = $generate_placeholder_markup($keys);
$placeholder_markup = (string) $placeholder_markup;
$x['#attached']['placeholders'] = [ $x['#attached']['placeholders'] = [
$placeholder_markup => [ $placeholder_markup => [
'#lazy_builder' => ['Drupal\Tests\Core\Render\PlaceholdersTest::callback', $args], '#lazy_builder' => ['Drupal\Tests\Core\Render\PlaceholdersTest::callback', $args],

View File

@ -8,7 +8,6 @@
namespace Drupal\Tests\Core\Transliteration; namespace Drupal\Tests\Core\Transliteration;
use Drupal\Component\Utility\Random; use Drupal\Component\Utility\Random;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Transliteration\PhpTransliteration; use Drupal\Core\Transliteration\PhpTransliteration;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
@ -59,12 +58,7 @@ class PhpTransliterationTest extends UnitTestCase {
$transliteration = new PhpTransliteration(NULL, $module_handler); $transliteration = new PhpTransliteration(NULL, $module_handler);
$actual = $transliteration->transliterate($original, $langcode); $actual = $transliteration->transliterate($original, $langcode);
$this->assertSame($expected, $actual, SafeMarkup::format('@original transliteration to @actual is identical to @expected for language @langcode in service instance.', array( $this->assertSame($expected, $actual, "'$printable' transliteration to '$actual' is identical to '$expected' for language '$langcode' in service instance.");
'@original' => $printable,
'@langcode' => $langcode,
'@expected' => $expected,
'@actual' => $actual,
)));
} }
/** /**

View File

@ -7,10 +7,10 @@
namespace Drupal\Tests\Core\Utility { namespace Drupal\Tests\Core\Utility {
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\GeneratedUrl; use Drupal\Core\GeneratedUrl;
use Drupal\Core\Language\Language; use Drupal\Core\Language\Language;
use Drupal\Core\Link; use Drupal\Core\Link;
use Drupal\Core\Render\SafeString;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\Core\Utility\LinkGenerator; use Drupal\Core\Utility\LinkGenerator;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
@ -376,7 +376,7 @@ class LinkGeneratorTest extends UnitTestCase {
// between SafeMarkup and the LinkGenerator. // between SafeMarkup and the LinkGenerator.
$url = new Url('test_route_5', array()); $url = new Url('test_route_5', array());
$url->setUrlGenerator($this->urlGenerator); $url->setUrlGenerator($this->urlGenerator);
$result = $this->linkGenerator->generate(SafeMarkup::set('<em>HTML output</em>'), $url); $result = $this->linkGenerator->generate(SafeString::create('<em>HTML output</em>'), $url);
$this->assertLink(array( $this->assertLink(array(
'attributes' => array('href' => '/test-route-5'), 'attributes' => array('href' => '/test-route-5'),
'child' => array( 'child' => array(

View File

@ -2,13 +2,13 @@
/** /**
* @file * @file
* Contains \Drupal\Tests\Standards\DrupalStandardsListener. * Contains \Drupal\Tests\Listeners\DrupalStandardsListener.
* *
* Listener for PHPUnit tests, to enforce various coding standards within test * Listener for PHPUnit tests, to enforce various coding standards within test
* runs. * runs.
*/ */
namespace Drupal\Tests\Standards; namespace Drupal\Tests\Listeners;
/** /**
* Listens for PHPUnit tests and fails those with invalid coverage annotations. * Listens for PHPUnit tests and fails those with invalid coverage annotations.

View File

@ -0,0 +1,35 @@
<?php
/**
* @file
* Contains \Drupal\Tests\Listeners\SafeMarkupSideEffects.
*
* Listener for PHPUnit tests, to enforce that data providers don't add to the
* SafeMarkup static safe string list.
*/
namespace Drupal\Tests\Listeners;
use Drupal\Component\Utility\SafeMarkup;
/**
* Listens for PHPUnit tests and fails those with SafeMarkup side effects.
*/
class SafeMarkupSideEffects extends \PHPUnit_Framework_BaseTestListener {
/**
* {@inheritdoc}
*/
public function startTestSuite(\PHPUnit_Framework_TestSuite $suite) {
// Use a static so we only do this test once after all the data providers
// have run.
static $tested = FALSE;
if ($suite->getName() !== '' && !$tested) {
$tested = TRUE;
if (!empty(SafeMarkup::getAll())) {
throw new \RuntimeException('SafeMarkup string list polluted by data providers');
}
}
}
}

View File

@ -48,6 +48,12 @@ abstract class UnitTestCase extends \PHPUnit_Framework_TestCase {
FileCacheFactory::setConfiguration(['default' => ['class' => '\Drupal\Component\FileCache\NullFileCache']]); FileCacheFactory::setConfiguration(['default' => ['class' => '\Drupal\Component\FileCache\NullFileCache']]);
$this->root = dirname(dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__)))); $this->root = dirname(dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__))));
// Reset the static list of SafeStrings to prevent bleeding between tests.
$reflected_class = new \ReflectionClass('\Drupal\Component\Utility\SafeMarkup');
$reflected_property = $reflected_class->getProperty('safeStrings');
$reflected_property->setAccessible(true);
$reflected_property->setValue([]);
} }
/** /**