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 $language_manager|\PHPUnit_Framework_MockObject_MockObject
50 protected $languageManager;
52 protected function setUp() {
53 $this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
55 $this->entity = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityInterface');
57 $this->routeProvider = $this->getMock('Drupal\Core\Routing\RouteProviderInterface');
60 ->expects($this->any())
61 ->method('getRouteByName')
62 ->with('entity.configurable_language.edit_form')
63 ->will($this->returnValue(new Route('/admin/config/regional/language/edit/{configurable_language}')));
66 'class' => '\Drupal\config_translation\ConfigEntityMapper',
67 'base_route_name' => 'entity.configurable_language.edit_form',
68 'title' => '@label language',
70 'entity_type' => 'configurable_language',
71 'route_name' => 'config_translation.item.overview.entity.configurable_language.edit_form',
74 $typed_config_manager = $this->getMock('Drupal\Core\Config\TypedConfigManagerInterface');
76 $locale_config_manager = $this->getMockBuilder('Drupal\locale\LocaleConfigManager')
77 ->disableOriginalConstructor()
80 $this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
82 $this->configEntityMapper = new ConfigEntityMapper(
83 'configurable_language',
85 $this->getConfigFactoryStub(),
86 $typed_config_manager,
87 $locale_config_manager,
88 $this->getMock('Drupal\config_translation\ConfigMapperManagerInterface'),
90 $this->getStringTranslationStub(),
92 $this->languageManager
97 * Tests ConfigEntityMapper::setEntity() and ConfigEntityMapper::getEntity().
99 public function testEntityGetterAndSetter() {
101 ->expects($this->once())
104 ->will($this->returnValue('entity_id'));
106 $entity_type = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
108 ->expects($this->any())
109 ->method('getConfigPrefix')
110 ->will($this->returnValue('config_prefix'));
112 ->expects($this->once())
113 ->method('getDefinition')
114 ->with('configurable_language')
115 ->will($this->returnValue($entity_type));
118 $this->assertNull($this->configEntityMapper->getEntity());
120 $result = $this->configEntityMapper->setEntity($this->entity);
121 $this->assertTrue($result);
123 // Ensure that the getter provides the entity.
124 $this->assertEquals($this->entity, $this->configEntityMapper->getEntity());
126 // Ensure that the configuration name was added to the mapper.
127 $plugin_definition = $this->configEntityMapper->getPluginDefinition();
128 $this->assertTrue(in_array('config_prefix.entity_id', $plugin_definition['names']));
130 // Make sure setEntity() returns FALSE when called a second time.
131 $result = $this->configEntityMapper->setEntity($this->entity);
132 $this->assertFalse($result);
136 * Tests ConfigEntityMapper::getOverviewRouteParameters().
138 public function testGetOverviewRouteParameters() {
139 $entity_type = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
141 ->expects($this->once())
142 ->method('getDefinition')
143 ->with('configurable_language')
144 ->will($this->returnValue($entity_type));
145 $this->configEntityMapper->setEntity($this->entity);
148 ->expects($this->once())
151 ->will($this->returnValue('entity_id'));
153 $result = $this->configEntityMapper->getOverviewRouteParameters();
155 $this->assertSame(['configurable_language' => 'entity_id'], $result);
159 * Tests ConfigEntityMapper::getType().
161 public function testGetType() {
162 $result = $this->configEntityMapper->getType();
163 $this->assertSame('configurable_language', $result);
167 * Tests ConfigEntityMapper::getTypeName().
169 public function testGetTypeName() {
170 $entity_type = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
171 $entity_type->expects($this->once())
173 ->will($this->returnValue('test'));
175 ->expects($this->once())
176 ->method('getDefinition')
177 ->with('configurable_language')
178 ->will($this->returnValue($entity_type));
180 $result = $this->configEntityMapper->getTypeName();
181 $this->assertSame('test', $result);
185 * Tests ConfigEntityMapper::getTypeLabel().
187 public function testGetTypeLabel() {
188 $entity_type = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
189 $entity_type->expects($this->once())
191 ->will($this->returnValue('test'));
193 ->expects($this->once())
194 ->method('getDefinition')
195 ->with('configurable_language')
196 ->will($this->returnValue($entity_type));
198 $result = $this->configEntityMapper->getTypeLabel();
199 $this->assertSame('test', $result);
203 * Tests ConfigEntityMapper::getOperations().
205 public function testGetOperations() {
206 $result = $this->configEntityMapper->getOperations();
211 'url' => Url::fromRoute('config_translation.entity_list', ['mapper_id' => 'configurable_language']),
215 $this->assertEquals($expected, $result);