This patch was originally inside of a really large patch, was suggested to split into smaller patches.

Tests needed: common.inc
http://drupal.org/node/276272

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

grndlvl’s picture

Priority: Normal » Critical

Just getting same priority as parent task.

Damien Tournoud’s picture

Status: Needs review » Needs work
+  /**
+   * Helper function: asserts that format_date() creates the same formated
+   * string as gmdate() does.
+   */

This is not an unit test: a proper unit test assert that the result of a function call is identical to the known expected behavior. Comparing the result of format_date() (that uses gmdate() internally) to the result of gmdate() has no real purpose.

What you should do here is prepare an test case of some specific date values and their known text representations, and use that to verify that format_date() works correctly. Including some known corner-case values is of course better (leap years, days with leap seconds, negative timestamp values, etc.).

grndlvl’s picture

I do not believe so that would be going through and testing out what gmdate() does internally to drupal, this is to check the 'Format of the date' based on drupal parameters such as localized time.

Damien Tournoud’s picture

I do not believe so that would be going through and testing out what gmdate() does internally to drupal, this is to check the 'Format of the date' based on drupal parameters such as localized time.

This makes even less sense. In a nutshell you are currently doing (simplified version of your assertFormatDate):

  // Get the current system time and add the time zone.
  $localized_time = $time + $time_zone;
  $date = gmdate($format, $time);
  $drupal_date = format_date($time, $format);

So (1) you are assuming that the current language is english, and (2) you are basically testing only one line of format_date(), the one that does:

  $timestamp += $timezone;
grndlvl’s picture

It is the whole purpose of the function format_date to

Format a date with the given configured format or a custom format string.

Drupal allows administrators to select formatting strings for 'small', 'medium' and 'large' date formats. This function can handle these formats, as well as any custom format.

Which I fell this unit test does test the functionality of format_date.

Also I feel your proposed solution would be the same other than the addition of checking gmmdate as well or am I overlooking something.

What you should do here is prepare an test case of some specific date values and their known text representations, and use that to verify that format_date() works correctly. Including some known corner-case values is of course better (leap years, days with leap seconds, negative timestamp values, etc.).

mfb’s picture

Component: tests » simpletest.module
Status: Needs work » Needs review

Here's a fresh start on format_date() unit tests.

mfb’s picture

FileSize
4.41 KB

drupal.org ate my patch :(

mfb’s picture

FileSize
4.28 KB

Removed some debug code from the assertions.

Dries’s picture

Status: Needs review » Fixed

Committed to CVS HEAD. Thanks.

mfb’s picture

Status: Fixed » Needs review
FileSize
5 KB

Some of these assertions fail if the test environment has spanish language installed. I modified it to create a custom language "xx".

mfb’s picture

FileSize
4.29 KB

Add a test for backslash followed by escaped format string.

jrchamp’s picture

@mfb: Thank you for pointing me in the right direction. That patch looks great and should be reasonably effective in identifying issues with the format_date function.

I think that it might be worthwhile to add an "every special format character" test condition, i.e:

$format = 'F A a e D l M T';

That was the condition that resulted in my adding 0-9, + and : to the regex.

It occurred to me that numbers would be an issue when I visited the "List of Supported Timezones" "Others" page.

mfb’s picture

@jrchamp: There shouldn't be any numbers colons or plus signs because Drupal doesn't allow the "Other" time zones to be configured. As it says, "Please do not use any of the timezones listed here (besides UTC), they only exist for backward compatible reasons." The only way they would show up was someone manually hacking their database :)

jrchamp’s picture

@mfb: Nice catch! It turns out that if you don't call date_timezone_set(), it returns "July PM pm +00:00 Wed Wednesday Jul GMT+0000" instead of "July PM pm UTC Wed Wednesday Jul UTC". Thankfully this shouldn't happen.

Dries’s picture

I committed the patch at #11 because it is an improvement. I'm _not_ marking this fix because we have follow-up patches to make.

Status: Needs review » Needs work

The last submitted patch failed testing.

Dries’s picture

Status: Needs work » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.