View source
<?php
namespace Drupal\Tests\config_translation\Unit;
use Drupal\config_translation\ConfigNamesMapper;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Language\Language;
use Drupal\Core\Routing\RouteMatch;
use Drupal\Core\Url;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\Routing\Route;
class ConfigNamesMapperTest extends UnitTestCase {
protected $pluginDefinition;
protected $configNamesMapper;
protected $localeConfigManager;
protected $typedConfigManager;
protected $configMapperManager;
protected $baseRoute;
protected $routeProvider;
protected $urlGenerator;
protected $languageManager;
protected function setUp() {
$this->routeProvider = $this
->getMock('Drupal\\Core\\Routing\\RouteProviderInterface');
$this->pluginDefinition = array(
'class' => '\\Drupal\\config_translation\\ConfigNamesMapper',
'base_route_name' => 'system.site_information_settings',
'title' => 'System information',
'names' => array(
'system.site',
),
'weight' => 42,
);
$this->typedConfigManager = $this
->getMock('Drupal\\Core\\Config\\TypedConfigManagerInterface');
$this->localeConfigManager = $this
->getMockBuilder('Drupal\\locale\\LocaleConfigManager')
->disableOriginalConstructor()
->getMock();
$this->configMapperManager = $this
->getMock('Drupal\\config_translation\\ConfigMapperManagerInterface');
$this->urlGenerator = $this
->getMock('Drupal\\Core\\Routing\\UrlGeneratorInterface');
$container = new ContainerBuilder();
$container
->set('url_generator', $this->urlGenerator);
\Drupal::setContainer($container);
$this->baseRoute = new Route('/admin/config/system/site-information');
$this->routeProvider
->expects($this
->any())
->method('getRouteByName')
->with('system.site_information_settings')
->will($this
->returnValue($this->baseRoute));
$this->languageManager = $this
->getMock('Drupal\\Core\\Language\\LanguageManagerInterface');
$this->configNamesMapper = new TestConfigNamesMapper('system.site_information_settings', $this->pluginDefinition, $this
->getConfigFactoryStub(), $this->typedConfigManager, $this->localeConfigManager, $this->configMapperManager, $this->routeProvider, $this
->getStringTranslationStub(), $this->languageManager);
}
public function testGetTitle() {
$result = $this->configNamesMapper
->getTitle();
$this
->assertSame($this->pluginDefinition['title'], (string) $result);
}
public function testGetBaseRouteName() {
$result = $this->configNamesMapper
->getBaseRouteName();
$this
->assertSame($this->pluginDefinition['base_route_name'], $result);
}
public function testGetBaseRouteParameters() {
$result = $this->configNamesMapper
->getBaseRouteParameters();
$this
->assertSame(array(), $result);
}
public function testGetBaseRoute() {
$result = $this->configNamesMapper
->getBaseRoute();
$this
->assertSame($this->baseRoute, $result);
}
public function testGetBasePath() {
$this->urlGenerator
->expects($this
->once())
->method('getPathFromRoute')
->with('system.site_information_settings', [])
->willReturn('/admin/config/system/site-information');
$result = $this->configNamesMapper
->getBasePath();
$this
->assertSame('/admin/config/system/site-information', $result);
}
public function testGetOverviewRouteName() {
$result = $this->configNamesMapper
->getOverviewRouteName();
$expected = 'config_translation.item.overview.' . $this->pluginDefinition['base_route_name'];
$this
->assertSame($expected, $result);
}
public function testGetOverviewRouteParameters() {
$result = $this->configNamesMapper
->getOverviewRouteParameters();
$this
->assertSame(array(), $result);
}
public function testGetOverviewRoute() {
$expected = new Route('/admin/config/system/site-information/translate', array(
'_controller' => '\\Drupal\\config_translation\\Controller\\ConfigTranslationController::itemPage',
'plugin_id' => 'system.site_information_settings',
), array(
'_config_translation_overview_access' => 'TRUE',
));
$result = $this->configNamesMapper
->getOverviewRoute();
$this
->assertSame(serialize($expected), serialize($result));
}
public function testGetOverviewPath() {
$this->urlGenerator
->expects($this
->once())
->method('getPathFromRoute')
->with('config_translation.item.overview.system.site_information_settings', [])
->willReturn('/admin/config/system/site-information/translate');
$result = $this->configNamesMapper
->getOverviewPath();
$this
->assertSame('/admin/config/system/site-information/translate', $result);
}
public function testGetAddRouteName() {
$result = $this->configNamesMapper
->getAddRouteName();
$expected = 'config_translation.item.add.' . $this->pluginDefinition['base_route_name'];
$this
->assertSame($expected, $result);
}
public function testGetAddRouteParameters() {
$route_match = new RouteMatch('example', new Route('/test/{langcode}'), [
'langcode' => 'xx',
]);
$this->configNamesMapper
->populateFromRouteMatch($route_match);
$expected = array(
'langcode' => 'xx',
);
$result = $this->configNamesMapper
->getAddRouteParameters();
$this
->assertSame($expected, $result);
}
public function testGetAddRoute() {
$expected = new Route('/admin/config/system/site-information/translate/{langcode}/add', array(
'_form' => '\\Drupal\\config_translation\\Form\\ConfigTranslationAddForm',
'plugin_id' => 'system.site_information_settings',
), array(
'_config_translation_form_access' => 'TRUE',
));
$result = $this->configNamesMapper
->getAddRoute();
$this
->assertSame(serialize($expected), serialize($result));
}
public function testGetEditRouteName() {
$result = $this->configNamesMapper
->getEditRouteName();
$expected = 'config_translation.item.edit.' . $this->pluginDefinition['base_route_name'];
$this
->assertSame($expected, $result);
}
public function testGetEditRouteParameters() {
$route_match = new RouteMatch('example', new Route('/test/{langcode}'), [
'langcode' => 'xx',
]);
$this->configNamesMapper
->populateFromRouteMatch($route_match);
$expected = array(
'langcode' => 'xx',
);
$result = $this->configNamesMapper
->getEditRouteParameters();
$this
->assertSame($expected, $result);
}
public function testGetEditRoute() {
$expected = new Route('/admin/config/system/site-information/translate/{langcode}/edit', array(
'_form' => '\\Drupal\\config_translation\\Form\\ConfigTranslationEditForm',
'plugin_id' => 'system.site_information_settings',
), array(
'_config_translation_form_access' => 'TRUE',
));
$result = $this->configNamesMapper
->getEditRoute();
$this
->assertSame(serialize($expected), serialize($result));
}
public function testGetDeleteRouteName() {
$result = $this->configNamesMapper
->getDeleteRouteName();
$expected = 'config_translation.item.delete.' . $this->pluginDefinition['base_route_name'];
$this
->assertSame($expected, $result);
}
public function testGetDeleteRouteParameters() {
$route_match = new RouteMatch('example', new Route('/test/{langcode}'), [
'langcode' => 'xx',
]);
$this->configNamesMapper
->populateFromRouteMatch($route_match);
$expected = array(
'langcode' => 'xx',
);
$result = $this->configNamesMapper
->getDeleteRouteParameters();
$this
->assertSame($expected, $result);
}
public function testGetDeleteRoute() {
$expected = new Route('/admin/config/system/site-information/translate/{langcode}/delete', array(
'_form' => '\\Drupal\\config_translation\\Form\\ConfigTranslationDeleteForm',
'plugin_id' => 'system.site_information_settings',
), array(
'_config_translation_form_access' => 'TRUE',
));
$result = $this->configNamesMapper
->getDeleteRoute();
$this
->assertSame(serialize($expected), serialize($result));
}
public function testGetConfigNames() {
$result = $this->configNamesMapper
->getConfigNames();
$this
->assertSame($this->pluginDefinition['names'], $result);
}
public function testAddConfigName() {
$names = $this->configNamesMapper
->getConfigNames();
$this->configNamesMapper
->addConfigName('test');
$names[] = 'test';
$result = $this->configNamesMapper
->getConfigNames();
$this
->assertSame($names, $result);
}
public function testGetWeight() {
$result = $this->configNamesMapper
->getWeight();
$this
->assertSame($this->pluginDefinition['weight'], $result);
}
public function testPopulateFromRouteMatch() {
$this
->assertSame(NULL, $this->configNamesMapper
->getInternalLangcode());
$route_match = new RouteMatch('example', new Route('/test/{langcode}'));
$this->configNamesMapper
->populateFromRouteMatch($route_match);
$this
->assertSame(NULL, $this->configNamesMapper
->getInternalLangcode());
$route_match = new RouteMatch('example', new Route('/test/{langcode}'), [
'langcode' => 'xx',
]);
$this->configNamesMapper
->populateFromRouteMatch($route_match);
$this
->assertSame('xx', $this->configNamesMapper
->getInternalLangcode());
$route_match = new RouteMatch('example', new Route('/test/{langcode}'));
$this->configNamesMapper
->populateFromRouteMatch($route_match);
$this
->assertSame(NULL, $this->configNamesMapper
->getInternalLangcode());
}
public function testGetTypeLabel() {
$result = $this->configNamesMapper
->getTypeLabel();
$this
->assertSame($this->pluginDefinition['title'], (string) $result);
}
public function testGetLangcode() {
$config_factory = $this
->getConfigFactoryStub(array(
'system.site' => array(
'key' => 'value',
),
));
$this->configNamesMapper
->setConfigFactory($config_factory);
$result = $this->configNamesMapper
->getLangcode();
$this
->assertSame('en', $result);
$config_factory = $this
->getConfigFactoryStub(array(
'system.site' => array(
'langcode' => 'xx',
),
));
$this->configNamesMapper
->setConfigFactory($config_factory);
$result = $this->configNamesMapper
->getLangcode();
$this
->assertSame('xx', $result);
$this->configNamesMapper
->addConfigName('system.maintenance');
$config_factory = $this
->getConfigFactoryStub(array(
'system.site' => array(
'langcode' => 'xx',
),
'system.maintenance' => array(
'langcode' => 'xx',
),
));
$this->configNamesMapper
->setConfigFactory($config_factory);
$result = $this->configNamesMapper
->getLangcode();
$this
->assertSame('xx', $result);
$config_factory = $this
->getConfigFactoryStub(array(
'system.site' => array(
'langcode' => 'xx',
),
'system.maintenance' => array(
'langcode' => 'yy',
),
));
$this->configNamesMapper
->setConfigFactory($config_factory);
try {
$this->configNamesMapper
->getLangcode();
$this
->fail();
} catch (\RuntimeException $e) {
}
}
public function testGetConfigData() {
$configs = array(
'system.site' => array(
'name' => 'Drupal',
'slogan' => 'Come for the software, stay for the community!',
),
'system.maintenance' => array(
'enabled' => FALSE,
'message' => '@site is currently under maintenance.',
),
'system.rss' => array(
'items' => array(
'limit' => 10,
'view_mode' => 'rss',
),
),
);
$this->configNamesMapper
->setConfigNames(array_keys($configs));
$config_factory = $this
->getConfigFactoryStub($configs);
$this->configNamesMapper
->setConfigFactory($config_factory);
$result = $this->configNamesMapper
->getConfigData();
$this
->assertSame($configs, $result);
}
public function testHasSchema(array $mock_return_values, $expected) {
$config_names = range(1, count($mock_return_values));
$this->configNamesMapper
->setConfigNames($config_names);
$map = array();
foreach ($config_names as $i => $config_name) {
$map[] = array(
$config_name,
$mock_return_values[$i],
);
}
$this->typedConfigManager
->expects($this
->any())
->method('hasConfigSchema')
->will($this
->returnValueMap($map));
$result = $this->configNamesMapper
->hasSchema();
$this
->assertSame($expected, $result);
}
public function providerTestHasSchema() {
return array(
array(
array(
TRUE,
),
TRUE,
),
array(
array(
FALSE,
),
FALSE,
),
array(
array(
TRUE,
TRUE,
TRUE,
),
TRUE,
),
array(
array(
TRUE,
FALSE,
TRUE,
),
FALSE,
),
);
}
public function testHasTranslatable(array $mock_return_values, $expected) {
$config_names = range(1, count($mock_return_values));
$this->configNamesMapper
->setConfigNames($config_names);
$map = array();
foreach ($config_names as $i => $config_name) {
$map[] = isset($mock_return_values[$i]) ? array(
$config_name,
$mock_return_values[$i],
) : array();
}
$this->configMapperManager
->expects($this
->any())
->method('hasTranslatable')
->will($this
->returnValueMap($map));
$result = $this->configNamesMapper
->hasTranslatable();
$this
->assertSame($expected, $result);
}
public function providerTestHasTranslatable() {
return array(
array(
array(),
FALSE,
),
array(
array(
TRUE,
),
TRUE,
),
array(
array(
FALSE,
),
FALSE,
),
array(
array(
TRUE,
TRUE,
TRUE,
),
TRUE,
),
array(
array(
FALSE,
FALSE,
FALSE,
),
FALSE,
),
array(
array(
TRUE,
FALSE,
TRUE,
),
TRUE,
),
);
}
public function testHasTranslation(array $mock_return_values, $expected) {
$language = new Language();
$config_names = range(1, count($mock_return_values));
$this->configNamesMapper
->setConfigNames($config_names);
$map = array();
foreach ($config_names as $i => $config_name) {
$map[] = array(
$config_name,
$language
->getId(),
$mock_return_values[$i],
);
}
$this->localeConfigManager
->expects($this
->any())
->method('hasTranslation')
->will($this
->returnValueMap($map));
$result = $this->configNamesMapper
->hasTranslation($language);
$this
->assertSame($expected, $result);
}
public function providerTestHasTranslation() {
return array(
array(
array(
TRUE,
),
TRUE,
),
array(
array(
FALSE,
),
FALSE,
),
array(
array(
TRUE,
TRUE,
TRUE,
),
TRUE,
),
array(
array(
FALSE,
FALSE,
TRUE,
),
TRUE,
),
array(
array(
FALSE,
FALSE,
FALSE,
),
FALSE,
),
);
}
public function testGetTypeName() {
$result = $this->configNamesMapper
->getTypeName();
$this
->assertSame('Settings', (string) $result);
}
public function testGetOperations() {
$expected = array(
'translate' => array(
'title' => 'Translate',
'url' => Url::fromRoute('config_translation.item.overview.system.site_information_settings'),
),
);
$result = $this->configNamesMapper
->getOperations();
$this
->assertEquals($expected, $result);
}
}
class TestConfigNamesMapper extends ConfigNamesMapper {
public function getInternalLangcode() {
return isset($this->langcode) ? $this->langcode : NULL;
}
public function setConfigNames(array $config_names) {
$this->pluginDefinition['names'] = $config_names;
}
public function setConfigFactory(ConfigFactoryInterface $config_factory) {
$this->configFactory = $config_factory;
}
}