Upgraded drupal core with security updates
[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
96     $token = $this->getMockBuilder('Drupal\Core\Utility\Token')
97       ->disableOriginalConstructor()
98       ->getMock();
99     $token->expects($this->any())
100       ->method('replace')
101       ->willReturnArgument(0);
102     $container = new ContainerBuilder();
103     $container->set('token', $token);
104     \Drupal::setContainer($container);
105   }
106
107   /**
108    * Ensures that the entity manager returns an entity storage.
109    */
110   protected function setupEntityManager() {
111     $this->entityManager->expects($this->any())
112       ->method('getStorage')
113       ->with('entity_test')
114       ->willReturn($this->entityStorage);
115     $this->entityManager->expects($this->any())
116       ->method('getViewBuilder')
117       ->with('entity_test')
118       ->willReturn($this->entityViewBuilder);
119   }
120
121   /**
122    * Data provider for testing different types of tokens.
123    *
124    * @return array
125    */
126   public function providerTestTokens() {
127     return [
128       ['{{ raw_arguments.test1 }}', 5],
129       ['{{ arguments.test2 }}', 6],
130       ['{{ test_render_token }}', 7],
131       ['{{ test:global_token }}', 8],
132     ];
133   }
134
135   /**
136    * @covers ::render
137    * @covers ::defineOptions
138    * @covers ::init
139    */
140   public function testRenderWithId() {
141     $this->setupEntityManager();
142     $options = [
143       'target' => 1,
144       'tokenize' => FALSE,
145     ];
146
147     /** @var \Drupal\Core\Entity\EntityInterface $entity */
148     $entity = $this->getMock('Drupal\Core\Entity\EntityInterface');
149     $entity->expects($this->once())
150       ->method('access')
151       ->willReturn(TRUE);
152
153     $this->entityStorage->expects($this->never())
154       ->method('loadByProperties');
155     $this->entityManager->expects($this->any())
156       ->method('loadEntityByConfigTarget')
157       ->willReturn($entity);
158     $this->entityViewBuilder->expects($this->once())
159       ->method('view')
160       ->with($entity, 'default')
161       ->willReturn(['#markup' => 'hallo']);
162
163     $this->entityHandler->init($this->executable, $this->display, $options);
164
165     $result = $this->entityHandler->render();
166     $this->assertEquals(['#markup' => 'hallo'], $result);
167   }
168
169   /**
170    * @covers ::render
171    * @covers ::defineOptions
172    * @covers ::init
173    *
174    * @dataProvider providerTestTokens
175    */
176   public function testRenderWithIdAndToken($token, $id) {
177     $this->setupEntityManager();
178     $options = [
179       'target' => $token,
180       'tokenize' => TRUE,
181     ];
182
183     $entity = $this->getMock('Drupal\Core\Entity\EntityInterface');
184     $entity->expects($this->once())
185       ->method('access')
186       ->willReturn(TRUE);
187
188     $this->stylePlugin->expects($this->once())
189       ->method('tokenizeValue')
190       ->with($token, 0)
191       ->willReturn($id);
192
193     $this->entityStorage->expects($this->never())
194       ->method('loadByProperties');
195     $this->entityStorage->expects($this->once())
196       ->method('load')
197       ->with($id)
198       ->willReturn($entity);
199     $this->entityViewBuilder->expects($this->once())
200       ->method('view')
201       ->with($entity, 'default')
202       ->willReturn(['#markup' => 'hallo']);
203
204     $this->entityHandler->init($this->executable, $this->display, $options);
205
206     $result = $this->entityHandler->render();
207     $this->assertEquals(['#markup' => 'hallo'], $result);
208   }
209
210   /**
211    * @covers ::render
212    * @covers ::defineOptions
213    * @covers ::init
214    */
215   public function testRenderWithUuid() {
216     $this->setupEntityManager();
217     $uuid = '1d52762e-b9d8-4177-908f-572d1a5845a4';
218     $options = [
219       'target' => $uuid,
220       'tokenize' => FALSE,
221     ];
222     $entity = $this->getMock('Drupal\Core\Entity\EntityInterface');
223     $entity->expects($this->once())
224       ->method('access')
225       ->willReturn(TRUE);
226
227     $this->entityStorage->expects($this->never())
228       ->method('load');
229     $this->entityManager->expects($this->once())
230       ->method('loadEntityByConfigTarget')
231       ->willReturn($entity);
232     $this->entityViewBuilder->expects($this->once())
233       ->method('view')
234       ->with($entity, 'default')
235       ->willReturn(['#markup' => 'hallo']);
236
237     $this->entityHandler->init($this->executable, $this->display, $options);
238
239     $result = $this->entityHandler->render();
240     $this->assertEquals(['#markup' => 'hallo'], $result);
241   }
242
243   /**
244    * @covers ::calculateDependencies
245    *
246    * @dataProvider providerTestTokens
247    */
248   public function testCalculateDependenciesWithPlaceholder($token, $id) {
249     $this->setupEntityManager();
250
251     $options = [
252       'target' => $token,
253     ];
254     $this->entityHandler->init($this->executable, $this->display, $options);
255
256     $this->assertEquals([], $this->entityHandler->calculateDependencies());
257   }
258
259   /**
260    * @covers ::calculateDependencies
261    */
262   public function testCalculateDependenciesWithUuid() {
263     $this->setupEntityManager();
264
265     $uuid = '1d52762e-b9d8-4177-908f-572d1a5845a4';
266     $entity = $this->getMock('Drupal\Core\Entity\EntityInterface');
267     $entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
268     $entity->expects($this->once())
269       ->method('getConfigDependencyName')
270       ->willReturn('entity_test:test-bundle:1d52762e-b9d8-4177-908f-572d1a5845a4');
271     $this->entityStorage->expects($this->never())
272       ->method('load');
273     $this->entityManager->expects($this->once())
274       ->method('loadEntityByConfigTarget')
275       ->willReturn($entity);
276     $entity_type->expects($this->once())
277       ->method('getConfigDependencyKey')
278       ->willReturn('content');
279     $this->entityManager->expects($this->once())
280       ->method('getDefinition')
281       ->willReturn($entity_type);
282
283     $options = [
284       'target' => $uuid,
285     ];
286     $this->entityHandler->init($this->executable, $this->display, $options);
287
288     $this->assertEquals(['content' => ['entity_test:test-bundle:1d52762e-b9d8-4177-908f-572d1a5845a4']], $this->entityHandler->calculateDependencies());
289   }
290
291   /**
292    * @covers ::calculateDependencies
293    */
294   public function testCalculateDependenciesWithEntityId() {
295     $this->setupEntityManager();
296
297     $entity = $this->getMock('Drupal\Core\Entity\EntityInterface');
298     $entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
299     $entity->expects($this->once())
300       ->method('getConfigDependencyName')
301       ->willReturn('entity_test:test-bundle:1d52762e-b9d8-4177-908f-572d1a5845a4');
302     $this->entityManager->expects($this->once())
303       ->method('loadEntityByConfigTarget')
304       ->willReturn($entity);
305     $this->entityStorage->expects($this->never())
306       ->method('loadByProperties');
307     $entity_type->expects($this->once())
308       ->method('getConfigDependencyKey')
309       ->willReturn('content');
310     $this->entityManager->expects($this->once())
311       ->method('getDefinition')
312       ->willReturn($entity_type);
313
314     $options = [
315       'target' => 1,
316     ];
317     $this->entityHandler->init($this->executable, $this->display, $options);
318
319     $this->assertEquals(['content' => ['entity_test:test-bundle:1d52762e-b9d8-4177-908f-572d1a5845a4']], $this->entityHandler->calculateDependencies());
320   }
321
322 }