function RendererTestBase::randomContextValue

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Render/RendererTestBase.php \Drupal\Tests\Core\Render\RendererTestBase::randomContextValue()
  2. 8.9.x core/tests/Drupal/Tests/Core/Render/RendererTestBase.php \Drupal\Tests\Core\Render\RendererTestBase::randomContextValue()
  3. 11.x core/tests/Drupal/Tests/Core/Render/RendererTestBase.php \Drupal\Tests\Core\Render\RendererTestBase::randomContextValue()

Generates a random context value for the placeholder tests.

The #context array used by the placeholder #lazy_builder callback will generally be used to provide metadata like entity IDs, field machine names, paths, etc. for JavaScript replacement of content or assets. In this test, the #lazy_builder callback PlaceholdersTest::callback() renders the context inside test HTML, so using any random string would sometimes cause random test failures because the test output would not be parseable. Instead, we provide random tokens for replacement.

See also

PlaceholdersTest::callback()

https://www.drupal.org/node/2151609

5 calls to RendererTestBase::randomContextValue()
RendererPlaceholdersTest::generatePlaceholderElement in core/tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php
Generates an element with a placeholder.
RendererPlaceholdersTest::providerPlaceholders in core/tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php
Provides the two classes of placeholders: cacheable and uncacheable.
RendererPlaceholdersTest::testRecursivePlaceholder in core/tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php
Tests a placeholder that adds another placeholder.
RendererTest::assertAccess in core/tests/Drupal/Tests/Core/Render/RendererTest.php
Asserts that a render array with access checking renders correctly.
RendererTest::providerTestRenderBasic in core/tests/Drupal/Tests/Core/Render/RendererTest.php
Provides a list of render arrays to test basic rendering.

File

core/tests/Drupal/Tests/Core/Render/RendererTestBase.php, line 219

Class

RendererTestBase
Base class for the actual unit tests testing \Drupal\Core\Render\Renderer.

Namespace

Drupal\Tests\Core\Render

Code

protected static function randomContextValue() : string {
    $tokens = [
        'llama',
        'alpaca',
        'camel',
        'moose',
        'elk',
    ];
    return $tokens[mt_rand(0, 4)];
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.