3 namespace Drupal\Tests\Core\Config\Entity;
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\Tests\UnitTestCase;
9 * @coversDefaultClass \Drupal\Core\Entity\EntityDisplayModeBase
12 class EntityDisplayModeBaseUnitTest extends UnitTestCase {
15 * The entity under test.
17 * @var \Drupal\Core\Entity\EntityDisplayModeBase|\PHPUnit_Framework_MockObject_MockObject
22 * The entity type used for testing.
24 * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
26 protected $entityInfo;
29 * The entity manager used for testing.
31 * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
33 protected $entityManager;
36 * The ID of the type of the entity under test.
40 protected $entityType;
43 * The UUID generator used for testing.
45 * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
52 protected function setUp() {
53 $this->entityType = $this->randomMachineName();
55 $this->entityInfo = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
56 $this->entityInfo->expects($this->any())
57 ->method('getProvider')
58 ->will($this->returnValue('entity'));
60 $this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
62 $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
64 $container = new ContainerBuilder();
65 $container->set('entity.manager', $this->entityManager);
66 $container->set('uuid', $this->uuid);
67 \Drupal::setContainer($container);
71 * @covers ::calculateDependencies
73 public function testCalculateDependencies() {
74 $target_entity_type_id = $this->randomMachineName(16);
76 $target_entity_type = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
77 $target_entity_type->expects($this->any())
78 ->method('getProvider')
79 ->will($this->returnValue('test_module'));
80 $values = ['targetEntityType' => $target_entity_type_id];
82 $this->entityManager->expects($this->at(0))
83 ->method('getDefinition')
84 ->with($target_entity_type_id)
85 ->will($this->returnValue($target_entity_type));
86 $this->entityManager->expects($this->at(1))
87 ->method('getDefinition')
88 ->with($this->entityType)
89 ->will($this->returnValue($this->entityInfo));
91 $this->entity = $this->getMockBuilder('\Drupal\Core\Entity\EntityDisplayModeBase')
92 ->setConstructorArgs([$values, $this->entityType])
93 ->setMethods(['getFilterFormat'])
96 $dependencies = $this->entity->calculateDependencies()->getDependencies();
97 $this->assertContains('test_module', $dependencies['module']);
101 * @covers ::setTargetType
103 public function testSetTargetType() {
105 $mock = $this->getMock(
106 'Drupal\Core\Entity\EntityDisplayModeBase',
108 [['something' => 'nothing'], 'test_type']
112 $bad_target = 'uninitialized';
113 $target = 'test_target_type';
115 // Gain access to the protected property.
116 $property = new \ReflectionProperty($mock, 'targetEntityType');
117 $property->setAccessible(TRUE);
118 // Set the property to a known state.
119 $property->setValue($mock, $bad_target);
121 // Set the target type.
122 $mock->setTargetType($target);
125 $this->assertNotEquals($bad_target, $property->getValue($mock));
126 $this->assertEquals($target, $property->getValue($mock));
130 * @covers ::getTargetType
132 public function testGetTargetType() {
134 $mock = $this->getMock(
135 'Drupal\Core\Entity\EntityDisplayModeBase',
137 [['something' => 'nothing'], 'test_type']
141 $target = 'test_target_type';
143 // Gain access to the protected property.
144 $property = new \ReflectionProperty($mock, 'targetEntityType');
145 $property->setAccessible(TRUE);
146 // Set the property to a known state.
147 $property->setValue($mock, $target);
149 // Get the target type.
150 $value = $mock->getTargetType($target);
153 $this->assertEquals($value, $property->getValue($mock));