From 618b932eafae719fe2d7c87ac32a9b3a0263c4ff Mon Sep 17 00:00:00 2001
From: Matias E. Fernandez <matias.fernandez@cando-image.com>
Date: Tue, 31 May 2011 13:32:23 +0200
Subject: [PATCH] Issue #981524: Save localized date formats.

---
 modules/system/system.module |    2 +-
 modules/system/system.test   |   70 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 70 insertions(+), 2 deletions(-)

diff --git a/modules/system/system.module b/modules/system/system.module
index 5af9ad4..be374b2 100644
--- a/modules/system/system.module
+++ b/modules/system/system.module
@@ -3862,7 +3862,7 @@ function system_date_format_save($date_format, $dfid = 0) {
   if (!empty($date_format['locales'])) {
     foreach ($date_format['locales'] as $langcode) {
       // Only proceed if language is enabled.
-      if (in_array($langcode, $languages)) {
+      if (isset($languages[$langcode])) {
         $is_existing = (bool) db_query_range('SELECT 1 FROM {date_format_locale} WHERE type = :type AND language = :language', 0, 1, array(':type' => $date_format['type'], ':language' => $langcode))->fetchField();
         if (!$is_existing) {
           $locale_format['language'] = $langcode;
diff --git a/modules/system/system.test b/modules/system/system.test
index be4e366..1222f18 100644
--- a/modules/system/system.test
+++ b/modules/system/system.test
@@ -939,7 +939,7 @@ class DateTimeFunctionalTest extends DrupalWebTestCase {
   }
 
   function setUp() {
-    parent::setUp();
+    parent::setUp(array('locale'));
 
     // Create admin user and log in admin user.
     $this->admin_user = $this->drupalCreateUser(array('administer site configuration'));
@@ -1048,6 +1048,74 @@ class DateTimeFunctionalTest extends DrupalWebTestCase {
     $this->assertEqual($this->getUrl(), url('admin/config/regional/date-time/formats', array('absolute' => TRUE)), t('Correct page redirection.'));
     $this->assertText(t('Removed date format'), 'Custom date format removed successfully.');
   }
+  /**
+   * Test if the date formats are stored properly.
+   */
+  function testDateFormatStorage() {
+    $date_format = array(
+      'type' => 'short',
+      'format' => 'dmYHis',
+      'locked' => 0,
+      'is_new' => 1,
+    );
+    system_date_format_save($date_format);
+
+    $format = db_select('date_formats', 'df')
+      ->fields('df', array('format'))
+      ->condition('type', 'short')
+      ->condition('format', 'dmYHis')
+      ->execute()
+      ->fetchField();
+    $this->verbose($format);
+    $this->assertEqual('dmYHis', $format, 'Unlocalized date format resides in general table.');
+
+    $format = db_select('date_format_locale', 'dfl')
+      ->fields('dfl', array('format'))
+      ->condition('type', 'short')
+      ->condition('format', 'dmYHis')
+      ->execute()
+      ->fetchField();
+    $this->assertFalse($format, 'Unlocalized date format resides not in localized table.');
+
+    // Enable German language
+    locale_add_language('de', NULL, NULL, LANGUAGE_LTR, '', '', TRUE, 'en');
+
+    $date_format = array(
+      'type' => 'short',
+      'format' => 'YMDHis',
+      'locales' => array('de', 'tr'),
+      'locked' => 0,
+      'is_new' => 1,
+    );
+    system_date_format_save($date_format);
+
+    $format = db_select('date_format_locale', 'dfl')
+      ->fields('dfl', array('format'))
+      ->condition('type', 'short')
+      ->condition('format', 'YMDHis')
+      ->condition('language', 'de')
+      ->execute()
+      ->fetchField();
+    $this->assertEqual('YMDHis', $format, 'Localized date format resides in localized table.');
+
+    $format = db_select('date_formats', 'df')
+      ->fields('df', array('format'))
+      ->condition('type', 'short')
+      ->condition('format', 'YMDHis')
+      ->execute()
+      ->fetchField();
+    $this->assertEqual('YMDHis', $format, 'Localized date format resides in general table too.');
+
+    $format = db_select('date_format_locale', 'dfl')
+      ->fields('dfl', array('format'))
+      ->condition('type', 'short')
+      ->condition('format', 'YMDHis')
+      ->condition('language', 'tr')
+      ->execute()
+      ->fetchColumn();
+    $this->assertFalse($format, 'Localized date format for disabled language is ignored.');
+  }
+
 }
 
 class PageTitleFiltering extends DrupalWebTestCase {
-- 
1.7.5.1

