L10nUpdateTest.test 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <?php
  2. /**
  3. * @file
  4. * Contains L10nUpdateTest.
  5. */
  6. /**
  7. * Tests for update translations.
  8. */
  9. class L10nUpdateTest extends L10nUpdateTestBase {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Update translations',
  13. 'description' => 'Tests for updating the interface translations of projects.',
  14. 'group' => 'Localization Update',
  15. );
  16. }
  17. function setUp() {
  18. parent::setUp();
  19. $admin_user = $this->drupalCreateUser(array('administer modules', 'administer site configuration', 'administer languages', 'access administration pages', 'translate interface'));
  20. $this->drupalLogin($admin_user);
  21. // We use German as test language. This language must match the translation
  22. // file that come with the l10n_update_test module (test.de.po) and can therefore
  23. // not be chosen randomly.
  24. $this->addLanguage('de');
  25. module_load_include('compare.inc', 'l10n_update');
  26. module_load_include('fetch.inc', 'l10n_update');
  27. }
  28. /**
  29. * Checks if a list of translatable projects gets build.
  30. */
  31. function testUpdateProjects() {
  32. module_load_include('compare.inc', 'l10n_update');
  33. variable_set('l10n_update_test_projects_alter', TRUE);
  34. // Make the test modules look like a normal custom module. i.e. make the
  35. // modules not hidden. l10n_update_test_system_info_alter() modifies the project
  36. // info of the l10n_update_test and l10n_update_test_translate modules.
  37. variable_set('l10n_update_test_system_info_alter', TRUE);
  38. $this->resetAll();
  39. // Check if interface translation data is collected from hook_info.
  40. $projects = l10n_update_project_list();
  41. $this->assertFalse(isset($projects['l10n_update_test_translate']), 'Hidden module not found');
  42. $this->assertEqual($projects['l10n_update_test']['info']['interface translation server pattern'], 'sites/all/modules/l10n_update/tests/test.%language.po', 'Interface translation parameter found in project info.');
  43. $this->assertEqual($projects['l10n_update_test']['name'] , 'l10n_update_test', format_string('%key found in project info.', array('%key' => 'interface translation project')));
  44. }
  45. /**
  46. * Checks if local or remote translation sources are detected.
  47. *
  48. * The translation status process by default checks the status of the
  49. * installed projects. For testing purpose a predefined set of modules with
  50. * fixed file names and release versions is used. This custom project
  51. * definition is applied using a hook_l10n_update_projects_alter
  52. * implementation in the l10n_update_test module.
  53. *
  54. * This test generates a set of local and remote translation files in their
  55. * respective local and remote translation directory. The test checks whether
  56. * the most recent files are selected in the different check scenarios: check
  57. * for local files only, check for both local and remote files.
  58. */
  59. function testUpdateCheckStatus() {
  60. // Set a flag to let the l10n_update_test module replace the project data with a
  61. // set of test projects.
  62. variable_set('l10n_update_test_projects_alter', TRUE);
  63. // Create local and remote translations files.
  64. $this->setTranslationFiles();
  65. variable_set('l10n_update_default_filename', '%project-%release.%language._po');
  66. // Set the test conditions.
  67. $edit = array(
  68. 'l10n_update_check_mode' => L10N_UPDATE_USE_SOURCE_LOCAL,
  69. );
  70. $this->drupalPost('admin/config/regional/language/update', $edit, t('Save configuration'));
  71. // Get status of translation sources at local file system.
  72. $this->drupalGet('admin/config/regional/translate/check');
  73. $result = l10n_update_get_status();
  74. $this->assertEqual($result['contrib_module_one']['de']->type, L10N_UPDATE_LOCAL, 'Translation of contrib_module_one found');
  75. $this->assertEqual($result['contrib_module_one']['de']->timestamp, $this->timestamp_old, 'Translation timestamp found');
  76. $this->assertEqual($result['contrib_module_two']['de']->type, L10N_UPDATE_LOCAL, 'Translation of contrib_module_two found');
  77. $this->assertEqual($result['contrib_module_two']['de']->timestamp, $this->timestamp_new, 'Translation timestamp found');
  78. $this->assertEqual($result['l10n_update_test']['de']->type, L10N_UPDATE_LOCAL, 'Translation of l10n_update_test found');
  79. $this->assertEqual($result['custom_module_one']['de']->type, L10N_UPDATE_LOCAL, 'Translation of custom_module_one found');
  80. // Set the test conditions.
  81. $edit = array(
  82. 'l10n_update_check_mode' => L10N_UPDATE_USE_SOURCE_REMOTE_AND_LOCAL,
  83. );
  84. $this->drupalPost('admin/config/regional/language/update', $edit, t('Save configuration'));
  85. // Get status of translation sources at both local and remote locations.
  86. $this->drupalGet('admin/config/regional/translate/check');
  87. $result = l10n_update_get_status();
  88. $this->assertEqual($result['contrib_module_one']['de']->type, L10N_UPDATE_REMOTE, 'Translation of contrib_module_one found');
  89. $this->assertEqual($result['contrib_module_one']['de']->timestamp, $this->timestamp_new, 'Translation timestamp found');
  90. $this->assertEqual($result['contrib_module_two']['de']->type, L10N_UPDATE_LOCAL, 'Translation of contrib_module_two found');
  91. $this->assertEqual($result['contrib_module_two']['de']->timestamp, $this->timestamp_new, 'Translation timestamp found');
  92. $this->assertEqual($result['contrib_module_three']['de']->type, L10N_UPDATE_LOCAL, 'Translation of contrib_module_three found');
  93. $this->assertEqual($result['contrib_module_three']['de']->timestamp, $this->timestamp_old, 'Translation timestamp found');
  94. $this->assertEqual($result['l10n_update_test']['de']->type, L10N_UPDATE_LOCAL, 'Translation of l10n_update_test found');
  95. $this->assertEqual($result['custom_module_one']['de']->type, L10N_UPDATE_LOCAL, 'Translation of custom_module_one found');
  96. }
  97. /**
  98. * Tests translation import from remote sources.
  99. *
  100. * Test conditions:
  101. * - Source: remote and local files
  102. * - Import overwrite: all existing translations
  103. */
  104. function testUpdateImportSourceRemote() {
  105. // Build the test environment.
  106. $this->setTranslationFiles();
  107. $this-> setCurrentTranslations();
  108. variable_set('l10n_update_default_filename', '%project-%release.%language._po');
  109. // Set the update conditions for this test.
  110. $edit = array(
  111. 'l10n_update_check_mode' => L10N_UPDATE_USE_SOURCE_REMOTE_AND_LOCAL,
  112. 'overwrite' => LOCALE_IMPORT_OVERWRITE,
  113. );
  114. $this->drupalPost('admin/config/regional/language/update', $edit, t('Save configuration'));
  115. // Get the translation status.
  116. $this->drupalGet('admin/config/regional/translate/check');
  117. // Check the status on the Available translation status page.
  118. $this->assertRaw('<label class="element-invisible" for="edit-langcodes-de">Update German </label>', 'German language found');
  119. $this->assertText('Updates for: Contributed module one, Contributed module two, Custom module one, Locale test', 'Updates found');
  120. $this->assertText('Contributed module one (' . format_date($this->timestamp_new, 'medium') . ')', 'Updates for Contrib module one');
  121. $this->assertText('Contributed module two (' . format_date($this->timestamp_new, 'medium') . ')', 'Updates for Contrib module two');
  122. // Execute the translation update.
  123. $this->drupalPost('admin/config/regional/translate/update', array(), t('Update translations'));
  124. // Check if the translation has been updated, using the status cache.
  125. $status = l10n_update_get_status();
  126. $this->assertEqual($status['contrib_module_one']['de']->type, L10N_UPDATE_CURRENT, 'Translation of contrib_module_one found');
  127. $this->assertEqual($status['contrib_module_two']['de']->type, L10N_UPDATE_CURRENT, 'Translation of contrib_module_two found');
  128. $this->assertEqual($status['contrib_module_three']['de']->type, L10N_UPDATE_CURRENT, 'Translation of contrib_module_three found');
  129. // Check the new translation status.
  130. // The static cache needs to be flushed first to get the most recent data
  131. // from the database. The function was called earlier during this test.
  132. drupal_static_reset('l10n_update_get_file_history');
  133. $history = l10n_update_get_file_history();
  134. $this->assertTrue($history['contrib_module_one']['de']->timestamp >= $this->timestamp_now, 'Translation of contrib_module_one is imported');
  135. $this->assertTrue($history['contrib_module_one']['de']->last_checked >= $this->timestamp_now, 'Translation of contrib_module_one is updated');
  136. $this->assertEqual($history['contrib_module_two']['de']->timestamp, $this->timestamp_new, 'Translation of contrib_module_two is imported');
  137. $this->assertTrue($history['contrib_module_two']['de']->last_checked >= $this->timestamp_now, 'Translation of contrib_module_two is updated');
  138. $this->assertEqual($history['contrib_module_three']['de']->timestamp, $this->timestamp_medium, 'Translation of contrib_module_three is not imported');
  139. $this->assertEqual($history['contrib_module_three']['de']->last_checked, $this->timestamp_medium, 'Translation of contrib_module_three is not updated');
  140. // Check whether existing translations have (not) been overwritten.
  141. $this->assertEqual(t('January', array(), array('langcode' => 'de')), 'Januar_1', 'Translation of January');
  142. $this->assertEqual(t('February', array(), array('langcode' => 'de')), 'Februar_2', 'Translation of February');
  143. $this->assertEqual(t('March', array(), array('langcode' => 'de')), 'Marz_2', 'Translation of March');
  144. $this->assertEqual(t('April', array(), array('langcode' => 'de')), 'April_2', 'Translation of April');
  145. $this->assertEqual(t('May', array(), array('langcode' => 'de')), 'Mai_customized', 'Translation of May');
  146. $this->assertEqual(t('June', array(), array('langcode' => 'de')), 'Juni', 'Translation of June');
  147. $this->assertEqual(t('Monday', array(), array('langcode' => 'de')), 'Montag', 'Translation of Monday');
  148. }
  149. /**
  150. * Tests translation import from local sources.
  151. *
  152. * Test conditions:
  153. * - Source: local files only
  154. * - Import overwrite: all existing translations
  155. */
  156. function testUpdateImportSourceLocal() {
  157. // Build the test environment.
  158. $this->setTranslationFiles();
  159. $this-> setCurrentTranslations();
  160. variable_set('l10n_update_default_filename', '%project-%release.%language._po');
  161. // Set the update conditions for this test.
  162. $edit = array(
  163. 'l10n_update_check_mode' => L10N_UPDATE_USE_SOURCE_LOCAL,
  164. 'overwrite' => LOCALE_IMPORT_OVERWRITE,
  165. );
  166. $this->drupalPost('admin/config/regional/language/update', $edit, t('Save configuration'));
  167. // Execute the translation update.
  168. $this->drupalGet('admin/config/regional/translate/check');
  169. $this->drupalPost('admin/config/regional/translate/update', array(), t('Update translations'));
  170. // Check if the translation has been updated, using the status cache.
  171. $status = l10n_update_get_status();
  172. $this->assertEqual($status['contrib_module_one']['de']->type, L10N_UPDATE_CURRENT, 'Translation of contrib_module_one found');
  173. $this->assertEqual($status['contrib_module_two']['de']->type, L10N_UPDATE_CURRENT, 'Translation of contrib_module_two found');
  174. $this->assertEqual($status['contrib_module_three']['de']->type, L10N_UPDATE_CURRENT, 'Translation of contrib_module_three found');
  175. // Check the new translation status.
  176. // The static cache needs to be flushed first to get the most recent data
  177. // from the database. The function was called earlier during this test.
  178. drupal_static_reset('l10n_update_get_file_history');
  179. $history = l10n_update_get_file_history();
  180. $this->assertTrue($history['contrib_module_one']['de']->timestamp >= $this->timestamp_medium, 'Translation of contrib_module_one is imported');
  181. $this->assertEqual($history['contrib_module_one']['de']->last_checked, $this->timestamp_medium, 'Translation of contrib_module_one is updated');
  182. $this->assertEqual($history['contrib_module_two']['de']->timestamp, $this->timestamp_new, 'Translation of contrib_module_two is imported');
  183. $this->assertTrue($history['contrib_module_two']['de']->last_checked >= $this->timestamp_now, 'Translation of contrib_module_two is updated');
  184. $this->assertEqual($history['contrib_module_three']['de']->timestamp, $this->timestamp_medium, 'Translation of contrib_module_three is not imported');
  185. $this->assertEqual($history['contrib_module_three']['de']->last_checked, $this->timestamp_medium, 'Translation of contrib_module_three is not updated');
  186. // Check whether existing translations have (not) been overwritten.
  187. $this->assertEqual(t('January', array(), array('langcode' => 'de')), 'Januar_customized', 'Translation of January');
  188. $this->assertEqual(t('February', array(), array('langcode' => 'de')), 'Februar_2', 'Translation of February');
  189. $this->assertEqual(t('March', array(), array('langcode' => 'de')), 'Marz_2', 'Translation of March');
  190. $this->assertEqual(t('April', array(), array('langcode' => 'de')), 'April_2', 'Translation of April');
  191. $this->assertEqual(t('May', array(), array('langcode' => 'de')), 'Mai_customized', 'Translation of May');
  192. $this->assertEqual(t('June', array(), array('langcode' => 'de')), 'Juni', 'Translation of June');
  193. $this->assertEqual(t('Monday', array(), array('langcode' => 'de')), 'Montag', 'Translation of Monday');
  194. }
  195. /**
  196. * Tests translation import and only overwrite non-customized translations.
  197. *
  198. * Test conditions:
  199. * - Source: remote and local files
  200. * - Import overwrite: only overwrite non-customized translations
  201. */
  202. function testUpdateImportModeNonCustomized() {
  203. // Build the test environment.
  204. $this->setTranslationFiles();
  205. $this-> setCurrentTranslations();
  206. variable_set('l10n_update_default_filename', '%project-%release.%language._po');
  207. // Set the test conditions.
  208. $edit = array(
  209. 'l10n_update_check_mode' => L10N_UPDATE_USE_SOURCE_REMOTE_AND_LOCAL,
  210. 'overwrite' => L10N_UPDATE_OVERWRITE_NON_CUSTOMIZED,
  211. );
  212. $this->drupalPost('admin/config/regional/language/update', $edit, t('Save configuration'));
  213. // Execute translation update.
  214. $this->drupalGet('admin/config/regional/translate/check');
  215. $this->drupalPost('admin/config/regional/translate/update', array(), t('Update translations'));
  216. // Check whether existing translations have (not) been overwritten.
  217. $this->assertEqual(t('January', array(), array('langcode' => 'de')), 'Januar_customized', 'Translation of January');
  218. $this->assertEqual(t('February', array(), array('langcode' => 'de')), 'Februar_customized', 'Translation of February');
  219. $this->assertEqual(t('March', array(), array('langcode' => 'de')), 'Marz_2', 'Translation of March');
  220. $this->assertEqual(t('April', array(), array('langcode' => 'de')), 'April_2', 'Translation of April');
  221. $this->assertEqual(t('May', array(), array('langcode' => 'de')), 'Mai_customized', 'Translation of May');
  222. $this->assertEqual(t('June', array(), array('langcode' => 'de')), 'Juni', 'Translation of June');
  223. $this->assertEqual(t('Monday', array(), array('langcode' => 'de')), 'Montag', 'Translation of Monday');
  224. }
  225. /**
  226. * Tests translation import and don't overwrite any translation.
  227. *
  228. * Test conditions:
  229. * - Source: remote and local files
  230. * - Import overwrite: don't overwrite any existing translation
  231. */
  232. function testUpdateImportModeNone() {
  233. // Build the test environment.
  234. $this->setTranslationFiles();
  235. $this-> setCurrentTranslations();
  236. variable_set('l10n_update_default_filename', '%project-%release.%language._po');
  237. // Set the test conditions.
  238. $edit = array(
  239. 'l10n_update_check_mode' => L10N_UPDATE_USE_SOURCE_REMOTE_AND_LOCAL,
  240. 'overwrite' => LOCALE_IMPORT_KEEP,
  241. );
  242. $this->drupalPost('admin/config/regional/language/update', $edit, t('Save configuration'));
  243. // Execute translation update.
  244. $this->drupalGet('admin/config/regional/translate/check');
  245. $this->drupalPost('admin/config/regional/translate/update', array(), t('Update translations'));
  246. // Check whether existing translations have (not) been overwritten.
  247. $this->assertTranslation('January', 'Januar_customized', 'de');
  248. $this->assertTranslation('February', 'Februar_customized', 'de');
  249. $this->assertTranslation('March', 'Marz', 'de');
  250. $this->assertTranslation('April', 'April_2', 'de');
  251. $this->assertTranslation('May', 'Mai_customized', 'de');
  252. $this->assertTranslation('June', 'Juni', 'de');
  253. $this->assertTranslation('Monday', 'Montag', 'de');
  254. }
  255. /**
  256. * Tests automatic translation import when a module is enabled.
  257. */
  258. function testEnableUninstallModule() {
  259. // Make the hidden test modules look like a normal custom module.
  260. variable_set('l10n_update_test_system_info_alter', TRUE);
  261. // Check if there is no translation yet.
  262. $this->assertTranslation('Tuesday', '', 'de');
  263. // Enable a module.
  264. $edit = array(
  265. 'modules[Testing][l10n_update_test_translate][enable]' => '1',
  266. );
  267. $this->drupalPost('admin/modules', $edit, t('Save configuration'));
  268. // Check if translations have been imported.
  269. $this->assertRaw(t('One translation file imported. %number translations were added, %update translations were updated and %delete translations were removed.',
  270. array('%number' => 0, '%update' => 7, '%delete' => 0)), 'One translation file imported.');
  271. $this->assertTranslation('Tuesday', 'Dienstag', 'de');
  272. // // Disable and uninstall a module
  273. // module_disable(array('l10n_update_test_translate'));
  274. // $edit = array(
  275. // 'uninstall[l10n_update_test_translate]' => '1',
  276. // );
  277. // $this->drupalPost('admin/modules/uninstall', $edit, t('Uninstall'));
  278. // $this->drupalPost(NULL, array(), t('Uninstall'));
  279. //
  280. // // Check if the file data is removed from the database.
  281. // $history = l10n_update_get_file_history();
  282. // $this->assertFalse(isset($history['l10n_update_test_translate']), 'Project removed from the file history');
  283. // $projects = l10n_update_get_projects();
  284. // $this->assertFalse(isset($projects['l10n_update_test_translate']), 'Project removed from the project list');
  285. }
  286. /**
  287. * Tests automatic translation import when a langauge is enabled.
  288. *
  289. * When a language is added, the system will check for translations files of
  290. * enabled modules and will import them. When a language is removed the system
  291. * will remove all translations of that langugue from the database.
  292. */
  293. function testEnableLanguage() {
  294. // Make the hidden test modules look like a normal custom module.
  295. variable_set('l10n_update_test_system_info_alter', TRUE);
  296. // Enable a module.
  297. $edit = array(
  298. 'modules[Testing][l10n_update_test_translate][enable]' => '1',
  299. );
  300. $this->drupalPost('admin/modules', $edit, t('Save configuration'));
  301. // Check if there is no Dutch translation yet.
  302. $this->assertTranslation('Extraday', '', 'nl');
  303. $this->assertTranslation('Tuesday', 'Dienstag', 'de');
  304. // Add a language.
  305. $this->addLanguage('nl');
  306. // Check if the right number of translations are added.
  307. $this->assertRaw(t('One translation file imported. %number translations were added, %update translations were updated and %delete translations were removed.',
  308. array('%number' => 0, '%update' => 8, '%delete' => 0)), 'One language added.');
  309. $this->assertTranslation('Extraday', 'extra dag', 'nl');
  310. // Check if the language data is added to the database.
  311. $result = db_query("SELECT project FROM {l10n_update_file} WHERE language='nl'")->fetchField();
  312. $this->assertTrue((boolean) $result, 'Files removed from file history');
  313. // Remove a language.
  314. $this->drupalPost('admin/config/regional/language/delete/nl', array(), t('Delete'));
  315. // Check if the language data is removed from the database.
  316. $result = db_query("SELECT project FROM {l10n_update_file} WHERE language='nl'")->fetchField();
  317. $this->assertFalse($result, 'Files removed from file history');
  318. // Check that the Dutch translation is gone.
  319. $this->assertTranslation('Extraday', '', 'nl');
  320. $this->assertTranslation('Tuesday', 'Dienstag', 'de');
  321. }
  322. /**
  323. * Tests automatic translation import when a custom langauge is enabled.
  324. */
  325. function testEnableCustomLanguage() {
  326. // Make the hidden test modules look like a normal custom module.
  327. variable_set('l10n_update_test_system_info_alter', TRUE);
  328. // Enable a module.
  329. $edit = array(
  330. 'modules[Testing][l10n_update_test_translate][enable]' => '1',
  331. );
  332. $this->drupalPost('admin/modules', $edit, t('Save configuration'));
  333. // Create and enable a custom language with language code 'xx' and a random
  334. // name.
  335. $langcode = 'xx';
  336. $name = $this->randomName(16);
  337. $edit = array(
  338. 'langcode' => $langcode,
  339. 'name' => $name,
  340. 'native' => $name,
  341. 'prefix' => $langcode,
  342. 'direction' => '0',
  343. );
  344. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
  345. drupal_static_reset('language_list');
  346. $languages = language_list();
  347. $this->assertTrue(isset($languages[$langcode]), format_string('Language %langcode added.', array('%langcode' => $langcode)));
  348. // Ensure the translation file is automatically imported when the language
  349. // was added.
  350. $this->assertText(t('One translation file imported.'), 'Language file automatically imported.');
  351. $this->assertText(t('One translation string was skipped because of disallowed or malformed HTML'), 'Language file automatically imported.');
  352. // Ensure the strings were successfully imported.
  353. $search = array(
  354. 'string' => 'lundi',
  355. 'language' => $langcode,
  356. 'translation' => 'translated',
  357. );
  358. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  359. $this->assertNoText(t('No strings available.'), 'String successfully imported.');
  360. // Ensure the multiline string was imported.
  361. $search = array(
  362. 'string' => 'Source string for multiline translation',
  363. 'language' => $langcode,
  364. 'translation' => 'all',
  365. );
  366. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  367. $this->assertText('Source string for multiline translation', 'String successfully imported.');
  368. // Ensure 'Allowed HTML source string' was imported but the translation for
  369. // 'Another allowed HTML source string' was not because it contains invalid
  370. // HTML.
  371. $search = array(
  372. 'string' => 'HTML source string',
  373. 'language' => $langcode,
  374. 'translation' => 'translated',
  375. );
  376. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  377. // $this->assertText('Allowed HTML source string', 'String successfully imported.');
  378. $this->assertNoText('Another allowed HTML source string', 'String with disallowed translation not imported.');
  379. }
  380. }