Permalink
Browse files

Revert changes in behavior made to timeAgoInWords()

Changes made in [0729aca] changed
the generated text to not match the behavior in 2.1.
  • Loading branch information...
1 parent eb7b66b commit 3f78216e9654d21f7d892931369d51b8c972794c @markstory markstory committed Jun 25, 2012
Showing with 67 additions and 60 deletions.
  1. +63 −50 lib/Cake/Test/Case/Utility/CakeTimeTest.php
  2. +4 −10 lib/Cake/Utility/CakeTime.php
@@ -96,10 +96,10 @@ public function testToQuarter() {
*/
public function testTimeAgoInWords() {
$result = $this->Time->timeAgoInWords('-1 week');
- $this->assertEquals(date('l d/m, H:i', strtotime('-1 week')), $result);
+ $this->assertEquals('1 week ago', $result);
$result = $this->Time->timeAgoInWords('+1 week');
- $this->assertEquals('On ' . date('l d/m, H:i', strtotime('+1 week')), $result);
+ $this->assertEquals('1 week', $result);
$result = $this->Time->timeAgoInWords(
strtotime('+4 months +2 weeks +3 days'),
@@ -156,20 +156,6 @@ public function testTimeAgoInWords() {
$this->assertEquals('2 months, 1 week, 6 days', $result);
$result = $this->Time->timeAgoInWords(
- strtotime('-1 month -1 week -6 days'),
- array('end' => '1 year', 'accuracy' => array('month' => 'month'))
- );
- $this->assertEquals('1 month ago', $result);
-
- $timestamp = strtotime('-1 years -2 weeks -3 days');
- $result = $this->Time->timeAgoInWords(
- $timestamp,
- array('accuracy' => array('year' => 'year'))
- );
- $expected = 'on ' . date('j/n/y', $timestamp);
- $this->assertEquals($expected, $result);
-
- $result = $this->Time->timeAgoInWords(
strtotime('+8 years +4 months +2 weeks +3 days'),
array('accuracy' => array('year' => 'year'), 'end' => '+10 years')
);
@@ -204,25 +190,8 @@ public function testTimeAgoInWords() {
$expected = '1 year';
$this->assertEquals($expected, $result);
- for ($i = 0; $i > -200; $i--) {
- $seconds = round($i + ($i * $i));
- $time = strtotime('+' . $seconds . ' seconds');
- $result = $this->Time->timeAgoInWords($time);
-
- if ($time == time()) {
- $expected = 'just now';
- $this->assertEquals($expected, $result);
- } elseif (date("Y-m-d", $time) == date("Y-m-d")) {
- $expected = 'Today, ' . date("H:i", $time);
- $this->assertEquals($expected, $result);
- }
- }
-
- $result = $this->Time->timeAgoInWords(
- strtotime('-2 years -5 months -2 days'),
- array('end' => '3 years')
- );
- $this->assertEquals('2 years, 5 months, 2 days ago', $result);
+ $result = $this->Time->timeAgoInWords(time());
+ $this->assertEquals('just now', $result);
$result = $this->Time->timeAgoInWords('2007-9-25');
$this->assertEquals('on 25/9/07', $result);
@@ -234,12 +203,6 @@ public function testTimeAgoInWords() {
$this->assertEquals('on 2007-09-25', $result);
$result = $this->Time->timeAgoInWords(
- strtotime('-2 weeks -2 days'),
- 'Y-m-d'
- );
- $this->assertEquals('2 weeks, 2 days ago', $result);
-
- $result = $this->Time->timeAgoInWords(
strtotime('+2 weeks +2 days'),
'Y-m-d'
);
@@ -273,40 +236,90 @@ public function testTimeAgoInWords() {
$this->assertEquals('3 months, 5 days', $result);
$result = $this->Time->timeAgoInWords(
- strtotime('-2 months -2 days'),
+ strtotime('+2 months +2 days'),
array('end' => '3 month')
);
- $this->assertEquals('2 months, 2 days ago', $result);
+ $this->assertRegExp('/2 months/', $result);
$result = $this->Time->timeAgoInWords(
+ strtotime('+2 months +2 days'),
+ array('end' => '1 month', 'format' => 'Y-m-d')
+ );
+ $this->assertEquals('on ' . date('Y-m-d', strtotime('+2 months +2 days')), $result);
+ }
+
+/**
+ * test timeAgoInWords() with negative values.
+ *
+ * @return void
+ */
+ public function testTimeAgoInWordsNegativeValues() {
+ $result = $this->Time->timeAgoInWords(
strtotime('-2 months -2 days'),
array('end' => '3 month')
);
$this->assertEquals('2 months, 2 days ago', $result);
$result = $this->Time->timeAgoInWords(
- strtotime('+2 months +2 days'),
+ strtotime('-2 months -2 days'),
array('end' => '3 month')
);
- $this->assertRegExp('/2 months/', $result);
+ $this->assertEquals('2 months, 2 days ago', $result);
$result = $this->Time->timeAgoInWords(
- strtotime('+2 months +2 days'),
+ strtotime('-2 months -2 days'),
array('end' => '1 month', 'format' => 'Y-m-d')
);
- $this->assertEquals('on ' . date('Y-m-d', strtotime('+2 months +2 days')), $result);
+ $this->assertEquals('on ' . date('Y-m-d', strtotime('-2 months -2 days')), $result);
$result = $this->Time->timeAgoInWords(
- strtotime('-2 months -2 days'),
- array('end' => '1 month', 'format' => 'Y-m-d')
+ strtotime('-2 years -5 months -2 days'),
+ array('end' => '3 years')
);
- $this->assertEquals('on ' . date('Y-m-d', strtotime('-2 months -2 days')), $result);
+ $this->assertEquals('2 years, 5 months, 2 days ago', $result);
+
+ $result = $this->Time->timeAgoInWords(
+ strtotime('-2 weeks -2 days'),
+ 'Y-m-d'
+ );
+ $this->assertEquals('2 weeks, 2 days ago', $result);
+
+ $time = strtotime('-3 years -12 months');
+ $result = $this->Time->timeAgoInWords($time);
+ $expected = 'on ' . date('j/n/y', $time);
+ $this->assertEquals($expected, $result);
+
+ $result = $this->Time->timeAgoInWords(
+ strtotime('-1 month -1 week -6 days'),
+ array('end' => '1 year', 'accuracy' => array('month' => 'month'))
+ );
+ $this->assertEquals('1 month ago', $result);
+
+ $timestamp = strtotime('-1 years -2 weeks -3 days');
+ $result = $this->Time->timeAgoInWords(
+ $timestamp,
+ array('accuracy' => array('year' => 'year'))
+ );
+ $expected = 'on ' . date('j/n/y', $timestamp);
+ $this->assertEquals($expected, $result);
$result = $this->Time->timeAgoInWords(
strtotime('-13 months -5 days'),
array('end' => '2 years')
);
$this->assertEquals('1 year, 1 month, 5 days ago', $result);
+
+ $result = $this->Time->timeAgoInWords(strtotime('-2 hours'));
+ $expected = '2 hours ago';
+ $this->assertEquals($expected, $result);
+
+ $result = $this->Time->timeAgoInWords(strtotime('-12 minutes'));
+ $expected = '12 minutes ago';
+ $this->assertEquals($expected, $result);
+
+ $result = $this->Time->timeAgoInWords(strtotime('-12 seconds'));
+ $expected = '12 seconds ago';
+ $this->assertEquals($expected, $result);
}
/**
@@ -700,10 +700,10 @@ public static function timeAgoInWords($dateTime, $options = array()) {
$accuracy = self::$wordAccuracy;
if (is_array($options)) {
- if (isset($options['userOffset'])) {
- $timezone = $options['userOffset'];
- } elseif (isset($options['timezone'])) {
+ if (isset($options['timezone'])) {
$timezone = $options['timezone'];
+ } elseif (isset($options['userOffset'])) {
+ $timezone = $options['userOffset'];
}
if (isset($options['accuracy'])) {
@@ -718,12 +718,11 @@ public static function timeAgoInWords($dateTime, $options = array()) {
if (isset($options['format'])) {
$format = $options['format'];
- unset($options['format']);
}
if (isset($options['end'])) {
$end = $options['end'];
- unset($options['end']);
}
+ unset($options['end'], $options['format']);
} else {
$format = $options;
}
@@ -840,11 +839,6 @@ public static function timeAgoInWords($dateTime, $options = array()) {
}
}
- // If within the last or next 7 days
- if (self::wasWithinLast('7 days', $dateTime, $timezone) || self::isWithinNext('7 days', $dateTime, $timezone)) {
- $relativeDate = self::niceShort($dateTime , $timezone);
- }
-
// If now
if ($diff == 0) {
$relativeDate = __d('cake', 'just now', 'just now');

0 comments on commit 3f78216

Please sign in to comment.