Version 1
[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\Tests\UnitTestCase;
7
8 /**
9  * @coversDefaultClass \Drupal\Core\Entity\EntityDisplayModeBase
10  * @group Config
11  */
12 class EntityDisplayModeBaseUnitTest extends UnitTestCase {
13
14   /**
15    * The entity under test.
16    *
17    * @var \Drupal\Core\Entity\EntityDisplayModeBase|\PHPUnit_Framework_MockObject_MockObject
18    */
19   protected $entity;
20
21   /**
22    * The entity type used for testing.
23    *
24    * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
25    */
26   protected $entityInfo;
27
28   /**
29    * The entity manager used for testing.
30    *
31    * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
32    */
33   protected $entityManager;
34
35   /**
36    * The ID of the type of the entity under test.
37    *
38    * @var string
39    */
40   protected $entityType;
41
42   /**
43    * The UUID generator used for testing.
44    *
45    * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
46    */
47   protected $uuid;
48
49   /**
50    * {@inheritdoc}
51    */
52   protected function setUp() {
53     $this->entityType = $this->randomMachineName();
54
55     $this->entityInfo = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
56     $this->entityInfo->expects($this->any())
57       ->method('getProvider')
58       ->will($this->returnValue('entity'));
59
60     $this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
61
62     $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
63
64     $container = new ContainerBuilder();
65     $container->set('entity.manager', $this->entityManager);
66     $container->set('uuid', $this->uuid);
67     \Drupal::setContainer($container);
68   }
69
70   /**
71    * @covers ::calculateDependencies
72    */
73   public function testCalculateDependencies() {
74     $target_entity_type_id = $this->randomMachineName(16);
75
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];
81
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));
90
91     $this->entity = $this->getMockBuilder('\Drupal\Core\Entity\EntityDisplayModeBase')
92       ->setConstructorArgs([$values, $this->entityType])
93       ->setMethods(['getFilterFormat'])
94       ->getMock();
95
96     $dependencies = $this->entity->calculateDependencies()->getDependencies();
97     $this->assertContains('test_module', $dependencies['module']);
98   }
99
100   /**
101    * @covers ::setTargetType
102    */
103   public function testSetTargetType() {
104     // Generate mock.
105     $mock = $this->getMock(
106       'Drupal\Core\Entity\EntityDisplayModeBase',
107       NULL,
108       [['something' => 'nothing'], 'test_type']
109     );
110
111     // Some test values.
112     $bad_target = 'uninitialized';
113     $target = 'test_target_type';
114
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);
120
121     // Set the target type.
122     $mock->setTargetType($target);
123
124     // Test the outcome.
125     $this->assertNotEquals($bad_target, $property->getValue($mock));
126     $this->assertEquals($target, $property->getValue($mock));
127   }
128
129   /**
130    * @covers ::getTargetType
131    */
132   public function testGetTargetType() {
133     // Generate mock.
134     $mock = $this->getMock(
135       'Drupal\Core\Entity\EntityDisplayModeBase',
136       NULL,
137       [['something' => 'nothing'], 'test_type']
138     );
139
140     // A test value.
141     $target = 'test_target_type';
142
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);
148
149     // Get the target type.
150     $value = $mock->getTargetType($target);
151
152     // Test the outcome.
153     $this->assertEquals($value, $property->getValue($mock));
154   }
155
156 }