Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / views / tests / src / Unit / Plugin / area / EntityTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Unit\Plugin\area;
4
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\views\Plugin\views\area\Entity;
7 use Symfony\Component\DependencyInjection\ContainerBuilder;
8
9 /**
10  * @coversDefaultClass \Drupal\views\Plugin\views\area\Entity
11  * @group Entity
12  */
13 class EntityTest extends UnitTestCase {
14
15   /**
16    * The tested entity area handler.
17    *
18    * @var \Drupal\views\Plugin\views\area\Entity
19    */
20   protected $entityHandler;
21
22   /**
23    * The mocked entity manager.
24    *
25    * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
26    */
27   protected $entityManager;
28
29   /**
30    * The mocked entity storage.
31    *
32    * @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
33    */
34   protected $entityStorage;
35
36   /**
37    * The mocked entity view builder.
38    *
39    * @var \Drupal\Core\Entity\EntityViewBuilderInterface|\PHPUnit_Framework_MockObject_MockObject
40    */
41   protected $entityViewBuilder;
42
43   /**
44    * The mocked view executable.
45    *
46    * @var \Drupal\views\ViewExecutable|\PHPUnit_Framework_MockObject_MockObject
47    */
48   protected $executable;
49
50   /**
51    * The mocked display.
52    *
53    * @var \Drupal\views\Plugin\views\display\DisplayPluginBase|\PHPUnit_Framework_MockObject_MockObject
54    */
55   protected $display;
56
57   /**
58    * The mocked style plugin.
59    *
60    * @var \Drupal\views\Plugin\views\style\StylePluginBase|\PHPUnit_Framework_MockObject_MockObject
61    */
62   protected $stylePlugin;
63
64   /**
65    * {@inheritdoc}
66    */
67   protected function setUp() {
68     parent::setUp();
69
70     $this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
71     $this->entityStorage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
72     $this->entityViewBuilder = $this->getMock('Drupal\Core\Entity\EntityViewBuilderInterface');
73
74     $this->executable = $this->getMockBuilder('Drupal\views\ViewExecutable')
75       ->disableOriginalConstructor()
76       ->getMock();
77     $this->display = $this->getMockBuilder('Drupal\views\Plugin\views\display\DisplayPluginBase')
78       ->disableOriginalConstructor()
79       ->getMock();
80     $this->stylePlugin = $this->getMockBuilder('Drupal\views\Plugin\views\style\StylePluginBase')
81       ->disableOriginalConstructor()
82       ->getMock();
83     $this->executable->style_plugin = $this->stylePlugin;
84
85     $this->entityHandler = new Entity([], 'entity', ['entity_type' => 'entity_test'], $this->entityManager);
86
87     $this->display->expects($this->any())
88       ->method('getPlugin')
89       ->with('style')
90       ->willReturn($this->stylePlugin);
91     $this->executable->expects($this->any())
92       ->method('getStyle')
93       ->willReturn($this->stylePlugin);
94
95     $token = $this->getMockBuilder('Drupal\Core\Utility\Token')
96       ->disableOriginalConstructor()
97       ->getMock();
98     $token->expects($this->any())
99       ->method('replace')
100       ->willReturnArgument(0);
101     $container = new ContainerBuilder();
102     $container->set('token', $token);
103     \Drupal::setContainer($container);
104   }
105
106   /**
107    * Ensures that the entity manager returns an entity storage.
108    */
109   protected function setupEntityManager() {
110     $this->entityManager->expects($this->any())
111       ->method('getStorage')
112       ->with('entity_test')
113       ->willReturn($this->entityStorage);
114     $this->entityManager->expects($this->any())
115       ->method('getViewBuilder')
116       ->with('entity_test')
117       ->willReturn($this->entityViewBuilder);
118   }
119
120   /**
121    * Data provider for testing different types of tokens.
122    *
123    * @return array
124    */
125   public function providerTestTokens() {
126     return [
127       ['{{ raw_arguments.test1 }}', 5],
128       ['{{ arguments.test2 }}', 6],
129       ['{{ test_render_token }}', 7],
130       ['{{ test:global_token }}', 8],
131     ];
132   }
133
134   /**
135    * @covers ::render
136    * @covers ::defineOptions
137    * @covers ::init
138    */
139   public function testRenderWithId() {
140     $this->setupEntityManager();
141     $options = [
142       'target' => 1,
143       'tokenize' => FALSE,
144     ];
145
146     /** @var \Drupal\Core\Entity\EntityInterface $entity */
147     $entity = $this->getMock('Drupal\Core\Entity\EntityInterface');
148     $entity->expects($this->once())
149       ->method('access')
150       ->willReturn(TRUE);
151
152     $this->entityStorage->expects($this->never())
153       ->method('loadByProperties');
154     $this->entityManager->expects($this->any())
155       ->method('loadEntityByConfigTarget')
156       ->willReturn($entity);
157     $this->entityViewBuilder->expects($this->once())
158       ->method('view')
159       ->with($entity, 'default')
160       ->willReturn(['#markup' => 'hallo']);
161
162     $this->entityHandler->init($this->executable, $this->display, $options);
163
164     $result = $this->entityHandler->render();
165     $this->assertEquals(['#markup' => 'hallo'], $result);
166   }
167
168   /**
169    * @covers ::render
170    * @covers ::defineOptions
171    * @covers ::init
172    *
173    * @dataProvider providerTestTokens
174    */
175   public function testRenderWithIdAndToken($token, $id) {
176     $this->setupEntityManager();
177     $options = [
178       'target' => $token,
179       'tokenize' => TRUE,
180     ];
181
182     $entity = $this->getMock('Drupal\Core\Entity\EntityInterface');
183     $entity->expects($this->once())
184       ->method('access')
185       ->willReturn(TRUE);
186
187     $this->stylePlugin->expects($this->once())
188       ->method('tokenizeValue')
189       ->with($token, 0)
190       ->willReturn($id);
191
192     $this->entityStorage->expects($this->never())
193       ->method('loadByProperties');
194     $this->entityStorage->expects($this->once())
195       ->method('load')
196       ->with($id)
197       ->willReturn($entity);
198     $this->entityViewBuilder->expects($this->once())
199       ->method('view')
200       ->with($entity, 'default')
201       ->willReturn(['#markup' => 'hallo']);
202
203     $this->entityHandler->init($this->executable, $this->display, $options);
204
205     $result = $this->entityHandler->render();
206     $this->assertEquals(['#markup' => 'hallo'], $result);
207   }
208
209   /**
210    * @covers ::render
211    * @covers ::defineOptions
212    * @covers ::init
213    */
214   public function testRenderWithUuid() {
215     $this->setupEntityManager();
216     $uuid = '1d52762e-b9d8-4177-908f-572d1a5845a4';
217     $options = [
218       'target' => $uuid,
219       'tokenize' => FALSE,
220     ];
221     $entity = $this->getMock('Drupal\Core\Entity\EntityInterface');
222     $entity->expects($this->once())
223       ->method('access')
224       ->willReturn(TRUE);
225
226     $this->entityStorage->expects($this->never())
227       ->method('load');
228     $this->entityManager->expects($this->once())
229       ->method('loadEntityByConfigTarget')
230       ->willReturn($entity);
231     $this->entityViewBuilder->expects($this->once())
232       ->method('view')
233       ->with($entity, 'default')
234       ->willReturn(['#markup' => 'hallo']);
235
236     $this->entityHandler->init($this->executable, $this->display, $options);
237
238     $result = $this->entityHandler->render();
239     $this->assertEquals(['#markup' => 'hallo'], $result);
240   }
241
242   /**
243    * @covers ::calculateDependencies
244    *
245    * @dataProvider providerTestTokens
246    */
247   public function testCalculateDependenciesWithPlaceholder($token, $id) {
248     $this->setupEntityManager();
249
250     $options = [
251       'target' => $token,
252     ];
253     $this->entityHandler->init($this->executable, $this->display, $options);
254
255     $this->assertEquals([], $this->entityHandler->calculateDependencies());
256   }
257
258   /**
259    * @covers ::calculateDependencies
260    */
261   public function testCalculateDependenciesWithUuid() {
262     $this->setupEntityManager();
263
264     $uuid = '1d52762e-b9d8-4177-908f-572d1a5845a4';
265     $entity = $this->getMock('Drupal\Core\Entity\EntityInterface');
266     $entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
267     $entity->expects($this->once())
268       ->method('getConfigDependencyName')
269       ->willReturn('entity_test:test-bundle:1d52762e-b9d8-4177-908f-572d1a5845a4');
270     $this->entityStorage->expects($this->never())
271       ->method('load');
272     $this->entityManager->expects($this->once())
273       ->method('loadEntityByConfigTarget')
274       ->willReturn($entity);
275     $entity_type->expects($this->once())
276       ->method('getConfigDependencyKey')
277       ->willReturn('content');
278     $this->entityManager->expects($this->once())
279       ->method('getDefinition')
280       ->willReturn($entity_type);
281
282     $options = [
283       'target' => $uuid,
284     ];
285     $this->entityHandler->init($this->executable, $this->display, $options);
286
287     $this->assertEquals(['content' => ['entity_test:test-bundle:1d52762e-b9d8-4177-908f-572d1a5845a4']], $this->entityHandler->calculateDependencies());
288   }
289
290   /**
291    * @covers ::calculateDependencies
292    */
293   public function testCalculateDependenciesWithEntityId() {
294     $this->setupEntityManager();
295
296     $entity = $this->getMock('Drupal\Core\Entity\EntityInterface');
297     $entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
298     $entity->expects($this->once())
299       ->method('getConfigDependencyName')
300       ->willReturn('entity_test:test-bundle:1d52762e-b9d8-4177-908f-572d1a5845a4');
301     $this->entityManager->expects($this->once())
302       ->method('loadEntityByConfigTarget')
303       ->willReturn($entity);
304     $this->entityStorage->expects($this->never())
305       ->method('loadByProperties');
306     $entity_type->expects($this->once())
307       ->method('getConfigDependencyKey')
308       ->willReturn('content');
309     $this->entityManager->expects($this->once())
310       ->method('getDefinition')
311       ->willReturn($entity_type);
312
313     $options = [
314       'target' => 1,
315     ];
316     $this->entityHandler->init($this->executable, $this->display, $options);
317
318     $this->assertEquals(['content' => ['entity_test:test-bundle:1d52762e-b9d8-4177-908f-572d1a5845a4']], $this->entityHandler->calculateDependencies());
319   }
320
321 }