function RendererTestBase::randomContextValue

Same name and namespace in other branches
  1. 8.9.x core/tests/Drupal/Tests/Core/Render/RendererTestBase.php \Drupal\Tests\Core\Render\RendererTestBase::randomContextValue()
  2. 10 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 be unparseable. 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 201

Class

RendererTestBase
Base class for the actual unit tests testing <a href="/api/drupal/core%21lib%21Drupal%21Core%21Render%21Renderer.php/class/Renderer/9" title="Turns a render array into an HTML string." class="local">\Drupal\Core\Render\Renderer</a>.

Namespace

Drupal\Tests\Core\Render

Code

protected function randomContextValue() {
    $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.