2012-02-06 14:13:51 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* TimeHelperTest file
|
|
|
|
*
|
2012-04-27 02:49:18 +00:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2012-02-06 14:13:51 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2012-02-06 14:13:51 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2012-04-27 02:49:18 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
2012-02-06 14:13:51 +00:00
|
|
|
* @package Cake.Test.Case.View.Helper
|
|
|
|
* @since CakePHP(tm) v 1.2.0.4206
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2012-02-06 14:13:51 +00:00
|
|
|
*/
|
2013-05-30 22:11:14 +00:00
|
|
|
|
2012-02-06 14:13:51 +00:00
|
|
|
App::uses('TimeHelper', 'View/Helper');
|
|
|
|
App::uses('View', 'View');
|
|
|
|
App::uses('CakeTime', 'Utility');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TimeHelperTestObject class
|
2013-05-30 22:11:14 +00:00
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.View.Helper
|
2012-02-06 14:13:51 +00:00
|
|
|
*/
|
|
|
|
class TimeHelperTestObject extends TimeHelper {
|
|
|
|
|
2012-02-13 02:14:33 +00:00
|
|
|
public function attach(CakeTimeMock $cakeTime) {
|
2012-02-14 16:14:18 +00:00
|
|
|
$this->_engine = $cakeTime;
|
2012-02-06 14:13:51 +00:00
|
|
|
}
|
|
|
|
|
2012-02-14 15:56:48 +00:00
|
|
|
public function engine() {
|
2012-02-14 16:14:18 +00:00
|
|
|
return $this->_engine;
|
2012-02-14 15:56:48 +00:00
|
|
|
}
|
|
|
|
|
2012-02-06 14:13:51 +00:00
|
|
|
}
|
|
|
|
|
2012-02-13 02:14:33 +00:00
|
|
|
/**
|
|
|
|
* CakeTimeMock class
|
2013-05-30 22:11:14 +00:00
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.View.Helper
|
2012-02-13 02:14:33 +00:00
|
|
|
*/
|
|
|
|
class CakeTimeMock {
|
|
|
|
}
|
|
|
|
|
2012-02-06 14:13:51 +00:00
|
|
|
/**
|
|
|
|
* TimeHelperTest class
|
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.View.Helper
|
|
|
|
*/
|
|
|
|
class TimeHelperTest extends CakeTestCase {
|
|
|
|
|
|
|
|
public $Time = null;
|
|
|
|
|
|
|
|
public $CakeTime = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* setUp method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setUp() {
|
2012-02-13 02:14:33 +00:00
|
|
|
parent::setUp();
|
|
|
|
$this->View = new View(null);
|
2012-02-06 14:13:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* tearDown method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function tearDown() {
|
2012-02-13 02:14:33 +00:00
|
|
|
unset($this->View);
|
|
|
|
parent::tearDown();
|
2012-02-06 14:13:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test CakeTime class methods are called correctly
|
|
|
|
*/
|
|
|
|
public function testTimeHelperProxyMethodCalls() {
|
|
|
|
$methods = array(
|
|
|
|
'convertSpecifiers', 'convert', 'serverOffset', 'fromString',
|
|
|
|
'nice', 'niceShort', 'daysAsSql', 'dayAsSql',
|
|
|
|
'isToday', 'isThisMonth', 'isThisYear', 'wasYesterday',
|
|
|
|
'isTomorrow', 'toQuarter', 'toUnix', 'toAtom', 'toRSS',
|
2013-08-08 01:56:58 +00:00
|
|
|
'wasWithinLast', 'gmt', 'format', 'i18nFormat',
|
2012-05-13 01:09:15 +00:00
|
|
|
);
|
2012-02-13 02:14:33 +00:00
|
|
|
$CakeTime = $this->getMock('CakeTimeMock', $methods);
|
|
|
|
$Time = new TimeHelperTestObject($this->View, array('engine' => 'CakeTimeMock'));
|
|
|
|
$Time->attach($CakeTime);
|
2012-02-06 14:13:51 +00:00
|
|
|
foreach ($methods as $method) {
|
2012-02-13 02:14:33 +00:00
|
|
|
$CakeTime->expects($this->at(0))->method($method);
|
|
|
|
$Time->{$method}('who', 'what', 'when', 'where', 'how');
|
2012-02-06 14:13:51 +00:00
|
|
|
}
|
2013-08-08 01:56:58 +00:00
|
|
|
|
|
|
|
$CakeTime = $this->getMock('CakeTimeMock', array('timeAgoInWords'));
|
|
|
|
$Time = new TimeHelperTestObject($this->View, array('engine' => 'CakeTimeMock'));
|
|
|
|
$Time->attach($CakeTime);
|
|
|
|
$CakeTime->expects($this->at(0))->method('timeAgoInWords');
|
|
|
|
$Time->timeAgoInWords('who', array('what'), array('when'), array('where'), array('how'));
|
2012-02-06 14:13:51 +00:00
|
|
|
}
|
|
|
|
|
2012-02-14 15:56:48 +00:00
|
|
|
/**
|
|
|
|
* test engine override
|
|
|
|
*/
|
|
|
|
public function testEngineOverride() {
|
|
|
|
App::build(array(
|
|
|
|
'Utility' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Utility' . DS)
|
2012-02-18 12:04:54 +00:00
|
|
|
), App::REGISTER);
|
2012-02-14 15:56:48 +00:00
|
|
|
$Time = new TimeHelperTestObject($this->View, array('engine' => 'TestAppEngine'));
|
|
|
|
$this->assertInstanceOf('TestAppEngine', $Time->engine());
|
|
|
|
|
|
|
|
App::build(array(
|
|
|
|
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
|
2012-02-18 12:04:54 +00:00
|
|
|
));
|
2012-02-14 15:56:48 +00:00
|
|
|
CakePlugin::load('TestPlugin');
|
|
|
|
$Time = new TimeHelperTestObject($this->View, array('engine' => 'TestPlugin.TestPluginEngine'));
|
|
|
|
$this->assertInstanceOf('TestPluginEngine', $Time->engine());
|
|
|
|
CakePlugin::unload('TestPlugin');
|
|
|
|
}
|
|
|
|
|
2012-05-13 01:09:15 +00:00
|
|
|
/**
|
|
|
|
* Test element wrapping in timeAgoInWords
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testTimeAgoInWords() {
|
|
|
|
$Time = new TimeHelper($this->View);
|
|
|
|
$timestamp = strtotime('+8 years, +4 months +2 weeks +3 days');
|
2012-05-13 01:16:57 +00:00
|
|
|
$result = $Time->timeAgoInWords($timestamp, array(
|
|
|
|
'end' => '1 years',
|
|
|
|
'element' => 'span'
|
|
|
|
));
|
2012-05-13 01:09:15 +00:00
|
|
|
$expected = array(
|
|
|
|
'span' => array(
|
|
|
|
'title' => $timestamp,
|
|
|
|
'class' => 'time-ago-in-words'
|
|
|
|
),
|
|
|
|
'on ' . date('j/n/y', $timestamp),
|
|
|
|
'/span'
|
|
|
|
);
|
|
|
|
$this->assertTags($result, $expected);
|
|
|
|
|
2012-05-13 01:16:57 +00:00
|
|
|
$result = $Time->timeAgoInWords($timestamp, array(
|
|
|
|
'end' => '1 years',
|
|
|
|
'element' => array(
|
|
|
|
'title' => 'testing',
|
|
|
|
'rel' => 'test'
|
|
|
|
)
|
|
|
|
));
|
|
|
|
$expected = array(
|
|
|
|
'span' => array(
|
|
|
|
'title' => 'testing',
|
|
|
|
'class' => 'time-ago-in-words',
|
|
|
|
'rel' => 'test'
|
|
|
|
),
|
|
|
|
'on ' . date('j/n/y', $timestamp),
|
|
|
|
'/span'
|
|
|
|
);
|
|
|
|
$this->assertTags($result, $expected);
|
|
|
|
|
2012-05-13 01:09:15 +00:00
|
|
|
$timestamp = strtotime('+2 weeks');
|
|
|
|
$result = $Time->timeAgoInWords(
|
|
|
|
$timestamp,
|
|
|
|
array('end' => '1 years', 'element' => 'div')
|
|
|
|
);
|
|
|
|
$expected = array(
|
|
|
|
'div' => array(
|
|
|
|
'title' => $timestamp,
|
|
|
|
'class' => 'time-ago-in-words'
|
|
|
|
),
|
|
|
|
'2 weeks',
|
|
|
|
'/div'
|
|
|
|
);
|
|
|
|
$this->assertTags($result, $expected);
|
|
|
|
}
|
|
|
|
|
2012-02-06 14:13:51 +00:00
|
|
|
}
|