5 * Contains \Drupal\Tests\config_translation\Unit\ConfigNamesMapperTest.
8 namespace Drupal\Tests\config_translation\Unit;
10 use Drupal\config_translation\ConfigNamesMapper;
11 use Drupal\Core\Config\ConfigFactoryInterface;
12 use Drupal\Core\DependencyInjection\ContainerBuilder;
13 use Drupal\Core\Language\Language;
14 use Drupal\Core\Routing\RouteMatch;
16 use Drupal\Tests\UnitTestCase;
17 use Symfony\Component\Routing\Route;
20 * Tests the functionality provided by the configuration names mapper.
22 * @group config_translation
24 class ConfigNamesMapperTest extends UnitTestCase {
27 * The plugin definition of the test mapper.
31 protected $pluginDefinition;
34 * The configuration names mapper to test.
36 * @see \Drupal\config_translation\ConfigNamesMapper
38 * @var \Drupal\Tests\config_translation\Unit\TestConfigNamesMapper
40 protected $configNamesMapper;
43 * The locale configuration manager.
45 * @var \Drupal\locale\LocaleConfigManager|\PHPUnit_Framework_MockObject_MockObject
47 protected $localeConfigManager;
50 * The locale configuration manager.
52 * @var \Drupal\locale\LocaleConfigManager|\PHPUnit_Framework_MockObject_MockObject
54 protected $typedConfigManager;
57 * The configuration mapper manager.
59 * @var \Drupal\config_translation\ConfigMapperManagerInterface|\PHPUnit_Framework_MockObject_MockObject
61 protected $configMapperManager;
64 * The base route used for testing.
66 * @var \Symfony\Component\Routing\Route
71 * The route provider used for testing.
73 * @var \Drupal\Core\Routing\RouteProviderInterface|\PHPUnit_Framework_MockObject_MockObject
75 protected $routeProvider;
78 * The mocked URL generator.
80 * @var \Drupal\Core\Routing\UrlGeneratorInterface|\PHPUnit_Framework_MockObject_MockObject
82 protected $urlGenerator;
85 * The mocked language manager.
87 * @var \Drupal\Core\Language\LanguageManagerInterface $language_manager|\PHPUnit_Framework_MockObject_MockObject
89 protected $languageManager;
91 protected function setUp() {
92 $this->routeProvider = $this->getMock('Drupal\Core\Routing\RouteProviderInterface');
94 $this->pluginDefinition = [
95 'class' => '\Drupal\config_translation\ConfigNamesMapper',
96 'base_route_name' => 'system.site_information_settings',
97 'title' => 'System information',
98 'names' => ['system.site'],
102 $this->typedConfigManager = $this->getMock('Drupal\Core\Config\TypedConfigManagerInterface');
104 $this->localeConfigManager = $this->getMockBuilder('Drupal\locale\LocaleConfigManager')
105 ->disableOriginalConstructor()
108 $this->configMapperManager = $this->getMock('Drupal\config_translation\ConfigMapperManagerInterface');
110 $this->urlGenerator = $this->getMock('Drupal\Core\Routing\UrlGeneratorInterface');
111 $container = new ContainerBuilder();
112 $container->set('url_generator', $this->urlGenerator);
113 \Drupal::setContainer($container);
115 $this->baseRoute = new Route('/admin/config/system/site-information');
118 ->expects($this->any())
119 ->method('getRouteByName')
120 ->with('system.site_information_settings')
121 ->will($this->returnValue($this->baseRoute));
123 $this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
125 $this->configNamesMapper = new TestConfigNamesMapper(
126 'system.site_information_settings',
127 $this->pluginDefinition,
128 $this->getConfigFactoryStub(),
129 $this->typedConfigManager,
130 $this->localeConfigManager,
131 $this->configMapperManager,
132 $this->routeProvider,
133 $this->getStringTranslationStub(),
134 $this->languageManager
139 * Tests ConfigNamesMapper::getTitle().
141 public function testGetTitle() {
142 $result = $this->configNamesMapper->getTitle();
143 $this->assertSame($this->pluginDefinition['title'], (string) $result);
147 * Tests ConfigNamesMapper::getBaseRouteName().
149 public function testGetBaseRouteName() {
150 $result = $this->configNamesMapper->getBaseRouteName();
151 $this->assertSame($this->pluginDefinition['base_route_name'], $result);
155 * Tests ConfigNamesMapper::getBaseRouteParameters().
157 public function testGetBaseRouteParameters() {
158 $result = $this->configNamesMapper->getBaseRouteParameters();
159 $this->assertSame([], $result);
163 * Tests ConfigNamesMapper::getBaseRoute().
165 public function testGetBaseRoute() {
166 $result = $this->configNamesMapper->getBaseRoute();
167 $this->assertSame($this->baseRoute, $result);
171 * Tests ConfigNamesMapper::getBasePath().
173 public function testGetBasePath() {
174 $this->urlGenerator->expects($this->once())
175 ->method('getPathFromRoute')
176 ->with('system.site_information_settings', [])
177 ->willReturn('/admin/config/system/site-information');
178 $result = $this->configNamesMapper->getBasePath();
179 $this->assertSame('/admin/config/system/site-information', $result);
183 * Tests ConfigNamesMapper::getOverviewRouteName().
185 public function testGetOverviewRouteName() {
186 $result = $this->configNamesMapper->getOverviewRouteName();
187 $expected = 'config_translation.item.overview.' . $this->pluginDefinition['base_route_name'];
188 $this->assertSame($expected, $result);
192 * Tests ConfigNamesMapper::getOverviewRouteParameters().
194 public function testGetOverviewRouteParameters() {
195 $result = $this->configNamesMapper->getOverviewRouteParameters();
196 $this->assertSame([], $result);
200 * Tests ConfigNamesMapper::getOverviewRoute().
202 public function testGetOverviewRoute() {
203 $expected = new Route('/admin/config/system/site-information/translate',
205 '_controller' => '\Drupal\config_translation\Controller\ConfigTranslationController::itemPage',
206 'plugin_id' => 'system.site_information_settings',
209 '_config_translation_overview_access' => 'TRUE',
212 $result = $this->configNamesMapper->getOverviewRoute();
213 $this->assertSame(serialize($expected), serialize($result));
217 * Tests ConfigNamesMapper::getOverviewPath().
219 public function testGetOverviewPath() {
220 $this->urlGenerator->expects($this->once())
221 ->method('getPathFromRoute')
222 ->with('config_translation.item.overview.system.site_information_settings', [])
223 ->willReturn('/admin/config/system/site-information/translate');
225 $result = $this->configNamesMapper->getOverviewPath();
226 $this->assertSame('/admin/config/system/site-information/translate', $result);
230 * Tests ConfigNamesMapper::getAddRouteName().
232 public function testGetAddRouteName() {
233 $result = $this->configNamesMapper->getAddRouteName();
234 $expected = 'config_translation.item.add.' . $this->pluginDefinition['base_route_name'];
235 $this->assertSame($expected, $result);
239 * Tests ConfigNamesMapper::getAddRouteParameters().
241 public function testGetAddRouteParameters() {
242 $route_match = new RouteMatch('example', new Route('/test/{langcode}'), ['langcode' => 'xx']);
243 $this->configNamesMapper->populateFromRouteMatch($route_match);
245 $expected = ['langcode' => 'xx'];
246 $result = $this->configNamesMapper->getAddRouteParameters();
247 $this->assertSame($expected, $result);
251 * Tests ConfigNamesMapper::getAddRoute().
253 public function testGetAddRoute() {
254 $expected = new Route('/admin/config/system/site-information/translate/{langcode}/add',
256 '_form' => '\Drupal\config_translation\Form\ConfigTranslationAddForm',
257 'plugin_id' => 'system.site_information_settings',
260 '_config_translation_form_access' => 'TRUE',
263 $result = $this->configNamesMapper->getAddRoute();
264 $this->assertSame(serialize($expected), serialize($result));
268 * Tests ConfigNamesMapper::getEditRouteName().
270 public function testGetEditRouteName() {
271 $result = $this->configNamesMapper->getEditRouteName();
272 $expected = 'config_translation.item.edit.' . $this->pluginDefinition['base_route_name'];
273 $this->assertSame($expected, $result);
277 * Tests ConfigNamesMapper::getEditRouteParameters().
279 public function testGetEditRouteParameters() {
280 $route_match = new RouteMatch('example', new Route('/test/{langcode}'), ['langcode' => 'xx']);
281 $this->configNamesMapper->populateFromRouteMatch($route_match);
283 $expected = ['langcode' => 'xx'];
284 $result = $this->configNamesMapper->getEditRouteParameters();
285 $this->assertSame($expected, $result);
289 * Tests ConfigNamesMapper::getEditRoute().
291 public function testGetEditRoute() {
292 $expected = new Route('/admin/config/system/site-information/translate/{langcode}/edit',
294 '_form' => '\Drupal\config_translation\Form\ConfigTranslationEditForm',
295 'plugin_id' => 'system.site_information_settings',
298 '_config_translation_form_access' => 'TRUE',
301 $result = $this->configNamesMapper->getEditRoute();
302 $this->assertSame(serialize($expected), serialize($result));
306 * Tests ConfigNamesMapper::getDeleteRouteName().
308 public function testGetDeleteRouteName() {
309 $result = $this->configNamesMapper->getDeleteRouteName();
310 $expected = 'config_translation.item.delete.' . $this->pluginDefinition['base_route_name'];
311 $this->assertSame($expected, $result);
315 * Tests ConfigNamesMapper::getDeleteRouteParameters().
317 public function testGetDeleteRouteParameters() {
318 $route_match = new RouteMatch('example', new Route('/test/{langcode}'), ['langcode' => 'xx']);
319 $this->configNamesMapper->populateFromRouteMatch($route_match);
321 $expected = ['langcode' => 'xx']; $result = $this->configNamesMapper->getDeleteRouteParameters();
322 $this->assertSame($expected, $result);
326 * Tests ConfigNamesMapper::getRoute().
328 public function testGetDeleteRoute() {
329 $expected = new Route('/admin/config/system/site-information/translate/{langcode}/delete',
331 '_form' => '\Drupal\config_translation\Form\ConfigTranslationDeleteForm',
332 'plugin_id' => 'system.site_information_settings',
335 '_config_translation_form_access' => 'TRUE',
338 $result = $this->configNamesMapper->getDeleteRoute();
339 $this->assertSame(serialize($expected), serialize($result));
343 * Tests ConfigNamesMapper::getConfigNames().
345 public function testGetConfigNames() {
346 $result = $this->configNamesMapper->getConfigNames();
347 $this->assertSame($this->pluginDefinition['names'], $result);
351 * Tests ConfigNamesMapper::addConfigName().
353 public function testAddConfigName() {
354 $names = $this->configNamesMapper->getConfigNames();
355 $this->configNamesMapper->addConfigName('test');
357 $result = $this->configNamesMapper->getConfigNames();
358 $this->assertSame($names, $result);
362 * Tests ConfigNamesMapper::getWeight().
364 public function testGetWeight() {
365 $result = $this->configNamesMapper->getWeight();
366 $this->assertSame($this->pluginDefinition['weight'], $result);
370 * Tests ConfigNamesMapper::populateFromRouteMatch().
372 public function testPopulateFromRouteMatch() {
373 // Make sure the language code is not set initially.
374 $this->assertSame(NULL, $this->configNamesMapper->getInternalLangcode());
376 // Test that an empty request does not set the language code.
377 $route_match = new RouteMatch('example', new Route('/test/{langcode}'));
378 $this->configNamesMapper->populateFromRouteMatch($route_match);
379 $this->assertSame(NULL, $this->configNamesMapper->getInternalLangcode());
381 // Test that a request with a 'langcode' attribute sets the language code.
382 $route_match = new RouteMatch('example', new Route('/test/{langcode}'), ['langcode' => 'xx']);
383 $this->configNamesMapper->populateFromRouteMatch($route_match);
384 $this->assertSame('xx', $this->configNamesMapper->getInternalLangcode());
386 // Test that the language code gets unset with the wrong request.
387 $route_match = new RouteMatch('example', new Route('/test/{langcode}'));
388 $this->configNamesMapper->populateFromRouteMatch($route_match);
389 $this->assertSame(NULL, $this->configNamesMapper->getInternalLangcode());
393 * Tests ConfigNamesMapper::getTypeLabel().
395 public function testGetTypeLabel() {
396 $result = $this->configNamesMapper->getTypeLabel();
397 $this->assertSame($this->pluginDefinition['title'], (string) $result);
401 * Tests ConfigNamesMapper::getLangcode().
403 public function testGetLangcode() {
404 // Test that the getLangcode() falls back to 'en', if no explicit language
406 $config_factory = $this->getConfigFactoryStub([
407 'system.site' => ['key' => 'value'],
409 $this->configNamesMapper->setConfigFactory($config_factory);
410 $result = $this->configNamesMapper->getLangcode();
411 $this->assertSame('en', $result);
413 // Test that getLangcode picks up the language code provided by the
415 $config_factory = $this->getConfigFactoryStub([
416 'system.site' => ['langcode' => 'xx'],
418 $this->configNamesMapper->setConfigFactory($config_factory);
419 $result = $this->configNamesMapper->getLangcode();
420 $this->assertSame('xx', $result);
422 // Test that getLangcode() works for multiple configuration names.
423 $this->configNamesMapper->addConfigName('system.maintenance');
424 $config_factory = $this->getConfigFactoryStub([
425 'system.site' => ['langcode' => 'xx'],
426 'system.maintenance' => ['langcode' => 'xx'],
428 $this->configNamesMapper->setConfigFactory($config_factory);
429 $result = $this->configNamesMapper->getLangcode();
430 $this->assertSame('xx', $result);
432 // Test that getLangcode() throws an exception when different language codes
434 $config_factory = $this->getConfigFactoryStub([
435 'system.site' => ['langcode' => 'xx'],
436 'system.maintenance' => ['langcode' => 'yy'],
438 $this->configNamesMapper->setConfigFactory($config_factory);
440 $this->configNamesMapper->getLangcode();
443 catch (\RuntimeException $e) {
448 * Tests ConfigNamesMapper::getConfigData().
450 public function testGetConfigData() {
454 'slogan' => 'Come for the software, stay for the community!',
456 'system.maintenance' => [
458 'message' => '@site is currently under maintenance.',
463 'view_mode' => 'rss',
468 $this->configNamesMapper->setConfigNames(array_keys($configs));
469 $config_factory = $this->getConfigFactoryStub($configs);
470 $this->configNamesMapper->setConfigFactory($config_factory);
472 $result = $this->configNamesMapper->getConfigData();
473 $this->assertSame($configs, $result);
477 * Tests ConfigNamesMapper::hasSchema().
479 * @param array $mock_return_values
480 * An array of values that the mocked locale configuration manager should
481 * return for hasConfigSchema().
482 * @param bool $expected
483 * The expected return value of ConfigNamesMapper::hasSchema().
485 * @dataProvider providerTestHasSchema
487 public function testHasSchema(array $mock_return_values, $expected) {
488 // As the configuration names are arbitrary, simply use integers.
489 $config_names = range(1, count($mock_return_values));
490 $this->configNamesMapper->setConfigNames($config_names);
493 foreach ($config_names as $i => $config_name) {
494 $map[] = [$config_name, $mock_return_values[$i]];
496 $this->typedConfigManager
497 ->expects($this->any())
498 ->method('hasConfigSchema')
499 ->will($this->returnValueMap($map));
501 $result = $this->configNamesMapper->hasSchema();
502 $this->assertSame($expected, $result);
506 * Provides data for ConfigMapperTest::testHasSchema().
509 * An array of arrays, where each inner array has an array of values that
510 * the mocked locale configuration manager should return for
511 * hasConfigSchema() as the first value and the expected return value of
512 * ConfigNamesMapper::hasSchema() as the second value.
514 public function providerTestHasSchema() {
518 [[TRUE, TRUE, TRUE], TRUE],
519 [[TRUE, FALSE, TRUE], FALSE],
524 * Tests ConfigNamesMapper::hasTranslatable().
526 * @param array $mock_return_values
527 * An array of values that the mocked configuration mapper manager should
528 * return for hasTranslatable().
529 * @param bool $expected
530 * The expected return value of ConfigNamesMapper::hasTranslatable().
532 * @dataProvider providerTestHasTranslatable
534 public function testHasTranslatable(array $mock_return_values, $expected) {
535 // As the configuration names are arbitrary, simply use integers.
536 $config_names = range(1, count($mock_return_values));
537 $this->configNamesMapper->setConfigNames($config_names);
540 foreach ($config_names as $i => $config_name) {
541 $map[] = isset($mock_return_values[$i]) ? [$config_name, $mock_return_values[$i]] : [];
543 $this->configMapperManager
544 ->expects($this->any())
545 ->method('hasTranslatable')
546 ->will($this->returnValueMap($map));
548 $result = $this->configNamesMapper->hasTranslatable();
549 $this->assertSame($expected, $result);
553 * Provides data for ConfigNamesMapperTest::testHasTranslatable().
556 * An array of arrays, where each inner array has an array of values that
557 * the mocked configuration mapper manager should return for
558 * hasTranslatable() as the first value and the expected return value of
559 * ConfigNamesMapper::hasTranslatable() as the second value.
561 public function providerTestHasTranslatable() {
566 [[TRUE, TRUE, TRUE], TRUE],
567 [[FALSE, FALSE, FALSE], FALSE],
568 [[TRUE, FALSE, TRUE], TRUE],
573 * Tests ConfigNamesMapper::hasTranslation().
575 * @param array $mock_return_values
576 * An array of values that the mocked configuration mapper manager should
577 * return for hasTranslation().
578 * @param bool $expected
579 * The expected return value of ConfigNamesMapper::hasTranslation().
581 * @dataProvider providerTestHasTranslation
583 public function testHasTranslation(array $mock_return_values, $expected) {
584 $language = new Language();
586 // As the configuration names are arbitrary, simply use integers.
587 $config_names = range(1, count($mock_return_values));
588 $this->configNamesMapper->setConfigNames($config_names);
591 foreach ($config_names as $i => $config_name) {
592 $map[] = [$config_name, $language->getId(), $mock_return_values[$i]];
594 $this->localeConfigManager
595 ->expects($this->any())
596 ->method('hasTranslation')
597 ->will($this->returnValueMap($map));
599 $result = $this->configNamesMapper->hasTranslation($language);
600 $this->assertSame($expected, $result);
604 * Provides data for for ConfigNamesMapperTest::testHasTranslation().
607 * An array of arrays, where each inner array has an array of values that
608 * the mocked configuration mapper manager should return for
609 * hasTranslation() as the first value and the expected return value of
610 * ConfigNamesMapper::hasTranslation() as the second value.
612 public function providerTestHasTranslation() {
616 [[TRUE, TRUE, TRUE], TRUE],
617 [[FALSE, FALSE, TRUE], TRUE],
618 [[FALSE, FALSE, FALSE], FALSE],
623 * Tests ConfigNamesMapper::getTypeName().
625 public function testGetTypeName() {
626 $result = $this->configNamesMapper->getTypeName();
627 $this->assertSame('Settings', (string) $result);
631 * Tests ConfigNamesMapper::hasTranslation().
633 public function testGetOperations() {
636 'title' => 'Translate',
637 'url' => Url::fromRoute('config_translation.item.overview.system.site_information_settings'),
640 $result = $this->configNamesMapper->getOperations();
641 $this->assertEquals($expected, $result);
647 * Defines a test mapper class.
649 class TestConfigNamesMapper extends ConfigNamesMapper {
652 * Gets the internal language code of this mapper, if any.
654 * This method is not to be confused with
655 * ConfigMapperInterface::getLangcode().
657 * @return string|null
658 * The language code of this mapper if it is set; NULL otherwise.
660 public function getInternalLangcode() {
661 return isset($this->langcode) ? $this->langcode : NULL;
665 * Sets the list of configuration names.
667 * @param array $config_names
669 public function setConfigNames(array $config_names) {
670 $this->pluginDefinition['names'] = $config_names;
674 * Sets the configuration factory.
676 * @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory
677 * The config factory to set.
679 public function setConfigFactory(ConfigFactoryInterface $config_factory) {
680 $this->configFactory = $config_factory;