View source
<?php
namespace Drupal\Tests\Core\Render;
use Drupal\Component\Utility\Html;
use Drupal\Core\Cache\Cache;
class PlaceholderGeneratorTest extends RendererTestBase {
protected $placeholderGenerator;
public function testCreatePlaceholderGeneratesValidHtmlMarkup(array $element) {
$build = $this->placeholderGenerator
->createPlaceholder($element);
$original_placeholder_markup = (string) $build['#markup'];
$processed_placeholder_markup = Html::serialize(Html::load($build['#markup']));
$this
->assertEquals($original_placeholder_markup, $processed_placeholder_markup);
}
public function testRenderPlaceholdersDifferentSortedContextsTags() {
$contexts_1 = [
'user',
'foo',
];
$contexts_2 = [
'foo',
'user',
];
$tags_1 = [
'current-temperature',
'foo',
];
$tags_2 = [
'foo',
'current-temperature',
];
$test_element = [
'#cache' => [
'max-age' => Cache::PERMANENT,
],
'#lazy_builder' => [
'Drupal\\Tests\\Core\\Render\\PlaceholdersTest::callback',
[
'foo' => TRUE,
],
],
];
$test_element['#cache']['contexts'] = $contexts_1;
$test_element['#cache']['tags'] = $tags_1;
$placeholder_element1 = $this->placeholderGenerator
->createPlaceholder($test_element);
$test_element['#cache']['contexts'] = $contexts_2;
$test_element['#cache']['tags'] = $tags_1;
$placeholder_element2 = $this->placeholderGenerator
->createPlaceholder($test_element);
$test_element['#cache']['contexts'] = $contexts_1;
$test_element['#cache']['tags'] = $tags_2;
$placeholder_element3 = $this->placeholderGenerator
->createPlaceholder($test_element);
$this
->assertSame((string) $placeholder_element1['#markup'], (string) $placeholder_element2['#markup']);
$this
->assertSame((string) $placeholder_element1['#markup'], (string) $placeholder_element3['#markup']);
}
public function providerCreatePlaceholderGeneratesValidHtmlMarkup() {
return [
'multiple-arguments' => [
[
'#lazy_builder' => [
'Drupal\\Tests\\Core\\Render\\PlaceholdersTest::callback',
[
'foo',
'bar',
],
],
],
],
'special-character-&' => [
[
'#lazy_builder' => [
'Drupal\\Tests\\Core\\Render\\PlaceholdersTest::callback',
[
'foo&bar',
],
],
],
],
'special-character-"' => [
[
'#lazy_builder' => [
'Drupal\\Tests\\Core\\Render\\PlaceholdersTest::callback',
[
'foo"bar',
],
],
],
],
'special-character-<' => [
[
'#lazy_builder' => [
'Drupal\\Tests\\Core\\Render\\PlaceholdersTest::callback',
[
'foo<bar',
],
],
],
],
'special-character->' => [
[
'#lazy_builder' => [
'Drupal\\Tests\\Core\\Render\\PlaceholdersTest::callback',
[
'foo>bar',
],
],
],
],
];
}
}