Refactor tests to compare rendered output rather than values in build arrays

Created on 30 April 2014, about 10 years ago
Updated 7 March 2024, 3 months ago

Problem/Motivation

In #2099131: Use #pre_render pattern for entity render caching → , the render process was separated into a "cache tag collection" phase and a "renderable building" phase. The building is postponed through a #pre_render callback set by the entity view builder class. This change was made to delay the costly building of an entity's renderable array until after a cache entry check is conducted.

Because of this change, tests may no longer assume that the full renderable array is available by simply calling entity_view or entity_view_multiple. The full renderable build for an entity is really only available once drupal_render has run. This has, in fact, always been true since #pre_render functions were always capable of altering the render array.

The code changes in #2099131 introduced the method WebTestBase::drupalBuildEntityView() that returns the full renderable array for an entity; it invokes the entity's view builder and invokes its #pre_render functions.

Rather than use a method that artificially pulls together pieces of different code paths, we should really only rely on the existing code paths, namely, the output of drupal_render().

Proposed resolution

Refactor tests to use drupal_render() if they currently use entity_view, entity_view_multiple or WebTestBase::drupalBuildEntityView. Then assert against the rendered output strings.

Or, since drupal_render manipulates the passed-in array by reference, the build can be passed to drupal_render() and then assessed. For example:

$controller = $this->entityManager->getViewBuilder($entity_type);
$build = $controller->view($entity);
drupal_render($build);
$this->assertEqual($build['label']['#markup'], $values[$current_langcode]['name']);

Remove WebTestBase::drupalBuildEntityView.

User interface changes

None.

API changes

None.

Original report by @jessebeach →

Postponed until

#2099131: Use #pre_render pattern for entity render caching →

📌 Task
Status

Postponed: needs info

Version

11.0 🔥

Component
PHPUnit  →

Last updated 1 minute ago

Created by

🇺🇸United States jessebeach

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇳🇿New Zealand quietone New Zealand

    There has been no action here for 9 years. Is this task still relevant?

    Since we need more information to move forward with this issue, I am setting the status to Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.

    Thanks!

  • Status changed to Postponed: needs info 3 months ago
  • 🇳🇿New Zealand quietone New Zealand
Production build 0.69.0 2024