class PlaceholderGeneratorTest
Same name and namespace in other branches
- 11.x core/tests/Drupal/Tests/Core/Render/PlaceholderGeneratorTest.php \Drupal\Tests\Core\Render\PlaceholderGeneratorTest
- 10 core/tests/Drupal/Tests/Core/Render/PlaceholderGeneratorTest.php \Drupal\Tests\Core\Render\PlaceholderGeneratorTest
- 9 core/tests/Drupal/Tests/Core/Render/PlaceholderGeneratorTest.php \Drupal\Tests\Core\Render\PlaceholderGeneratorTest
- 8.9.x core/tests/Drupal/Tests/Core/Render/PlaceholderGeneratorTest.php \Drupal\Tests\Core\Render\PlaceholderGeneratorTest
Tests Drupal\Core\Render\PlaceholderGenerator.
Attributes
#[CoversClass(PlaceholderGenerator::class)]
#[Group('Render')]
Hierarchy
- class \Drupal\Tests\UnitTestCase uses \Drupal\Tests\DrupalTestCaseTrait, \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\Core\Render\RendererTestBase extends \Drupal\Tests\UnitTestCase
- class \Drupal\Tests\Core\Render\PlaceholderGeneratorTest extends \Drupal\Tests\Core\Render\RendererTestBase
- class \Drupal\Tests\Core\Render\RendererTestBase extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of PlaceholderGeneratorTest
File
-
core/
tests/ Drupal/ Tests/ Core/ Render/ PlaceholderGeneratorTest.php, line 17
Namespace
Drupal\Tests\Core\RenderView source
class PlaceholderGeneratorTest extends RendererTestBase {
/**
* The tested placeholder generator.
*
* @var \Drupal\Core\Render\PlaceholderGenerator
*/
protected $placeholderGenerator;
/**
* Ensure that the generated placeholder markup is valid.
*
* If it is not, then simply using DOMDocument on HTML that contains
* placeholders may modify the placeholders' markup, which would make it
* impossible to replace the placeholders: the placeholder markup in
* #attached versus that in the HTML processed by DOMDocument would no longer
* match.
*/
public function testCreatePlaceholderGeneratesValidHtmlMarkup(array $element) : void {
$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);
}
/**
* Tests the creation of an element with a #lazy_builder callback.
*
* Between two renders neither the cache contexts nor tags sort should change.
* A placeholder should generate the same hash, so it is not rendered twice.
*
* @legacy-covers ::createPlaceholder
*/
public function testRenderPlaceholdersDifferentSortedContextsTags() : void {
$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);
// Verify placeholder and specially hash are same with different contexts
// order.
$this->assertSame((string) $placeholder_element1['#markup'], (string) $placeholder_element2['#markup']);
// Verify placeholder and specially hash are same with different tags order.
$this->assertSame((string) $placeholder_element1['#markup'], (string) $placeholder_element3['#markup']);
}
/**
* @return array
* An array of test cases with different placeholder inputs.
*/
public static function providerCreatePlaceholderGeneratesValidHtmlMarkup() : array {
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',
],
],
],
],
];
}
}
Members
| Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title | Overrides |
|---|---|---|---|---|---|---|
| DrupalTestCaseTrait::checkErrorHandlerOnTearDown | public | function | Checks the test error handler after test execution. | |||
| ExpectDeprecationTrait::expectDeprecation | Deprecated | public | function | Adds an expected deprecation. | ||
| ExpectDeprecationTrait::regularExpressionForFormatDescription | private | function | ||||
| PlaceholderGeneratorTest::$placeholderGenerator | protected | property | The tested placeholder generator. | Overrides RendererTestBase::$placeholderGenerator | ||
| PlaceholderGeneratorTest::providerCreatePlaceholderGeneratesValidHtmlMarkup | public static | function | ||||
| PlaceholderGeneratorTest::testCreatePlaceholderGeneratesValidHtmlMarkup | public | function | Ensure that the generated placeholder markup is valid. | |||
| PlaceholderGeneratorTest::testRenderPlaceholdersDifferentSortedContextsTags | public | function | Tests the creation of an element with a #lazy_builder callback. | |||
| RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |||
| RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | |||
| RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | |||
| RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | |||
| RendererTestBase::$cacheContextsManager | protected | property | ||||
| RendererTestBase::$cacheFactory | protected | property | ||||
| RendererTestBase::$callableResolver | protected | property | The mocked controller resolver. | |||
| RendererTestBase::$currentUserRole | protected | property | The simulated "current" user role, for use in tests with cache contexts. | |||
| RendererTestBase::$datetimeTime | protected | property | System time service. | |||
| RendererTestBase::$elementInfo | protected | property | The mocked element info. | |||
| RendererTestBase::$memoryCache | protected | property | ||||
| RendererTestBase::$renderCache | protected | property | The tested render cache. | |||
| RendererTestBase::$renderer | protected | property | The tested renderer. | |||
| RendererTestBase::$rendererConfig | protected | property | The mocked renderer configuration. | |||
| RendererTestBase::$requestStack | protected | property | ||||
| RendererTestBase::$themeManager | protected | property | The mocked theme manager. | |||
| RendererTestBase::assertRenderCacheItem | protected | function | Asserts a render cache item. | |||
| RendererTestBase::randomContextValue | protected static | function | Generates a random context value for the placeholder tests. | |||
| RendererTestBase::setUp | protected | function | Overrides UnitTestCase::setUp | 4 | ||
| RendererTestBase::setUpMemoryCache | protected | function | Sets up a memory-based render cache back-end. | |||
| RendererTestBase::setUpRequest | protected | function | Sets up a request object on the request stack. | |||
| RendererTestBase::setUpUnusedCache | protected | function | Sets up a render cache back-end that is asserted to be never used. | |||
| UnitTestCase::$root | protected | property | The app root. | |||
| UnitTestCase::getClassResolverStub | protected | function | Returns a stub class resolver. | |||
| UnitTestCase::getConfigFactoryStub | public | function | Returns a stub config factory that behaves according to the passed array. | |||
| UnitTestCase::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | |||
| UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |||
| UnitTestCase::setDebugDumpHandler | public static | function | Registers the dumper CLI handler when the DebugDump extension is enabled. | |||
| UnitTestCase::setupMockIterator | protected | function | Set up a traversable class mock to return specific items when iterated. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.