3 namespace Drupal\Tests\config_translation\Unit;
5 use Drupal\config_translation\ConfigEntityMapper;
7 use Drupal\Tests\UnitTestCase;
8 use Symfony\Component\Routing\Route;
11 * Tests the functionality provided by the configuration entity mapper.
13 * @group config_translation
15 class ConfigEntityMapperTest extends UnitTestCase {
18 * The configuration entity mapper to test.
20 * @var \Drupal\config_translation\ConfigEntityMapper
22 protected $configEntityMapper;
25 * The entity manager used for testing.
27 * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
29 protected $entityManager;
32 * The entity instance used for testing.
34 * @var \Drupal\Core\Entity\EntityInterface|\PHPUnit_Framework_MockObject_MockObject
39 * The route provider used for testing.
41 * @var \Drupal\Core\Routing\RouteProviderInterface|\PHPUnit_Framework_MockObject_MockObject
43 protected $routeProvider;
46 * The mocked language manager.
48 * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject
50 protected $languageManager;
53 * The mocked event dispatcher.
55 * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject
57 protected $eventDispatcher;
59 protected function setUp() {
60 $this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
62 $this->entity = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityInterface');
64 $this->routeProvider = $this->getMock('Drupal\Core\Routing\RouteProviderInterface');
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}')));
73 'class' => '\Drupal\config_translation\ConfigEntityMapper',
74 'base_route_name' => 'entity.configurable_language.edit_form',
75 'title' => '@label language',
77 'entity_type' => 'configurable_language',
78 'route_name' => 'config_translation.item.overview.entity.configurable_language.edit_form',
81 $typed_config_manager = $this->getMock('Drupal\Core\Config\TypedConfigManagerInterface');
83 $locale_config_manager = $this->getMockBuilder('Drupal\locale\LocaleConfigManager')
84 ->disableOriginalConstructor()
87 $this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
89 $this->eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
91 $this->configEntityMapper = new ConfigEntityMapper(
92 'configurable_language',
94 $this->getConfigFactoryStub(),
95 $typed_config_manager,
96 $locale_config_manager,
97 $this->getMock('Drupal\config_translation\ConfigMapperManagerInterface'),
99 $this->getStringTranslationStub(),
100 $this->entityManager,
101 $this->languageManager,
102 $this->eventDispatcher
107 * Tests ConfigEntityMapper::setEntity() and ConfigEntityMapper::getEntity().
109 public function testEntityGetterAndSetter() {
111 ->expects($this->once())
114 ->will($this->returnValue('entity_id'));
116 $entity_type = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
118 ->expects($this->any())
119 ->method('getConfigPrefix')
120 ->will($this->returnValue('config_prefix'));
122 ->expects($this->once())
123 ->method('getDefinition')
124 ->with('configurable_language')
125 ->will($this->returnValue($entity_type));
128 $this->assertNull($this->configEntityMapper->getEntity());
130 $result = $this->configEntityMapper->setEntity($this->entity);
131 $this->assertTrue($result);
133 // Ensure that the getter provides the entity.
134 $this->assertEquals($this->entity, $this->configEntityMapper->getEntity());
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']));
140 // Make sure setEntity() returns FALSE when called a second time.
141 $result = $this->configEntityMapper->setEntity($this->entity);
142 $this->assertFalse($result);
146 * Tests ConfigEntityMapper::getOverviewRouteParameters().
148 public function testGetOverviewRouteParameters() {
149 $entity_type = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
151 ->expects($this->once())
152 ->method('getDefinition')
153 ->with('configurable_language')
154 ->will($this->returnValue($entity_type));
155 $this->configEntityMapper->setEntity($this->entity);
158 ->expects($this->once())
161 ->will($this->returnValue('entity_id'));
163 $result = $this->configEntityMapper->getOverviewRouteParameters();
165 $this->assertSame(['configurable_language' => 'entity_id'], $result);
169 * Tests ConfigEntityMapper::getType().
171 public function testGetType() {
172 $result = $this->configEntityMapper->getType();
173 $this->assertSame('configurable_language', $result);
177 * Tests ConfigEntityMapper::getTypeName().
179 public function testGetTypeName() {
180 $entity_type = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
181 $entity_type->expects($this->once())
183 ->will($this->returnValue('test'));
185 ->expects($this->once())
186 ->method('getDefinition')
187 ->with('configurable_language')
188 ->will($this->returnValue($entity_type));
190 $result = $this->configEntityMapper->getTypeName();
191 $this->assertSame('test', $result);
195 * Tests ConfigEntityMapper::getTypeLabel().
197 public function testGetTypeLabel() {
198 $entity_type = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
199 $entity_type->expects($this->once())
201 ->will($this->returnValue('test'));
203 ->expects($this->once())
204 ->method('getDefinition')
205 ->with('configurable_language')
206 ->will($this->returnValue($entity_type));
208 $result = $this->configEntityMapper->getTypeLabel();
209 $this->assertSame('test', $result);
213 * Tests ConfigEntityMapper::getOperations().
215 public function testGetOperations() {
216 $result = $this->configEntityMapper->getOperations();
221 'url' => Url::fromRoute('config_translation.entity_list', ['mapper_id' => 'configurable_language']),
225 $this->assertEquals($expected, $result);