EntityViewBuilderTest.php 8.33 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\system\Tests\Entity\EntityViewBuilderTest.
6 7 8 9 10
 */

namespace Drupal\system\Tests\Entity;

/**
11
 * Tests the entity view builder.
12 13
 *
 * @group Entity
14
 */
15
class EntityViewBuilderTest extends EntityUnitTestBase {
16 17 18 19 20 21 22 23 24 25 26

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('entity_reference');

  /**
   * {@inheritdoc}
   */
27
  protected function setUp() {
28 29 30 31 32 33 34
    parent::setUp();
    $this->installConfig(array('entity_test'));
  }

  /**
   * Tests entity render cache handling.
   */
35
  public function testEntityViewBuilderCache() {
36 37
    $cache_contexts = \Drupal::service("cache_contexts");

38
    // Force a request via GET so we can get drupal_render() cache working.
39 40 41
    $request = \Drupal::request();
    $request_method = $request->server->get('REQUEST_METHOD');
    $request->setMethod('GET');
42

43
    $entity_test = $this->createTestEntity('entity_test');
44 45 46

    // Test that new entities (before they are saved for the first time) do not
    // generate a cache entry.
47
    $build = $this->container->get('entity.manager')->getViewBuilder('entity_test')->view($entity_test, 'full');
48
    $this->assertTrue(isset($build['#cache']) && array_keys($build['#cache']) == array('tags'), 'The render array element of new (unsaved) entities is not cached, but does have cache tags set.');
49 50 51

    // Get a fully built entity view render array.
    $entity_test->save();
52
    $build = $this->container->get('entity.manager')->getViewBuilder('entity_test')->view($entity_test, 'full');
53
    $cid = implode(':', $cache_contexts->convertTokensToKeys($build['#cache']['keys']));
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    $bin = $build['#cache']['bin'];

    // Mock the build array to not require the theme registry.
    unset($build['#theme']);
    $build['#markup'] = 'entity_render_test';

    // Test that a cache entry is created.
    drupal_render($build);
    $this->assertTrue($this->container->get('cache.' . $bin)->get($cid), 'The entity render element has been cached.');

    // Re-save the entity and check that the cache entry has been deleted.
    $entity_test->save();
    $this->assertFalse($this->container->get('cache.' . $bin)->get($cid), 'The entity render cache has been cleared when the entity was saved.');

    // Rebuild the render array (creating a new cache entry in the process) and
    // delete the entity to check the cache entry is deleted.
    unset($build['#printed']);
    drupal_render($build);
    $this->assertTrue($this->container->get('cache.' . $bin)->get($cid), 'The entity render element has been cached.');
    $entity_test->delete();
    $this->assertFalse($this->container->get('cache.' . $bin)->get($cid), 'The entity render cache has been cleared when the entity was deleted.');

    // Restore the previous request method.
77
    $request->setMethod($request_method);
78 79 80 81 82
  }

  /**
   * Tests entity render cache with references.
   */
83
  public function testEntityViewBuilderCacheWithReferences() {
84 85
    $cache_contexts = \Drupal::service("cache_contexts");

86
    // Force a request via GET so we can get drupal_render() cache working.
87 88 89
    $request = \Drupal::request();
    $request_method = $request->server->get('REQUEST_METHOD');
    $request->setMethod('GET');
90 91

    // Create an entity reference field and an entity that will be referenced.
92
    entity_reference_create_field('entity_test', 'entity_test', 'reference_field', 'Reference', 'entity_test');
93
    entity_get_display('entity_test', 'entity_test', 'full')->setComponent('reference_field', [
94
      'type' => 'entity_reference_entity_view',
95 96
      'settings' => ['link' => FALSE],
    ])->save();
97
    $entity_test_reference = $this->createTestEntity('entity_test');
98 99 100
    $entity_test_reference->save();

    // Get a fully built entity view render array for the referenced entity.
101
    $build = $this->container->get('entity.manager')->getViewBuilder('entity_test')->view($entity_test_reference, 'full');
102
    $cid_reference = implode(':', $cache_contexts->convertTokensToKeys($build['#cache']['keys']));
103 104 105 106 107 108 109 110 111 112 113
    $bin_reference = $build['#cache']['bin'];

    // Mock the build array to not require the theme registry.
    unset($build['#theme']);
    $build['#markup'] = 'entity_render_test';
    drupal_render($build);

    // Test that a cache entry was created for the referenced entity.
    $this->assertTrue($this->container->get('cache.' . $bin_reference)->get($cid_reference), 'The entity render element for the referenced entity has been cached.');

    // Create another entity that references the first one.
114
    $entity_test = $this->createTestEntity('entity_test');
115
    $entity_test->reference_field->entity = $entity_test_reference;
116
    $entity_test->reference_field->access = TRUE;
117 118 119
    $entity_test->save();

    // Get a fully built entity view render array.
120
    $build = $this->container->get('entity.manager')->getViewBuilder('entity_test')->view($entity_test, 'full');
121
    $cid = implode(':', $cache_contexts->convertTokensToKeys($build['#cache']['keys']));
122 123 124 125 126 127 128 129 130 131 132
    $bin = $build['#cache']['bin'];

    // Mock the build array to not require the theme registry.
    unset($build['#theme']);
    $build['#markup'] = 'entity_render_test';
    drupal_render($build);

    // Test that a cache entry is created.
    $this->assertTrue($this->container->get('cache.' . $bin)->get($cid), 'The entity render element has been cached.');

    // Save the entity and verify that both cache entries have been deleted.
133
    $entity_test_reference->save();
134 135 136 137
    $this->assertFalse($this->container->get('cache.' . $bin)->get($cid), 'The entity render cache has been cleared when the entity was deleted.');
    $this->assertFalse($this->container->get('cache.' . $bin_reference)->get($cid_reference), 'The entity render cache for the referenced entity has been cleared when the entity was deleted.');

    // Restore the previous request method.
138
    $request->setMethod($request_method);
139 140 141 142 143
  }

  /**
   * Tests entity render cache toggling.
   */
144 145
  public function testEntityViewBuilderCacheToggling() {
    $entity_test = $this->createTestEntity('entity_test');
146 147 148 149
    $entity_test->save();

    // Test a view mode in default conditions: render caching is enabled for
    // the entity type and the view mode.
150
    $build = $this->container->get('entity.manager')->getViewBuilder('entity_test')->view($entity_test, 'full');
151
    $this->assertTrue(isset($build['#cache']) && array_keys($build['#cache']) == array('tags', 'keys', 'bin') , 'A view mode with render cache enabled has the correct output (cache tags, keys and bin).');
152 153

    // Test that a view mode can opt out of render caching.
154
    $build = $this->container->get('entity.manager')->getViewBuilder('entity_test')->view($entity_test, 'test');
155
    $this->assertTrue(isset($build['#cache']) && array_keys($build['#cache']) == array('tags'), 'A view mode with render cache disabled has the correct output (only cache tags).');
156 157 158 159

    // Test that an entity type can opt out of render caching completely.
    $entity_test_no_cache = $this->createTestEntity('entity_test_label');
    $entity_test_no_cache->save();
160
    $build = $this->container->get('entity.manager')->getViewBuilder('entity_test_label')->view($entity_test_no_cache, 'full');
161
    $this->assertTrue(isset($build['#cache']) && array_keys($build['#cache']) == array('tags'), 'An entity type can opt out of render caching regardless of view mode configuration, but always has cache tags set.');
162 163
  }

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
  /**
   * Tests weighting of display components.
   */
  public function testEntityViewBuilderWeight() {
    // Set a weight for the label component.
    entity_get_display('entity_test', 'entity_test', 'full')
      ->setComponent('label', array('weight' => 20))
      ->save();

    // Create and build a test entity.
    $entity_test = $this->createTestEntity('entity_test');
    $view =  $this->container->get('entity.manager')->getViewBuilder('entity_test')->view($entity_test, 'full');
    drupal_render($view);

    // Check that the weight is respected.
    $this->assertEqual($view['label']['#weight'], 20, 'The weight of a display component is respected.');
  }

182 183 184 185 186 187 188 189 190 191 192 193
  /**
   * Creates an entity for testing.
   *
   * @param string $entity_type
   *   The entity type.
   *
   * @return \Drupal\Core\Entity\EntityInterface
   *   The created entity.
   */
  protected function createTestEntity($entity_type) {
    $data = array(
      'bundle' => $entity_type,
194
      'name' => $this->randomMachineName(),
195
    );
196
    return $this->container->get('entity.manager')->getStorage($entity_type)->create($data);
197 198 199
  }

}