Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Config / Entity / EntityDisplayModeBaseUnitTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Config\Entity;
4
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\Core\Entity\EntityManager;
7 use Drupal\Core\Entity\EntityTypeManagerInterface;
8 use Drupal\Tests\UnitTestCase;
9
10 /**
11  * @coversDefaultClass \Drupal\Core\Entity\EntityDisplayModeBase
12  * @group Config
13  */
14 class EntityDisplayModeBaseUnitTest extends UnitTestCase {
15
16   /**
17    * The entity under test.
18    *
19    * @var \Drupal\Core\Entity\EntityDisplayModeBase|\PHPUnit_Framework_MockObject_MockObject
20    */
21   protected $entity;
22
23   /**
24    * The entity type used for testing.
25    *
26    * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
27    */
28   protected $entityInfo;
29
30   /**
31    * The entity manager used for testing.
32    *
33    * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
34    */
35   protected $entityManager;
36
37   /**
38    * The entity type manager used for testing.
39    *
40    * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
41    */
42   protected $entityTypeManager;
43
44   /**
45    * The ID of the type of the entity under test.
46    *
47    * @var string
48    */
49   protected $entityType;
50
51   /**
52    * The UUID generator used for testing.
53    *
54    * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
55    */
56   protected $uuid;
57
58   /**
59    * {@inheritdoc}
60    */
61   protected function setUp() {
62     $this->entityType = $this->randomMachineName();
63
64     $this->entityInfo = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
65     $this->entityInfo->expects($this->any())
66       ->method('getProvider')
67       ->will($this->returnValue('entity'));
68
69     $this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
70
71     $this->entityManager = new EntityManager();
72
73     $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
74
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);
79
80     // Inject the container into entity.manager so it can defer to
81     // entity_type.manager.
82     $this->entityManager->setContainer($container);
83
84     \Drupal::setContainer($container);
85   }
86
87   /**
88    * @covers ::calculateDependencies
89    */
90   public function testCalculateDependencies() {
91     $target_entity_type_id = $this->randomMachineName(16);
92
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];
98
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));
107
108     $this->entity = $this->getMockBuilder('\Drupal\Core\Entity\EntityDisplayModeBase')
109       ->setConstructorArgs([$values, $this->entityType])
110       ->setMethods(['getFilterFormat'])
111       ->getMock();
112
113     $dependencies = $this->entity->calculateDependencies()->getDependencies();
114     $this->assertContains('test_module', $dependencies['module']);
115   }
116
117   /**
118    * @covers ::setTargetType
119    */
120   public function testSetTargetType() {
121     // Generate mock.
122     $mock = $this->getMock(
123       'Drupal\Core\Entity\EntityDisplayModeBase',
124       NULL,
125       [['something' => 'nothing'], 'test_type']
126     );
127
128     // Some test values.
129     $bad_target = 'uninitialized';
130     $target = 'test_target_type';
131
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);
137
138     // Set the target type.
139     $mock->setTargetType($target);
140
141     // Test the outcome.
142     $this->assertNotEquals($bad_target, $property->getValue($mock));
143     $this->assertEquals($target, $property->getValue($mock));
144   }
145
146   /**
147    * @covers ::getTargetType
148    */
149   public function testGetTargetType() {
150     // Generate mock.
151     $mock = $this->getMock(
152       'Drupal\Core\Entity\EntityDisplayModeBase',
153       NULL,
154       [['something' => 'nothing'], 'test_type']
155     );
156
157     // A test value.
158     $target = 'test_target_type';
159
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);
165
166     // Get the target type.
167     $value = $mock->getTargetType($target);
168
169     // Test the outcome.
170     $this->assertEquals($value, $property->getValue($mock));
171   }
172
173 }