#353883 by Damien Tournoud: extends the locale importation test.

From: Damien Tournoud <damien@tournoud.net>


---

 modules/locale/locale.test |   73 ++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 66 insertions(+), 7 deletions(-)


diff --git modules/locale/locale.test modules/locale/locale.test
index 623a7ed..cdb0013 100644
--- modules/locale/locale.test
+++ modules/locale/locale.test
@@ -201,6 +201,12 @@ class LocaleImportFunctionalTest extends DrupalWebTestCase {
    * Test importation of standalone .po files.
    */
   function testStandalonePoFile() {
+    // Include locale.inc to load LOCALE_IMPORT_* constants.
+    require_once DRUPAL_ROOT . '/includes/locale.inc';
+
+    // Disable the locale cache so that we can track strings.
+    variable_set('locale_cache_strings', 0);
+
     // Try importing a .po file.
     $name = tempnam(file_directory_temp(), "po_");
     file_put_contents($name, $this->getPoFile());
@@ -209,14 +215,64 @@ class LocaleImportFunctionalTest extends DrupalWebTestCase {
       'files[file]' => $name,
     ), t('Import'));
     unlink($name);
+    // Empty the static cache.
+    locale(NULL, NULL, TRUE);
 
-    // The importation should automatically create the corresponding language.
+    // The importation should automatically create the corresponding language,
+    // and the importation should have created 8 strings.
     $this->assertRaw(t('The language %language has been created.', array('%language' => 'French')), t('The language has been automatically created'));
+    $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' => 8, '%update' => 0, '%delete' => 0)), t('The translation file was successfully imported'));
+    $this->assertEqual(t('Monday', array(), 'fr'), 'lundi', t('The translation of %original in French is %translation.', array('%original' => 'Monday', '%translation' => 'lundi')));
 
-    // The importation should have create 7 strings.
-    $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' => 7, '%update' => 0, '%delete' => 0)), t('The translation file was successfully imported'));
+    // Now try to import a .po file only different by one string in KEEP mode.
+    $name = tempnam(file_directory_temp(), "po_");
+    file_put_contents($name, $this->getPoFile("Test string translation2"));
+    $this->drupalPost('admin/build/translate/import', array(
+      'langcode' => 'fr',
+      'files[file]' => $name,
+      'mode' => LOCALE_IMPORT_KEEP,
+    ), t('Import'));
+    unlink($name);
+    // Empty the static cache.
+    locale(NULL, NULL, TRUE);
+
+    // The importation should have not imported any strings.
+    $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' => 0)), t('The translation file was successfully imported'));
+    $this->assertEqual(t('Test string', array(), 'fr'), 'Test string translation', t("The translation of a modified term haven't changed when importing in LOCALE_IMPORT_KEEP mode"));
 
-    // Try importing a .po file with script.
+    // Now try to import a .po file only different by one string in OVERWRITE mode.
+    $name = tempnam(file_directory_temp(), "po_");
+    file_put_contents($name, $this->getPoFile("Test string translation2"));
+    $this->drupalPost('admin/build/translate/import', array(
+      'langcode' => 'fr',
+      'files[file]' => $name,
+      'mode' => LOCALE_IMPORT_OVERWRITE,
+    ), t('Import'));
+    unlink($name);
+    // Empty the static cache.
+    locale(NULL, NULL, TRUE);
+
+    // The importation should have updated 8 strings.
+    $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' => 8, '%delete' => 0)), t('The translation file was successfully imported'));
+    $this->assertEqual(t('Test string', array(), 'fr'), 'Test string translation2', t("The translation of a modified term have changed when importing in LOCALE_IMPORT_OVERWRITE mode"));
+
+    // Now try to remove a translation in OVERWRITE mode.
+    $name = tempnam(file_directory_temp(), "po_");
+    file_put_contents($name, $this->getPoFile(""));
+    $this->drupalPost('admin/build/translate/import', array(
+      'langcode' => 'fr',
+      'files[file]' => $name,
+      'mode' => LOCALE_IMPORT_OVERWRITE,
+    ), t('Import'));
+    unlink($name);
+    // Empty the static cache.
+    locale(NULL, NULL, TRUE);
+
+    // The importation should have updated 8 strings and deleted one.
+    $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' => 7, '%delete' => 1)), t('The translation file was successfully imported'));
+    $this->assertEqual(t('Test string', array(), 'fr'), 'Test string', t("The translation of a removed term have been removed in LOCALE_IMPORT_OVERWRITE mode"));
+
+    // Now, try importing a .po file containing bad HTML.
     $name = tempnam(file_directory_temp(), "po_");
     file_put_contents($name, $this->getBadPoFile());
     $this->drupalPost('admin/build/translate/import', array(
@@ -224,17 +280,17 @@ class LocaleImportFunctionalTest extends DrupalWebTestCase {
       'files[file]' => $name,
     ), t('Import'));
     unlink($name);
+
     // The importation should have created 1 string and rejected 2.
     $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.'));
     $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.');
     $this->assertRaw($skip_message, t('Unsafe strings were skipped.'));
-
   }
 
   /**
    * Helper function that returns a proper .po file.
    */
-  function getPoFile() {
+  function getPoFile($test_string = "Test string translation") {
     return <<< EOF
 msgid ""
 msgstr ""
@@ -264,11 +320,14 @@ msgstr "samedi"
 
 msgid "Sunday"
 msgstr "dimanche"
+
+msgid "Test string"
+msgstr "$test_string"
 EOF;
   }
 
   /**
-   * Helper function that returns a proper .po file.
+   * Helper function that returns an invalid .po file.
    */
   function getBadPoFile() {
     return <<< EOF
