View source
<?php
namespace Drupal\Tests\language\Functional;
use Drupal\Core\Language\LanguageInterface;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\Tests\BrowserTestBase;
class LanguageConfigurationElementTest extends BrowserTestBase {
protected static $modules = [
'taxonomy',
'node',
'language',
'language_elements_test',
'field_ui',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$user = $this
->drupalCreateUser([
'access administration pages',
'administer languages',
'administer content types',
]);
$this
->drupalLogin($user);
}
public function testLanguageConfigurationElement() {
$this
->drupalGet('language-tests/language_configuration_element');
$edit['lang_configuration[langcode]'] = 'current_interface';
$edit['lang_configuration[language_alterable]'] = FALSE;
$this
->submitForm($edit, 'Save');
$lang_conf = ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'some_bundle');
$this
->assertEquals('current_interface', $lang_conf
->getDefaultLangcode());
$this
->assertFalse($lang_conf
->isLanguageAlterable());
$this
->drupalGet('language-tests/language_configuration_element');
$this
->assertTrue($this
->assertSession()
->optionExists('edit-lang-configuration-langcode', 'current_interface')
->isSelected());
$this
->assertSession()
->checkboxNotChecked('edit-lang-configuration-language-alterable');
$this
->drupalGet('language-tests/language_configuration_element');
$edit['lang_configuration[langcode]'] = 'authors_default';
$edit['lang_configuration[language_alterable]'] = TRUE;
$this
->submitForm($edit, 'Save');
$lang_conf = ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'some_bundle');
$this
->assertEquals('authors_default', $lang_conf
->getDefaultLangcode());
$this
->assertTrue($lang_conf
->isLanguageAlterable());
$this
->drupalGet('language-tests/language_configuration_element');
$this
->assertTrue($this
->assertSession()
->optionExists('edit-lang-configuration-langcode', 'authors_default')
->isSelected());
$this
->assertSession()
->checkboxChecked('edit-lang-configuration-language-alterable');
$edit = [
'name' => 'Page',
'type' => 'page',
'language_configuration[langcode]' => 'authors_default',
'language_configuration[language_alterable]' => TRUE,
];
$this
->drupalGet('admin/structure/types/add');
$this
->submitForm($edit, 'Save and manage fields');
$this
->drupalGet('admin/structure/types/manage/page');
$this
->assertTrue($this
->assertSession()
->optionExists('edit-language-configuration-langcode', 'authors_default')
->isSelected());
$this
->assertSession()
->checkboxChecked('edit-language-configuration-language-alterable');
}
public function testDefaultLangcode() {
foreach ([
'aa',
'bb',
'cc',
] as $language_code) {
ConfigurableLanguage::create([
'id' => $language_code,
'label' => $this
->randomMachineName(),
])
->save();
}
ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'custom_bundle')
->setLanguageAlterable(TRUE)
->setDefaultLangcode('bb')
->save();
$langcode = language_get_default_langcode('entity_test', 'custom_bundle');
$this
->assertEquals('bb', $langcode);
ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'custom_bundle')
->setLanguageAlterable(TRUE)
->setDefaultLangcode('current_interface')
->save();
$langcode = language_get_default_langcode('entity_test', 'custom_bundle');
$language_interface = \Drupal::languageManager()
->getCurrentLanguage();
$this
->assertEquals($langcode, $language_interface
->getId());
$old_default = \Drupal::languageManager()
->getDefaultLanguage();
$configurable_language = ConfigurableLanguage::load($old_default
->getId());
$this
->assertTrue($configurable_language
->isDefault(), 'The en language entity is flagged as the default language.');
$this
->config('system.site')
->set('default_langcode', 'cc')
->save();
ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'custom_bundle')
->setLanguageAlterable(TRUE)
->setDefaultLangcode(LanguageInterface::LANGCODE_SITE_DEFAULT)
->save();
$langcode = language_get_default_langcode('entity_test', 'custom_bundle');
$this
->assertEquals('cc', $langcode);
$configurable_language = ConfigurableLanguage::load($old_default
->getId());
$this
->assertFalse($configurable_language
->isDefault(), 'The en language entity is not flagged as the default language.');
$configurable_language = ConfigurableLanguage::load('cc');
$this
->assertTrue($configurable_language
->isDefault(), 'The cc language entity is flagged as the default language.');
$some_user = $this
->drupalCreateUser();
$some_user->preferred_langcode = 'bb';
$some_user
->save();
$this
->drupalLogin($some_user);
ContentLanguageSettings::create([
'target_entity_type_id' => 'entity_test',
'target_bundle' => 'some_bundle',
])
->setLanguageAlterable(TRUE)
->setDefaultLangcode('authors_default')
->save();
$this
->drupalGet('language-tests/language_configuration_element_test');
$this
->assertTrue($this
->assertSession()
->optionExists('edit-langcode', 'bb')
->isSelected());
}
public function testNodeTypeUpdate() {
if ($this->profile != 'standard') {
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
}
$admin_user = $this
->drupalCreateUser([
'administer content types',
]);
$this
->drupalLogin($admin_user);
$edit = [
'language_configuration[langcode]' => 'current_interface',
'language_configuration[language_alterable]' => TRUE,
];
$this
->drupalGet('admin/structure/types/manage/article');
$this
->submitForm($edit, 'Save content type');
$configuration = ContentLanguageSettings::loadByEntityTypeBundle('node', 'article');
$uuid = $configuration
->uuid();
$this
->assertEquals('current_interface', $configuration
->getDefaultLangcode(), 'The default language configuration has been saved on the Article content type.');
$this
->assertTrue($configuration
->isLanguageAlterable(), 'The alterable language configuration has been saved on the Article content type.');
$edit = [
'title_label' => 'Name',
];
$this
->drupalGet('admin/structure/types/manage/article');
$this
->submitForm($edit, 'Save content type');
$configuration = ContentLanguageSettings::loadByEntityTypeBundle('node', 'article');
$this
->assertEquals('current_interface', $configuration
->getDefaultLangcode(), 'The default language configuration has been kept on the updated Article content type.');
$this
->assertTrue($configuration
->isLanguageAlterable(), 'The alterable language configuration has been kept on the updated Article content type.');
$this
->assertEquals($uuid, $configuration
->uuid(), 'The language configuration uuid has been kept on the updated Article content type.');
}
public function testNodeTypeDelete() {
if ($this->profile != 'standard') {
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
}
$admin_user = $this
->drupalCreateUser([
'administer content types',
]);
$this
->drupalLogin($admin_user);
$edit = [
'language_configuration[langcode]' => 'authors_default',
'language_configuration[language_alterable]' => TRUE,
];
$this
->drupalGet('admin/structure/types/manage/article');
$this
->submitForm($edit, 'Save content type');
$configuration = \Drupal::entityTypeManager()
->getStorage('language_content_settings')
->load('node.article');
$this
->assertNotEmpty($configuration, 'The language configuration is present.');
$this
->drupalGet('admin/structure/types/manage/article/delete');
$this
->submitForm([], 'Delete');
\Drupal::entityTypeManager()
->getStorage('language_content_settings')
->resetCache();
$configuration = \Drupal::entityTypeManager()
->getStorage('language_content_settings')
->load('node.article');
$this
->assertNull($configuration, 'The language configuration was deleted after bundle was deleted.');
}
public function testTaxonomyVocabularyUpdate() {
$vocabulary = Vocabulary::create([
'name' => 'Country',
'vid' => 'country',
]);
$vocabulary
->save();
$admin_user = $this
->drupalCreateUser([
'administer taxonomy',
]);
$this
->drupalLogin($admin_user);
$edit = [
'default_language[langcode]' => 'current_interface',
'default_language[language_alterable]' => TRUE,
];
$this
->drupalGet('admin/structure/taxonomy/manage/country');
$this
->submitForm($edit, 'Save');
$configuration = ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', 'country');
$uuid = $configuration
->uuid();
$this
->assertEquals('current_interface', $configuration
->getDefaultLangcode(), 'The default language configuration has been saved on the Country vocabulary.');
$this
->assertTrue($configuration
->isLanguageAlterable(), 'The alterable language configuration has been saved on the Country vocabulary.');
$edit = [
'name' => 'Nation',
];
$this
->drupalGet('admin/structure/taxonomy/manage/country');
$this
->submitForm($edit, 'Save');
$configuration = ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', 'country');
$this
->assertEquals('current_interface', $configuration
->getDefaultLangcode(), 'The default language configuration has been kept on the updated Country vocabulary.');
$this
->assertTrue($configuration
->isLanguageAlterable(), 'The alterable language configuration has been kept on the updated Country vocabulary.');
$this
->assertEquals($uuid, $configuration
->uuid(), 'The language configuration uuid has been kept on the updated Country vocabulary.');
}
}