Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / config_translation / tests / src / Unit / ConfigEntityMapperTest.php
1 <?php
2
3 namespace Drupal\Tests\config_translation\Unit;
4
5 use Drupal\config_translation\ConfigEntityMapper;
6 use Drupal\Core\Url;
7 use Drupal\Tests\UnitTestCase;
8 use Symfony\Component\Routing\Route;
9
10 /**
11  * Tests the functionality provided by the configuration entity mapper.
12  *
13  * @group config_translation
14  */
15 class ConfigEntityMapperTest extends UnitTestCase {
16
17   /**
18    * The configuration entity mapper to test.
19    *
20    * @var \Drupal\config_translation\ConfigEntityMapper
21    */
22   protected $configEntityMapper;
23
24   /**
25    * The entity manager used for testing.
26    *
27    * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
28    */
29   protected $entityManager;
30
31   /**
32    * The entity instance used for testing.
33    *
34    * @var \Drupal\Core\Entity\EntityInterface|\PHPUnit_Framework_MockObject_MockObject
35    */
36   protected $entity;
37
38   /**
39    * The route provider used for testing.
40    *
41    * @var \Drupal\Core\Routing\RouteProviderInterface|\PHPUnit_Framework_MockObject_MockObject
42    */
43   protected $routeProvider;
44
45   /**
46    * The mocked language manager.
47    *
48    * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject
49    */
50   protected $languageManager;
51
52   /**
53    * The mocked event dispatcher.
54    *
55    * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject
56    */
57   protected $eventDispatcher;
58
59   protected function setUp() {
60     $this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
61
62     $this->entity = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityInterface');
63
64     $this->routeProvider = $this->getMock('Drupal\Core\Routing\RouteProviderInterface');
65
66     $this->routeProvider
67       ->expects($this->any())
68       ->method('getRouteByName')
69       ->with('entity.configurable_language.edit_form')
70       ->will($this->returnValue(new Route('/admin/config/regional/language/edit/{configurable_language}')));
71
72     $definition = [
73       'class' => '\Drupal\config_translation\ConfigEntityMapper',
74       'base_route_name' => 'entity.configurable_language.edit_form',
75       'title' => '@label language',
76       'names' => [],
77       'entity_type' => 'configurable_language',
78       'route_name' => 'config_translation.item.overview.entity.configurable_language.edit_form',
79     ];
80
81     $typed_config_manager = $this->getMock('Drupal\Core\Config\TypedConfigManagerInterface');
82
83     $locale_config_manager = $this->getMockBuilder('Drupal\locale\LocaleConfigManager')
84       ->disableOriginalConstructor()
85       ->getMock();
86
87     $this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
88
89     $this->eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
90
91     $this->configEntityMapper = new ConfigEntityMapper(
92       'configurable_language',
93       $definition,
94       $this->getConfigFactoryStub(),
95       $typed_config_manager,
96       $locale_config_manager,
97       $this->getMock('Drupal\config_translation\ConfigMapperManagerInterface'),
98       $this->routeProvider,
99       $this->getStringTranslationStub(),
100       $this->entityManager,
101       $this->languageManager,
102       $this->eventDispatcher
103     );
104   }
105
106   /**
107    * Tests ConfigEntityMapper::setEntity() and ConfigEntityMapper::getEntity().
108    */
109   public function testEntityGetterAndSetter() {
110     $this->entity
111       ->expects($this->once())
112       ->method('id')
113       ->with()
114       ->will($this->returnValue('entity_id'));
115
116     $entity_type = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
117     $entity_type
118       ->expects($this->any())
119       ->method('getConfigPrefix')
120       ->will($this->returnValue('config_prefix'));
121     $this->entityManager
122       ->expects($this->once())
123       ->method('getDefinition')
124       ->with('configurable_language')
125       ->will($this->returnValue($entity_type));
126
127     // No entity is set.
128     $this->assertNull($this->configEntityMapper->getEntity());
129
130     $result = $this->configEntityMapper->setEntity($this->entity);
131     $this->assertTrue($result);
132
133     // Ensure that the getter provides the entity.
134     $this->assertEquals($this->entity, $this->configEntityMapper->getEntity());
135
136     // Ensure that the configuration name was added to the mapper.
137     $plugin_definition = $this->configEntityMapper->getPluginDefinition();
138     $this->assertTrue(in_array('config_prefix.entity_id', $plugin_definition['names']));
139
140     // Make sure setEntity() returns FALSE when called a second time.
141     $result = $this->configEntityMapper->setEntity($this->entity);
142     $this->assertFalse($result);
143   }
144
145   /**
146    * Tests ConfigEntityMapper::getOverviewRouteParameters().
147    */
148   public function testGetOverviewRouteParameters() {
149     $entity_type = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
150     $this->entityManager
151       ->expects($this->once())
152       ->method('getDefinition')
153       ->with('configurable_language')
154       ->will($this->returnValue($entity_type));
155     $this->configEntityMapper->setEntity($this->entity);
156
157     $this->entity
158       ->expects($this->once())
159       ->method('id')
160       ->with()
161       ->will($this->returnValue('entity_id'));
162
163     $result = $this->configEntityMapper->getOverviewRouteParameters();
164
165     $this->assertSame(['configurable_language' => 'entity_id'], $result);
166   }
167
168   /**
169    * Tests ConfigEntityMapper::getType().
170    */
171   public function testGetType() {
172     $result = $this->configEntityMapper->getType();
173     $this->assertSame('configurable_language', $result);
174   }
175
176   /**
177    * Tests ConfigEntityMapper::getTypeName().
178    */
179   public function testGetTypeName() {
180     $entity_type = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
181     $entity_type->expects($this->once())
182       ->method('getLabel')
183       ->will($this->returnValue('test'));
184     $this->entityManager
185       ->expects($this->once())
186       ->method('getDefinition')
187       ->with('configurable_language')
188       ->will($this->returnValue($entity_type));
189
190     $result = $this->configEntityMapper->getTypeName();
191     $this->assertSame('test', $result);
192   }
193
194   /**
195    * Tests ConfigEntityMapper::getTypeLabel().
196    */
197   public function testGetTypeLabel() {
198     $entity_type = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
199     $entity_type->expects($this->once())
200       ->method('getLabel')
201       ->will($this->returnValue('test'));
202     $this->entityManager
203       ->expects($this->once())
204       ->method('getDefinition')
205       ->with('configurable_language')
206       ->will($this->returnValue($entity_type));
207
208     $result = $this->configEntityMapper->getTypeLabel();
209     $this->assertSame('test', $result);
210   }
211
212   /**
213    * Tests ConfigEntityMapper::getOperations().
214    */
215   public function testGetOperations() {
216     $result = $this->configEntityMapper->getOperations();
217
218     $expected = [
219       'list' => [
220         'title' => 'List',
221         'url' => Url::fromRoute('config_translation.entity_list', ['mapper_id' => 'configurable_language']),
222       ],
223     ];
224
225     $this->assertEquals($expected, $result);
226   }
227
228 }