PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Core/Libs/Test/Case/View/Helper/InfiniTimeHelperTest.php

http://github.com/infinitas/infinitas
PHP | 247 lines | 128 code | 48 blank | 71 comment | 0 complexity | a8be514ca07c39670efcd435506117a3 MD5 | raw file
  1. <?php
  2. App::uses('CakeSession', 'Model/Datasource');
  3. App::uses('Controller', 'Controller');
  4. App::uses('Helper', 'View');
  5. App::uses('AppHelper', 'View/Helper');
  6. App::uses('InfiniTimeHelper', 'Libs.View/Helper');
  7. /**
  8. * TheHtmlTestController class
  9. *
  10. * @package Cake.Test.Case.View.Helper
  11. */
  12. class TheInfiniTimeTestController extends Controller {
  13. /**
  14. * name property
  15. *
  16. * @var string 'TheTest'
  17. */
  18. public $name = 'TheTest';
  19. /**
  20. * uses property
  21. *
  22. * @var mixed null
  23. */
  24. public $uses = null;
  25. }
  26. class TestInfiniTimeHelper extends CakeTestCase {
  27. /**
  28. * Contains a list of test methods to run
  29. *
  30. * If it is set to false all the methods will run. Otherwise pass in an array
  31. * with a list of tests to run.
  32. *
  33. * @var mixed
  34. */
  35. public $tests = false;
  36. /**
  37. * Contains the backup of the system timezone
  38. *
  39. * @var string
  40. */
  41. private $__timeZoneBackup = null;
  42. /**
  43. * setUp method
  44. *
  45. * @return void
  46. */
  47. public function setUp() {
  48. parent::setUp();
  49. $this->skipif (true);
  50. $this->View = $this->getMock('View', array('append'), array(new TheInfiniTimeTestController()));
  51. $this->InfiniTime = new InfiniTimeHelper($this->View);
  52. $this->InfiniTime->request = new CakeRequest(null, false);
  53. $this->InfiniTime->request->webroot = '';
  54. Configure::write('Asset.timestamp', false);
  55. $this->__timeZoneBackup = date_default_timezone_get();
  56. date_default_timezone_set('UTC');
  57. }
  58. /**
  59. * tearDown method
  60. *
  61. * @return void
  62. */
  63. public function tearDown() {
  64. date_default_timezone_set($this->__timeZoneBackup);
  65. parent::tearDown();
  66. unset($this->InfiniTime);
  67. }
  68. /**
  69. * Tests fromString
  70. *
  71. * @test Enter description here
  72. */
  73. public function testFromString() {
  74. CakeSession::write('Auth.User.time_zone', 'Europe/London');
  75. $result = $this->InfiniTime->fromString('');
  76. $this->assertFalse($result);
  77. $result = $this->InfiniTime->fromString(0);
  78. $this->assertFalse($result);
  79. $result = $this->InfiniTime->fromString('2007-09-10 18:00:00');
  80. $expected = strtotime('2007-09-10 18:00:00') + 3600;
  81. $this->assertEqual($result, $expected);
  82. //Test overwriting the timezone offset
  83. $result = $this->InfiniTime->fromString('2007-09-10 18:00:00', 5);
  84. $expected = strtotime('2007-09-10 18:00:00') + (3600 * 5);
  85. $this->assertEqual($result, $expected);
  86. }
  87. /**
  88. * Tests nice strings
  89. *
  90. * @test Enter description here
  91. */
  92. public function testNiceStrings() {
  93. /**
  94. * Test with non UTC server timezone
  95. */
  96. date_default_timezone_set('Europe/London');
  97. $dateString = '2011-08-17 16:39:00';
  98. CakeSession::write('Auth.User.time_zone', 'Europe/London');
  99. $this->assertEqual('Wed, Aug 17th 2011, 16:39', $this->InfiniTime->nice($dateString));
  100. CakeSession::write('Auth.User.time_zone', 'Europe/Brussels');
  101. $this->assertEqual('Wed, Aug 17th 2011, 17:39', $this->InfiniTime->nice($dateString));
  102. /**
  103. * Test with UTC server timezone
  104. */
  105. date_default_timezone_set('UTC');
  106. //Test timezones in DST
  107. $dateString = '2011-08-17 16:00:00';
  108. CakeSession::write('Auth.User.time_zone', 'Europe/London');
  109. $this->assertEqual('Wed, Aug 17th 2011, 17:00', $this->InfiniTime->nice($dateString));
  110. CakeSession::write('Auth.User.time_zone', 'Europe/Brussels');
  111. $this->assertEqual('Wed, Aug 17th 2011, 18:00', $this->InfiniTime->nice($dateString));
  112. CakeSession::write('Auth.User.time_zone', 'America/Mexico_City');
  113. $this->assertEqual('Wed, Aug 17th 2011, 11:00', $this->InfiniTime->nice($dateString));
  114. //Test timezones currently not in DST
  115. $dateString = '2011-01-10 12:00:00';
  116. CakeSession::write('Auth.User.time_zone', 'Europe/London');
  117. $this->assertEqual('Mon, Jan 10th 2011, 12:00', $this->InfiniTime->nice($dateString));
  118. CakeSession::write('Auth.User.time_zone', 'Europe/Brussels');
  119. $this->assertEqual('Mon, Jan 10th 2011, 13:00', $this->InfiniTime->nice($dateString));
  120. CakeSession::write('Auth.User.time_zone', 'America/Mexico_City');
  121. $this->assertEqual('Mon, Jan 10th 2011, 06:00', $this->InfiniTime->nice($dateString));
  122. //Test a non hour offset
  123. CakeSession::write('Auth.User.time_zone', 'America/Caracas');
  124. $this->assertEqual('Mon, Jan 10th 2011, 07:30', $this->InfiniTime->nice($dateString));
  125. CakeSession::write('Auth.User.time_zone', 'Europe/London');
  126. $this->assertEqual('Jul 17th 2004, 17:00', $this->InfiniTime->niceShort('2004-07-17 16:00:00'));
  127. }
  128. /**
  129. * test relative time functions
  130. */
  131. public function testRelativeTimeFunctions() {
  132. CakeSession::write('Auth.User.time_zone', 'America/New_York');
  133. $this->assertFalse($this->InfiniTime->isToday('01:00:00'));
  134. $this->assertTrue($this->InfiniTime->isToday('10:00:00'));
  135. $monday = date('Y-m-d', strtotime('-' . (date('N')-1) . 'DAY'));
  136. $this->assertFalse($this->InfiniTime->isThisWeek($monday . ' 01:00:00'));
  137. $this->assertTrue($this->InfiniTime->isThisWeek($monday . ' 10:00:00'));
  138. $firstDayOfMonth = date('Y-m-d', mktime(date('H'), date('i') , date('s'), date('n'), 1, date('Y')));
  139. $this->assertFalse($this->InfiniTime->isThisMonth($firstDayOfMonth . ' 01:00:00'));
  140. $this->assertTrue($this->InfiniTime->isThisMonth($firstDayOfMonth . ' 10:00:00'));
  141. $firstDayOfYear = date('Y-m-d', mktime(date('H'), date('i') , date('s'), 1, 1, date('Y')));
  142. $this->assertFalse($this->InfiniTime->isThisYear($firstDayOfYear . ' 01:00:00'));
  143. $this->assertTrue($this->InfiniTime->isThisYear($firstDayOfYear . ' 10:00:00'));
  144. $this->assertTrue($this->InfiniTime->wasYesterday('01:00:00'));
  145. $this->assertFalse($this->InfiniTime->wasYesterday('10:00:00'));
  146. $this->assertFalse($this->InfiniTime->isTomorrow(date('Y-m-d', strtotime('+1 DAY')) . ' 01:00:00'));
  147. $this->assertTrue($this->InfiniTime->isTomorrow(date('Y-m-d', strtotime('+1 DAY')) . ' 10:00:00'));
  148. return; // @todo fix-tests
  149. CakeSession::write('Auth.User.time_zone', 'Europe/Brussels');
  150. $result = $this->InfiniTime->relativeTime('1998-05-20 10:00:00', array('format' => 'Y-m-d H:i:s'));
  151. $this->assertEqual('on 1998-05-20 12:00:00', $result);
  152. }
  153. /**
  154. * test formatting
  155. */
  156. public function testOutputFormats() {
  157. CakeSession::write('Auth.User.time_zone', 'America/New_York');
  158. $timestamp = $this->InfiniTime->toUnix('2011-08-18 10:42:00');
  159. $this->assertEqual('2011-08-18 06:42:00', date('Y-m-d H:i:s', $timestamp));
  160. $this->assertEqual('2011-08-18T06:42:00Z', $this->InfiniTime->toAtom('2011-08-18 10:42:00'));
  161. $this->assertEqual('Thu, 18 Aug 2011 06:42:00 -0400', $this->InfiniTime->toRSS('2011-08-18 10:42:00'));
  162. CakeSession::write('Auth.User.time_zone', 'Europe/London');
  163. $this->assertEqual('Thu, 18 Aug 2011 11:42:00 +0100', $this->InfiniTime->toRSS('2011-08-18 10:42:00'));
  164. $this->assertEqual('Thu, 20 Jan 2011 18:00:15 +0000', $this->InfiniTime->toRSS('2011-01-20 18:00:15'));
  165. CakeSession::write('Auth.User.time_zone', 'Europe/Brussels');
  166. $this->assertEqual('Thu, 18 Aug 2011 12:42:00 +0200', $this->InfiniTime->toRSS('2011-08-18 10:42:00'));
  167. }
  168. /**
  169. * Tests timeAgoInWords
  170. */
  171. public function testTimeAgoInWord() {
  172. CakeSession::write('Auth.User.time_zone', 'Europe/Brussels');
  173. $result = $this->InfiniTime->timeAgoInWords(strtotime('-2 hours'));
  174. $this->AssertEqual('2 hours ago', $result);
  175. $result = $this->InfiniTime->timeAgoInWords('2010-05-10 10:00:00', array('format' => 'Y-m-d H:i:s'));
  176. $this->assertEqual('on 2010-05-10 12:00:00', $result);
  177. $result = $this->InfiniTime->timeAgoInWords(strtotime('1995-01-10 10:00:00'), array('format' => 'Y-m-d H:i:s'));
  178. $this->assertEqual('on 1995-01-10 11:00:00', $result);
  179. //Test overwriting the offset
  180. $result = $this->InfiniTime->timeAgoInWords(strtotime('1995-01-10 10:00:00'), array('format' => 'Y-m-d H:i:s', 'userOffset' => 5));
  181. $this->assertEqual('on 1995-01-10 15:00:00', $result);
  182. }
  183. /**
  184. * test other methods
  185. */
  186. public function testVarious() {
  187. CakeSession::write('Auth.User.time_zone', 'Europe/London');
  188. $result = $this->InfiniTime->dayAsSql('2009-10-30', 'Item.created');
  189. $this->assertEquals("(Item.created >= '2009-10-30 00:00:00') AND (Item.created <= '2009-10-30 23:59:59')", $result);
  190. $result = $this->InfiniTime->format('Y-m-d H:i:s', '2010-10-24 14:15:10');
  191. $this->assertEquals('2010-10-24 15:15:10', $result);
  192. $this->assertTrue($this->InfiniTime->wasWithinLast('5 hours', strtotime('-2 hours')));
  193. CakeSession::write('Auth.User.time_zone', 'Europe/Brussels');
  194. setlocale(LC_TIME, 'nl_NL');
  195. $result = $this->InfiniTime->i18nFormat('2010-11-15 15:14:00', '%A, %e %B %Y %H:%M');
  196. $this->assertEquals('maandag, 15 november 2010 16:14', $result);
  197. setlocale(LC_TIME, NULL);
  198. }
  199. }