Skip navigation
Help

locale.test

  1. drupal
    1. 7 drupal/modules/locale/locale.test

Tests for locale.module.

The test file includes:

  • a functional test for the language configuration forms;
  • functional tests for the translation functionalities, including searching;
  • a functional test for the PO files import feature, including validation;
  • functional tests for translations and templates export feature;
  • functional tests for the uninstall process;
  • a functional test for the language switching feature;
  • a functional test for a user's ability to change their default language;
  • a functional test for configuring a different path alias per language;
  • a functional test for configuring a different path alias per language;
  • a functional test for multilingual support by content type and on nodes.
  • a functional test for multilingual fields.
  • a functional test for comment language.
  • a functional test fot language types/negotiation info.

Classes

NameDescription
LocaleCommentLanguageFunctionalTestFunctional tests for comment language.
LocaleConfigurationTestFunctional tests for the language configuration forms.
LocaleContentFunctionalTestFunctional tests for multilingual support on nodes.
LocaleDateFormatsFunctionalTestFunctional tests for localizing date formats.
LocaleExportFunctionalTestFunctional tests for the export of translation files.
LocaleImportFunctionalTestFunctional tests for the import of translation files.
LocaleInstallTestTests for the st() function.
LocaleLanguageNegotiationInfoFunctionalTestFunctional test for language types/negotiation info.
LocaleLanguageSwitchingFunctionalTestFunctional tests for the language switching feature.
LocaleMultilingualFieldsFunctionalTestFunctional test for multilingual fields.
LocalePathFunctionalTestFunctional tests for configuring a different path alias per language.
LocaleTranslationFunctionalTestFunctional test for string translation and validation.
LocaleUILanguageNegotiationTestTest UI language negotiation 1. URL (PATH) > DEFAULT UI Language base on URL prefix, browser language preference has no influence: admin/config UI in site default language zh-hans/admin/config UI in Chinese blah-blah/admin/config 404 2. URL (PATH)…
LocaleUninstallFrenchFunctionalTestLocale uninstall with French UI functional test.
LocaleUninstallFunctionalTestLocale uninstall with English UI functional test.
LocaleUrlRewritingTestTest that URL rewriting works as expected.
LocaleUserCreationTestFunctional test for language handling during user creation.
LocaleUserLanguageFunctionalTestFunctional tests for a user's ability to change their default language.

File

drupal/modules/locale/locale.test
View source
  1. <?php
  2. /**
  3. * @file
  4. * Tests for locale.module.
  5. *
  6. * The test file includes:
  7. * - a functional test for the language configuration forms;
  8. * - functional tests for the translation functionalities, including searching;
  9. * - a functional test for the PO files import feature, including validation;
  10. * - functional tests for translations and templates export feature;
  11. * - functional tests for the uninstall process;
  12. * - a functional test for the language switching feature;
  13. * - a functional test for a user's ability to change their default language;
  14. * - a functional test for configuring a different path alias per language;
  15. * - a functional test for configuring a different path alias per language;
  16. * - a functional test for multilingual support by content type and on nodes.
  17. * - a functional test for multilingual fields.
  18. * - a functional test for comment language.
  19. * - a functional test fot language types/negotiation info.
  20. */
  21. /**
  22. * Functional tests for the language configuration forms.
  23. */
  24. class LocaleConfigurationTest extends DrupalWebTestCase {
  25. public static function getInfo() {
  26. return array(
  27. 'name' => 'Language configuration',
  28. 'description' => 'Adds a new locale and tests changing its status and the default language.',
  29. 'group' => 'Locale',
  30. );
  31. }
  32. function setUp() {
  33. parent::setUp('locale');
  34. }
  35. /**
  36. * Functional tests for adding, editing and deleting languages.
  37. */
  38. function testLanguageConfiguration() {
  39. global $base_url;
  40. // User to add and remove language.
  41. $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages'));
  42. $this->drupalLogin($admin_user);
  43. // Add predefined language.
  44. $edit = array(
  45. 'langcode' => 'fr',
  46. );
  47. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
  48. $this->assertText('fr', t('Language added successfully.'));
  49. $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.'));
  50. // Add custom language.
  51. // Code for the language.
  52. $langcode = 'xx';
  53. // The English name for the language.
  54. $name = $this->randomName(16);
  55. // The native name for the language.
  56. $native = $this->randomName(16);
  57. // The domain prefix.
  58. $prefix = $langcode;
  59. $edit = array(
  60. 'langcode' => $langcode,
  61. 'name' => $name,
  62. 'native' => $native,
  63. 'prefix' => $prefix,
  64. 'direction' => '0',
  65. );
  66. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
  67. $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.'));
  68. $this->assertText($langcode, t('Language code found.'));
  69. $this->assertText($name, t('Name found.'));
  70. $this->assertText($native, t('Native found.'));
  71. $this->assertText($native, t('Test language added.'));
  72. // Check if we can change the default language.
  73. $path = 'admin/config/regional/language';
  74. $this->drupalGet($path);
  75. $this->assertFieldChecked('edit-site-default-en', t('English is the default language.'));
  76. // Change the default language.
  77. $edit = array(
  78. 'site_default' => $langcode,
  79. );
  80. $this->drupalPost(NULL, $edit, t('Save configuration'));
  81. $this->assertNoFieldChecked('edit-site-default-en', t('Default language updated.'));
  82. $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.'));
  83. // Check if a valid language prefix is added after changing the default
  84. // language.
  85. $this->drupalGet('admin/config/regional/language/edit/en');
  86. $this->assertFieldByXPath('//input[@name="prefix"]', 'en', t('A valid path prefix has been added to the previous default language.'));
  87. // Ensure we can't delete the default language.
  88. $this->drupalGet('admin/config/regional/language/delete/' . $langcode);
  89. $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.'));
  90. $this->assertText(t('The default language cannot be deleted.'), t('Failed to delete the default language.'));
  91. // Check if we can disable a language.
  92. $edit = array(
  93. 'enabled[en]' => FALSE,
  94. );
  95. $this->drupalPost($path, $edit, t('Save configuration'));
  96. $this->assertNoFieldChecked('edit-enabled-en', t('Language disabled.'));
  97. // Set disabled language to be the default and ensure it is re-enabled.
  98. $edit = array(
  99. 'site_default' => 'en',
  100. );
  101. $this->drupalPost(NULL, $edit, t('Save configuration'));
  102. $this->assertFieldChecked('edit-enabled-en', t('Default language re-enabled.'));
  103. // Ensure 'edit' link works.
  104. $this->clickLink(t('edit'));
  105. $this->assertTitle(t('Edit language | Drupal'), t('Page title is "Edit language".'));
  106. // Edit a language.
  107. $name = $this->randomName(16);
  108. $edit = array(
  109. 'name' => $name,
  110. );
  111. $this->drupalPost('admin/config/regional/language/edit/' . $langcode, $edit, t('Save language'));
  112. $this->assertRaw($name, t('The language has been updated.'));
  113. $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.'));
  114. // Ensure 'delete' link works.
  115. $this->drupalGet('admin/config/regional/language');
  116. $this->clickLink(t('delete'));
  117. $this->assertText(t('Are you sure you want to delete the language'), t('"delete" link is correct.'));
  118. // Delete an enabled language.
  119. $this->drupalGet('admin/config/regional/language/delete/' . $langcode);
  120. // First test the 'cancel' link.
  121. $this->clickLink(t('Cancel'));
  122. $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.'));
  123. $this->assertRaw($name, t('The language was not deleted.'));
  124. // Delete the language for real. This a confirm form, we do not need any
  125. // fields changed.
  126. $this->drupalPost('admin/config/regional/language/delete/' . $langcode, array(), t('Delete'));
  127. // We need raw here because %locale will add HTML.
  128. $this->assertRaw(t('The language %locale has been removed.', array('%locale' => $name)), t('The test language has been removed.'));
  129. $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.'));
  130. // Verify that language is no longer found.
  131. $this->drupalGet('admin/config/regional/language/delete/' . $langcode);
  132. $this->assertResponse(404, t('Language no longer found.'));
  133. // Make sure the "language_count" variable has been updated correctly.
  134. drupal_static_reset('language_list');
  135. $enabled = language_list('enabled');
  136. $this->assertEqual(variable_get('language_count', 1), count($enabled[1]), t('Language count is correct.'));
  137. // Delete a disabled language.
  138. // Disable an enabled language.
  139. $edit = array(
  140. 'enabled[fr]' => FALSE,
  141. );
  142. $this->drupalPost($path, $edit, t('Save configuration'));
  143. $this->assertNoFieldChecked('edit-enabled-fr', t('French language disabled.'));
  144. // Get the count of enabled languages.
  145. drupal_static_reset('language_list');
  146. $enabled = language_list('enabled');
  147. // Delete the disabled language.
  148. $this->drupalPost('admin/config/regional/language/delete/fr', array(), t('Delete'));
  149. // We need raw here because %locale will add HTML.
  150. $this->assertRaw(t('The language %locale has been removed.', array('%locale' => 'French')), t('Disabled language has been removed.'));
  151. $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.'));
  152. // Verify that language is no longer found.
  153. $this->drupalGet('admin/config/regional/language/delete/fr');
  154. $this->assertResponse(404, t('Language no longer found.'));
  155. // Make sure the "language_count" variable has not changed.
  156. $this->assertEqual(variable_get('language_count', 1), count($enabled[1]), t('Language count is correct.'));
  157. // Ensure we can't delete the English language.
  158. $this->drupalGet('admin/config/regional/language/delete/en');
  159. $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.'));
  160. $this->assertText(t('The English language cannot be deleted.'), t('Failed to delete English language.'));
  161. }
  162. }
  163. /**
  164. * Functional test for string translation and validation.
  165. */
  166. class LocaleTranslationFunctionalTest extends DrupalWebTestCase {
  167. public static function getInfo() {
  168. return array(
  169. 'name' => 'String translate, search and validate',
  170. 'description' => 'Adds a new locale and translates its name. Checks the validation of translation strings and search results.',
  171. 'group' => 'Locale',
  172. );
  173. }
  174. function setUp() {
  175. parent::setUp('locale');
  176. }
  177. /**
  178. * Adds a language and tests string translation by users with the appropriate permissions.
  179. */
  180. function testStringTranslation() {
  181. global $base_url;
  182. // User to add and remove language.
  183. $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages'));
  184. // User to translate and delete string.
  185. $translate_user = $this->drupalCreateUser(array('translate interface', 'access administration pages'));
  186. // Code for the language.
  187. $langcode = 'xx';
  188. // The English name for the language. This will be translated.
  189. $name = $this->randomName(16);
  190. // The native name for the language.
  191. $native = $this->randomName(16);
  192. // The domain prefix.
  193. $prefix = $langcode;
  194. // This is the language indicator on the translation search screen for
  195. // untranslated strings. Copied straight from locale.inc.
  196. $language_indicator = "<em class=\"locale-untranslated\">$langcode</em> ";
  197. // This will be the translation of $name.
  198. $translation = $this->randomName(16);
  199. // Add custom language.
  200. $this->drupalLogin($admin_user);
  201. $edit = array(
  202. 'langcode' => $langcode,
  203. 'name' => $name,
  204. 'native' => $native,
  205. 'prefix' => $prefix,
  206. 'direction' => '0',
  207. );
  208. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
  209. // Add string.
  210. t($name, array(), array('langcode' => $langcode));
  211. // Reset locale cache.
  212. locale_reset();
  213. $this->assertText($langcode, t('Language code found.'));
  214. $this->assertText($name, t('Name found.'));
  215. $this->assertText($native, t('Native found.'));
  216. // No t() here, we do not want to add this string to the database and it's
  217. // surely not translated yet.
  218. $this->assertText($native, t('Test language added.'));
  219. $this->drupalLogout();
  220. // Search for the name and translate it.
  221. $this->drupalLogin($translate_user);
  222. $search = array(
  223. 'string' => $name,
  224. 'language' => 'all',
  225. 'translation' => 'all',
  226. 'group' => 'all',
  227. );
  228. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  229. // assertText() seems to remove the input field where $name always could be
  230. // found, so this is not a false assert. See how assertNoText succeeds
  231. // later.
  232. $this->assertText($name, t('Search found the name.'));
  233. $this->assertRaw($language_indicator, t('Name is untranslated.'));
  234. // Assume this is the only result, given the random name.
  235. $this->clickLink(t('edit'));
  236. // We save the lid from the path.
  237. $matches = array();
  238. preg_match('!admin/config/regional/translate/edit/(\d+)!', $this->getUrl(), $matches);
  239. $lid = $matches[1];
  240. // No t() here, it's surely not translated yet.
  241. $this->assertText($name, t('name found on edit screen.'));
  242. $edit = array(
  243. "translations[$langcode]" => $translation,
  244. );
  245. $this->drupalPost(NULL, $edit, t('Save translations'));
  246. $this->assertText(t('The string has been saved.'), t('The string has been saved.'));
  247. $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/translate', array('absolute' => TRUE)), t('Correct page redirection.'));
  248. $this->assertTrue($name != $translation && t($name, array(), array('langcode' => $langcode)) == $translation, t('t() works.'));
  249. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  250. // The indicator should not be here.
  251. $this->assertNoRaw($language_indicator, t('String is translated.'));
  252. // Try to edit a non-existent string and ensure we're redirected correctly.
  253. // Assuming we don't have 999,999 strings already.
  254. $random_lid = 999999;
  255. $this->drupalGet('admin/config/regional/translate/edit/' . $random_lid);
  256. $this->assertText(t('String not found'), t('String not found.'));
  257. $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/translate', array('absolute' => TRUE)), t('Correct page redirection.'));
  258. $this->drupalLogout();
  259. // Delete the language.
  260. $this->drupalLogin($admin_user);
  261. $path = 'admin/config/regional/language/delete/' . $langcode;
  262. // This a confirm form, we do not need any fields changed.
  263. $this->drupalPost($path, array(), t('Delete'));
  264. // We need raw here because %locale will add HTML.
  265. $this->assertRaw(t('The language %locale has been removed.', array('%locale' => $name)), t('The test language has been removed.'));
  266. // Reload to remove $name.
  267. $this->drupalGet($path);
  268. $this->assertNoText($langcode, t('Language code not found.'));
  269. $this->assertNoText($name, t('Name not found.'));
  270. $this->assertNoText($native, t('Native not found.'));
  271. $this->drupalLogout();
  272. // Delete the string.
  273. $this->drupalLogin($translate_user);
  274. $search = array(
  275. 'string' => $name,
  276. 'language' => 'all',
  277. 'translation' => 'all',
  278. 'group' => 'all',
  279. );
  280. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  281. // Assume this is the only result, given the random name.
  282. $this->clickLink(t('delete'));
  283. $this->assertText(t('Are you sure you want to delete the string'), t('"delete" link is correct.'));
  284. // Delete the string.
  285. $path = 'admin/config/regional/translate/delete/' . $lid;
  286. $this->drupalGet($path);
  287. // First test the 'cancel' link.
  288. $this->clickLink(t('Cancel'));
  289. $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/translate', array('absolute' => TRUE)), t('Correct page redirection.'));
  290. $this->assertRaw($name, t('The string was not deleted.'));
  291. // Delete the name string.
  292. $this->drupalPost('admin/config/regional/translate/delete/' . $lid, array(), t('Delete'));
  293. $this->assertText(t('The string has been removed.'), t('The string has been removed message.'));
  294. $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/translate', array('absolute' => TRUE)), t('Correct page redirection.'));
  295. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  296. $this->assertNoText($name, t('Search now can not find the name.'));
  297. }
  298. /*
  299. * Adds a language and checks that the JavaScript translation files are
  300. * properly created and rebuilt on deletion.
  301. */
  302. function testJavaScriptTranslation() {
  303. $user = $this->drupalCreateUser(array('translate interface', 'administer languages', 'access administration pages'));
  304. $this->drupalLogin($user);
  305. $langcode = 'xx';
  306. // The English name for the language. This will be translated.
  307. $name = $this->randomName(16);
  308. // The native name for the language.
  309. $native = $this->randomName(16);
  310. // The domain prefix.
  311. $prefix = $langcode;
  312. // Add custom language.
  313. $edit = array(
  314. 'langcode' => $langcode,
  315. 'name' => $name,
  316. 'native' => $native,
  317. 'prefix' => $prefix,
  318. 'direction' => '0',
  319. );
  320. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
  321. drupal_static_reset('language_list');
  322. // Build the JavaScript translation file.
  323. $this->drupalGet('admin/config/regional/translate/translate');
  324. // Retrieve the id of the first string available in the {locales_source}
  325. // table and translate it.
  326. $query = db_select('locales_source', 'l');
  327. $query->addExpression('min(l.lid)', 'lid');
  328. $result = $query->condition('l.location', '%.js%', 'LIKE')
  329. ->condition('l.textgroup', 'default')
  330. ->execute();
  331. $url = 'admin/config/regional/translate/edit/' . $result->fetchObject()->lid;
  332. $edit = array('translations['. $langcode .']' => $this->randomName());
  333. $this->drupalPost($url, $edit, t('Save translations'));
  334. // Trigger JavaScript translation parsing and building.
  335. require_once DRUPAL_ROOT . '/includes/locale.inc';
  336. _locale_rebuild_js($langcode);
  337. // Retrieve the JavaScript translation hash code for the custom language to
  338. // check that the translation file has been properly built.
  339. $file = db_select('languages', 'l')
  340. ->fields('l', array('javascript'))
  341. ->condition('language', $langcode)
  342. ->execute()
  343. ->fetchObject();
  344. $js_file = 'public://' . variable_get('locale_js_directory', 'languages') . '/' . $langcode . '_' . $file->javascript . '.js';
  345. $this->assertTrue($result = file_exists($js_file), t('JavaScript file created: %file', array('%file' => $result ? $js_file : t('not found'))));
  346. // Test JavaScript translation rebuilding.
  347. file_unmanaged_delete($js_file);
  348. $this->assertTrue($result = !file_exists($js_file), t('JavaScript file deleted: %file', array('%file' => $result ? $js_file : t('found'))));
  349. cache_clear_all();
  350. _locale_rebuild_js($langcode);
  351. $this->assertTrue($result = file_exists($js_file), t('JavaScript file rebuilt: %file', array('%file' => $result ? $js_file : t('not found'))));
  352. }
  353. /**
  354. * Tests the validation of the translation input.
  355. */
  356. function testStringValidation() {
  357. global $base_url;
  358. // User to add language and strings.
  359. $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages', 'translate interface'));
  360. $this->drupalLogin($admin_user);
  361. $langcode = 'xx';
  362. // The English name for the language. This will be translated.
  363. $name = $this->randomName(16);
  364. // The native name for the language.
  365. $native = $this->randomName(16);
  366. // The domain prefix.
  367. $prefix = $langcode;
  368. // This is the language indicator on the translation search screen for
  369. // untranslated strings. Copied straight from locale.inc.
  370. $language_indicator = "<em class=\"locale-untranslated\">$langcode</em> ";
  371. // These will be the invalid translations of $name.
  372. $key = $this->randomName(16);
  373. $bad_translations[$key] = "<script>alert('xss');</script>" . $key;
  374. $key = $this->randomName(16);
  375. $bad_translations[$key] = '<img SRC="javascript:alert(\'xss\');">' . $key;
  376. $key = $this->randomName(16);
  377. $bad_translations[$key] = '<<SCRIPT>alert("xss");//<</SCRIPT>' . $key;
  378. $key = $this->randomName(16);
  379. $bad_translations[$key] ="<BODY ONLOAD=alert('xss')>" . $key;
  380. // Add custom language.
  381. $edit = array(
  382. 'langcode' => $langcode,
  383. 'name' => $name,
  384. 'native' => $native,
  385. 'prefix' => $prefix,
  386. 'direction' => '0',
  387. );
  388. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
  389. // Add string.
  390. t($name, array(), array('langcode' => $langcode));
  391. // Reset locale cache.
  392. $search = array(
  393. 'string' => $name,
  394. 'language' => 'all',
  395. 'translation' => 'all',
  396. 'group' => 'all',
  397. );
  398. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  399. // Find the edit path.
  400. $content = $this->drupalGetContent();
  401. $this->assertTrue(preg_match('@(admin/config/regional/translate/edit/[0-9]+)@', $content, $matches), t('Found the edit path.'));
  402. $path = $matches[0];
  403. foreach ($bad_translations as $key => $translation) {
  404. $edit = array(
  405. "translations[$langcode]" => $translation,
  406. );
  407. $this->drupalPost($path, $edit, t('Save translations'));
  408. // Check for a form error on the textarea.
  409. $form_class = $this->xpath('//form[@id="locale-translate-edit-form"]//textarea/@class');
  410. $this->assertNotIdentical(FALSE, strpos($form_class[0], 'error'), t('The string was rejected as unsafe.'));
  411. $this->assertNoText(t('The string has been saved.'), t('The string was not saved.'));
  412. }
  413. }
  414. /**
  415. * Tests translation search form.
  416. */
  417. function testStringSearch() {
  418. global $base_url;
  419. // User to add and remove language.
  420. $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages'));
  421. // User to translate and delete string.
  422. $translate_user = $this->drupalCreateUser(array('translate interface', 'access administration pages'));
  423. // Code for the language.
  424. $langcode = 'xx';
  425. // The English name for the language. This will be translated.
  426. $name = $this->randomName(16);
  427. // The native name for the language.
  428. $native = $this->randomName(16);
  429. // The domain prefix.
  430. $prefix = $langcode;
  431. // This is the language indicator on the translation search screen for
  432. // untranslated strings. Copied straight from locale.inc.
  433. $language_indicator = "<em class=\"locale-untranslated\">$langcode</em> ";
  434. // This will be the translation of $name.
  435. $translation = $this->randomName(16);
  436. // Add custom language.
  437. $this->drupalLogin($admin_user);
  438. $edit = array(
  439. 'langcode' => $langcode,
  440. 'name' => $name,
  441. 'native' => $native,
  442. 'prefix' => $prefix,
  443. 'direction' => '0',
  444. );
  445. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
  446. // Add string.
  447. t($name, array(), array('langcode' => $langcode));
  448. // Reset locale cache.
  449. locale_reset();
  450. $this->drupalLogout();
  451. // Search for the name.
  452. $this->drupalLogin($translate_user);
  453. $search = array(
  454. 'string' => $name,
  455. 'language' => 'all',
  456. 'translation' => 'all',
  457. 'group' => 'all',
  458. );
  459. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  460. // assertText() seems to remove the input field where $name always could be
  461. // found, so this is not a false assert. See how assertNoText succeeds
  462. // later.
  463. $this->assertText($name, t('Search found the string.'));
  464. // Ensure untranslated string doesn't appear if searching on 'only
  465. // translated strings'.
  466. $search = array(
  467. 'string' => $name,
  468. 'language' => 'all',
  469. 'translation' => 'translated',
  470. 'group' => 'all',
  471. );
  472. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  473. $this->assertText(t('No strings available.'), t("Search didn't find the string."));
  474. // Ensure untranslated string appears if searching on 'only untranslated
  475. // strings'.
  476. $search = array(
  477. 'string' => $name,
  478. 'language' => 'all',
  479. 'translation' => 'untranslated',
  480. 'group' => 'all',
  481. );
  482. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  483. $this->assertNoText(t('No strings available.'), t('Search found the string.'));
  484. // Add translation.
  485. // Assume this is the only result, given the random name.
  486. $this->clickLink(t('edit'));
  487. // We save the lid from the path.
  488. $matches = array();
  489. preg_match('!admin/config/regional/translate/edit/(\d)+!', $this->getUrl(), $matches);
  490. $lid = $matches[1];
  491. $edit = array(
  492. "translations[$langcode]" => $translation,
  493. );
  494. $this->drupalPost(NULL, $edit, t('Save translations'));
  495. // Ensure translated string does appear if searching on 'only
  496. // translated strings'.
  497. $search = array(
  498. 'string' => $translation,
  499. 'language' => 'all',
  500. 'translation' => 'translated',
  501. 'group' => 'all',
  502. );
  503. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  504. $this->assertNoText(t('No strings available.'), t('Search found the translation.'));
  505. // Ensure translated source string doesn't appear if searching on 'only
  506. // untranslated strings'.
  507. $search = array(
  508. 'string' => $name,
  509. 'language' => 'all',
  510. 'translation' => 'untranslated',
  511. 'group' => 'all',
  512. );
  513. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  514. $this->assertText(t('No strings available.'), t("Search didn't find the source string."));
  515. // Ensure translated string doesn't appear if searching on 'only
  516. // untranslated strings'.
  517. $search = array(
  518. 'string' => $translation,
  519. 'language' => 'all',
  520. 'translation' => 'untranslated',
  521. 'group' => 'all',
  522. );
  523. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  524. $this->assertText(t('No strings available.'), t("Search didn't find the translation."));
  525. // Ensure translated string does appear if searching on the custom language.
  526. $search = array(
  527. 'string' => $translation,
  528. 'language' => $langcode,
  529. 'translation' => 'all',
  530. 'group' => 'all',
  531. );
  532. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  533. $this->assertNoText(t('No strings available.'), t('Search found the translation.'));
  534. // Ensure translated string doesn't appear if searching on English.
  535. $search = array(
  536. 'string' => $translation,
  537. 'language' => 'en',
  538. 'translation' => 'all',
  539. 'group' => 'all',
  540. );
  541. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  542. $this->assertText(t('No strings available.'), t("Search didn't find the translation."));
  543. // Search for a string that isn't in the system.
  544. $unavailable_string = $this->randomName(16);
  545. $search = array(
  546. 'string' => $unavailable_string,
  547. 'language' => 'all',
  548. 'translation' => 'all',
  549. 'group' => 'all',
  550. );
  551. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  552. $this->assertText(t('No strings available.'), t("Search didn't find the invalid string."));
  553. }
  554. }
  555. /**
  556. * Functional tests for the import of translation files.
  557. */
  558. class LocaleImportFunctionalTest extends DrupalWebTestCase {
  559. public static function getInfo() {
  560. return array(
  561. 'name' => 'Translation import',
  562. 'description' => 'Tests the import of locale files.',
  563. 'group' => 'Locale',
  564. );
  565. }
  566. /**
  567. * A user able to create languages and import translations.
  568. */
  569. protected $admin_user = NULL;
  570. function setUp() {
  571. parent::setUp('locale', 'locale_test');
  572. $this->admin_user = $this->drupalCreateUser(array('administer languages', 'translate interface', 'access administration pages'));
  573. $this->drupalLogin($this->admin_user);
  574. }
  575. /**
  576. * Test import of standalone .po files.
  577. */
  578. function testStandalonePoFile() {
  579. // Try importing a .po file.
  580. $this->importPoFile($this->getPoFile(), array(
  581. 'langcode' => 'fr',
  582. ));
  583. // The import should automatically create the corresponding language.
  584. $this->assertRaw(t('The language %language has been created.', array('%language' => 'French')), t('The language has been automatically created.'));
  585. // The import should have created 7 strings.
  586. $this->assertRaw(t('The translation was successfully imported. There are %number newly created translated strings, %update strings were updated and %delete strings were removed.', array('%number' => 9, '%update' => 0, '%delete' => 0)), t('The translation file was successfully imported.'));
  587. // This import should have saved plural forms to have 2 variants.
  588. $this->assert(db_query("SELECT plurals FROM {languages} WHERE language = 'fr'")->fetchField() == 2, t('Plural number initialized.'));
  589. // Ensure we were redirected correctly.
  590. $this->assertEqual($this->getUrl(), url('admin/config/regional/translate', array('absolute' => TRUE)), t('Correct page redirection.'));
  591. // Try importing a .po file with invalid tags in the default text group.
  592. $this->importPoFile($this->getBadPoFile(), array(
  593. 'langcode' => 'fr',
  594. ));
  595. // The import should have created 1 string and rejected 2.
  596. $this->assertRaw(t('The translation was successfully imported. There are %number newly created translated strings, %update strings were updated and %delete strings were removed.', array('%number' => 1, '%update' => 0, '%delete' => 0)), t('The translation file was successfully imported.'));
  597. $skip_message = format_plural(2, 'One translation string was skipped because it contains disallowed HTML.', '@count translation strings were skipped because they contain disallowed HTML.');
  598. $this->assertRaw($skip_message, t('Unsafe strings were skipped.'));
  599. // Try importing a .po file with invalid tags in a non default text group.
  600. $this->importPoFile($this->getBadPoFile(), array(
  601. 'langcode' => 'fr',
  602. 'group' => 'custom',
  603. ));
  604. // The import should have created 3 strings.
  605. $this->assertRaw(t('The translation was successfully imported. There are %number newly created translated strings, %update strings were updated and %delete strings were removed.', array('%number' => 3, '%update' => 0, '%delete' => 0)), t('The translation file was successfully imported.'));
  606. // Try importing a .po file which doesn't exist.
  607. $name = $this->randomName(16);
  608. $this->drupalPost('admin/config/regional/translate/import', array(
  609. 'langcode' => 'fr',
  610. 'files[file]' => $name,
  611. 'group' => 'custom',
  612. ), t('Import'));
  613. $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/import', array('absolute' => TRUE)), t('Correct page redirection.'));
  614. $this->assertText(t('File to import not found.'), t('File to import not found message.'));
  615. // Try importing a .po file with overriding strings, and ensure existing
  616. // strings are kept.
  617. $this->importPoFile($this->getOverwritePoFile(), array(
  618. 'langcode' => 'fr',
  619. 'mode' => 1, // Existing strings are kept, only new strings are added.
  620. ));
  621. // The import should have created 1 string.
  622. $this->assertRaw(t('The translation was successfully imported. There are %number newly created translated strings, %update strings were updated and %delete strings were removed.', array('%number' => 1, '%update' => 0, '%delete' => 0)), t('The translation file was successfully imported.'));
  623. // Ensure string wasn't overwritten.
  624. $search = array(
  625. 'string' => 'Montag',
  626. 'language' => 'fr',
  627. 'translation' => 'translated',
  628. 'group' => 'all',
  629. );
  630. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  631. $this->assertText(t('No strings available.'), t('String not overwritten by imported string.'));
  632. // This import should not have changed number of plural forms.
  633. $this->assert(db_query("SELECT plurals FROM {languages} WHERE language = 'fr'")->fetchField() == 2, t('Plural numbers untouched.'));
  634. // Try importing a .po file with overriding strings, and ensure existing
  635. // strings are overwritten.
  636. $this->importPoFile($this->getOverwritePoFile(), array(
  637. 'langcode' => 'fr',
  638. 'mode' => 0, // Strings in the uploaded file replace existing ones, new ones are added.
  639. ));
  640. // The import should have updated 2 strings.
  641. $this->assertRaw(t('The translation was successfully imported. There are %number newly created translated strings, %update strings were updated and %delete strings were removed.', array('%number' => 0, '%update' => 2, '%delete' => 0)), t('The translation file was successfully imported.'));
  642. // Ensure string was overwritten.
  643. $search = array(
  644. 'string' => 'Montag',
  645. 'language' => 'fr',
  646. 'translation' => 'translated',
  647. 'group' => 'all',
  648. );
  649. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  650. $this->assertNoText(t('No strings available.'), t('String overwritten by imported string.'));
  651. // This import should have changed number of plural forms.
  652. $this->assert(db_query("SELECT plurals FROM {languages} WHERE language = 'fr'")->fetchField() == 3, t('Plural numbers changed.'));
  653. }
  654. /**
  655. * Test automatic import of a module's translation files when a language is
  656. * enabled.
  657. */
  658. function testAutomaticModuleTranslationImportLanguageEnable() {
  659. // Code for the language - manually set to match the test translation file.
  660. $langcode = 'xx';
  661. // The English name for the language.
  662. $name = $this->randomName(16);
  663. // The native name for the language.
  664. $native = $this->randomName(16);
  665. // The domain prefix.
  666. $prefix = $langcode;
  667. // Create a custom language.
  668. $edit = array(
  669. 'langcode' => $langcode,
  670. 'name' => $name,
  671. 'native' => $native,
  672. 'prefix' => $prefix,
  673. 'direction' => '0',
  674. );
  675. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
  676. // Ensure the translation file was automatically imported when language was
  677. // added.
  678. $this->assertText(t('One translation file imported for the enabled modules.'), t('Language file automatically imported.'));
  679. // Ensure strings were successfully imported.
  680. $search = array(
  681. 'string' => 'lundi',
  682. 'language' => $langcode,
  683. 'translation' => 'translated',
  684. 'group' => 'all',
  685. );
  686. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  687. $this->assertNoText(t('No strings available.'), t('String successfully imported.'));
  688. }
  689. /**
  690. * Test msgctxt context support.
  691. */
  692. function testLanguageContext() {
  693. // Try importing a .po file.
  694. $this->importPoFile($this->getPoFileWithContext(), array(
  695. 'langcode' => 'hr',
  696. ));
  697. $this->assertIdentical(t('May', array(), array('langcode' => 'hr', 'context' => 'Long month name')), 'Svibanj', t('Long month name context is working.'));
  698. $this->assertIdentical(t('May', array(), array('langcode' => 'hr')), 'Svi.', t('Default context is working.'));
  699. }
  700. /**
  701. * Test empty msgstr at end of .po file see #611786.
  702. */
  703. function testEmptyMsgstr() {
  704. $langcode = 'hu';
  705. // Try importing a .po file.
  706. $this->importPoFile($this->getPoFileWithMsgstr(), array(
  707. 'langcode' => $langcode,
  708. ));
  709. $this->assertRaw(t('The translation was successfully imported. There are %number newly created translated strings, %update strings were updated and %delete strings were removed.', array('%number' => 1, '%update' => 0, '%delete' => 0)), t('The translation file was successfully imported.'));
  710. $this->assertIdentical(t('Operations', array(), array('langcode' => $langcode)), 'Műveletek', t('String imported and translated.'));
  711. // Try importing a .po file.
  712. $this->importPoFile($this->getPoFileWithEmptyMsgstr(), array(
  713. 'langcode' => $langcode,
  714. 'mode' => 0,
  715. ));
  716. $this->assertRaw(t('The translation was successfully imported. There are %number newly created translated strings, %update strings were updated and %delete strings were removed.', array('%number' => 0, '%update' => 0, '%delete' => 1)), t('The translation file was successfully imported.'));
  717. // This is the language indicator on the translation search screen for
  718. // untranslated strings. Copied straight from locale.inc.
  719. $language_indicator = "<em class=\"locale-untranslated\">$langcode</em> ";
  720. $str = "Operations";
  721. $search = array(
  722. 'string' => $str,
  723. 'language' => 'all',
  724. 'translation' => 'all',
  725. 'group' => 'all',
  726. );
  727. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  728. // assertText() seems to remove the input field where $str always could be
  729. // found, so this is not a false assert.
  730. $this->assertText($str, t('Search found the string.'));
  731. $this->assertRaw($language_indicator, t('String is untranslated again.'));
  732. }
  733. /**
  734. * Helper function: import a standalone .po file in a given language.
  735. *
  736. * @param $contents
  737. * Contents of the .po file to import.
  738. * @param $options
  739. * Additional options to pass to the translation import form.
  740. */
  741. function importPoFile($contents, array $options = array()) {
  742. $name = tempnam('temporary://', "po_") . '.po';
  743. file_put_contents($name, $contents);
  744. $options['files[file]'] = $name;
  745. $this->drupalPost('admin/config/regional/translate/import', $options, t('Import'));
  746. drupal_unlink($name);
  747. }
  748. /**
  749. * Helper function that returns a proper .po file.
  750. */
  751. function getPoFile() {
  752. return <<< EOF
  753. msgid ""
  754. msgstr ""
  755. "Project-Id-Version: Drupal 7\\n"
  756. "MIME-Version: 1.0\\n"
  757. "Content-Type: text/plain; charset=UTF-8\\n"
  758. "Content-Transfer-Encoding: 8bit\\n"
  759. "Plural-Forms: nplurals=2; plural=(n > 1);\\n"
  760. msgid "One sheep"
  761. msgid_plural "@count sheep"
  762. msgstr[0] "un mouton"
  763. msgstr[1] "@count moutons"
  764. msgid "Monday"
  765. msgstr "lundi"
  766. msgid "Tuesday"
  767. msgstr "mardi"
  768. msgid "Wednesday"
  769. msgstr "mercredi"
  770. msgid "Thursday"
  771. msgstr "jeudi"
  772. msgid "Friday"
  773. msgstr "vendredi"
  774. msgid "Saturday"
  775. msgstr "samedi"
  776. msgid "Sunday"
  777. msgstr "dimanche"
  778. EOF;
  779. }
  780. /**
  781. * Helper function that returns a bad .po file.
  782. */
  783. function getBadPoFile() {
  784. return <<< EOF
  785. msgid ""
  786. msgstr ""
  787. "Project-Id-Version: Drupal 7\\n"
  788. "MIME-Version: 1.0\\n"
  789. "Content-Type: text/plain; charset=UTF-8\\n"
  790. "Content-Transfer-Encoding: 8bit\\n"
  791. "Plural-Forms: nplurals=2; plural=(n > 1);\\n"
  792. msgid "Save configuration"
  793. msgstr "Enregistrer la configuration"
  794. msgid "edit"
  795. msgstr "modifier<img SRC="javascript:alert(\'xss\');">"
  796. msgid "delete"
  797. msgstr "supprimer<script>alert('xss');</script>"
  798. EOF;
  799. }
  800. /**
  801. * Helper function that returns a proper .po file, for testing overwriting
  802. * existing translations.
  803. */
  804. function getOverwritePoFile() {
  805. return <<< EOF
  806. msgid ""
  807. msgstr ""
  808. "Project-Id-Version: Drupal 7\\n"
  809. "MIME-Version: 1.0\\n"
  810. "Content-Type: text/plain; charset=UTF-8\\n"
  811. "Content-Transfer-Encoding: 8bit\\n"
  812. "Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;\\n"
  813. msgid "Monday"
  814. msgstr "Montag"
  815. msgid "Day"
  816. msgstr "Jour"
  817. EOF;
  818. }
  819. /**
  820. * Helper function that returns a .po file with context.
  821. */
  822. function getPoFileWithContext() {
  823. // Croatian (code hr) is one the the languages that have a different
  824. // form for the full name and the abbreviated name for the month May.
  825. return <<< EOF
  826. msgid ""
  827. msgstr ""
  828. "Project-Id-Version: Drupal 7\\n"
  829. "MIME-Version: 1.0\\n"
  830. "Content-Type: text/plain; charset=UTF-8\\n"
  831. "Content-Transfer-Encoding: 8bit\\n"
  832. "Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;\\n"
  833. msgctxt "Long month name"
  834. msgid "May"
  835. msgstr "Svibanj"
  836. msgid "May"
  837. msgstr "Svi."
  838. EOF;
  839. }
  840. /**
  841. * Helper function that returns a .po file with an empty last item.
  842. */
  843. function getPoFileWithEmptyMsgstr() {
  844. return <<< EOF
  845. msgid ""
  846. msgstr ""
  847. "Project-Id-Version: Drupal 7\\n"
  848. "MIME-Version: 1.0\\n"
  849. "Content-Type: text/plain; charset=UTF-8\\n"
  850. "Content-Transfer-Encoding: 8bit\\n"
  851. "Plural-Forms: nplurals=2; plural=(n > 1);\\n"
  852. msgid "Operations"
  853. msgstr ""
  854. EOF;
  855. }
  856. /**
  857. * Helper function that returns a .po file with an empty last item.
  858. */
  859. function getPoFileWithMsgstr() {
  860. return <<< EOF
  861. msgid ""
  862. msgstr ""
  863. "Project-Id-Version: Drupal 7\\n"
  864. "MIME-Version: 1.0\\n"
  865. "Content-Type: text/plain; charset=UTF-8\\n"
  866. "Content-Transfer-Encoding: 8bit\\n"
  867. "Plural-Forms: nplurals=2; plural=(n > 1);\\n"
  868. msgid "Operations"
  869. msgstr "Műveletek"
  870. msgid "Will not appear in Drupal core, so we can ensure the test passes"
  871. msgstr ""
  872. EOF;
  873. }
  874. }
  875. /**
  876. * Functional tests for the export of translation files.
  877. */
  878. class LocaleExportFunctionalTest extends DrupalWebTestCase {
  879. public static function getInfo() {
  880. return array(
  881. 'name' => 'Translation export',
  882. 'description' => 'Tests the exportation of locale files.',
  883. 'group' => 'Locale',
  884. );
  885. }
  886. /**
  887. * A user able to create languages and export translations.
  888. */
  889. protected $admin_user = NULL;
  890. function setUp() {
  891. parent::setUp('locale', 'locale_test');
  892. $this->admin_user = $this->drupalCreateUser(array('administer languages', 'translate interface', 'access administration pages'));
  893. $this->drupalLogin($this->admin_user);
  894. }
  895. /**
  896. * Test exportation of translations.
  897. */
  898. function testExportTranslation() {
  899. // First import some known translations.
  900. // This will also automatically enable the 'fr' language.
  901. $name = tempnam('temporary://', "po_") . '.po';
  902. file_put_contents($name, $this->getPoFile());
  903. $this->drupalPost('admin/config/regional/translate/import', array(
  904. 'langcode' => 'fr',
  905. 'files[file]' => $name,
  906. ), t('Import'));
  907. drupal_unlink($name);
  908. // Get the French translations.
  909. $this->drupalPost('admin/config/regional/translate/export', array(
  910. 'langcode' => 'fr',
  911. ), t('Export'));
  912. // Ensure we have a translation file.
  913. $this->assertRaw('# French translation of Drupal', t('Exported French translation file.'));
  914. // Ensure our imported translations exist in the file.
  915. $this->assertRaw('msgstr "lundi"', t('French translations present in exported file.'));
  916. }
  917. /**
  918. * Test exportation of translation template file.
  919. */
  920. function testExportTranslationTemplateFile() {
  921. // Get the translation template file.
  922. // There are two 'Export' buttons on this page, but it somehow works. It'd
  923. // be better if we could use the submit button id like documented but that
  924. // doesn't work.
  925. $this->drupalPost('admin/config/regional/translate/export', array(), t('Export'));
  926. // Ensure we have a translation file.
  927. $this->assertRaw('# LANGUAGE translation of PROJECT', t('Exported translation template file.'));
  928. }
  929. /**
  930. * Helper function that returns a proper .po file.
  931. */
  932. function getPoFile() {
  933. return <<< EOF
  934. msgid ""
  935. msgstr ""
  936. "Project-Id-Version: Drupal 6\\n"
  937. "MIME-Version: 1.0\\n"
  938. "Content-Type: text/plain; charset=UTF-8\\n"
  939. "Content-Transfer-Encoding: 8bit\\n"
  940. "Plural-Forms: nplurals=2; plural=(n > 1);\\n"
  941. msgid "Monday"
  942. msgstr "lundi"
  943. EOF;
  944. }
  945. }
  946. /**
  947. * Tests for the st() function.
  948. */
  949. class LocaleInstallTest extends DrupalWebTestCase {
  950. public static function getInfo() {
  951. return array(
  952. 'name' => 'String translation using st()',
  953. 'description' => 'Tests that st() works like t().',
  954. 'group' => 'Locale',
  955. );
  956. }
  957. function setUp() {
  958. parent::setUp('locale');
  959. // st() lives in install.inc, so ensure that it is loaded for all tests.
  960. require_once DRUPAL_ROOT . '/includes/install.inc';
  961. }
  962. /**
  963. * Verify that function signatures of t() and st() are equal.
  964. */
  965. function testFunctionSignatures() {
  966. $reflector_t = new ReflectionFunction('t');
  967. $reflector_st = new ReflectionFunction('st');
  968. $this->assertEqual($reflector_t->getParameters(), $reflector_st->getParameters(), t('Function signatures of t() and st() are equal.'));
  969. }
  970. }
  971. /**
  972. * Locale uninstall with English UI functional test.
  973. */
  974. class LocaleUninstallFunctionalTest extends DrupalWebTestCase {
  975. public static function getInfo() {
  976. return array(
  977. 'name' => 'Locale uninstall (EN)',
  978. 'description' => 'Tests the uninstall process using the built-in UI language.',
  979. 'group' => 'Locale',
  980. );
  981. }
  982. /**
  983. * The default language set for the UI before uninstall.
  984. */
  985. protected $language;
  986. function setUp() {
  987. parent::setUp('locale');
  988. $this->language = 'en';
  989. }
  990. /**
  991. * Check if the values of the Locale variables are correct after uninstall.
  992. */
  993. function testUninstallProcess() {
  994. $locale_module = array('locale');
  995. // Add a new language and optionally set it as default.
  996. require_once DRUPAL_ROOT . '/includes/locale.inc';
  997. locale_add_language('fr', 'French', 'Français', LANGUAGE_LTR, '', '', TRUE, $this->language == 'fr');
  998. // Check the UI language.
  999. drupal_language_initialize();
  1000. global $language;
  1001. $this->assertEqual($language->language, $this->language, t('Current language: %lang', array('%lang' => $language->language)));
  1002. // Enable multilingual workflow option for articles.
  1003. variable_set('language_content_type_article', 1);
  1004. // Change JavaScript translations directory.
  1005. variable_set('locale_js_directory', 'js_translations');
  1006. // Build the JavaScript translation file for French.
  1007. $user = $this->drupalCreateUser(array('translate interface', 'access administration pages'));
  1008. $this->drupalLogin($user);
  1009. $this->drupalGet('admin/config/regional/translate/translate');
  1010. $string = db_query('SELECT min(lid) AS lid FROM {locales_source} WHERE location LIKE :location AND textgroup = :textgroup', array(
  1011. ':location' => '%.js%',
  1012. ':textgroup' => 'default',
  1013. ))->fetchObject();
  1014. $edit = array('translations[fr]' => 'french translation');
  1015. $this->drupalPost('admin/config/regional/translate/edit/' . $string->lid, $edit, t('Save translations'));
  1016. _locale_rebuild_js('fr');
  1017. $file = db_query('SELECT javascript FROM {languages} WHERE language = :language', array(':language' => 'fr'))->fetchObject();
  1018. $js_file = 'public://' . variable_get('locale_js_directory', 'languages') . '/fr_' . $file->javascript . '.js';
  1019. $this->assertTrue($result = file_exists($js_file), t('JavaScript file created: %file', array('%file' => $result ? $js_file : t('none'))));
  1020. // Disable string caching.
  1021. variable_set('locale_cache_strings', 0);
  1022. // Change language negotiation options.
  1023. drupal_load('module', 'locale');
  1024. variable_set('language_types', drupal_language_types() + array('language_custom' => TRUE));
  1025. variable_set('language_negotiation_' . LANGUAGE_TYPE_INTERFACE, locale_language_negotiation_info());
  1026. variable_set('language_negotiation_' . LANGUAGE_TYPE_CONTENT, locale_language_negotiation_info());
  1027. variable_set('language_negotiation_' . LANGUAGE_TYPE_URL, locale_language_negotiation_info());
  1028. // Change language providers settings.
  1029. variable_set('locale_language_negotiation_url_part', LOCALE_LANGUAGE_NEGOTIATION_URL_PREFIX);
  1030. variable_set('locale_language_negotiation_session_param', TRUE);
  1031. // Uninstall Locale.
  1032. module_disable($locale_module);
  1033. drupal_uninstall_modules($locale_module);
  1034. // Visit the front page.
  1035. $this->drupalGet('');
  1036. // Check the init language logic.
  1037. drupal_language_initialize();
  1038. $this->assertEqual($language->language, 'en', t('Language after uninstall: %lang', array('%lang' => $language->language)));
  1039. // Check JavaScript files deletion.
  1040. $this->assertTrue($result = !file_exists($js_file), t('JavaScript file deleted: %file', array('%file' => $result ? $js_file : t('found'))));
  1041. // Check language count.
  1042. $language_count = variable_get('language_count', 1);
  1043. $this->assertEqual($language_count, 1, t('Language count: %count', array('%count' => $language_count)));
  1044. // Check language negotiation.
  1045. require_once DRUPAL_ROOT . '/includes/language.inc';
  1046. $this->assertTrue(count(language_types()) == count(drupal_language_types()), t('Language types reset'));
  1047. $language_negotiation = language_negotiation_get(LANGUAGE_TYPE_INTERFACE) == LANGUAGE_NEGOTIATION_DEFAULT;
  1048. $this->assertTrue($language_negotiation, t('Interface language negotiation: %setting', array('%setting' => t($language_negotiation ? 'none' : 'set'))));
  1049. $language_negotiation = language_negotiation_get(LANGUAGE_TYPE_CONTENT) == LANGUAGE_NEGOTIATION_DEFAULT;
  1050. $this->assertTrue($language_negotiation, t('Content language negotiation: %setting', array('%setting' => t($language_negotiation ? 'none' : 'set'))));
  1051. $language_negotiation = language_negotiation_get(LANGUAGE_TYPE_URL) == LANGUAGE_NEGOTIATION_DEFAULT;
  1052. $this->assertTrue($language_negotiation, t('URL language negotiation: %setting', array('%setting' => t($language_negotiation ? 'none' : 'set'))));
  1053. // Check language providers settings.
  1054. $this->assertFalse(variable_get('locale_language_negotiation_url_part', FALSE), t('URL language provider indicator settings cleared.'));
  1055. $this->assertFalse(variable_get('locale_language_negotiation_session_param', FALSE), t('Visit language provider settings cleared.'));
  1056. // Check JavaScript parsed.
  1057. $javascript_parsed_count = count(variable_get('javascript_parsed', array()));
  1058. $this->assertEqual($javascript_parsed_count, 0, t('JavaScript parsed count: %count', array('%count' => $javascript_parsed_count)));
  1059. // Check multilingual workflow option for articles.
  1060. $multilingual = variable_get('language_content_type_article', 0);
  1061. $this->assertEqual($multilingual, 0, t('Multilingual workflow option: %status', array('%status' => t($multilingual ? 'enabled': 'disabled'))));
  1062. // Check JavaScript translations directory.
  1063. $locale_js_directory = variable_get('locale_js_directory', 'languages');
  1064. $this->assertEqual($locale_js_directory, 'languages', t('JavaScript translations directory: %dir', array('%dir' => $locale_js_directory)));
  1065. // Check string caching.
  1066. $locale_cache_strings = variable_get('locale_cache_strings', 1);
  1067. $this->assertEqual($locale_cache_strings, 1, t('String caching: %status', array('%status' => t($locale_cache_strings ? 'enabled': 'disabled'))));
  1068. }
  1069. }
  1070. /**
  1071. * Locale uninstall with French UI functional test.
  1072. *
  1073. * Because this class extends LocaleUninstallFunctionalTest, it doesn't require a new
  1074. * test of its own. Rather, it switches the default UI language in setUp and then
  1075. * runs the testUninstallProcess (which it inherits from LocaleUninstallFunctionalTest)
  1076. * to test with this new language.
  1077. */
  1078. class LocaleUninstallFrenchFunctionalTest extends LocaleUninstallFunctionalTest {
  1079. public static function getInfo() {
  1080. return array(
  1081. 'name' => 'Locale uninstall (FR)',
  1082. 'description' => 'Tests the uninstall process using French as interface language.',
  1083. 'group' => 'Locale',
  1084. );
  1085. }
  1086. function setUp() {
  1087. parent::setUp();
  1088. $this->language = 'fr';
  1089. }
  1090. }
  1091. /**
  1092. * Functional tests for the language switching feature.
  1093. */
  1094. class LocaleLanguageSwitchingFunctionalTest extends DrupalWebTestCase {
  1095. public static function getInfo() {
  1096. return array(
  1097. 'name' => 'Language switching',
  1098. 'description' => 'Tests for the language switching feature.',
  1099. 'group' => 'Locale',
  1100. );
  1101. }
  1102. function setUp() {
  1103. parent::setUp('locale');
  1104. // Create and login user.
  1105. $admin_user = $this->drupalCreateUser(array('administer blocks', 'administer languages', 'translate interface', 'access administration pages'));
  1106. $this->drupalLogin($admin_user);
  1107. }
  1108. /**
  1109. * Functional tests for the language switcher block.
  1110. */
  1111. function testLanguageBlock() {
  1112. // Enable the language switching block.
  1113. $language_type = LANGUAGE_TYPE_INTERFACE;
  1114. $edit = array(
  1115. "blocks[locale_{$language_type}][region]" => 'sidebar_first',
  1116. );
  1117. $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
  1118. // Add language.
  1119. $edit = array(
  1120. 'langcode' => 'fr',
  1121. );
  1122. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
  1123. // Enable URL language detection and selection.
  1124. $edit = array('language[enabled][locale-url]' => '1');
  1125. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  1126. // Assert that the language switching block is displayed on the frontpage.
  1127. $this->drupalGet('');
  1128. $this->assertText(t('Languages'), t('Language switcher block found.'));
  1129. // Assert that only the current language is marked as active.
  1130. list($language_switcher) = $this->xpath('//div[@id=:id]/div[@class="content"]', array(':id' => 'block-locale-' . $language_type));
  1131. $links = array(
  1132. 'active' => array(),
  1133. 'inactive' => array(),
  1134. );
  1135. $anchors = array(
  1136. 'active' => array(),
  1137. 'inactive' => array(),
  1138. );
  1139. foreach ($language_switcher->ul->li as $link) {
  1140. $classes = explode(" ", (string) $link['class']);
  1141. list($language) = array_intersect($classes, array('en', 'fr'));
  1142. if (in_array('active', $classes)) {
  1143. $links['active'][] = $language;
  1144. }
  1145. else {
  1146. $links['inactive'][] = $language;
  1147. }
  1148. $anchor_classes = explode(" ", (string) $link->a['class']);
  1149. if (in_array('active', $anchor_classes)) {
  1150. $anchors['active'][] = $language;
  1151. }
  1152. else {
  1153. $anchors['inactive'][] = $language;
  1154. }
  1155. }
  1156. $this->assertIdentical($links, array('active' => array('en'), 'inactive' => array('fr')), t('Only the current language list item is marked as active on the language switcher block.'));
  1157. $this->assertIdentical($anchors, array('active' => array('en'), 'inactive' => array('fr')), t('Only the current language anchor is marked as active on the language switcher block.'));
  1158. }
  1159. }
  1160. /**
  1161. * Functional tests for a user's ability to change their default language.
  1162. */
  1163. class LocaleUserLanguageFunctionalTest extends DrupalWebTestCase {
  1164. public static function getInfo() {
  1165. return array(
  1166. 'name' => 'User language settings',
  1167. 'description' => "Tests user's ability to change their default language.",
  1168. 'group' => 'Locale',
  1169. );
  1170. }
  1171. function setUp() {
  1172. parent::setUp('locale');
  1173. }
  1174. /**
  1175. * Test if user can change their default language.
  1176. */
  1177. function testUserLanguageConfiguration() {
  1178. global $base_url;
  1179. // User to add and remove language.
  1180. $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages'));
  1181. // User to change their default language.
  1182. $web_user = $this->drupalCreateUser();
  1183. // Add custom language.
  1184. $this->drupalLogin($admin_user);
  1185. // Code for the language.
  1186. $langcode = 'xx';
  1187. // The English name for the language.
  1188. $name = $this->randomName(16);
  1189. // The native name for the language.
  1190. $native = $this->randomName(16);
  1191. // The domain prefix.
  1192. $prefix = 'xx';
  1193. $edit = array(
  1194. 'langcode' => $langcode,
  1195. 'name' => $name,
  1196. 'native' => $native,
  1197. 'prefix' => $prefix,
  1198. 'direction' => '0',
  1199. );
  1200. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
  1201. // Add custom language and disable it.
  1202. // Code for the language.
  1203. $langcode_disabled = 'xx-yy';
  1204. // The English name for the language. This will be translated.
  1205. $name_disabled = $this->randomName(16);
  1206. // The native name for the language.
  1207. $native_disabled = $this->randomName(16);
  1208. // The domain prefix.
  1209. $prefix_disabled = $langcode_disabled;
  1210. $edit = array(
  1211. 'langcode' => $langcode_disabled,
  1212. 'name' => $name_disabled,
  1213. 'native' => $native_disabled,
  1214. 'prefix' => $prefix_disabled,
  1215. 'direction' => '0',
  1216. );
  1217. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
  1218. // Disable the language.
  1219. $edit = array(
  1220. 'enabled[' . $langcode_disabled . ']' => FALSE,
  1221. );
  1222. $this->drupalPost('admin/config/regional/language', $edit, t('Save configuration'));
  1223. $this->drupalLogout();
  1224. // Login as normal user and edit account settings.
  1225. $this->drupalLogin($web_user);
  1226. $path = 'user/' . $web_user->uid . '/edit';
  1227. $this->drupalGet($path);
  1228. // Ensure language settings fieldset is available.
  1229. $this->assertText(t('Language settings'), t('Language settings available.'));
  1230. // Ensure custom language is present.
  1231. $this->assertText($name, t('Language present on form.'));
  1232. // Ensure disabled language isn't present.
  1233. $this->assertNoText($name_disabled, t('Disabled language not present on form.'));
  1234. // Switch to our custom language.
  1235. $edit = array(
  1236. 'language' => $langcode,
  1237. );
  1238. $this->drupalPost($path, $edit, t('Save'));
  1239. // Ensure form was submitted successfully.
  1240. $this->assertText(t('The changes have been saved.'), t('Changes were saved.'));
  1241. // Check if language was changed.
  1242. $elements = $this->xpath('//input[@id=:id]', array(':id' => 'edit-language-' . $langcode));
  1243. $this->assertTrue(isset($elements[0]) && !empty($elements[0]['checked']), t('Default language successfully updated.'));
  1244. $this->drupalLogout();
  1245. }
  1246. }
  1247. /**
  1248. * Functional test for language handling during user creation.
  1249. */
  1250. class LocaleUserCreationTest extends DrupalWebTestCase {
  1251. public static function getInfo() {
  1252. return array(
  1253. 'name' => 'User creation',
  1254. 'description' => 'Tests whether proper language is stored for new users and access to language selector.',
  1255. 'group' => 'Locale',
  1256. );
  1257. }
  1258. function setUp() {
  1259. parent::setUp('locale');
  1260. variable_set('user_register', USER_REGISTER_VISITORS);
  1261. }
  1262. /**
  1263. * Functional test for language handling during user creation.
  1264. */
  1265. function testLocalUserCreation() {
  1266. // User to add and remove language and create new users.
  1267. $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages', 'administer users'));
  1268. $this->drupalLogin($admin_user);
  1269. // Add predefined language.
  1270. $langcode = 'fr';
  1271. $edit = array(
  1272. 'langcode' => 'fr',
  1273. );
  1274. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
  1275. $this->assertText($langcode, t('Language added successfully.'));
  1276. $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.'));
  1277. // Set language negotiation.
  1278. $edit = array(
  1279. 'language[enabled][locale-url]' => TRUE,
  1280. );
  1281. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  1282. $this->assertText(t('Language negotiation configuration saved.'), t('Set language negotiation.'));
  1283. // Check if the language selector is available on admin/people/create and
  1284. // set to the currently active language.
  1285. $this->drupalGet($langcode . '/admin/people/create');
  1286. $this->assertFieldChecked("edit-language-$langcode", t('Global language set in the language selector.'));
  1287. // Create a user with the admin/people/create form and check if the correct
  1288. // language is set.
  1289. $username = $this->randomName(10);
  1290. $edit = array(
  1291. 'name' => $username,
  1292. 'mail' => $this->randomName(4) . '@example.com',
  1293. 'pass[pass1]' => $username,
  1294. 'pass[pass2]' => $username,
  1295. );
  1296. $this->drupalPost($langcode . '/admin/people/create', $edit, t('Create new account'));
  1297. $user = user_load_by_name($username);
  1298. $this->assertEqual($user->language, $langcode, t('New user has correct language set.'));
  1299. // Register a new user and check if the language selector is hidden.
  1300. $this->drupalLogout();
  1301. $this->drupalGet($langcode . '/user/register');
  1302. $this->assertNoFieldByName('language[fr]', t('Language selector is not accessible.'));
  1303. $username = $this->randomName(10);
  1304. $edit = array(
  1305. 'name' => $username,
  1306. 'mail' => $this->randomName(4) . '@example.com',
  1307. );
  1308. $this->drupalPost($langcode . '/user/register', $edit, t('Create new account'));
  1309. $user = user_load_by_name($username);
  1310. $this->assertEqual($user->language, $langcode, t('New user has correct language set.'));
  1311. // Test if the admin can use the language selector and if the
  1312. // correct language is was saved.
  1313. $user_edit = $langcode . '/user/' . $user->uid . '/edit';
  1314. $this->drupalLogin($admin_user);
  1315. $this->drupalGet($user_edit);
  1316. $this->assertFieldChecked("edit-language-$langcode", t('Language selector is accessible and correct language is selected.'));
  1317. // Set pass_raw so we can login the new user.
  1318. $user->pass_raw = $this->randomName(10);
  1319. $edit = array(
  1320. 'pass[pass1]' => $user->pass_raw,
  1321. 'pass[pass2]' => $user->pass_raw,
  1322. );
  1323. $this->drupalPost($user_edit, $edit, t('Save'));
  1324. $this->drupalLogin($user);
  1325. $this->drupalGet($user_edit);
  1326. $this->assertFieldChecked("edit-language-$langcode", t('Language selector is accessible and correct language is selected.'));
  1327. }
  1328. }
  1329. /**
  1330. * Functional tests for configuring a different path alias per language.
  1331. */
  1332. class LocalePathFunctionalTest extends DrupalWebTestCase {
  1333. public static function getInfo() {
  1334. return array(
  1335. 'name' => 'Path language settings',
  1336. 'description' => 'Checks you can configure a language for individual url aliases.',
  1337. 'group' => 'Locale',
  1338. );
  1339. }
  1340. function setUp() {
  1341. parent::setUp('locale', 'path');
  1342. }
  1343. /**
  1344. * Test if a language can be associated with a path alias.
  1345. */
  1346. function testPathLanguageConfiguration() {
  1347. global $base_url;
  1348. // User to add and remove language.
  1349. $admin_user = $this->drupalCreateUser(array('administer languages', 'create page content', 'administer url aliases', 'create url aliases', 'access administration pages'));
  1350. // Add custom language.
  1351. $this->drupalLogin($admin_user);
  1352. // Code for the language.
  1353. $langcode = 'xx';
  1354. // The English name for the language.
  1355. $name = $this->randomName(16);
  1356. // The native name for the language.
  1357. $native = $this->randomName(16);
  1358. // The domain prefix.
  1359. $prefix = $langcode;
  1360. $edit = array(
  1361. 'langcode' => $langcode,
  1362. 'name' => $name,
  1363. 'native' => $native,
  1364. 'prefix' => $prefix,
  1365. 'direction' => '0',
  1366. );
  1367. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
  1368. // Check that the "xx" front page is not available when path prefixes are
  1369. // not enabled yet.
  1370. $this->drupalPost('admin/config/regional/language/configure', array(), t('Save settings'));
  1371. $this->drupalGet($prefix);
  1372. $this->assertResponse(404, t('The "xx" front page is not available yet.'));
  1373. // Enable URL language detection and selection.
  1374. $edit = array('language[enabled][locale-url]' => 1);
  1375. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  1376. // Create a node.
  1377. $node = $this->drupalCreateNode(array('type' => 'page'));
  1378. // Create a path alias in default language (English).
  1379. $path = 'admin/config/search/path/add';
  1380. $english_path = $this->randomName(8);
  1381. $edit = array(
  1382. 'source' => 'node/' . $node->nid,
  1383. 'alias' => $english_path,
  1384. 'language' => 'en',
  1385. );
  1386. $this->drupalPost($path, $edit, t('Save'));
  1387. // Create a path alias in new custom language.
  1388. $custom_language_path = $this->randomName(8);
  1389. $edit = array(
  1390. 'source' => 'node/' . $node->nid,
  1391. 'alias' => $custom_language_path,
  1392. 'language' => $langcode,
  1393. );
  1394. $this->drupalPost($path, $edit, t('Save'));
  1395. // Confirm English language path alias works.
  1396. $this->drupalGet($english_path);
  1397. $this->assertText($node->title, t('English alias works.'));
  1398. // Confirm custom language path alias works.
  1399. $this->drupalGet($prefix . '/' . $custom_language_path);
  1400. $this->assertText($node->title, t('Custom language alias works.'));
  1401. // Create a custom path.
  1402. $custom_path = $this->randomName(8);
  1403. // Check priority of language for alias by source path.
  1404. $edit = array(
  1405. 'source' => 'node/' . $node->nid,
  1406. 'alias' => $custom_path,
  1407. 'language' => LANGUAGE_NONE,
  1408. );
  1409. path_save($edit);
  1410. $lookup_path = drupal_lookup_path('alias', 'node/' . $node->nid, 'en');
  1411. $this->assertEqual($english_path, $lookup_path, t('English language alias has priority.'));
  1412. // Same check for language 'xx'.
  1413. $lookup_path = drupal_lookup_path('alias', 'node/' . $node->nid, $prefix);
  1414. $this->assertEqual($custom_language_path, $lookup_path, t('Custom language alias has priority.'));
  1415. path_delete($edit);
  1416. // Create language nodes to check priority of aliases.
  1417. $first_node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
  1418. $second_node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
  1419. // Assign a custom path alias to the first node with the English language.
  1420. $edit = array(
  1421. 'source' => 'node/' . $first_node->nid,
  1422. 'alias' => $custom_path,
  1423. 'language' => 'en',
  1424. );
  1425. path_save($edit);
  1426. // Assign a custom path alias to second node with LANGUAGE_NONE.
  1427. $edit = array(
  1428. 'source' => 'node/' . $second_node->nid,
  1429. 'alias' => $custom_path,
  1430. 'language' => LANGUAGE_NONE,
  1431. );
  1432. path_save($edit);
  1433. // Test that both node titles link to our path alias.
  1434. $this->drupalGet('<front>');
  1435. $custom_path_url = base_path() . (variable_get('clean_url', 0) ? $custom_path : '?q=' . $custom_path);
  1436. $elements = $this->xpath('//a[@href=:href and .=:title]', array(':href' => $custom_path_url, ':title' => $first_node->title));
  1437. $this->assertTrue(!empty($elements), t('First node links to the path alias.'));
  1438. $elements = $this->xpath('//a[@href=:href and .=:title]', array(':href' => $custom_path_url, ':title' => $second_node->title));
  1439. $this->assertTrue(!empty($elements), t('Second node links to the path alias.'));
  1440. // Confirm that the custom path leads to the first node.
  1441. $this->drupalGet($custom_path);
  1442. $this->assertText($first_node->title, t('Custom alias returns first node.'));
  1443. // Confirm that the custom path with prefix leads to the second node.
  1444. $this->drupalGet($prefix . '/' . $custom_path);
  1445. $this->assertText($second_node->title, t('Custom alias with prefix returns second node.'));
  1446. }
  1447. }
  1448. /**
  1449. * Functional tests for multilingual support on nodes.
  1450. */
  1451. class LocaleContentFunctionalTest extends DrupalWebTestCase {
  1452. public static function getInfo() {
  1453. return array(
  1454. 'name' => 'Content language settings',
  1455. 'description' => 'Checks you can enable multilingual support on content types and configure a language for a node.',
  1456. 'group' => 'Locale',
  1457. );
  1458. }
  1459. function setUp() {
  1460. parent::setUp('locale');
  1461. }
  1462. /**
  1463. * Test if a content type can be set to multilingual and language setting is
  1464. * present on node add and edit forms.
  1465. */
  1466. function testContentTypeLanguageConfiguration() {
  1467. global $base_url;
  1468. // User to add and remove language.
  1469. $admin_user = $this->drupalCreateUser(array('administer languages', 'administer content types', 'access administration pages'));
  1470. // User to create a node.
  1471. $web_user = $this->drupalCreateUser(array('create article content', 'create page content', 'edit any page content'));
  1472. // Add custom language.
  1473. $this->drupalLogin($admin_user);
  1474. // Code for the language.
  1475. $langcode = 'xx';
  1476. // The English name for the language.
  1477. $name = $this->randomName(16);
  1478. // The native name for the language.
  1479. $native = $this->randomName(16);
  1480. // The domain prefix.
  1481. $prefix = $langcode;
  1482. $edit = array(
  1483. 'langcode' => $langcode,
  1484. 'name' => $name,
  1485. 'native' => $native,
  1486. 'prefix' => $prefix,
  1487. 'direction' => '0',
  1488. );
  1489. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
  1490. // Add disabled custom language.
  1491. // Code for the language.
  1492. $langcode_disabled = 'xx-yy';
  1493. // The English name for the language.
  1494. $name_disabled = $this->randomName(16);
  1495. // The native name for the language.
  1496. $native_disabled = $this->randomName(16);
  1497. // The domain prefix.
  1498. $prefix_disabled = $langcode_disabled;
  1499. $edit = array(
  1500. 'langcode' => $langcode_disabled,
  1501. 'name' => $name_disabled,
  1502. 'native' => $native_disabled,
  1503. 'prefix' => $prefix_disabled,
  1504. 'direction' => '0',
  1505. );
  1506. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
  1507. // Disable second custom language.
  1508. $path = 'admin/config/regional/language';
  1509. $edit = array(
  1510. 'enabled[' . $langcode_disabled . ']' => FALSE,
  1511. );
  1512. $this->drupalPost($path, $edit, t('Save configuration'));
  1513. // Set "Basic page" content type to use multilingual support.
  1514. $this->drupalGet('admin/structure/types/manage/page');
  1515. $this->assertText(t('Multilingual support'), t('Multilingual support fieldset present on content type configuration form.'));
  1516. $edit = array(
  1517. 'language_content_type' => 1,
  1518. );
  1519. $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
  1520. $this->assertRaw(t('The content type %type has been updated.', array('%type' => 'Basic page')), t('Basic page content type has been updated.'));
  1521. $this->drupalLogout();
  1522. // Verify language selection is not present on add article form.
  1523. $this->drupalLogin($web_user);
  1524. $this->drupalGet('node/add/article');
  1525. // Verify language select list is not present.
  1526. $this->assertNoFieldByName('language', NULL, t('Language select not present on add article form.'));
  1527. // Verify language selection appears on add "Basic page" form.
  1528. $this->drupalGet('node/add/page');
  1529. // Verify language select list is present.
  1530. $this->assertFieldByName('language', NULL, t('Language select present on add Basic page form.'));
  1531. // Ensure enabled language appears.
  1532. $this->assertText($name, t('Enabled language present.'));
  1533. // Ensure disabled language doesn't appear.
  1534. $this->assertNoText($name_disabled, t('Disabled language not present.'));
  1535. // Create "Basic page" content.
  1536. $node_title = $this->randomName();
  1537. $node_body = $this->randomName();
  1538. $edit = array(
  1539. 'type' => 'page',
  1540. 'title' => $node_title,
  1541. 'body' => array($langcode => array(array('value' => $node_body))),
  1542. 'language' => $langcode,
  1543. );
  1544. $node = $this->drupalCreateNode($edit);
  1545. // Edit the content and ensure correct language is selected.
  1546. $path = 'node/' . $node->nid . '/edit';
  1547. $this->drupalGet($path);
  1548. $this->assertRaw('<option value="' . $langcode . '" selected="selected">' . $name . '</option>', t('Correct language selected.'));
  1549. // Ensure we can change the node language.
  1550. $edit = array(
  1551. 'language' => 'en',
  1552. );
  1553. $this->drupalPost($path, $edit, t('Save'));
  1554. $this->assertRaw(t('%title has been updated.', array('%title' => $node_title)), t('Basic page content updated.'));
  1555. $this->drupalLogout();
  1556. }
  1557. }
  1558. /**
  1559. * Test UI language negotiation
  1560. * 1. URL (PATH) > DEFAULT
  1561. * UI Language base on URL prefix, browser language preference has no
  1562. * influence:
  1563. * admin/config
  1564. * UI in site default language
  1565. * zh-hans/admin/config
  1566. * UI in Chinese
  1567. * blah-blah/admin/config
  1568. * 404
  1569. * 2. URL (PATH) > BROWSER > DEFAULT
  1570. * admin/config
  1571. * UI in user's browser language preference if the site has that
  1572. * language enabled, if not, the default language
  1573. * zh-hans/admin/config
  1574. * UI in Chinese
  1575. * blah-blah/admin/config
  1576. * 404
  1577. * 3. URL (DOMAIN) > DEFAULT
  1578. * http://example.com/admin/config
  1579. * UI language in site default
  1580. * http://example.cn/admin/config
  1581. * UI language in Chinese
  1582. */
  1583. class LocaleUILanguageNegotiationTest extends DrupalWebTestCase {
  1584. public static function getInfo() {
  1585. return array(
  1586. 'name' => 'UI language negotiation',
  1587. 'description' => 'Test UI language switching by url path prefix and domain.',
  1588. 'group' => 'Locale',
  1589. );
  1590. }
  1591. function setUp() {
  1592. parent::setUp('locale', 'locale_test');
  1593. require_once DRUPAL_ROOT . '/includes/language.inc';
  1594. drupal_load('module', 'locale');
  1595. $admin_user = $this->drupalCreateUser(array('administer languages', 'translate interface', 'access administration pages', 'administer blocks'));
  1596. $this->drupalLogin($admin_user);
  1597. }
  1598. /**
  1599. * Tests for language switching by URL path.
  1600. */
  1601. function testUILanguageNegotiation() {
  1602. // A few languages to switch to.
  1603. // This one is unknown, should get the default lang version.
  1604. $language_unknown = 'blah-blah';
  1605. // For testing browser lang preference.
  1606. $language_browser_fallback = 'vi';
  1607. // For testing path prefix.
  1608. $language = 'zh-hans';
  1609. // For setting browser language preference to 'vi'.
  1610. $http_header_browser_fallback = array("Accept-Language: $language_browser_fallback;q=1");
  1611. // For setting browser language preference to some unknown.
  1612. $http_header_blah = array("Accept-Language: blah;q=1");
  1613. // This domain should switch the UI to Chinese.
  1614. $language_domain = 'example.cn';
  1615. // Setup the site languages by installing two languages.
  1616. require_once DRUPAL_ROOT . '/includes/locale.inc';
  1617. locale_add_language($language_browser_fallback);
  1618. locale_add_language($language);
  1619. // We will look for this string in the admin/config screen to see if the
  1620. // corresponding translated string is shown.
  1621. $default_string = 'Configure languages for content and the user interface';
  1622. // Set the default language in order for the translated string to be registered
  1623. // into database when seen by t(). Without doing this, our target string
  1624. // is for some reason not found when doing translate search. This might
  1625. // be some bug.
  1626. drupal_static_reset('language_list');
  1627. $languages = language_list('enabled');
  1628. variable_set('language_default', $languages[1]['vi']);
  1629. // First visit this page to make sure our target string is searchable.
  1630. $this->drupalGet('admin/config');
  1631. // Now the t()'ed string is in db so switch the language back to default.
  1632. variable_del('language_default');
  1633. // Translate the string.
  1634. $language_browser_fallback_string = "In $language_browser_fallback In $language_browser_fallback In $language_browser_fallback";
  1635. $language_string = "In $language In $language In $language";
  1636. // Do a translate search of our target string.
  1637. $edit = array( 'string' => $default_string);
  1638. $this->drupalPost('admin/config/regional/translate/translate', $edit, t('Filter'));
  1639. // Should find the string and now click edit to post translated string.
  1640. $this->clickLink('edit');
  1641. $edit = array(
  1642. "translations[$language_browser_fallback]" => $language_browser_fallback_string,
  1643. "translations[$language]" => $language_string,
  1644. );
  1645. $this->drupalPost(NULL, $edit, t('Save translations'));
  1646. // Configure URL language rewrite.
  1647. variable_set('locale_language_negotiation_url_type', LANGUAGE_TYPE_INTERFACE);
  1648. $tests = array(
  1649. // Default, browser preference should have no influence.
  1650. array(
  1651. 'language_negotiation' => array(LOCALE_LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_DEFAULT),
  1652. 'path' => 'admin/config',
  1653. 'expect' => $default_string,
  1654. 'http_header' => $http_header_browser_fallback,
  1655. 'message' => 'URL (PATH) > DEFAULT: no language prefix, UI language is default and the browser language preference setting is not used.',
  1656. ),
  1657. // Language prefix.
  1658. array(
  1659. 'language_negotiation' => array(LOCALE_LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_DEFAULT),
  1660. 'path' => "$language/admin/config",
  1661. 'expect' => $language_string,
  1662. 'http_header' => $http_header_browser_fallback,
  1663. 'message' => 'URL (PATH) > DEFAULT: with language prefix, UI language is switched based on path prefix',
  1664. ),
  1665. // Default, go by browser preference.
  1666. array(
  1667. 'language_negotiation' => array(LOCALE_LANGUAGE_NEGOTIATION_URL, LOCALE_LANGUAGE_NEGOTIATION_BROWSER),
  1668. 'path' => 'admin/config',
  1669. 'expect' => $language_browser_fallback_string,
  1670. 'http_header' => $http_header_browser_fallback,
  1671. 'message' => 'URL (PATH) > BROWSER: no language prefix, UI language is determined by browser language preference',
  1672. ),
  1673. // Prefix, switch to the language.
  1674. array(
  1675. 'language_negotiation' => array(LOCALE_LANGUAGE_NEGOTIATION_URL, LOCALE_LANGUAGE_NEGOTIATION_BROWSER),
  1676. 'path' => "$language/admin/config",
  1677. 'expect' => $language_string,
  1678. 'http_header' => $http_header_browser_fallback,
  1679. 'message' => 'URL (PATH) > BROWSER: with langage prefix, UI language is based on path prefix',
  1680. ),
  1681. // Default, browser language preference is not one of site's lang.
  1682. array(
  1683. 'language_negotiation' => array(LOCALE_LANGUAGE_NEGOTIATION_URL, LOCALE_LANGUAGE_NEGOTIATION_BROWSER, LANGUAGE_NEGOTIATION_DEFAULT),
  1684. 'path' => 'admin/config',
  1685. 'expect' => $default_string,
  1686. 'http_header' => $http_header_blah,
  1687. 'message' => 'URL (PATH) > BROWSER > DEFAULT: no language prefix and browser language preference set to unknown language should use default language',
  1688. ),
  1689. );
  1690. foreach ($tests as $test) {
  1691. $this->runTest($test);
  1692. }
  1693. // Unknown language prefix should return 404.
  1694. variable_set('language_negotiation_' . LANGUAGE_TYPE_INTERFACE, locale_language_negotiation_info());
  1695. $this->drupalGet("$language_unknown/admin/config", array(), $http_header_browser_fallback);
  1696. $this->assertResponse(404, "Unknown language path prefix should return 404");
  1697. // Setup for domain negotiation, first configure the language to have domain
  1698. // URL.
  1699. $edit = array('prefix' => '', 'domain' => "http://$language_domain");
  1700. $this->drupalPost("admin/config/regional/language/edit/$language", $edit, t('Save language'));
  1701. // Set the site to use domain language negotiation.
  1702. $tests = array(
  1703. // Default domain, browser preference should have no influence.
  1704. array(
  1705. 'language_negotiation' => array(LOCALE_LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_DEFAULT),
  1706. 'locale_language_negotiation_url_part' => LOCALE_LANGUAGE_NEGOTIATION_URL_DOMAIN,
  1707. 'path' => 'admin/config',
  1708. 'expect' => $default_string,
  1709. 'http_header' => $http_header_browser_fallback,
  1710. 'message' => 'URL (DOMAIN) > DEFAULT: default domain should get default language',
  1711. ),
  1712. // Language domain specific URL, we set the $_SERVER['HTTP_HOST'] in
  1713. // locale_test.module hook_boot() to simulate this.
  1714. array(
  1715. 'language_negotiation' => array(LOCALE_LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_DEFAULT),
  1716. 'locale_language_negotiation_url_part' => LOCALE_LANGUAGE_NEGOTIATION_URL_DOMAIN,
  1717. 'locale_test_domain' => $language_domain,
  1718. 'path' => 'admin/config',
  1719. 'expect' => $language_string,
  1720. 'http_header' => $http_header_browser_fallback,
  1721. 'message' => 'URL (DOMAIN) > DEFAULT: domain example.cn should switch to Chinese',
  1722. ),
  1723. );
  1724. foreach ($tests as $test) {
  1725. $this->runTest($test);
  1726. }
  1727. }
  1728. private function runTest($test) {
  1729. if (!empty($test['language_negotiation'])) {
  1730. $negotiation = array_flip($test['language_negotiation']);
  1731. language_negotiation_set(LANGUAGE_TYPE_INTERFACE, $negotiation);
  1732. }
  1733. if (!empty($test['locale_language_negotiation_url_part'])) {
  1734. variable_set('locale_language_negotiation_url_part', $test['locale_language_negotiation_url_part']);
  1735. }
  1736. if (!empty($test['locale_test_domain'])) {
  1737. variable_set('locale_test_domain', $test['locale_test_domain']);
  1738. }
  1739. $this->drupalGet($test['path'], array(), $test['http_header']);
  1740. $this->assertText($test['expect'], $test['message']);
  1741. }
  1742. /**
  1743. * Test URL language detection when the requested URL has no language.
  1744. */
  1745. function testUrlLanguageFallback() {
  1746. // Add the Italian language.
  1747. $language_browser_fallback = 'it';
  1748. locale_add_language($language_browser_fallback);
  1749. $languages = language_list();
  1750. // Enable the path prefix for the default language: this way any unprefixed
  1751. // URL must have a valid fallback value.
  1752. $edit = array('prefix' => 'en');
  1753. $this->drupalPost('admin/config/regional/language/edit/en', $edit, t('Save language'));
  1754. // Enable browser and URL language detection.
  1755. $edit = array(
  1756. 'language[enabled][locale-browser]' => TRUE,
  1757. 'language[enabled][locale-url]' => TRUE,
  1758. 'language[weight][locale-browser]' => -8,
  1759. 'language[weight][locale-url]' => -10,
  1760. );
  1761. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  1762. $this->drupalGet('admin/config/regional/language/configure');
  1763. // Enable the language switcher block.
  1764. $edit = array('blocks[locale_language][region]' => 'sidebar_first');
  1765. $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
  1766. // Access the front page without specifying any valid URL language prefix
  1767. // and having as browser language preference a non-default language.
  1768. $http_header = array("Accept-Language: $language_browser_fallback;q=1");
  1769. $this->drupalGet('', array(), $http_header);
  1770. // Check that the language switcher active link matches the given browser
  1771. // language.
  1772. $args = array(':url' => base_path() . (!empty($GLOBALS['conf']['clean_url']) ? $language_browser_fallback : "?q=$language_browser_fallback"));
  1773. $fields = $this->xpath('//div[@id="block-locale-language"]//a[@class="language-link active" and @href=:url]', $args);
  1774. $this->assertTrue($fields[0] == $languages[$language_browser_fallback]->native, t('The browser language is the URL active language'));
  1775. // Check that URLs are rewritten using the given browser language.
  1776. $fields = $this->xpath('//div[@id="site-name"]//a[@rel="home" and @href=:url]//span', $args);
  1777. $this->assertTrue($fields[0] == 'Drupal', t('URLs are rewritten using the browser language.'));
  1778. }
  1779. }
  1780. /**
  1781. * Test that URL rewriting works as expected.
  1782. */
  1783. class LocaleUrlRewritingTest extends DrupalWebTestCase {
  1784. public static function getInfo() {
  1785. return array(
  1786. 'name' => 'URL rewriting',
  1787. 'description' => 'Test that URL rewriting works as expected.',
  1788. 'group' => 'Locale',
  1789. );
  1790. }
  1791. function setUp() {
  1792. parent::setUp('locale');
  1793. // Create and login user.
  1794. $this->web_user = $this->drupalCreateUser(array('administer languages', 'access administration pages'));
  1795. $this->drupalLogin($this->web_user);
  1796. // Install French language.
  1797. $edit = array();
  1798. $edit['langcode'] = 'fr';
  1799. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
  1800. // Install Italian language.
  1801. $edit = array();
  1802. $edit['langcode'] = 'it';
  1803. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
  1804. // Disable Italian language.
  1805. $edit = array('enabled[it]' => FALSE);
  1806. $this->drupalPost('admin/config/regional/language', $edit, t('Save configuration'));
  1807. // Enable URL language detection and selection.
  1808. $edit = array('language[enabled][locale-url]' => 1);
  1809. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  1810. // Reset static caching.
  1811. drupal_static_reset('language_list');
  1812. drupal_static_reset('locale_url_outbound_alter');
  1813. drupal_static_reset('locale_language_url_rewrite_url');
  1814. }
  1815. /**
  1816. * Check that disabled or non-installed languages are not considered.
  1817. */
  1818. function testUrlRewritingEdgeCases() {
  1819. // Check URL rewriting with a disabled language.
  1820. $languages = language_list();
  1821. $this->checkUrl($languages['it'], t('Path language is ignored if language is disabled.'), t('URL language negotiation does not work with disabled languages'));
  1822. // Check URL rewriting with a non-installed language.
  1823. $non_existing = language_default();
  1824. $non_existing->language = $this->randomName();
  1825. $non_existing->prefix = $this->randomName();
  1826. $this->checkUrl($non_existing, t('Path language is ignored if language is not installed.'), t('URL language negotiation does not work with non-installed languages'));
  1827. }
  1828. /**
  1829. * Check URL rewriting for the given language.
  1830. *
  1831. * The test is performed with a fixed URL (the default front page) to simply
  1832. * check that language prefixes are not added to it and that the prefixed URL
  1833. * is actually not working.
  1834. */
  1835. private function checkUrl($language, $message1, $message2) {
  1836. $options = array('language' => $language);
  1837. $base_path = trim(base_path(), '/');
  1838. $rewritten_path = trim(str_replace(array('?q=', $base_path), '', url('node', $options)), '/');
  1839. $segments = explode('/', $rewritten_path, 2);
  1840. $prefix = $segments[0];
  1841. $path = isset($segments[1]) ? $segments[1] : $prefix;
  1842. // If the rewritten URL has not a language prefix we pick the right one from
  1843. // the language object so we can always check the prefixed URL.
  1844. if ($this->assertNotEqual($language->prefix, $prefix, $message1)) {
  1845. $prefix = $language->prefix;
  1846. }
  1847. $this->drupalGet("$prefix/$path");
  1848. $this->assertResponse(404, $message2);
  1849. }
  1850. }
  1851. /**
  1852. * Functional test for multilingual fields.
  1853. */
  1854. class LocaleMultilingualFieldsFunctionalTest extends DrupalWebTestCase {
  1855. public static function getInfo() {
  1856. return array(
  1857. 'name' => 'Multilingual fields',
  1858. 'description' => 'Test multilingual support for fields.',
  1859. 'group' => 'Locale',
  1860. );
  1861. }
  1862. function setUp() {
  1863. parent::setUp('locale');
  1864. // Setup users.
  1865. $admin_user = $this->drupalCreateUser(array('administer languages', 'administer content types', 'access administration pages', 'create page content', 'edit own page content'));
  1866. $this->drupalLogin($admin_user);
  1867. // Add a new language.
  1868. require_once DRUPAL_ROOT . '/includes/locale.inc';
  1869. locale_add_language('it', 'Italian', 'Italiano', LANGUAGE_LTR, '', '', TRUE, FALSE);
  1870. // Enable URL language detection and selection.
  1871. $edit = array('language[enabled][locale-url]' => '1');
  1872. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  1873. // Set "Basic page" content type to use multilingual support.
  1874. $edit = array(
  1875. 'language_content_type' => 1,
  1876. );
  1877. $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
  1878. $this->assertRaw(t('The content type %type has been updated.', array('%type' => 'Basic page')), t('Basic page content type has been updated.'));
  1879. // Make node body translatable.
  1880. $field = field_info_field('body');
  1881. $field['translatable'] = TRUE;
  1882. field_update_field($field);
  1883. }
  1884. /**
  1885. * Test if field languages are correctly set through the node form.
  1886. */
  1887. function testMultilingualNodeForm() {
  1888. // Create "Basic page" content.
  1889. $langcode = LANGUAGE_NONE;
  1890. $title_key = "title";
  1891. $title_value = $this->randomName(8);
  1892. $body_key = "body[$langcode][0][value]";
  1893. $body_value = $this->randomName(16);
  1894. // Create node to edit.
  1895. $edit = array();
  1896. $edit[$title_key] = $title_value;
  1897. $edit[$body_key] = $body_value;
  1898. $edit['language'] = 'en';
  1899. $this->drupalPost('node/add/page', $edit, t('Save'));
  1900. // Check that the node exists in the database.
  1901. $node = $this->drupalGetNodeByTitle($edit[$title_key]);
  1902. $this->assertTrue($node, t('Node found in database.'));
  1903. $assert = isset($node->body['en']) && !isset($node->body[LANGUAGE_NONE]) && $node->body['en'][0]['value'] == $body_value;
  1904. $this->assertTrue($assert, t('Field language correctly set.'));
  1905. // Change node language.
  1906. $this->drupalGet("node/$node->nid/edit");
  1907. $edit = array(
  1908. $title_key => $this->randomName(8),
  1909. 'language' => 'it'
  1910. );
  1911. $this->drupalPost(NULL, $edit, t('Save'));
  1912. $node = $this->drupalGetNodeByTitle($edit[$title_key]);
  1913. $this->assertTrue($node, t('Node found in database.'));
  1914. $assert = isset($node->body['it']) && !isset($node->body['en']) && $node->body['it'][0]['value'] == $body_value;
  1915. $this->assertTrue($assert, t('Field language correctly changed.'));
  1916. // Enable content language URL detection.
  1917. language_negotiation_set(LANGUAGE_TYPE_CONTENT, array(LOCALE_LANGUAGE_NEGOTIATION_URL => 0));
  1918. // Test multilingual field language fallback logic.
  1919. $this->drupalGet("it/node/$node->nid");
  1920. $this->assertRaw($body_value, t('Body correctly displayed using Italian as requested language'));
  1921. $this->drupalGet("node/$node->nid");
  1922. $this->assertRaw($body_value, t('Body correctly displayed using English as requested language'));
  1923. }
  1924. /*
  1925. * Test multilingual field display settings.
  1926. */
  1927. function testMultilingualDisplaySettings() {
  1928. // Create "Basic page" content.
  1929. $langcode = LANGUAGE_NONE;
  1930. $title_key = "title";
  1931. $title_value = $this->randomName(8);
  1932. $body_key = "body[$langcode][0][value]";
  1933. $body_value = $this->randomName(16);
  1934. // Create node to edit.
  1935. $edit = array();
  1936. $edit[$title_key] = $title_value;
  1937. $edit[$body_key] = $body_value;
  1938. $edit['language'] = 'en';
  1939. $this->drupalPost('node/add/page', $edit, t('Save'));
  1940. // Check that the node exists in the database.
  1941. $node = $this->drupalGetNodeByTitle($edit[$title_key]);
  1942. $this->assertTrue($node, t('Node found in database.'));
  1943. // Check if node body is showed.
  1944. $this->drupalGet("node/$node->nid");
  1945. $body = $this->xpath('//div[@id=:id]//div[@property="content:encoded"]/p', array(':id' => 'node-' . $node->nid));
  1946. $this->assertEqual(current($body), $node->body['en'][0]['value'], 'Node body is correctly showed.');
  1947. }
  1948. }
  1949. /**
  1950. * Functional tests for comment language.
  1951. */
  1952. class LocaleCommentLanguageFunctionalTest extends DrupalWebTestCase {
  1953. public static function getInfo() {
  1954. return array(
  1955. 'name' => 'Comment language',
  1956. 'description' => 'Tests for comment language.',
  1957. 'group' => 'Locale',
  1958. );
  1959. }
  1960. function setUp() {
  1961. parent::setUp('locale', 'locale_test');
  1962. // Create and login user.
  1963. $admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer languages', 'access administration pages', 'administer content types', 'create article content'));
  1964. $this->drupalLogin($admin_user);
  1965. // Add language.
  1966. $edit = array('langcode' => 'fr');
  1967. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
  1968. // Set "Article" content type to use multilingual support.
  1969. $edit = array('language_content_type' => 1);
  1970. $this->drupalPost('admin/structure/types/manage/article', $edit, t('Save content type'));
  1971. // Enable content language negotiation UI.
  1972. variable_set('locale_test_content_language_type', TRUE);
  1973. // Set interface language detection to user and content language detection
  1974. // to URL. Disable inheritance from interface language to ensure content
  1975. // language will fall back to the default language if no URL language can be
  1976. // detected.
  1977. $edit = array(
  1978. 'language[enabled][locale-user]' => TRUE,
  1979. 'language_content[enabled][locale-url]' => TRUE,
  1980. 'language_content[enabled][locale-interface]' => FALSE,
  1981. );
  1982. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  1983. // Change user language preference, this way interface language is always
  1984. // French no matter what path prefix the URLs have.
  1985. $edit = array('language' => 'fr');
  1986. $this->drupalPost("user/{$admin_user->uid}/edit", $edit, t('Save'));
  1987. }
  1988. /**
  1989. * Test that comment language is properly set.
  1990. */
  1991. function testCommentLanguage() {
  1992. drupal_static_reset('language_list');
  1993. // Create two nodes, one for english and one for french, and comment each
  1994. // node using both english and french as content language by changing URL
  1995. // language prefixes. Meanwhile interface language is always French, which
  1996. // is the user language preference. This way we can ensure that node
  1997. // language and interface language do not influence comment language, as
  1998. // only content language has to.
  1999. foreach (language_list() as $node_langcode => $node_language) {
  2000. $language_none = LANGUAGE_NONE;
  2001. // Create "Article" content.
  2002. $title = $this->randomName();
  2003. $edit = array(
  2004. "title" => $title,
  2005. "body[$language_none][0][value]" => $this->randomName(),
  2006. "language" => $node_langcode,
  2007. );
  2008. $this->drupalPost("node/add/article", $edit, t('Save'));
  2009. $node = $this->drupalGetNodeByTitle($title);
  2010. foreach (language_list() as $langcode => $language) {
  2011. // Post a comment with content language $langcode.
  2012. $prefix = empty($language->prefix) ? '' : $language->prefix . '/';
  2013. $edit = array("comment_body[$language_none][0][value]" => $this->randomName());
  2014. $this->drupalPost("{$prefix}node/{$node->nid}", $edit, t('Save'));
  2015. // Check that comment language matches the current content language.
  2016. $comment = db_select('comment', 'c')
  2017. ->fields('c')
  2018. ->condition('nid', $node->nid)
  2019. ->orderBy('cid', 'DESC')
  2020. ->execute()
  2021. ->fetchObject();
  2022. $args = array('%node_language' => $node_langcode, '%comment_language' => $comment->language, '%langcode' => $langcode);
  2023. $this->assertEqual($comment->language, $langcode, t('The comment posted with content language %langcode and belonging to the node with language %node_language has language %comment_language', $args));
  2024. }
  2025. }
  2026. }
  2027. }
  2028. /**
  2029. * Functional tests for localizing date formats.
  2030. */
  2031. class LocaleDateFormatsFunctionalTest extends DrupalWebTestCase {
  2032. public static function getInfo() {
  2033. return array(
  2034. 'name' => 'Localize date formats',
  2035. 'description' => 'Tests for the localization of date formats.',
  2036. 'group' => 'Locale',
  2037. );
  2038. }
  2039. function setUp() {
  2040. parent::setUp('locale');
  2041. // Create and login user.
  2042. $admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer languages', 'access administration pages', 'create article content'));
  2043. $this->drupalLogin($admin_user);
  2044. }
  2045. /**
  2046. * Functional tests for localizing date formats.
  2047. */
  2048. function testLocalizeDateFormats() {
  2049. // Add language.
  2050. $edit = array(
  2051. 'langcode' => 'fr',
  2052. );
  2053. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
  2054. // Set language negotiation.
  2055. $language_type = LANGUAGE_TYPE_INTERFACE;
  2056. $edit = array(
  2057. "{$language_type}[enabled][locale-url]" => TRUE,
  2058. );
  2059. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  2060. // Configure date formats.
  2061. $this->drupalGet('admin/config/regional/date-time/locale');
  2062. $this->assertText('Français', 'Configured languages appear.');
  2063. $edit = array(
  2064. 'date_format_long' => 'd.m.Y - H:i',
  2065. 'date_format_medium' => 'd.m.Y - H:i',
  2066. 'date_format_short' => 'd.m.Y - H:i',
  2067. );
  2068. $this->drupalPost('admin/config/regional/date-time/locale/fr/edit', $edit, t('Save configuration'));
  2069. $this->assertText(t('Configuration saved.'), 'French date formats updated.');
  2070. $edit = array(
  2071. 'date_format_long' => 'j M Y - g:ia',
  2072. 'date_format_medium' => 'j M Y - g:ia',
  2073. 'date_format_short' => 'j M Y - g:ia',
  2074. );
  2075. $this->drupalPost('admin/config/regional/date-time/locale/en/edit', $edit, t('Save configuration'));
  2076. $this->assertText(t('Configuration saved.'), 'English date formats updated.');
  2077. // Create node content.
  2078. $node = $this->drupalCreateNode(array('type' => 'article'));
  2079. // Configure format for the node posted date changes with the language.
  2080. $this->drupalGet('node/' . $node->nid);
  2081. $english_date = format_date($node->created, 'custom', 'j M Y');
  2082. $this->assertText($english_date, t('English date format appears'));
  2083. $this->drupalGet('fr/node/' . $node->nid);
  2084. $french_date = format_date($node->created, 'custom', 'd.m.Y');
  2085. $this->assertText($french_date, t('French date format appears'));
  2086. }
  2087. }
  2088. /**
  2089. * Functional test for language types/negotiation info.
  2090. */
  2091. class LocaleLanguageNegotiationInfoFunctionalTest extends DrupalWebTestCase {
  2092. public static function getInfo() {
  2093. return array(
  2094. 'name' => 'Language negotiation info',
  2095. 'description' => 'Tests alterations to language types/negotiation info.',
  2096. 'group' => 'Locale',
  2097. );
  2098. }
  2099. function setUp() {
  2100. parent::setUp('locale');
  2101. require_once DRUPAL_ROOT .'/includes/language.inc';
  2102. $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages', 'view the administration theme'));
  2103. $this->drupalLogin($admin_user);
  2104. $this->drupalPost('admin/config/regional/language/add', array('langcode' => 'it'), t('Add language'));
  2105. }
  2106. /**
  2107. * Tests alterations to language types/negotiation info.
  2108. */
  2109. function testInfoAlterations() {
  2110. // Enable language type/negotiation info alterations.
  2111. variable_set('locale_test_language_types', TRUE);
  2112. variable_set('locale_test_language_negotiation_info', TRUE);
  2113. $this->languageNegotiationUpdate();
  2114. // Check that fixed language types are properly configured without the need
  2115. // of saving the language negotiation settings.
  2116. $this->checkFixedLanguageTypes();
  2117. // Make the content language type configurable by updating the language
  2118. // negotiation settings with the proper flag enabled.
  2119. variable_set('locale_test_content_language_type', TRUE);
  2120. $this->languageNegotiationUpdate();
  2121. $type = LANGUAGE_TYPE_CONTENT;
  2122. $language_types = variable_get('language_types', drupal_language_types());
  2123. $this->assertTrue($language_types[$type], t('Content language type is configurable.'));
  2124. // Enable some core and custom language providers. The test language type is
  2125. // supposed to be configurable.
  2126. $test_type = 'test_language_type';
  2127. $provider = LOCALE_LANGUAGE_NEGOTIATION_INTERFACE;
  2128. $test_provider = 'test_language_provider';
  2129. $form_field = $type . '[enabled]['. $provider .']';
  2130. $edit = array(
  2131. $form_field => TRUE,
  2132. $type . '[enabled][' . $test_provider . ']' => TRUE,
  2133. $test_type . '[enabled][' . $test_provider . ']' => TRUE,
  2134. );
  2135. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  2136. // Remove the interface language provider by updating the language
  2137. // negotiation settings with the proper flag enabled.
  2138. variable_set('locale_test_language_negotiation_info_alter', TRUE);
  2139. $this->languageNegotiationUpdate();
  2140. $negotiation = variable_get("language_negotiation_$type", array());
  2141. $this->assertFalse(isset($negotiation[$provider]), t('Interface language provider removed from the stored settings.'));
  2142. $this->assertNoFieldByXPath("//input[@name=\"$form_field\"]", NULL, t('Interface language provider unavailable.'));
  2143. // Check that type-specific language providers can be assigned only to the
  2144. // corresponding language types.
  2145. foreach (language_types_configurable() as $type) {
  2146. $form_field = $type . '[enabled][test_language_provider_ts]';
  2147. if ($type == $test_type) {
  2148. $this->assertFieldByXPath("//input[@name=\"$form_field\"]", NULL, t('Type-specific test language provider available for %type.', array('%type' => $type)));
  2149. }
  2150. else {
  2151. $this->assertNoFieldByXPath("//input[@name=\"$form_field\"]", NULL, t('Type-specific test language provider unavailable for %type.', array('%type' => $type)));
  2152. }
  2153. }
  2154. // Check language negotiation results.
  2155. $this->drupalGet('');
  2156. $last = variable_get('locale_test_language_negotiation_last', array());
  2157. foreach (language_types() as $type) {
  2158. $langcode = $last[$type];
  2159. $value = $type == LANGUAGE_TYPE_CONTENT || strpos($type, 'test') !== FALSE ? 'it' : 'en';
  2160. $this->assertEqual($langcode, $value, t('The negotiated language for %type is %language', array('%type' => $type, '%language' => $langcode)));
  2161. }
  2162. // Disable locale_test and check that everything is set back to the original
  2163. // status.
  2164. $this->languageNegotiationUpdate('disable');
  2165. // Check that only the core language types are available.
  2166. foreach (language_types() as $type) {
  2167. $this->assertTrue(strpos($type, 'test') === FALSE, t('The %type language is still available', array('%type' => $type)));
  2168. }
  2169. // Check that fixed language types are properly configured, even those
  2170. // previously set to configurable.
  2171. $this->checkFixedLanguageTypes();
  2172. // Check that unavailable language providers are not present in the
  2173. // negotiation settings.
  2174. $negotiation = variable_get("language_negotiation_$type", array());
  2175. $this->assertFalse(isset($negotiation[$test_provider]), t('The disabled test language provider is not part of the content language negotiation settings.'));
  2176. // Check that configuration page presents the correct options and settings.
  2177. $this->assertNoRaw(t('Test language detection'), t('No test language type configuration available.'));
  2178. $this->assertNoRaw(t('This is a test language provider'), t('No test language provider available.'));
  2179. }
  2180. /**
  2181. * Update language types/negotiation information.
  2182. *
  2183. * Manually invoke locale_modules_enabled()/locale_modules_disabled() since
  2184. * they would not be invoked after enabling/disabling locale_test the first
  2185. * time.
  2186. */
  2187. private function languageNegotiationUpdate($op = 'enable') {
  2188. static $last_op = NULL;
  2189. $modules = array('locale_test');
  2190. // Enable/disable locale_test only if we did not already before.
  2191. if ($last_op != $op) {
  2192. $function = "module_{$op}";
  2193. $function($modules);
  2194. // Reset hook implementation cache.
  2195. module_implements(NULL, FALSE, TRUE);
  2196. }
  2197. drupal_static_reset('language_types_info');
  2198. drupal_static_reset('language_negotiation_info');
  2199. $function = "locale_modules_{$op}d";
  2200. if (function_exists($function)) {
  2201. $function($modules);
  2202. }
  2203. $this->drupalGet('admin/config/regional/language/configure');
  2204. }
  2205. /**
  2206. * Check that language negotiation for fixed types matches the stored one.
  2207. */
  2208. private function checkFixedLanguageTypes() {
  2209. drupal_static_reset('language_types_info');
  2210. foreach (language_types_info() as $type => $info) {
  2211. if (isset($info['fixed'])) {
  2212. $negotiation = variable_get("language_negotiation_$type", array());
  2213. $equal = count($info['fixed']) == count($negotiation);
  2214. while ($equal && list($id) = each($negotiation)) {
  2215. list(, $info_id) = each($info['fixed']);
  2216. $equal = $info_id == $id;
  2217. }
  2218. $this->assertTrue($equal, t('language negotiation for %type is properly set up', array('%type' => $type)));
  2219. }
  2220. }
  2221. }
  2222. }