3 namespace Drupal\Tests\views\Unit\Plugin\area;
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\views\Plugin\views\area\Entity;
7 use Symfony\Component\DependencyInjection\ContainerBuilder;
10 * @coversDefaultClass \Drupal\views\Plugin\views\area\Entity
13 class EntityTest extends UnitTestCase {
16 * The tested entity area handler.
18 * @var \Drupal\views\Plugin\views\area\Entity
20 protected $entityHandler;
23 * The mocked entity manager.
25 * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
27 protected $entityManager;
30 * The mocked entity storage.
32 * @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
34 protected $entityStorage;
37 * The mocked entity view builder.
39 * @var \Drupal\Core\Entity\EntityViewBuilderInterface|\PHPUnit_Framework_MockObject_MockObject
41 protected $entityViewBuilder;
44 * The mocked view executable.
46 * @var \Drupal\views\ViewExecutable|\PHPUnit_Framework_MockObject_MockObject
48 protected $executable;
53 * @var \Drupal\views\Plugin\views\display\DisplayPluginBase|\PHPUnit_Framework_MockObject_MockObject
58 * The mocked style plugin.
60 * @var \Drupal\views\Plugin\views\style\StylePluginBase|\PHPUnit_Framework_MockObject_MockObject
62 protected $stylePlugin;
67 protected function setUp() {
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');
74 $this->executable = $this->getMockBuilder('Drupal\views\ViewExecutable')
75 ->disableOriginalConstructor()
77 $this->display = $this->getMockBuilder('Drupal\views\Plugin\views\display\DisplayPluginBase')
78 ->disableOriginalConstructor()
80 $this->stylePlugin = $this->getMockBuilder('Drupal\views\Plugin\views\style\StylePluginBase')
81 ->disableOriginalConstructor()
83 $this->executable->style_plugin = $this->stylePlugin;
85 $this->entityHandler = new Entity([], 'entity', ['entity_type' => 'entity_test'], $this->entityManager);
87 $this->display->expects($this->any())
90 ->willReturn($this->stylePlugin);
91 $this->executable->expects($this->any())
93 ->willReturn($this->stylePlugin);
96 $token = $this->getMockBuilder('Drupal\Core\Utility\Token')
97 ->disableOriginalConstructor()
99 $token->expects($this->any())
101 ->willReturnArgument(0);
102 $container = new ContainerBuilder();
103 $container->set('token', $token);
104 \Drupal::setContainer($container);
108 * Ensures that the entity manager returns an entity storage.
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);
122 * Data provider for testing different types of tokens.
126 public function providerTestTokens() {
128 ['{{ raw_arguments.test1 }}', 5],
129 ['{{ arguments.test2 }}', 6],
130 ['{{ test_render_token }}', 7],
131 ['{{ test:global_token }}', 8],
137 * @covers ::defineOptions
140 public function testRenderWithId() {
141 $this->setupEntityManager();
147 /** @var \Drupal\Core\Entity\EntityInterface $entity */
148 $entity = $this->getMock('Drupal\Core\Entity\EntityInterface');
149 $entity->expects($this->once())
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())
160 ->with($entity, 'default')
161 ->willReturn(['#markup' => 'hallo']);
163 $this->entityHandler->init($this->executable, $this->display, $options);
165 $result = $this->entityHandler->render();
166 $this->assertEquals(['#markup' => 'hallo'], $result);
171 * @covers ::defineOptions
174 * @dataProvider providerTestTokens
176 public function testRenderWithIdAndToken($token, $id) {
177 $this->setupEntityManager();
183 $entity = $this->getMock('Drupal\Core\Entity\EntityInterface');
184 $entity->expects($this->once())
188 $this->stylePlugin->expects($this->once())
189 ->method('tokenizeValue')
193 $this->entityStorage->expects($this->never())
194 ->method('loadByProperties');
195 $this->entityStorage->expects($this->once())
198 ->willReturn($entity);
199 $this->entityViewBuilder->expects($this->once())
201 ->with($entity, 'default')
202 ->willReturn(['#markup' => 'hallo']);
204 $this->entityHandler->init($this->executable, $this->display, $options);
206 $result = $this->entityHandler->render();
207 $this->assertEquals(['#markup' => 'hallo'], $result);
212 * @covers ::defineOptions
215 public function testRenderWithUuid() {
216 $this->setupEntityManager();
217 $uuid = '1d52762e-b9d8-4177-908f-572d1a5845a4';
222 $entity = $this->getMock('Drupal\Core\Entity\EntityInterface');
223 $entity->expects($this->once())
227 $this->entityStorage->expects($this->never())
229 $this->entityManager->expects($this->once())
230 ->method('loadEntityByConfigTarget')
231 ->willReturn($entity);
232 $this->entityViewBuilder->expects($this->once())
234 ->with($entity, 'default')
235 ->willReturn(['#markup' => 'hallo']);
237 $this->entityHandler->init($this->executable, $this->display, $options);
239 $result = $this->entityHandler->render();
240 $this->assertEquals(['#markup' => 'hallo'], $result);
244 * @covers ::calculateDependencies
246 * @dataProvider providerTestTokens
248 public function testCalculateDependenciesWithPlaceholder($token, $id) {
249 $this->setupEntityManager();
254 $this->entityHandler->init($this->executable, $this->display, $options);
256 $this->assertEquals([], $this->entityHandler->calculateDependencies());
260 * @covers ::calculateDependencies
262 public function testCalculateDependenciesWithUuid() {
263 $this->setupEntityManager();
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())
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);
286 $this->entityHandler->init($this->executable, $this->display, $options);
288 $this->assertEquals(['content' => ['entity_test:test-bundle:1d52762e-b9d8-4177-908f-572d1a5845a4']], $this->entityHandler->calculateDependencies());
292 * @covers ::calculateDependencies
294 public function testCalculateDependenciesWithEntityId() {
295 $this->setupEntityManager();
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);
317 $this->entityHandler->init($this->executable, $this->display, $options);
319 $this->assertEquals(['content' => ['entity_test:test-bundle:1d52762e-b9d8-4177-908f-572d1a5845a4']], $this->entityHandler->calculateDependencies());