Chris@0: drupalCreateUser(['access administration pages', 'administer languages', 'administer content types']); Chris@0: $this->drupalLogin($user); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the language settings have been saved. Chris@0: */ Chris@0: public function testLanguageConfigurationElement() { Chris@0: $this->drupalGet('language-tests/language_configuration_element'); Chris@0: $edit['lang_configuration[langcode]'] = 'current_interface'; Chris@0: $edit['lang_configuration[language_alterable]'] = FALSE; Chris@0: $this->drupalPostForm(NULL, $edit, 'Save'); Chris@0: $lang_conf = ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'some_bundle'); Chris@0: Chris@0: // Check that the settings have been saved. Chris@0: $this->assertEqual($lang_conf->getDefaultLangcode(), 'current_interface'); Chris@0: $this->assertFalse($lang_conf->isLanguageAlterable()); Chris@0: $this->drupalGet('language-tests/language_configuration_element'); Chris@0: $this->assertOptionSelected('edit-lang-configuration-langcode', 'current_interface'); Chris@0: $this->assertNoFieldChecked('edit-lang-configuration-language-alterable'); Chris@0: Chris@0: // Reload the page and save again. Chris@0: $this->drupalGet('language-tests/language_configuration_element'); Chris@0: $edit['lang_configuration[langcode]'] = 'authors_default'; Chris@0: $edit['lang_configuration[language_alterable]'] = TRUE; Chris@0: $this->drupalPostForm(NULL, $edit, 'Save'); Chris@0: $lang_conf = ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'some_bundle'); Chris@0: Chris@0: // Check that the settings have been saved. Chris@0: $this->assertEqual($lang_conf->getDefaultLangcode(), 'authors_default'); Chris@0: $this->assertTrue($lang_conf->isLanguageAlterable()); Chris@0: $this->drupalGet('language-tests/language_configuration_element'); Chris@0: $this->assertOptionSelected('edit-lang-configuration-langcode', 'authors_default'); Chris@0: $this->assertFieldChecked('edit-lang-configuration-language-alterable'); Chris@0: Chris@0: // Test if content type settings have been saved. Chris@0: $edit = [ Chris@0: 'name' => 'Page', Chris@0: 'type' => 'page', Chris@0: 'language_configuration[langcode]' => 'authors_default', Chris@0: 'language_configuration[language_alterable]' => TRUE, Chris@0: ]; Chris@0: $this->drupalPostForm('admin/structure/types/add', $edit, 'Save and manage fields'); Chris@0: Chris@0: // Make sure the settings are saved when creating the content type. Chris@0: $this->drupalGet('admin/structure/types/manage/page'); Chris@0: $this->assertOptionSelected('edit-language-configuration-langcode', 'authors_default'); Chris@0: $this->assertFieldChecked('edit-language-configuration-language-alterable'); Chris@0: Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that the language_get_default_langcode() returns the correct values. Chris@0: */ Chris@0: public function testDefaultLangcode() { Chris@0: // Add some custom languages. Chris@0: foreach (['aa', 'bb', 'cc'] as $language_code) { Chris@0: ConfigurableLanguage::create([ Chris@0: 'id' => $language_code, Chris@0: 'label' => $this->randomMachineName(), Chris@0: ])->save(); Chris@0: } Chris@0: Chris@0: // Fixed language. Chris@0: ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'custom_bundle') Chris@0: ->setLanguageAlterable(TRUE) Chris@0: ->setDefaultLangcode('bb') Chris@0: ->save(); Chris@0: Chris@0: $langcode = language_get_default_langcode('entity_test', 'custom_bundle'); Chris@0: $this->assertEqual($langcode, 'bb'); Chris@0: Chris@0: // Current interface. Chris@0: ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'custom_bundle') Chris@0: ->setLanguageAlterable(TRUE) Chris@0: ->setDefaultLangcode('current_interface') Chris@0: ->save(); Chris@0: Chris@0: $langcode = language_get_default_langcode('entity_test', 'custom_bundle'); Chris@0: $language_interface = \Drupal::languageManager()->getCurrentLanguage(); Chris@0: $this->assertEqual($langcode, $language_interface->getId()); Chris@0: Chris@0: // Site's default. Chris@0: $old_default = \Drupal::languageManager()->getDefaultLanguage(); Chris@0: // Ensure the language entity default value is correct. Chris@0: $configurable_language = ConfigurableLanguage::load($old_default->getId()); Chris@0: $this->assertTrue($configurable_language->isDefault(), 'The en language entity is flagged as the default language.'); Chris@0: Chris@0: $this->config('system.site')->set('default_langcode', 'cc')->save(); Chris@0: ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'custom_bundle') Chris@0: ->setLanguageAlterable(TRUE) Chris@0: ->setDefaultLangcode(LanguageInterface::LANGCODE_SITE_DEFAULT) Chris@0: ->save(); Chris@0: $langcode = language_get_default_langcode('entity_test', 'custom_bundle'); Chris@0: $this->assertEqual($langcode, 'cc'); Chris@0: Chris@0: // Ensure the language entity default value is correct. Chris@0: $configurable_language = ConfigurableLanguage::load($old_default->getId()); Chris@0: $this->assertFalse($configurable_language->isDefault(), 'The en language entity is not flagged as the default language.'); Chris@0: $configurable_language = ConfigurableLanguage::load('cc'); Chris@0: // Check calling the Chris@0: // \Drupal\language\ConfigurableLanguageInterface::isDefault() method Chris@0: // directly. Chris@0: $this->assertTrue($configurable_language->isDefault(), 'The cc language entity is flagged as the default language.'); Chris@0: Chris@0: // Check the default value of a language field when authors preferred option Chris@0: // is selected. Chris@0: // Create first an user and assign a preferred langcode to him. Chris@0: $some_user = $this->drupalCreateUser(); Chris@0: $some_user->preferred_langcode = 'bb'; Chris@0: $some_user->save(); Chris@0: $this->drupalLogin($some_user); Chris@0: ContentLanguageSettings::create([ Chris@0: 'target_entity_type_id' => 'entity_test', Chris@0: 'target_bundle' => 'some_bundle', Chris@0: ])->setLanguageAlterable(TRUE) Chris@0: ->setDefaultLangcode('authors_default') Chris@0: ->save(); Chris@0: Chris@0: $this->drupalGet('language-tests/language_configuration_element_test'); Chris@0: $this->assertOptionSelected('edit-langcode', 'bb'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that the configuration is retained when the node type is updated. Chris@0: */ Chris@0: public function testNodeTypeUpdate() { Chris@0: // Create the article content type first if the profile used is not the Chris@0: // standard one. Chris@0: if ($this->profile != 'standard') { Chris@0: $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']); Chris@0: } Chris@0: $admin_user = $this->drupalCreateUser(['administer content types']); Chris@0: $this->drupalLogin($admin_user); Chris@0: $edit = [ Chris@0: 'language_configuration[langcode]' => 'current_interface', Chris@0: 'language_configuration[language_alterable]' => TRUE, Chris@0: ]; Chris@0: $this->drupalPostForm('admin/structure/types/manage/article', $edit, t('Save content type')); Chris@0: // Check the language default configuration for the articles. Chris@0: $configuration = ContentLanguageSettings::loadByEntityTypeBundle('node', 'article'); Chris@0: $uuid = $configuration->uuid(); Chris@0: $this->assertEqual($configuration->getDefaultLangcode(), 'current_interface', 'The default language configuration has been saved on the Article content type.'); Chris@0: $this->assertTrue($configuration->isLanguageAlterable(), 'The alterable language configuration has been saved on the Article content type.'); Chris@0: // Update the article content type by changing the title label. Chris@0: $edit = [ Chris@17: 'title_label' => 'Name', Chris@0: ]; Chris@0: $this->drupalPostForm('admin/structure/types/manage/article', $edit, t('Save content type')); Chris@0: // Check that we still have the settings for the updated node type. Chris@0: $configuration = ContentLanguageSettings::loadByEntityTypeBundle('node', 'article'); Chris@0: $this->assertEqual($configuration->getDefaultLangcode(), 'current_interface', 'The default language configuration has been kept on the updated Article content type.'); Chris@0: $this->assertTrue($configuration->isLanguageAlterable(), 'The alterable language configuration has been kept on the updated Article content type.'); Chris@0: $this->assertEqual($configuration->uuid(), $uuid, 'The language configuration uuid has been kept on the updated Article content type.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the language settings are deleted on bundle delete. Chris@0: */ Chris@0: public function testNodeTypeDelete() { Chris@0: // Create the article content type first if the profile used is not the Chris@0: // standard one. Chris@0: if ($this->profile != 'standard') { Chris@0: $this->drupalCreateContentType([ Chris@0: 'type' => 'article', Chris@17: 'name' => 'Article', Chris@0: ]); Chris@0: } Chris@0: $admin_user = $this->drupalCreateUser(['administer content types']); Chris@0: $this->drupalLogin($admin_user); Chris@0: Chris@0: // Create language configuration for the articles. Chris@0: $edit = [ Chris@0: 'language_configuration[langcode]' => 'authors_default', Chris@0: 'language_configuration[language_alterable]' => TRUE, Chris@0: ]; Chris@0: $this->drupalPostForm('admin/structure/types/manage/article', $edit, t('Save content type')); Chris@0: Chris@0: // Check the language default configuration for articles is present. Chris@0: $configuration = \Drupal::entityManager()->getStorage('language_content_settings')->load('node.article'); Chris@0: $this->assertTrue($configuration, 'The language configuration is present.'); Chris@0: Chris@0: // Delete 'article' bundle. Chris@0: $this->drupalPostForm('admin/structure/types/manage/article/delete', [], t('Delete')); Chris@0: Chris@0: // Check that the language configuration has been deleted. Chris@0: \Drupal::entityManager()->getStorage('language_content_settings')->resetCache(); Chris@0: $configuration = \Drupal::entityManager()->getStorage('language_content_settings')->load('node.article'); Chris@0: $this->assertFalse($configuration, 'The language configuration was deleted after bundle was deleted.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that the configuration is retained when a vocabulary is updated. Chris@0: */ Chris@0: public function testTaxonomyVocabularyUpdate() { Chris@0: $vocabulary = Vocabulary::create([ Chris@0: 'name' => 'Country', Chris@0: 'vid' => 'country', Chris@0: ]); Chris@0: $vocabulary->save(); Chris@0: Chris@0: $admin_user = $this->drupalCreateUser(['administer taxonomy']); Chris@0: $this->drupalLogin($admin_user); Chris@0: $edit = [ Chris@0: 'default_language[langcode]' => 'current_interface', Chris@0: 'default_language[language_alterable]' => TRUE, Chris@0: ]; Chris@0: $this->drupalPostForm('admin/structure/taxonomy/manage/country', $edit, t('Save')); Chris@0: Chris@0: // Check the language default configuration. Chris@0: $configuration = ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', 'country'); Chris@0: $uuid = $configuration->uuid(); Chris@0: $this->assertEqual($configuration->getDefaultLangcode(), 'current_interface', 'The default language configuration has been saved on the Country vocabulary.'); Chris@0: $this->assertTrue($configuration->isLanguageAlterable(), 'The alterable language configuration has been saved on the Country vocabulary.'); Chris@0: // Update the vocabulary. Chris@0: $edit = [ Chris@17: 'name' => 'Nation', Chris@0: ]; Chris@0: $this->drupalPostForm('admin/structure/taxonomy/manage/country', $edit, t('Save')); Chris@0: // Check that we still have the settings for the updated vocabulary. Chris@0: $configuration = ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', 'country'); Chris@0: $this->assertEqual($configuration->getDefaultLangcode(), 'current_interface', 'The default language configuration has been kept on the updated Country vocabulary.'); Chris@0: $this->assertTrue($configuration->isLanguageAlterable(), 'The alterable language configuration has been kept on the updated Country vocabulary.'); Chris@0: $this->assertEqual($configuration->uuid(), $uuid, 'The language configuration uuid has been kept on the updated Country vocabulary.'); Chris@0: } Chris@0: Chris@0: }