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);
95 $token = $this->getMockBuilder('Drupal\Core\Utility\Token')
96 ->disableOriginalConstructor()
98 $token->expects($this->any())
100 ->willReturnArgument(0);
101 $container = new ContainerBuilder();
102 $container->set('token', $token);
103 \Drupal::setContainer($container);
107 * Ensures that the entity manager returns an entity storage.
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);
121 * Data provider for testing different types of tokens.
125 public function providerTestTokens() {
127 ['{{ raw_arguments.test1 }}', 5],
128 ['{{ arguments.test2 }}', 6],
129 ['{{ test_render_token }}', 7],
130 ['{{ test:global_token }}', 8],
136 * @covers ::defineOptions
139 public function testRenderWithId() {
140 $this->setupEntityManager();
146 /** @var \Drupal\Core\Entity\EntityInterface $entity */
147 $entity = $this->getMock('Drupal\Core\Entity\EntityInterface');
148 $entity->expects($this->once())
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())
159 ->with($entity, 'default')
160 ->willReturn(['#markup' => 'hallo']);
162 $this->entityHandler->init($this->executable, $this->display, $options);
164 $result = $this->entityHandler->render();
165 $this->assertEquals(['#markup' => 'hallo'], $result);
170 * @covers ::defineOptions
173 * @dataProvider providerTestTokens
175 public function testRenderWithIdAndToken($token, $id) {
176 $this->setupEntityManager();
182 $entity = $this->getMock('Drupal\Core\Entity\EntityInterface');
183 $entity->expects($this->once())
187 $this->stylePlugin->expects($this->once())
188 ->method('tokenizeValue')
192 $this->entityStorage->expects($this->never())
193 ->method('loadByProperties');
194 $this->entityStorage->expects($this->once())
197 ->willReturn($entity);
198 $this->entityViewBuilder->expects($this->once())
200 ->with($entity, 'default')
201 ->willReturn(['#markup' => 'hallo']);
203 $this->entityHandler->init($this->executable, $this->display, $options);
205 $result = $this->entityHandler->render();
206 $this->assertEquals(['#markup' => 'hallo'], $result);
211 * @covers ::defineOptions
214 public function testRenderWithUuid() {
215 $this->setupEntityManager();
216 $uuid = '1d52762e-b9d8-4177-908f-572d1a5845a4';
221 $entity = $this->getMock('Drupal\Core\Entity\EntityInterface');
222 $entity->expects($this->once())
226 $this->entityStorage->expects($this->never())
228 $this->entityManager->expects($this->once())
229 ->method('loadEntityByConfigTarget')
230 ->willReturn($entity);
231 $this->entityViewBuilder->expects($this->once())
233 ->with($entity, 'default')
234 ->willReturn(['#markup' => 'hallo']);
236 $this->entityHandler->init($this->executable, $this->display, $options);
238 $result = $this->entityHandler->render();
239 $this->assertEquals(['#markup' => 'hallo'], $result);
243 * @covers ::calculateDependencies
245 * @dataProvider providerTestTokens
247 public function testCalculateDependenciesWithPlaceholder($token, $id) {
248 $this->setupEntityManager();
253 $this->entityHandler->init($this->executable, $this->display, $options);
255 $this->assertEquals([], $this->entityHandler->calculateDependencies());
259 * @covers ::calculateDependencies
261 public function testCalculateDependenciesWithUuid() {
262 $this->setupEntityManager();
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())
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);
285 $this->entityHandler->init($this->executable, $this->display, $options);
287 $this->assertEquals(['content' => ['entity_test:test-bundle:1d52762e-b9d8-4177-908f-572d1a5845a4']], $this->entityHandler->calculateDependencies());
291 * @covers ::calculateDependencies
293 public function testCalculateDependenciesWithEntityId() {
294 $this->setupEntityManager();
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);
316 $this->entityHandler->init($this->executable, $this->display, $options);
318 $this->assertEquals(['content' => ['entity_test:test-bundle:1d52762e-b9d8-4177-908f-572d1a5845a4']], $this->entityHandler->calculateDependencies());