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

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 1fec387a4317
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\Plugin\LanguageNegotiation\LanguageNegotiationUI;
Chris@0 7 use Drupal\Tests\BrowserTestBase;
Chris@0 8
Chris@0 9 /**
Chris@0 10 * Tests alterations to language types/negotiation info.
Chris@0 11 *
Chris@0 12 * @group language
Chris@0 13 */
Chris@0 14 class LanguageNegotiationInfoTest extends BrowserTestBase {
Chris@0 15
Chris@0 16 /**
Chris@0 17 * Modules to enable.
Chris@0 18 *
Chris@0 19 * @var array
Chris@0 20 */
Chris@0 21 public static $modules = ['language', 'content_translation'];
Chris@0 22
Chris@0 23 /**
Chris@0 24 * {@inheritdoc}
Chris@0 25 */
Chris@0 26 protected function setUp() {
Chris@0 27 parent::setUp();
Chris@0 28 $admin_user = $this->drupalCreateUser(['administer languages', 'access administration pages', 'view the administration theme', 'administer modules']);
Chris@0 29 $this->drupalLogin($admin_user);
Chris@0 30 $this->drupalPostForm('admin/config/regional/language/add', ['predefined_langcode' => 'it'], t('Add language'));
Chris@0 31 }
Chris@0 32
Chris@0 33 /**
Chris@0 34 * Returns the configurable language manager.
Chris@0 35 *
Chris@0 36 * @return \Drupal\language\ConfigurableLanguageManager
Chris@0 37 */
Chris@0 38 protected function languageManager() {
Chris@0 39 return $this->container->get('language_manager');
Chris@0 40 }
Chris@0 41
Chris@0 42 /**
Chris@0 43 * Sets state flags for language_test module.
Chris@0 44 *
Chris@0 45 * Ensures to correctly update data both in the child site and the test runner
Chris@0 46 * environment.
Chris@0 47 *
Chris@0 48 * @param array $values
Chris@0 49 * The key/value pairs to set in state.
Chris@0 50 */
Chris@0 51 protected function stateSet(array $values) {
Chris@0 52 // Set the new state values.
Chris@0 53 $this->container->get('state')->setMultiple($values);
Chris@0 54 // Refresh in-memory static state/config caches and static variables.
Chris@0 55 $this->refreshVariables();
Chris@0 56 // Refresh/rewrite language negotiation configuration, in order to pick up
Chris@0 57 // the manipulations performed by language_test module's info alter hooks.
Chris@0 58 $this->container->get('language_negotiator')->purgeConfiguration();
Chris@0 59 }
Chris@0 60
Chris@0 61 /**
Chris@0 62 * Tests alterations to language types/negotiation info.
Chris@0 63 */
Chris@0 64 public function testInfoAlterations() {
Chris@0 65 $this->stateSet([
Chris@0 66 // Enable language_test type info.
Chris@0 67 'language_test.language_types' => TRUE,
Chris@0 68 // Enable language_test negotiation info (not altered yet).
Chris@0 69 'language_test.language_negotiation_info' => TRUE,
Chris@0 70 // Alter LanguageInterface::TYPE_CONTENT to be configurable.
Chris@0 71 'language_test.content_language_type' => TRUE,
Chris@0 72 ]);
Chris@0 73 $this->container->get('module_installer')->install(['language_test']);
Chris@0 74 $this->resetAll();
Chris@0 75
Chris@0 76 // Check that fixed language types are properly configured without the need
Chris@0 77 // of saving the language negotiation settings.
Chris@0 78 $this->checkFixedLanguageTypes();
Chris@0 79
Chris@0 80 $type = LanguageInterface::TYPE_CONTENT;
Chris@0 81 $language_types = $this->languageManager()->getLanguageTypes();
Chris@0 82 $this->assertTrue(in_array($type, $language_types), 'Content language type is configurable.');
Chris@0 83
Chris@0 84 // Enable some core and custom language negotiation methods. The test
Chris@0 85 // language type is supposed to be configurable.
Chris@0 86 $test_type = 'test_language_type';
Chris@0 87 $interface_method_id = LanguageNegotiationUI::METHOD_ID;
Chris@0 88 $test_method_id = 'test_language_negotiation_method';
Chris@0 89 $form_field = $type . '[enabled][' . $interface_method_id . ']';
Chris@0 90 $edit = [
Chris@0 91 $form_field => TRUE,
Chris@0 92 $type . '[enabled][' . $test_method_id . ']' => TRUE,
Chris@0 93 $test_type . '[enabled][' . $test_method_id . ']' => TRUE,
Chris@0 94 $test_type . '[configurable]' => TRUE,
Chris@0 95 ];
Chris@0 96 $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
Chris@0 97
Chris@0 98 // Alter language negotiation info to remove interface language negotiation
Chris@0 99 // method.
Chris@0 100 $this->stateSet([
Chris@0 101 'language_test.language_negotiation_info_alter' => TRUE,
Chris@0 102 ]);
Chris@0 103
Chris@0 104 $negotiation = $this->config('language.types')->get('negotiation.' . $type . '.enabled');
Chris@0 105 $this->assertFalse(isset($negotiation[$interface_method_id]), 'Interface language negotiation method removed from the stored settings.');
Chris@0 106
Chris@0 107 $this->drupalGet('admin/config/regional/language/detection');
Chris@0 108 $this->assertNoFieldByName($form_field, NULL, 'Interface language negotiation method unavailable.');
Chris@0 109
Chris@0 110 // Check that type-specific language negotiation methods can be assigned
Chris@0 111 // only to the corresponding language types.
Chris@0 112 foreach ($this->languageManager()->getLanguageTypes() as $type) {
Chris@0 113 $form_field = $type . '[enabled][test_language_negotiation_method_ts]';
Chris@0 114 if ($type == $test_type) {
Chris@0 115 $this->assertFieldByName($form_field, NULL, format_string('Type-specific test language negotiation method available for %type.', ['%type' => $type]));
Chris@0 116 }
Chris@0 117 else {
Chris@0 118 $this->assertNoFieldByName($form_field, NULL, format_string('Type-specific test language negotiation method unavailable for %type.', ['%type' => $type]));
Chris@0 119 }
Chris@0 120 }
Chris@0 121
Chris@0 122 // Check language negotiation results.
Chris@0 123 $this->drupalGet('');
Chris@0 124 $last = $this->container->get('state')->get('language_test.language_negotiation_last');
Chris@0 125 foreach ($this->languageManager()->getDefinedLanguageTypes() as $type) {
Chris@0 126 $langcode = $last[$type];
Chris@0 127 $value = $type == LanguageInterface::TYPE_CONTENT || strpos($type, 'test') !== FALSE ? 'it' : 'en';
Chris@0 128 $this->assertEqual($langcode, $value, format_string('The negotiated language for %type is %language', ['%type' => $type, '%language' => $value]));
Chris@0 129 }
Chris@0 130
Chris@0 131 // Uninstall language_test and check that everything is set back to the
Chris@0 132 // original status.
Chris@0 133 $this->container->get('module_installer')->uninstall(['language_test']);
Chris@0 134 $this->rebuildContainer();
Chris@0 135
Chris@0 136 // Check that only the core language types are available.
Chris@0 137 foreach ($this->languageManager()->getDefinedLanguageTypes() as $type) {
Chris@0 138 $this->assertTrue(strpos($type, 'test') === FALSE, format_string('The %type language is still available', ['%type' => $type]));
Chris@0 139 }
Chris@0 140
Chris@0 141 // Check that fixed language types are properly configured, even those
Chris@0 142 // previously set to configurable.
Chris@0 143 $this->checkFixedLanguageTypes();
Chris@0 144
Chris@0 145 // Check that unavailable language negotiation methods are not present in
Chris@0 146 // the negotiation settings.
Chris@0 147 $negotiation = $this->config('language.types')->get('negotiation.' . $type . '.enabled');
Chris@0 148 $this->assertFalse(isset($negotiation[$test_method_id]), 'The disabled test language negotiation method is not part of the content language negotiation settings.');
Chris@0 149
Chris@0 150 // Check that configuration page presents the correct options and settings.
Chris@0 151 $this->assertNoRaw(t('Test language detection'), 'No test language type configuration available.');
Chris@0 152 $this->assertNoRaw(t('This is a test language negotiation method'), 'No test language negotiation method available.');
Chris@0 153 }
Chris@0 154
Chris@0 155 /**
Chris@0 156 * Check that language negotiation for fixed types matches the stored one.
Chris@0 157 */
Chris@0 158 protected function checkFixedLanguageTypes() {
Chris@0 159 $configurable = $this->languageManager()->getLanguageTypes();
Chris@0 160 foreach ($this->languageManager()->getDefinedLanguageTypesInfo() as $type => $info) {
Chris@0 161 if (!in_array($type, $configurable) && isset($info['fixed'])) {
Chris@0 162 $negotiation = $this->config('language.types')->get('negotiation.' . $type . '.enabled');
Chris@14 163 $equal = array_keys($negotiation) === array_values($info['fixed']);
Chris@0 164 $this->assertTrue($equal, format_string('language negotiation for %type is properly set up', ['%type' => $type]));
Chris@0 165 }
Chris@0 166 }
Chris@0 167 }
Chris@0 168
Chris@0 169 /**
Chris@0 170 * Tests altering config of configurable language types.
Chris@0 171 */
Chris@0 172 public function testConfigLangTypeAlterations() {
Chris@0 173 // Default of config.
Chris@0 174 $test_type = LanguageInterface::TYPE_CONTENT;
Chris@0 175 $this->assertFalse($this->isLanguageTypeConfigurable($test_type), 'Language type is not configurable.');
Chris@0 176
Chris@0 177 // Editing config.
Chris@0 178 $edit = [$test_type . '[configurable]' => TRUE];
Chris@0 179 $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
Chris@0 180 $this->assertTrue($this->isLanguageTypeConfigurable($test_type), 'Language type is now configurable.');
Chris@0 181
Chris@0 182 // After installing another module, the config should be the same.
Chris@0 183 $this->drupalPostForm('admin/modules', ['modules[test_module][enable]' => 1], t('Install'));
Chris@0 184 $this->assertTrue($this->isLanguageTypeConfigurable($test_type), 'Language type is still configurable.');
Chris@0 185
Chris@0 186 // After uninstalling the other module, the config should be the same.
Chris@0 187 $this->drupalPostForm('admin/modules/uninstall', ['uninstall[test_module]' => 1], t('Uninstall'));
Chris@0 188 $this->assertTrue($this->isLanguageTypeConfigurable($test_type), 'Language type is still configurable.');
Chris@0 189 }
Chris@0 190
Chris@0 191 /**
Chris@0 192 * Checks whether the given language type is configurable.
Chris@0 193 *
Chris@0 194 * @param string $type
Chris@0 195 * The language type.
Chris@0 196 *
Chris@0 197 * @return bool
Chris@0 198 * TRUE if the specified language type is configurable, FALSE otherwise.
Chris@0 199 */
Chris@0 200 protected function isLanguageTypeConfigurable($type) {
Chris@0 201 $configurable_types = $this->config('language.types')->get('configurable');
Chris@0 202 return in_array($type, $configurable_types);
Chris@0 203 }
Chris@0 204
Chris@0 205 }