Skip to content

RenderElementTestTrait

Trait for helping test render elements.


  • Full name: \Drupal\helper\Tests\RenderElementTestTrait

Methods

renderElement

Renders a render array.

protected renderElement(array& $elements): string

Parameters:

Parameter Type Description
$elements array
The elements to render.

Return Value:

The rendered string output (typically HTML).


removeWhiteSpace

{@inheritdoc}

protected removeWhiteSpace(): void

removeContentNewlines

Remove any newlines and spaces around them to just a single space.

protected removeContentNewlines(): void

updateFileUrlsToUri

Rewrite any absolute and relative file URLs to their short URIs.

protected updateFileUrlsToUri(): void

assertRenderElement

Assert rendering a render array.

public assertRenderElement(string|\Exception $expected, array $element, array|\Drupal\Core\Cache\CacheableDependencyInterface|null $expected_cache = null): void

Parameters:

Parameter Type Description
$expected string|\Exception
The expected markup HTML, or the exception to be expected. We cannot
test for both expected markup and warnings since PHPUnit halts on
any kind of exception, error, warning, or notice.
$element array
The element's render array.
$expected_cache array|\Drupal\Core\Cache\CacheableDependencyInterface|null
(optional) The expected values in the $element['#cache'] after
rendering. It is not currently possible to test for both an expected
exception and expected cache metadata at the same time, since exceptions
in PHPUnit end the test.

See Also:

  • \Drupal\helper\Tests\static::assertRenderCacheMetadata()

assertRenderCacheMetadata

Test if a render element has the expected cache metadata after rendering.

public assertRenderCacheMetadata(array|\Drupal\Core\Cache\CacheableDependencyInterface $expected_cache, array $element): void

Parameters:

Parameter Type Description
$expected_cache array|\Drupal\Core\Cache\CacheableDependencyInterface
The expected values in the $element['#cache'] after rendering.
$element array
The element's render array, once it has been rendered.