3 namespace Drupal\Tests\Core\Config\Entity;
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\Core\Entity\EntityManager;
7 use Drupal\Core\Entity\EntityTypeManagerInterface;
8 use Drupal\Tests\UnitTestCase;
11 * @coversDefaultClass \Drupal\Core\Entity\EntityDisplayModeBase
14 class EntityDisplayModeBaseUnitTest extends UnitTestCase {
17 * The entity under test.
19 * @var \Drupal\Core\Entity\EntityDisplayModeBase|\PHPUnit_Framework_MockObject_MockObject
24 * The entity type used for testing.
26 * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
28 protected $entityInfo;
31 * The entity manager used for testing.
33 * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
35 protected $entityManager;
38 * The entity type manager used for testing.
40 * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
42 protected $entityTypeManager;
45 * The ID of the type of the entity under test.
49 protected $entityType;
52 * The UUID generator used for testing.
54 * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
61 protected function setUp() {
62 $this->entityType = $this->randomMachineName();
64 $this->entityInfo = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
65 $this->entityInfo->expects($this->any())
66 ->method('getProvider')
67 ->will($this->returnValue('entity'));
69 $this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
71 $this->entityManager = new EntityManager();
73 $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
75 $container = new ContainerBuilder();
76 $container->set('entity.manager', $this->entityManager);
77 $container->set('entity_type.manager', $this->entityTypeManager);
78 $container->set('uuid', $this->uuid);
80 // Inject the container into entity.manager so it can defer to
81 // entity_type.manager.
82 $this->entityManager->setContainer($container);
84 \Drupal::setContainer($container);
88 * @covers ::calculateDependencies
90 public function testCalculateDependencies() {
91 $target_entity_type_id = $this->randomMachineName(16);
93 $target_entity_type = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
94 $target_entity_type->expects($this->any())
95 ->method('getProvider')
96 ->will($this->returnValue('test_module'));
97 $values = ['targetEntityType' => $target_entity_type_id];
99 $this->entityTypeManager->expects($this->at(0))
100 ->method('getDefinition')
101 ->with($target_entity_type_id)
102 ->will($this->returnValue($target_entity_type));
103 $this->entityTypeManager->expects($this->at(1))
104 ->method('getDefinition')
105 ->with($this->entityType)
106 ->will($this->returnValue($this->entityInfo));
108 $this->entity = $this->getMockBuilder('\Drupal\Core\Entity\EntityDisplayModeBase')
109 ->setConstructorArgs([$values, $this->entityType])
110 ->setMethods(['getFilterFormat'])
113 $dependencies = $this->entity->calculateDependencies()->getDependencies();
114 $this->assertContains('test_module', $dependencies['module']);
118 * @covers ::setTargetType
120 public function testSetTargetType() {
122 $mock = $this->getMock(
123 'Drupal\Core\Entity\EntityDisplayModeBase',
125 [['something' => 'nothing'], 'test_type']
129 $bad_target = 'uninitialized';
130 $target = 'test_target_type';
132 // Gain access to the protected property.
133 $property = new \ReflectionProperty($mock, 'targetEntityType');
134 $property->setAccessible(TRUE);
135 // Set the property to a known state.
136 $property->setValue($mock, $bad_target);
138 // Set the target type.
139 $mock->setTargetType($target);
142 $this->assertNotEquals($bad_target, $property->getValue($mock));
143 $this->assertEquals($target, $property->getValue($mock));
147 * @covers ::getTargetType
149 public function testGetTargetType() {
151 $mock = $this->getMock(
152 'Drupal\Core\Entity\EntityDisplayModeBase',
154 [['something' => 'nothing'], 'test_type']
158 $target = 'test_target_type';
160 // Gain access to the protected property.
161 $property = new \ReflectionProperty($mock, 'targetEntityType');
162 $property->setAccessible(TRUE);
163 // Set the property to a known state.
164 $property->setValue($mock, $target);
166 // Get the target type.
167 $value = $mock->getTargetType($target);
170 $this->assertEquals($value, $property->getValue($mock));