DateLibTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. <?php
  2. /* Copyright (C) 2010 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. * or see http://www.gnu.org/
  17. */
  18. /**
  19. * \file test/phpunit/DateLibTest.php
  20. * \ingroup test
  21. * \brief PHPUnit test
  22. * \remarks To run this script as CLI: phpunit filename.php
  23. */
  24. global $conf,$user,$langs,$db;
  25. //define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
  26. //require_once 'PHPUnit/Autoload.php';
  27. require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
  28. require_once dirname(__FILE__).'/../../htdocs/core/lib/date.lib.php';
  29. if (empty($user->id))
  30. {
  31. print "Load permissions for admin user nb 1\n";
  32. $user->fetch(1);
  33. $user->getrights();
  34. }
  35. $conf->global->MAIN_DISABLE_ALL_MAILS=1;
  36. /**
  37. * Class for PHPUnit tests
  38. *
  39. * @backupGlobals disabled
  40. * @backupStaticAttributes enabled
  41. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  42. */
  43. class DateLibTest extends PHPUnit_Framework_TestCase
  44. {
  45. protected $savconf;
  46. protected $savuser;
  47. protected $savlangs;
  48. protected $savdb;
  49. /**
  50. * Constructor
  51. * We save global variables into local variables
  52. *
  53. * @return DateLibTest
  54. */
  55. function __construct()
  56. {
  57. parent::__construct();
  58. //$this->sharedFixture
  59. global $conf,$user,$langs,$db;
  60. $this->savconf=$conf;
  61. $this->savuser=$user;
  62. $this->savlangs=$langs;
  63. $this->savdb=$db;
  64. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  65. //print " - db ".$db->db;
  66. print "\n";
  67. }
  68. // Static methods
  69. public static function setUpBeforeClass()
  70. {
  71. global $conf,$user,$langs,$db;
  72. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  73. print __METHOD__."\n";
  74. }
  75. // tear down after class
  76. public static function tearDownAfterClass()
  77. {
  78. global $conf,$user,$langs,$db;
  79. $db->rollback();
  80. print __METHOD__."\n";
  81. }
  82. /**
  83. * Init phpunit tests
  84. *
  85. * @return void
  86. */
  87. protected function setUp()
  88. {
  89. global $conf,$user,$langs,$db;
  90. $conf=$this->savconf;
  91. $user=$this->savuser;
  92. $langs=$this->savlangs;
  93. $db=$this->savdb;
  94. print __METHOD__."\n";
  95. }
  96. /**
  97. * End phpunit tests
  98. *
  99. * @return void
  100. */
  101. protected function tearDown()
  102. {
  103. print __METHOD__."\n";
  104. }
  105. /**
  106. * testNumBetweenDay
  107. *
  108. * @return void
  109. */
  110. public function testNumBetweenDay()
  111. {
  112. global $conf,$user,$langs,$db;
  113. $conf=$this->savconf;
  114. $user=$this->savuser;
  115. $langs=$this->savlangs;
  116. $db=$this->savdb;
  117. // With same hours
  118. $date1=dol_mktime(0, 0, 0, 1, 1, 2012);
  119. $date2=dol_mktime(0, 0, 0, 1, 2, 2012);
  120. $result=num_between_day($date1,$date2,1);
  121. print __METHOD__." result=".$result."\n";
  122. $this->assertEquals(2,$result);
  123. $result=num_between_day($date1,$date2,0);
  124. print __METHOD__." result=".$result."\n";
  125. $this->assertEquals(1,$result);
  126. // With different hours
  127. $date1=dol_mktime(0, 0, 0, 1, 1, 2012);
  128. $date2=dol_mktime(12, 0, 0, 1, 2, 2012);
  129. $result=num_between_day($date1,$date2,1);
  130. print __METHOD__." result=".$result."\n";
  131. $this->assertEquals(2,$result);
  132. $result=num_between_day($date1,$date2,0);
  133. print __METHOD__." result=".$result."\n";
  134. $this->assertEquals(1,$result);
  135. // With different date before and after sunlight hour (day to change sunlight hour is 2014-03-30)
  136. $date1=dol_mktime(0, 0, 0, 3, 28, 2014, true);
  137. $date2=dol_mktime(0, 0, 0, 3, 31, 2014, true);
  138. $result=num_between_day($date1,$date2,1);
  139. print __METHOD__." result=".$result."\n";
  140. $this->assertEquals(4,$result);
  141. $result=num_between_day($date1,$date2,0);
  142. print __METHOD__." result=".$result."\n";
  143. $this->assertEquals(3,$result);
  144. return $result;
  145. }
  146. /**
  147. * testNumPublicHoliday
  148. *
  149. * @return void
  150. */
  151. public function testNumPublicHoliday()
  152. {
  153. global $conf,$user,$langs,$db;
  154. $conf=$this->savconf;
  155. $user=$this->savuser;
  156. $langs=$this->savlangs;
  157. $db=$this->savdb;
  158. // With same hours - Tuesday/Wednesday jan 2013
  159. $date1=dol_mktime(0, 0, 0, 1, 1, 2013);
  160. $date2=dol_mktime(0, 0, 0, 1, 2, 2013);
  161. $result=num_public_holiday($date1,$date2,'FR',1);
  162. print __METHOD__." result=".$result."\n";
  163. $this->assertEquals(1,$result,'NumPublicHoliday for Tuesday/Wednesday jan 2013 for FR'); // 1 closed days
  164. $result=num_public_holiday($date1,$date2,'XX',1);
  165. print __METHOD__." result=".$result."\n";
  166. $this->assertEquals(0,$result,'NumPublicHoliday for Tuesday/Wednesday jan 2013 for XX'); // no closed days (country unknown)
  167. // With same hours - Friday/Sunday jan 2013
  168. $date1=dol_mktime(0, 0, 0, 1, 4, 2013);
  169. $date2=dol_mktime(0, 0, 0, 1, 6, 2013);
  170. $result=num_public_holiday($date1,$date2,'FR',1);
  171. print __METHOD__." result=".$result."\n";
  172. $this->assertEquals(2,$result,'NumPublicHoliday for FR'); // 1 opened day, 2 closed days
  173. $result=num_public_holiday($date1,$date2,'XX',1);
  174. print __METHOD__." result=".$result."\n";
  175. $this->assertEquals(2,$result,'NumPublicHoliday for XX'); // 1 opened day, 2 closed days (even if country unknown)
  176. }
  177. /**
  178. * testNumOpenDay
  179. *
  180. * @return void
  181. */
  182. public function testNumOpenDay()
  183. {
  184. global $conf,$user,$langs,$db;
  185. $conf=$this->savconf;
  186. $user=$this->savuser;
  187. $langs=$this->savlangs;
  188. $db=$this->savdb;
  189. // With same hours - Tuesday/Wednesday jan 2013
  190. $date1=dol_mktime(0, 0, 0, 1, 1, 2013);
  191. $date2=dol_mktime(0, 0, 0, 1, 2, 2013);
  192. $result=num_open_day($date1,$date2,0,1,0,'FR');
  193. print __METHOD__." result=".$result."\n";
  194. $this->assertEquals(1,$result,'NumOpenDay Tuesday/Wednesday jan 2013 for FR'); // 1 opened days
  195. $result=num_open_day($date1,$date2,0,1,0,'XX');
  196. print __METHOD__." result=".$result."\n";
  197. $this->assertEquals(2,$result,'NumOpenDay Tuesday/Wednesday jan 2013 for XX'); // 2 opened days (country unknown)
  198. // With same hours - Friday/Sunday jan 2013
  199. $date1=dol_mktime(0, 0, 0, 1, 4, 2013);
  200. $date2=dol_mktime(0, 0, 0, 1, 6, 2013);
  201. $result=num_open_day($date1,$date2,0,1,0,'FR');
  202. print __METHOD__." result=".$result."\n";
  203. $this->assertEquals(1,$result,'NumOpenDay for FR'); // 1 opened day, 2 closed
  204. $result=num_open_day($date1,$date2,'XX',1);
  205. print __METHOD__." result=".$result."\n";
  206. $this->assertEquals(1,$result,'NumOpenDay for XX'); // 1 opened day, 2 closes (even if country unknown)
  207. }
  208. /**
  209. * testConvertTime2Seconds
  210. *
  211. * @return void
  212. */
  213. public function testConvertTime2Seconds()
  214. {
  215. global $conf,$user,$langs,$db;
  216. $conf=$this->savconf;
  217. $user=$this->savuser;
  218. $langs=$this->savlangs;
  219. $db=$this->savdb;
  220. $result=convertTime2Seconds(1,1,2);
  221. print __METHOD__." result=".$result."\n";
  222. $this->assertEquals(3662,$result);
  223. return $result;
  224. }
  225. /**
  226. * testConvertSecondToTime
  227. *
  228. * @return void
  229. */
  230. public function testConvertSecondToTime()
  231. {
  232. global $conf,$user,$langs,$db;
  233. $conf=$this->savconf;
  234. $user=$this->savuser;
  235. $langs=$this->savlangs;
  236. $db=$this->savdb;
  237. $result=convertSecondToTime(0,'all',86400);
  238. print __METHOD__." result=".$result."\n";
  239. $this->assertEquals('0',$result);
  240. $result=convertSecondToTime(86400,'all',86400);
  241. print __METHOD__." result=".$result."\n";
  242. $this->assertSame('1 '.$langs->trans("Day"),$result);
  243. return $result;
  244. }
  245. /**
  246. * testDolPrintDate
  247. *
  248. * @return void
  249. */
  250. public function testDolPrintDate()
  251. {
  252. global $conf,$user,$langs,$db;
  253. $conf=$this->savconf;
  254. $user=$this->savuser;
  255. $langs=$this->savlangs;
  256. $db=$this->savdb;
  257. // Check %Y-%m-%d %H:%M:%S format
  258. $result=dol_print_date(0,'%Y-%m-%d %H:%M:%S',true);
  259. print __METHOD__." result=".$result."\n";
  260. $this->assertEquals('1970-01-01 00:00:00',$result);
  261. // Check %Y-%m-%d %H:%M:%S format
  262. $result=dol_print_date(16725225600,'%Y-%m-%d %H:%M:%S',true); // http://www.epochconverter.com/
  263. print __METHOD__." result=".$result."\n";
  264. $this->assertEquals('2500-01-01 00:00:00',$result);
  265. // Check %Y-%m-%d %H:%M:%S format
  266. $result=dol_print_date(-1830384000,'%Y-%m-%d %H:%M:%S',true); // http://www.epochconverter.com/
  267. print __METHOD__." result=".$result."\n";
  268. $this->assertEquals('1912-01-01 00:00:00',$result); // dol_print_date use TZ (good) but epoch converter does not use it.
  269. // Check %Y-%m-%d %H:%M:%S format
  270. $result=dol_print_date(-11676096000,'%Y-%m-%d %H:%M:%S',true); // http://www.epochconverter.com/
  271. print __METHOD__." result=".$result."\n";
  272. $this->assertEquals('1600-01-01 00:00:00',$result);
  273. // test with negative timezone
  274. $result=dol_print_date(-1,'%Y-%m-%d %H:%M:%S',true); // http://www.epochconverter.com/
  275. print __METHOD__." result=".$result."\n";
  276. $this->assertEquals('1969-12-31 23:59:59',$result);
  277. // Check dayhour format for fr_FR
  278. $outputlangs=new Translate('',$conf);
  279. $outputlangs->setDefaultLang('fr_FR');
  280. $outputlangs->load("main");
  281. $result=dol_print_date(0+24*3600,'dayhour',true,$outputlangs);
  282. print __METHOD__." result=".$result."\n";
  283. $this->assertEquals('02/01/1970 00:00',$result);
  284. // Check %a and %b format for fr_FR
  285. $result=dol_print_date(0,'%a %b %B',true,$outputlangs);
  286. print __METHOD__." result=".$result."\n";
  287. $this->assertEquals('Jeu Jan. Janvier',$result);
  288. // Check day format for en_US
  289. $outputlangs=new Translate('',$conf);
  290. $outputlangs->setDefaultLang('en_US');
  291. $outputlangs->load("main");
  292. $result=dol_print_date(0+24*3600,'day',true,$outputlangs);
  293. print __METHOD__." result=".$result."\n";
  294. $this->assertEquals('01/02/1970',$result);
  295. // Check %a and %b format for en_US
  296. $result=dol_print_date(0,'%a %b %B',true,$outputlangs);
  297. print __METHOD__." result=".$result."\n";
  298. $this->assertEquals('Thu Jan January',$result);
  299. return $result;
  300. }
  301. /**
  302. * testDolTimePlusDuree
  303. *
  304. * @return int
  305. */
  306. public function testDolTimePlusDuree()
  307. {
  308. global $conf,$user,$langs,$db;
  309. $conf=$this->savconf;
  310. $user=$this->savuser;
  311. $langs=$this->savlangs;
  312. $db=$this->savdb;
  313. // Check dayhour format for fr_FR
  314. $outputlangs=new Translate('',$conf);
  315. $outputlangs->setDefaultLang('fr_FR');
  316. $outputlangs->load("main");
  317. $result=dol_print_date(dol_time_plus_duree(dol_time_plus_duree(dol_time_plus_duree(0,1,'m'),1,'y'),1,'d'),'dayhour',true,$outputlangs);
  318. print __METHOD__." result=".$result."\n";
  319. $this->assertEquals('02/02/1971 00:00',$result);
  320. return $result;
  321. }
  322. /**
  323. * testDolStringToTime
  324. *
  325. * @return int
  326. */
  327. public function testDolStringToTime()
  328. {
  329. global $conf,$user,$langs,$db;
  330. $conf=$this->savconf;
  331. $user=$this->savuser;
  332. $langs=$this->savlangs;
  333. $db=$this->savdb;
  334. $stime='19700102';
  335. $result=dol_stringtotime($stime);
  336. print __METHOD__." result=".$result."\n";
  337. $this->assertEquals(86400,$result);
  338. $stime='1970-01-01T02:00:00Z';
  339. $result=dol_stringtotime($stime);
  340. print __METHOD__." result=".$result."\n";
  341. $this->assertEquals(7200,$result);
  342. $stime='1970-01-01 02:00:00';
  343. $result=dol_stringtotime($stime);
  344. print __METHOD__." result=".$result."\n";
  345. $this->assertEquals(7200,$result);
  346. $stime='19700101T020000Z';
  347. $result=dol_stringtotime($stime);
  348. print __METHOD__." result=".$result."\n";
  349. $this->assertEquals(7200,$result);
  350. $stime='19700101020000';
  351. $result=dol_stringtotime($stime);
  352. print __METHOD__." result=".$result."\n";
  353. $this->assertEquals(7200,$result);
  354. return $result;
  355. }
  356. /**
  357. * testDolGetFirstDayWeek
  358. *
  359. * @return int
  360. */
  361. public function testDolGetFirstDayWeek()
  362. {
  363. global $conf;
  364. $day=3; $month=2; $year=2015;
  365. $conf->global->MAIN_START_WEEK = 1; // start on monday
  366. $prev = dol_get_first_day_week($day, $month, $year);
  367. $this->assertEquals(2, (int) $prev['first_day']); // monday for month 2, year 2014 is the 2
  368. $day=3; $month=2; $year=2015;
  369. $conf->global->MAIN_START_WEEK = 0; // start on sunday
  370. $prev = dol_get_first_day_week($day, $month, $year);
  371. $this->assertEquals(1, (int) $prev['first_day']); // sunday for month 2, year 2015 is the 1st
  372. }
  373. }