Fixing timehelper::fromString when input time is invalid.

Tests added and renamed for better consistentancy with other tests.
Closes #5556

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7713 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
mark_story 2008-10-08 17:19:07 +00:00
parent e184838451
commit 5c962accc1
2 changed files with 64 additions and 38 deletions

View file

@ -54,16 +54,19 @@ class TimeHelper extends AppHelper {
* @return int Offset
*/
function serverOffset() {
return date('Z', time());
return date('Z', time());
}
/**
* Returns a UNIX timestamp, given either a UNIX timestamp or a valid strtotime() date string.
*
* @param string $dateString Datetime string
* @param int $userOffset User's offset from GMT (in hours)
* @return string Formatted date string
* @return string Parsed timestamp
*/
function fromString($dateString, $userOffset = null) {
if (empty($dateString)) {
return false;
}
if (is_integer($dateString) || is_numeric($dateString)) {
$date = intval($dateString);
} else {

View file

@ -266,10 +266,10 @@ class TimeTest extends CakeTestCase {
$result = $this->Time->timeAgoInWords(strtotime('-13 months, -5 days'), array('end' => '2 years'));
$this->assertEqual($result, '1 year, 1 month, 5 days ago');
$fourHours = $this->Time->timeAgoInWords(strtotime('-5 days, -2 hours'), array('userOffset' => -4));
$result = $this->Time->timeAgoInWords(strtotime('-5 days, -2 hours'), array('userOffset' => 4));
$this->assertEqual($fourHours, $result);
$this->assertEqual($fourHours, $result);
}
/**
* testRelative method
@ -284,12 +284,12 @@ class TimeTest extends CakeTestCase {
$this->assertEqual($result, '1 week');
}
/**
* testOfNice method
* testNice method
*
* @access public
* @return void
*/
function testOfNice() {
function testNice() {
$time = time() + 2 * DAY;
$this->assertEqual(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
@ -305,13 +305,13 @@ class TimeTest extends CakeTestCase {
$time = null;
$this->assertEqual(date('D, M jS Y, H:i', time()), $this->Time->nice($time));
}
/**
* testOfNiceShort method
/**
* testNiceShort method
*
* @access public
* @return void
*/
function testOfNiceShort() {
function testNiceShort() {
$time = time() + 2 * DAY;
if (date('Y', $time) == date('Y')) {
$this->assertEqual(date('M jS, H:i', $time), $this->Time->niceShort($time));
@ -325,13 +325,13 @@ class TimeTest extends CakeTestCase {
$time = time() - DAY;
$this->assertEqual('Yesterday, '.date('H:i', $time), $this->Time->niceShort($time));
}
/**
* testOfDaysAsSql method
/**
* testDaysAsSql method
*
* @access public
* @return void
*/
function testOfDaysAsSql() {
function testDaysAsSql() {
$begin = time();
$end = time() + DAY;
$field = 'my_field';
@ -339,12 +339,12 @@ class TimeTest extends CakeTestCase {
$this->assertEqual($expected, $this->Time->daysAsSql($begin, $end, $field));
}
/**
* testOfDayAsSql method
* testDayAsSql method
*
* @access public
* @return void
*/
function testOfDayAsSql() {
function testDayAsSql() {
$time = time();
$field = 'my_field';
$expected = '(my_field >= \''.date('Y-m-d', $time).' 00:00:00\') AND (my_field <= \''.date('Y-m-d', $time).' 23:59:59\')';
@ -365,30 +365,30 @@ class TimeTest extends CakeTestCase {
$this->assertEqual(false, $this->Time->toUnix(null));
}
/**
* testOfToAtom method
* testToAtom method
*
* @access public
* @return void
*/
function testOfToAtom() {
function testToAtom() {
$this->assertEqual(date('Y-m-d\TH:i:s\Z'), $this->Time->toAtom(time()));
}
/**
* testOfToRss method
* testToRss method
*
* @access public
* @return void
*/
function testOfToRss() {
function testToRss() {
$this->assertEqual(date('r'), $this->Time->toRss(time()));
}
/**
* testOfFormat method
* testFormat method
*
* @access public
* @return void
*/
function testOfFormat() {
function testFormat() {
$format = 'D-M-Y';
$arr = array(time(), strtotime('+1 days'), strtotime('+1 days'), strtotime('+0 days'));
foreach ($arr as $val) {
@ -404,7 +404,7 @@ class TimeTest extends CakeTestCase {
* @access public
* @return void
*/
function testOfGmt() {
function testGmt() {
$hour = 3;
$min = 4;
$sec = 2;
@ -425,12 +425,12 @@ class TimeTest extends CakeTestCase {
$this->assertEqual($expected, $this->Time->gmt(null));
}
/**
* testOfIsToday method
* testIsToday method
*
* @access public
* @return void
*/
function testOfIsToday() {
function testIsToday() {
$result = $this->Time->isToday('+1 day');
$this->assertFalse($result);
$result = $this->Time->isToday('+1 days');
@ -440,13 +440,13 @@ class TimeTest extends CakeTestCase {
$result = $this->Time->isToday('-1 day');
$this->assertFalse($result);
}
/**
* testOfIsThisWeek method
/**
* testIsThisWeek method
*
* @access public
* @return void
*/
function testOfIsThisWeek() {
function testIsThisWeek() {
switch (date('D')) {
case 'Mon' :
for ($i = 0; $i < 6; $i++) {
@ -499,13 +499,13 @@ class TimeTest extends CakeTestCase {
break;
}
}
/**
* testOfIsThisMonth method
/**
* testIsThisMonth method
*
* @access public
* @return void
*/
function testOfIsThisMonth() {
function testIsThisMonth() {
$result = $this->Time->isThisMonth('+0 day');
$this->assertTrue($result);
$result = $this->Time->isThisMonth($time = mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y')));
@ -516,25 +516,25 @@ class TimeTest extends CakeTestCase {
$this->assertFalse($result);
}
/**
* testOfIsThisYear method
/**
* testIsThisYear method
*
* @access public
* @return void
*/
function testOfIsThisYear() {
function testIsThisYear() {
$result = $this->Time->isThisYear('+0 day');
$this->assertTrue($result);
$result = $this->Time->isThisYear(mktime(0, 0, 0, mt_rand(1, 12), mt_rand(1, 28), date('Y')));
$this->assertTrue($result);
}
/**
* testOfWasYesterday method
* testWasYesterday method
*
* @access public
* @return void
*/
function testOfWasYesterday() {
function testWasYesterday() {
$result = $this->Time->wasYesterday('+1 day');
$this->assertFalse($result);
$result = $this->Time->wasYesterday('+1 days');
@ -549,12 +549,12 @@ class TimeTest extends CakeTestCase {
$this->assertFalse($result);
}
/**
* testOfIsTomorrow method
* testIsTomorrow method
*
* @access public
* @return void
*/
function testOfIsTomorrow() {
function testIsTomorrow() {
$result = $this->Time->isTomorrow('+1 day');
$this->assertTrue($result);
$result = $this->Time->isTomorrow('+1 days');
@ -565,12 +565,12 @@ class TimeTest extends CakeTestCase {
$this->assertFalse($result);
}
/**
* testOfWasWithinLast method
* testWasWithinLast method
*
* @access public
* @return void
*/
function testOfWasWithinLast() {
function testWasWithinLast() {
$this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
$this->assertTrue($this->Time->wasWithinLast('1 week', '-1 week'));
$this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
@ -624,6 +624,29 @@ class TimeTest extends CakeTestCase {
$result = $this->Time->fromString(time(), $yourTimezone);
$this->assertEqual($result, $expected);
}
/**
* test fromString()
*
* @access public
* @return void
*/
function testFromString() {
$result = $this->Time->fromString('');
$this->assertFalse($result);
$result = $this->Time->fromString(0, 0);
$this->assertFalse($result);
$result = $this->Time->fromString('+1 hour');
$expected = strtotime('+1 hour');
$this->assertEqual($result, $expected);
$timezone = date('Z', time());
$result = $this->Time->fromString('+1 hour', $timezone);
$expected = $this->Time->convert(strtotime('+1 hour'), $timezone);
$this->assertEqual($result, $expected);
}
/**
* tearDown method
*