PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/language/src/Tests/LanguageNegotiationInfoTest.php

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