annotate core/modules/language/tests/src/Functional/LanguageConfigurationElementTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\language\Functional;
Chris@0 4
Chris@0 5 use Drupal\Core\Language\LanguageInterface;
Chris@0 6 use Drupal\language\Entity\ConfigurableLanguage;
Chris@0 7 use Drupal\language\Entity\ContentLanguageSettings;
Chris@0 8 use Drupal\taxonomy\Entity\Vocabulary;
Chris@0 9 use Drupal\Tests\BrowserTestBase;
Chris@0 10
Chris@0 11 /**
Chris@0 12 * Tests the features of the language configuration element field.
Chris@0 13 *
Chris@0 14 * @group language
Chris@0 15 */
Chris@0 16 class LanguageConfigurationElementTest extends BrowserTestBase {
Chris@0 17
Chris@0 18 /**
Chris@0 19 * Modules to enable.
Chris@0 20 *
Chris@0 21 * @var array
Chris@0 22 */
Chris@0 23 public static $modules = ['taxonomy', 'node', 'language', 'language_elements_test', 'field_ui'];
Chris@0 24
Chris@0 25 protected function setUp() {
Chris@0 26 parent::setUp();
Chris@0 27 $user = $this->drupalCreateUser(['access administration pages', 'administer languages', 'administer content types']);
Chris@0 28 $this->drupalLogin($user);
Chris@0 29 }
Chris@0 30
Chris@0 31 /**
Chris@0 32 * Tests the language settings have been saved.
Chris@0 33 */
Chris@0 34 public function testLanguageConfigurationElement() {
Chris@0 35 $this->drupalGet('language-tests/language_configuration_element');
Chris@0 36 $edit['lang_configuration[langcode]'] = 'current_interface';
Chris@0 37 $edit['lang_configuration[language_alterable]'] = FALSE;
Chris@0 38 $this->drupalPostForm(NULL, $edit, 'Save');
Chris@0 39 $lang_conf = ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'some_bundle');
Chris@0 40
Chris@0 41 // Check that the settings have been saved.
Chris@0 42 $this->assertEqual($lang_conf->getDefaultLangcode(), 'current_interface');
Chris@0 43 $this->assertFalse($lang_conf->isLanguageAlterable());
Chris@0 44 $this->drupalGet('language-tests/language_configuration_element');
Chris@0 45 $this->assertOptionSelected('edit-lang-configuration-langcode', 'current_interface');
Chris@0 46 $this->assertNoFieldChecked('edit-lang-configuration-language-alterable');
Chris@0 47
Chris@0 48 // Reload the page and save again.
Chris@0 49 $this->drupalGet('language-tests/language_configuration_element');
Chris@0 50 $edit['lang_configuration[langcode]'] = 'authors_default';
Chris@0 51 $edit['lang_configuration[language_alterable]'] = TRUE;
Chris@0 52 $this->drupalPostForm(NULL, $edit, 'Save');
Chris@0 53 $lang_conf = ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'some_bundle');
Chris@0 54
Chris@0 55 // Check that the settings have been saved.
Chris@0 56 $this->assertEqual($lang_conf->getDefaultLangcode(), 'authors_default');
Chris@0 57 $this->assertTrue($lang_conf->isLanguageAlterable());
Chris@0 58 $this->drupalGet('language-tests/language_configuration_element');
Chris@0 59 $this->assertOptionSelected('edit-lang-configuration-langcode', 'authors_default');
Chris@0 60 $this->assertFieldChecked('edit-lang-configuration-language-alterable');
Chris@0 61
Chris@0 62 // Test if content type settings have been saved.
Chris@0 63 $edit = [
Chris@0 64 'name' => 'Page',
Chris@0 65 'type' => 'page',
Chris@0 66 'language_configuration[langcode]' => 'authors_default',
Chris@0 67 'language_configuration[language_alterable]' => TRUE,
Chris@0 68 ];
Chris@0 69 $this->drupalPostForm('admin/structure/types/add', $edit, 'Save and manage fields');
Chris@0 70
Chris@0 71 // Make sure the settings are saved when creating the content type.
Chris@0 72 $this->drupalGet('admin/structure/types/manage/page');
Chris@0 73 $this->assertOptionSelected('edit-language-configuration-langcode', 'authors_default');
Chris@0 74 $this->assertFieldChecked('edit-language-configuration-language-alterable');
Chris@0 75
Chris@0 76 }
Chris@0 77
Chris@0 78 /**
Chris@0 79 * Tests that the language_get_default_langcode() returns the correct values.
Chris@0 80 */
Chris@0 81 public function testDefaultLangcode() {
Chris@0 82 // Add some custom languages.
Chris@0 83 foreach (['aa', 'bb', 'cc'] as $language_code) {
Chris@0 84 ConfigurableLanguage::create([
Chris@0 85 'id' => $language_code,
Chris@0 86 'label' => $this->randomMachineName(),
Chris@0 87 ])->save();
Chris@0 88 }
Chris@0 89
Chris@0 90 // Fixed language.
Chris@0 91 ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'custom_bundle')
Chris@0 92 ->setLanguageAlterable(TRUE)
Chris@0 93 ->setDefaultLangcode('bb')
Chris@0 94 ->save();
Chris@0 95
Chris@0 96 $langcode = language_get_default_langcode('entity_test', 'custom_bundle');
Chris@0 97 $this->assertEqual($langcode, 'bb');
Chris@0 98
Chris@0 99 // Current interface.
Chris@0 100 ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'custom_bundle')
Chris@0 101 ->setLanguageAlterable(TRUE)
Chris@0 102 ->setDefaultLangcode('current_interface')
Chris@0 103 ->save();
Chris@0 104
Chris@0 105 $langcode = language_get_default_langcode('entity_test', 'custom_bundle');
Chris@0 106 $language_interface = \Drupal::languageManager()->getCurrentLanguage();
Chris@0 107 $this->assertEqual($langcode, $language_interface->getId());
Chris@0 108
Chris@0 109 // Site's default.
Chris@0 110 $old_default = \Drupal::languageManager()->getDefaultLanguage();
Chris@0 111 // Ensure the language entity default value is correct.
Chris@0 112 $configurable_language = ConfigurableLanguage::load($old_default->getId());
Chris@0 113 $this->assertTrue($configurable_language->isDefault(), 'The en language entity is flagged as the default language.');
Chris@0 114
Chris@0 115 $this->config('system.site')->set('default_langcode', 'cc')->save();
Chris@0 116 ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'custom_bundle')
Chris@0 117 ->setLanguageAlterable(TRUE)
Chris@0 118 ->setDefaultLangcode(LanguageInterface::LANGCODE_SITE_DEFAULT)
Chris@0 119 ->save();
Chris@0 120 $langcode = language_get_default_langcode('entity_test', 'custom_bundle');
Chris@0 121 $this->assertEqual($langcode, 'cc');
Chris@0 122
Chris@0 123 // Ensure the language entity default value is correct.
Chris@0 124 $configurable_language = ConfigurableLanguage::load($old_default->getId());
Chris@0 125 $this->assertFalse($configurable_language->isDefault(), 'The en language entity is not flagged as the default language.');
Chris@0 126 $configurable_language = ConfigurableLanguage::load('cc');
Chris@0 127 // Check calling the
Chris@0 128 // \Drupal\language\ConfigurableLanguageInterface::isDefault() method
Chris@0 129 // directly.
Chris@0 130 $this->assertTrue($configurable_language->isDefault(), 'The cc language entity is flagged as the default language.');
Chris@0 131
Chris@0 132 // Check the default value of a language field when authors preferred option
Chris@0 133 // is selected.
Chris@0 134 // Create first an user and assign a preferred langcode to him.
Chris@0 135 $some_user = $this->drupalCreateUser();
Chris@0 136 $some_user->preferred_langcode = 'bb';
Chris@0 137 $some_user->save();
Chris@0 138 $this->drupalLogin($some_user);
Chris@0 139 ContentLanguageSettings::create([
Chris@0 140 'target_entity_type_id' => 'entity_test',
Chris@0 141 'target_bundle' => 'some_bundle',
Chris@0 142 ])->setLanguageAlterable(TRUE)
Chris@0 143 ->setDefaultLangcode('authors_default')
Chris@0 144 ->save();
Chris@0 145
Chris@0 146 $this->drupalGet('language-tests/language_configuration_element_test');
Chris@0 147 $this->assertOptionSelected('edit-langcode', 'bb');
Chris@0 148 }
Chris@0 149
Chris@0 150 /**
Chris@0 151 * Tests that the configuration is retained when the node type is updated.
Chris@0 152 */
Chris@0 153 public function testNodeTypeUpdate() {
Chris@0 154 // Create the article content type first if the profile used is not the
Chris@0 155 // standard one.
Chris@0 156 if ($this->profile != 'standard') {
Chris@0 157 $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
Chris@0 158 }
Chris@0 159 $admin_user = $this->drupalCreateUser(['administer content types']);
Chris@0 160 $this->drupalLogin($admin_user);
Chris@0 161 $edit = [
Chris@0 162 'language_configuration[langcode]' => 'current_interface',
Chris@0 163 'language_configuration[language_alterable]' => TRUE,
Chris@0 164 ];
Chris@0 165 $this->drupalPostForm('admin/structure/types/manage/article', $edit, t('Save content type'));
Chris@0 166 // Check the language default configuration for the articles.
Chris@0 167 $configuration = ContentLanguageSettings::loadByEntityTypeBundle('node', 'article');
Chris@0 168 $uuid = $configuration->uuid();
Chris@0 169 $this->assertEqual($configuration->getDefaultLangcode(), 'current_interface', 'The default language configuration has been saved on the Article content type.');
Chris@0 170 $this->assertTrue($configuration->isLanguageAlterable(), 'The alterable language configuration has been saved on the Article content type.');
Chris@0 171 // Update the article content type by changing the title label.
Chris@0 172 $edit = [
Chris@17 173 'title_label' => 'Name',
Chris@0 174 ];
Chris@0 175 $this->drupalPostForm('admin/structure/types/manage/article', $edit, t('Save content type'));
Chris@0 176 // Check that we still have the settings for the updated node type.
Chris@0 177 $configuration = ContentLanguageSettings::loadByEntityTypeBundle('node', 'article');
Chris@0 178 $this->assertEqual($configuration->getDefaultLangcode(), 'current_interface', 'The default language configuration has been kept on the updated Article content type.');
Chris@0 179 $this->assertTrue($configuration->isLanguageAlterable(), 'The alterable language configuration has been kept on the updated Article content type.');
Chris@0 180 $this->assertEqual($configuration->uuid(), $uuid, 'The language configuration uuid has been kept on the updated Article content type.');
Chris@0 181 }
Chris@0 182
Chris@0 183 /**
Chris@0 184 * Tests the language settings are deleted on bundle delete.
Chris@0 185 */
Chris@0 186 public function testNodeTypeDelete() {
Chris@0 187 // Create the article content type first if the profile used is not the
Chris@0 188 // standard one.
Chris@0 189 if ($this->profile != 'standard') {
Chris@0 190 $this->drupalCreateContentType([
Chris@0 191 'type' => 'article',
Chris@17 192 'name' => 'Article',
Chris@0 193 ]);
Chris@0 194 }
Chris@0 195 $admin_user = $this->drupalCreateUser(['administer content types']);
Chris@0 196 $this->drupalLogin($admin_user);
Chris@0 197
Chris@0 198 // Create language configuration for the articles.
Chris@0 199 $edit = [
Chris@0 200 'language_configuration[langcode]' => 'authors_default',
Chris@0 201 'language_configuration[language_alterable]' => TRUE,
Chris@0 202 ];
Chris@0 203 $this->drupalPostForm('admin/structure/types/manage/article', $edit, t('Save content type'));
Chris@0 204
Chris@0 205 // Check the language default configuration for articles is present.
Chris@0 206 $configuration = \Drupal::entityManager()->getStorage('language_content_settings')->load('node.article');
Chris@0 207 $this->assertTrue($configuration, 'The language configuration is present.');
Chris@0 208
Chris@0 209 // Delete 'article' bundle.
Chris@0 210 $this->drupalPostForm('admin/structure/types/manage/article/delete', [], t('Delete'));
Chris@0 211
Chris@0 212 // Check that the language configuration has been deleted.
Chris@0 213 \Drupal::entityManager()->getStorage('language_content_settings')->resetCache();
Chris@0 214 $configuration = \Drupal::entityManager()->getStorage('language_content_settings')->load('node.article');
Chris@0 215 $this->assertFalse($configuration, 'The language configuration was deleted after bundle was deleted.');
Chris@0 216 }
Chris@0 217
Chris@0 218 /**
Chris@0 219 * Tests that the configuration is retained when a vocabulary is updated.
Chris@0 220 */
Chris@0 221 public function testTaxonomyVocabularyUpdate() {
Chris@0 222 $vocabulary = Vocabulary::create([
Chris@0 223 'name' => 'Country',
Chris@0 224 'vid' => 'country',
Chris@0 225 ]);
Chris@0 226 $vocabulary->save();
Chris@0 227
Chris@0 228 $admin_user = $this->drupalCreateUser(['administer taxonomy']);
Chris@0 229 $this->drupalLogin($admin_user);
Chris@0 230 $edit = [
Chris@0 231 'default_language[langcode]' => 'current_interface',
Chris@0 232 'default_language[language_alterable]' => TRUE,
Chris@0 233 ];
Chris@0 234 $this->drupalPostForm('admin/structure/taxonomy/manage/country', $edit, t('Save'));
Chris@0 235
Chris@0 236 // Check the language default configuration.
Chris@0 237 $configuration = ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', 'country');
Chris@0 238 $uuid = $configuration->uuid();
Chris@0 239 $this->assertEqual($configuration->getDefaultLangcode(), 'current_interface', 'The default language configuration has been saved on the Country vocabulary.');
Chris@0 240 $this->assertTrue($configuration->isLanguageAlterable(), 'The alterable language configuration has been saved on the Country vocabulary.');
Chris@0 241 // Update the vocabulary.
Chris@0 242 $edit = [
Chris@17 243 'name' => 'Nation',
Chris@0 244 ];
Chris@0 245 $this->drupalPostForm('admin/structure/taxonomy/manage/country', $edit, t('Save'));
Chris@0 246 // Check that we still have the settings for the updated vocabulary.
Chris@0 247 $configuration = ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', 'country');
Chris@0 248 $this->assertEqual($configuration->getDefaultLangcode(), 'current_interface', 'The default language configuration has been kept on the updated Country vocabulary.');
Chris@0 249 $this->assertTrue($configuration->isLanguageAlterable(), 'The alterable language configuration has been kept on the updated Country vocabulary.');
Chris@0 250 $this->assertEqual($configuration->uuid(), $uuid, 'The language configuration uuid has been kept on the updated Country vocabulary.');
Chris@0 251 }
Chris@0 252
Chris@0 253 }