Version 1
[yaffs-website] / web / core / modules / views / tests / src / Kernel / Handler / FieldRenderedEntityTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Kernel\Handler;
4
5 use Drupal\Core\Entity\Entity\EntityViewDisplay;
6 use Drupal\entity_test\Entity\EntityTest;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\field\Entity\FieldStorageConfig;
9 use Drupal\user\Entity\User;
10 use Drupal\views\Entity\View;
11 use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
12 use Drupal\views\Views;
13 use Drupal\Core\Entity\Entity\EntityViewMode;
14
15 /**
16  * Tests the core Drupal\views\Plugin\views\field\RenderedEntity handler.
17  *
18  * @group views
19  */
20 class FieldRenderedEntityTest extends ViewsKernelTestBase {
21
22   /**
23    * Modules to enable.
24    *
25    * @var array
26    */
27   public static $modules = ['entity_test', 'field'];
28
29   /**
30    * Views used by this test.
31    *
32    * @var array
33    */
34   public static $testViews = ['test_field_entity_test_rendered'];
35
36   /**
37    * The logged in user.
38    *
39    * @var \Drupal\user\UserInterface
40    */
41   protected $user;
42
43   /**
44    * {@inheritdoc}
45    */
46   protected function setUpFixtures() {
47     $this->installEntitySchema('user');
48     $this->installEntitySchema('entity_test');
49     $this->installConfig(['entity_test']);
50
51     EntityViewMode::create([
52       'id' => 'entity_test.foobar',
53       'targetEntityType' => 'entity_test',
54       'status' => TRUE,
55       'enabled' => TRUE,
56       'label' => 'My view mode',
57     ])->save();
58
59     $display = EntityViewDisplay::create([
60       'targetEntityType' => 'entity_test',
61       'bundle' => 'entity_test',
62       'mode' => 'foobar',
63       'label' => 'My view mode',
64       'status' => TRUE,
65     ]);
66     $display->save();
67
68     $field_storage = FieldStorageConfig::create([
69       'field_name' => 'test_field',
70       'entity_type' => 'entity_test',
71       'type' => 'string',
72     ]);
73     $field_storage->save();
74
75     $field_config = FieldConfig::create([
76       'field_name' => 'test_field',
77       'entity_type' => 'entity_test',
78       'bundle' => 'entity_test',
79     ]);
80     $field_config->save();
81
82     // Create some test entities.
83     for ($i = 1; $i <= 3; $i++) {
84       EntityTest::create([
85         'name' => "Article title $i",
86         'test_field' => "Test $i",
87       ])->save();
88     }
89
90     $this->user = User::create([
91       'name' => 'test user',
92     ]);
93     $this->user->save();
94
95     parent::setUpFixtures();
96   }
97
98   /**
99    * Tests the default rendered entity output.
100    */
101   public function testRenderedEntityWithoutField() {
102     \Drupal::currentUser()->setAccount($this->user);
103
104     EntityViewDisplay::load('entity_test.entity_test.foobar')
105       ->removeComponent('test_field')
106       ->save();
107
108     $view = Views::getView('test_field_entity_test_rendered');
109     $build = [
110       '#type' => 'view',
111       '#name' => 'test_field_entity_test_rendered',
112       '#view' => $view,
113       '#display_id' => 'default',
114     ];
115     $renderer = \Drupal::service('renderer');
116     $renderer->renderPlain($build);
117     for ($i = 1; $i <= 3; $i++) {
118       $view_field = $view->style_plugin->getField($i - 1, 'rendered_entity');
119       $search_result = strpos($view_field, "Test $i") !== FALSE;
120       $this->assertFalse($search_result, "The text 'Test $i' not found in the view.");
121     }
122
123     $this->assertConfigDependencies($view->storage);
124     $this->assertCacheabilityMetadata($build);
125   }
126
127   /**
128    * Ensures that the expected cacheability metadata is applied.
129    *
130    * @param array $build
131    *   The render array
132    */
133   protected function assertCacheabilityMetadata($build) {
134     $this->assertEqual([
135       'config:core.entity_view_display.entity_test.entity_test.foobar',
136       'config:views.view.test_field_entity_test_rendered',
137       'entity_test:1',
138       'entity_test:2',
139       'entity_test:3',
140       'entity_test_list',
141       'entity_test_view',
142     ], $build['#cache']['tags']);
143
144     $this->assertEqual([
145       'entity_test_view_grants',
146       'languages:language_interface',
147       'theme',
148       'url.query_args',
149       'user.permissions',
150     ], $build['#cache']['contexts']);
151   }
152
153   /**
154    * Ensures that the config dependencies are calculated the right way.
155    *
156    * @param \Drupal\views\Entity\View $storage
157    */
158   protected function assertConfigDependencies(View $storage) {
159     $storage->calculateDependencies();
160     $this->assertEqual([
161       'config' => ['core.entity_view_mode.entity_test.foobar'],
162       'module' => ['entity_test'],
163     ], $storage->getDependencies());
164   }
165
166   /**
167    * Tests the rendered entity output with the test field configured to show.
168    */
169   public function testRenderedEntityWithField() {
170     \Drupal::currentUser()->setAccount($this->user);
171
172     // Show the test_field on the entity_test.entity_test.foobar view display.
173     EntityViewDisplay::load('entity_test.entity_test.foobar')->setComponent('test_field', ['type' => 'string', 'label' => 'above'])->save();
174
175     $view = Views::getView('test_field_entity_test_rendered');
176     $build = [
177       '#type' => 'view',
178       '#name' => 'test_field_entity_test_rendered',
179       '#view' => $view,
180       '#display_id' => 'default',
181     ];
182
183     $renderer = \Drupal::service('renderer');
184     $renderer->renderPlain($build);
185     for ($i = 1; $i <= 3; $i++) {
186       $view_field = $view->style_plugin->getField($i - 1, 'rendered_entity');
187       $search_result = strpos($view_field, "Test $i") !== FALSE;
188       $this->assertTrue($search_result, "The text 'Test $i' found in the view.");
189     }
190
191     $this->assertConfigDependencies($view->storage);
192     $this->assertCacheabilityMetadata($build);
193   }
194
195 }