diff --git a/core/modules/image/src/Tests/ImageFieldTestBase.php b/core/modules/image/src/Tests/ImageFieldTestBase.php index e69f4884e2..b144ff647a 100644 --- a/core/modules/image/src/Tests/ImageFieldTestBase.php +++ b/core/modules/image/src/Tests/ImageFieldTestBase.php @@ -1,7 +1,8 @@ drupalCreateNode(['type' => 'article']); - // TODO: drupalBuildEntityView is not available. $article_built = $this->drupalBuildEntityView($article); $this->assertEqual( $article_built[$field_name][0]['#item']->target_id, diff --git a/core/modules/simpletest/src/WebTestBase.php b/core/modules/simpletest/src/WebTestBase.php index 2491bc84b3..39955ed48f 100644 --- a/core/modules/simpletest/src/WebTestBase.php +++ b/core/modules/simpletest/src/WebTestBase.php @@ -19,6 +19,7 @@ use Drupal\Core\Test\FunctionalTestSetupTrait; use Drupal\Core\Url; use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait; +use Drupal\Tests\EntityTestTrait; use Drupal\Tests\Traits\Core\CronRunTrait; use Drupal\Tests\TestFileCreationTrait; use Drupal\Tests\XdebugRequestTrait; @@ -45,6 +46,9 @@ createContentType as drupalCreateContentType; } use CronRunTrait; + use EntityTestTrait { + buildEntityView as drupalBuildEntityView; + } use AssertMailTrait { getMails as drupalGetMails; } @@ -210,64 +214,6 @@ public function __construct($test_id = NULL) { } /** - * Builds the renderable view of an entity. - * - * Entities postpone the composition of their renderable arrays to #pre_render - * functions in order to maximize cache efficacy. This means that the full - * renderable array for an entity is constructed in drupal_render(). Some - * tests require the complete renderable array for an entity outside of the - * drupal_render process in order to verify the presence of specific values. - * This method isolates the steps in the render process that produce an - * entity's renderable array. - * - * @param \Drupal\Core\Entity\EntityInterface $entity - * The entity to prepare a renderable array for. - * @param string $view_mode - * (optional) The view mode that should be used to build the entity. - * @param null $langcode - * (optional) For which language the entity should be prepared, defaults to - * the current content language. - * @param bool $reset - * (optional) Whether to clear the cache for this entity. - * @return array - * - * @see drupal_render() - */ - protected function drupalBuildEntityView(EntityInterface $entity, $view_mode = 'full', $langcode = NULL, $reset = FALSE) { - $ensure_fully_built = function(&$elements) use (&$ensure_fully_built) { - // If the default values for this element have not been loaded yet, populate - // them. - if (isset($elements['#type']) && empty($elements['#defaults_loaded'])) { - $elements += \Drupal::service('element_info')->getInfo($elements['#type']); - } - - // Make any final changes to the element before it is rendered. This means - // that the $element or the children can be altered or corrected before the - // element is rendered into the final text. - if (isset($elements['#pre_render'])) { - foreach ($elements['#pre_render'] as $callable) { - $elements = call_user_func($callable, $elements); - } - } - - // And recurse. - $children = Element::children($elements, TRUE); - foreach ($children as $key) { - $ensure_fully_built($elements[$key]); - } - }; - - $render_controller = $this->container->get('entity.manager')->getViewBuilder($entity->getEntityTypeId()); - if ($reset) { - $render_controller->resetCache([$entity->id()]); - } - $build = $render_controller->view($entity, $view_mode, $langcode); - $ensure_fully_built($build); - - return $build; - } - - /** * Checks to see whether a block appears on the page. * * @param \Drupal\block\Entity\Block $block diff --git a/core/tests/Drupal/Tests/EntityTestTrait.php b/core/tests/Drupal/Tests/EntityTestTrait.php new file mode 100644 index 0000000000..d9fee3efde --- /dev/null +++ b/core/tests/Drupal/Tests/EntityTestTrait.php @@ -0,0 +1,75 @@ +container->get('element_info')->getInfo($elements['#type']); + } + + // Make any final changes to the element before it is rendered. This means + // that the $element or the children can be altered or corrected before the + // element is rendered into the final text. + if (isset($elements['#pre_render'])) { + foreach ($elements['#pre_render'] as $callable) { + $elements = call_user_func($callable, $elements); + } + } + + // And recurse. + $children = Element::children($elements, TRUE); + foreach ($children as $key) { + $ensure_fully_built($elements[$key]); + } + }; + + $render_controller = $this->container->get('entity_type.manager')->getViewBuilder($entity->getEntityTypeId()); + if ($reset) { + $render_controller->resetCache([$entity->id()]); + } + $build = $render_controller->view($entity, $view_mode, $langcode); + $ensure_fully_built($build); + + return $build; + } + +}