View source
<?php
namespace Drupal\Tests\config_ignore\Functional;
use Drupal\Component\FileCache\FileCache;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\BrowserTestBase;
use Drush\TestTraits\DrushTestTrait;
class ConfigWithTranslationTest extends BrowserTestBase {
use DrushTestTrait;
protected static $modules = [
'config_ignore',
'config_translation',
'language',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
ConfigurableLanguage::createFromLangcode('ro')
->save();
$this
->translateConfig('user.role.anonymous', 'label', 'Utilizator anonim', 'ro');
$this
->translateConfig('user.role.authenticated', 'label', 'Utilizator autentificat', 'ro');
$this
->drush('config:export', [], [
'yes' => NULL,
]);
$this
->assertExportedValue('user.settings', 'anonymous', 'Anonymous');
$this
->assertExportedValue('user.role.anonymous', 'label', 'Anonymous user');
$this
->assertExportedValue('user.role.anonymous', 'label', 'Utilizator anonim', 'ro');
$this
->assertExportedValue('user.role.authenticated', 'label', 'Authenticated user');
$this
->assertExportedValue('user.role.authenticated', 'weight', 1);
$this
->assertExportedValue('user.role.authenticated', 'is_admin', FALSE);
$this
->assertExportedValue('user.role.authenticated', 'label', 'Utilizator autentificat', 'ro');
$this
->config('config_ignore.settings')
->set('ignored_config_entities', [
'user.role.anonymous',
'user.role.authenticated:weight',
'user.role.authenticated:is_admin',
])
->save();
}
public function testConfigStatus() {
$this
->config('user.settings')
->set('anonymous', 'Visitor')
->save();
$this
->config('user.role.anonymous')
->set('label', 'Visitor')
->save();
$this
->config('user.role.authenticated')
->set('label', 'Authenticated')
->set('weight', 2)
->set('is_admin', TRUE)
->save();
$this
->translateConfig('user.role.anonymous', 'label', 'Vizitator', 'ro');
$this
->translateConfig('user.role.authenticated', 'label', 'Logat', 'ro');
$this
->drush('config:status', [], [
'format' => 'json',
]);
$diff = (array) $this
->getOutputFromJSON();
$this
->assertCount(3, $diff);
$this
->assertArrayHasKey('config_ignore.settings', $diff);
$this
->assertSame([
'name' => 'config_ignore.settings',
'state' => 'Different',
], $diff['config_ignore.settings']);
$this
->assertArrayHasKey('user.settings', $diff);
$this
->assertSame([
'name' => 'user.settings',
'state' => 'Different',
], $diff['user.settings']);
$this
->assertArrayHasKey('user.role.authenticated', $diff);
$this
->assertSame([
'name' => 'user.role.authenticated',
'state' => 'Different',
], $diff['user.role.authenticated']);
}
public function testConfigExport() {
$this
->config('user.settings')
->set('anonymous', 'Visitor')
->save();
$this
->config('user.role.anonymous')
->set('label', 'Visitor')
->save();
$this
->config('user.role.authenticated')
->set('label', 'Authenticated')
->set('weight', 2)
->set('is_admin', TRUE)
->save();
$this
->translateConfig('user.role.anonymous', 'label', 'Vizitator', 'ro');
$this
->translateConfig('user.role.authenticated', 'label', 'Logat', 'ro');
$this
->drush('config:export', [], [
'yes' => NULL,
]);
$this
->assertExportedValue('user.settings', 'anonymous', 'Visitor');
$this
->assertExportedValue('user.role.anonymous', 'label', 'Anonymous user');
$this
->assertExportedValue('user.role.anonymous', 'label', 'Utilizator anonim', 'ro');
$this
->assertExportedValue('user.role.authenticated', 'label', 'Authenticated');
$this
->assertExportedValue('user.role.authenticated', 'weight', 1);
$this
->assertExportedValue('user.role.authenticated', 'is_admin', FALSE);
$this
->assertExportedValue('user.role.authenticated', 'label', 'Logat', 'ro');
$sync_storage = $this
->getSyncStorage();
$sync_storage
->delete('user.role.authenticated');
$this
->drush('config:export', [], [
'yes' => NULL,
]);
$data = $sync_storage
->read('user.role.authenticated');
$this
->assertArrayNotHasKey('weight', $data);
$this
->assertArrayNotHasKey('is_admin', $data);
}
public function testConfigImport() {
$this
->setConfigSyncValue('config_ignore.settings', 'ignored_config_entities', [
'user.role.anonymous',
'user.role.authenticated:weight',
'user.role.authenticated:is_admin',
]);
$this
->setConfigSyncValue('user.settings', 'anonymous', 'Visitor');
$this
->setConfigSyncValue('user.role.anonymous', 'label', 'Visitor');
$this
->setConfigSyncValue('user.role.authenticated', 'label', 'Authenticated');
$this
->setConfigSyncValue('user.role.authenticated', 'weight', 2);
$this
->setConfigSyncValue('user.role.authenticated', 'is_admin', TRUE);
$this
->setConfigSyncValue('user.role.anonymous', 'label', 'Vizitator', 'ro');
$this
->setConfigSyncValue('user.role.authenticated', 'label', 'Logat', 'ro');
$this
->assertExportedValue('user.settings', 'anonymous', 'Visitor');
$this
->assertExportedValue('user.role.anonymous', 'label', 'Visitor');
$this
->assertExportedValue('user.role.anonymous', 'label', 'Vizitator', 'ro');
$this
->assertExportedValue('user.role.authenticated', 'label', 'Authenticated');
$this
->assertExportedValue('user.role.authenticated', 'weight', 2);
$this
->assertExportedValue('user.role.authenticated', 'is_admin', TRUE);
$this
->drush('config:import', [], [
'yes' => NULL,
]);
\Drupal::configFactory()
->reset();
$this
->assertSame('Visitor', $this
->config('user.settings')
->get('anonymous'));
$this
->assertSame('Anonymous user', $this
->config('user.role.anonymous')
->get('label'));
$this
->assertSame('Authenticated', $this
->config('user.role.authenticated')
->get('label'));
$this
->assertEquals(1, $this
->config('user.role.authenticated')
->get('weight'));
$this
->assertFalse($this
->config('user.role.authenticated')
->get('is_admin'));
$language_manager = \Drupal::languageManager();
$original_language = $language_manager
->getConfigOverrideLanguage();
$translated = $language_manager
->getLanguageConfigOverride('ro', 'user.role.anonymous');
$this
->assertSame('Utilizator anonim', $translated
->get('label'));
$translated = $language_manager
->getLanguageConfigOverride('ro', 'user.role.authenticated');
$this
->assertSame('Logat', $translated
->get('label'));
$language_manager
->setConfigOverrideLanguage($original_language);
$active_storage = \Drupal::service('config.storage');
$active_storage
->delete('user.role.authenticated');
$this
->drush('config:import', [], [
'yes' => NULL,
]);
\Drupal::configFactory()
->reset('user.role.authenticated');
$this
->assertSame('Authenticated', $this
->config('user.role.authenticated')
->get('label'));
$this
->assertEquals(1, $this
->config('user.role.authenticated')
->get('weight'));
$this
->assertFalse((bool) $this
->config('user.role.authenticated')
->get('is_admin'));
}
protected function assertExportedValue($config_name, $key, $value, $langcode = NULL) {
FileCache::reset();
$sync_storage = $this
->getSyncStorage($langcode);
$this
->assertTrue($sync_storage
->exists($config_name));
$data = $sync_storage
->read($config_name);
$this
->assertSame($value, $data[$key]);
}
protected function translateConfig($config_name, $key, $value, $langcode) {
$language_manager = \Drupal::languageManager();
$original_language = $language_manager
->getConfigOverrideLanguage();
$translated = $language_manager
->getLanguageConfigOverride($langcode, $config_name);
$translated
->set($key, $value)
->save();
$language_manager
->setConfigOverrideLanguage($original_language);
}
protected function setConfigSyncValue($config_name, $key, $value, $langcode = NULL) {
$sync_storage = $this
->getSyncStorage($langcode);
$data = $sync_storage
->read($config_name);
$data[$key] = $value;
$sync_storage
->write($config_name, $data);
}
protected function getSyncStorage($langcode = NULL) {
$sync_storage = \Drupal::service('config.storage.sync');
if ($langcode) {
$sync_storage = $sync_storage
->createCollection("language.{$langcode}");
}
return $sync_storage;
}
}