DateLibTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. $conf->global->HOLIDAY_MORE_PUBLIC_HOLIDAYS='12-13,2019-12-14';
  177. $date1=dol_mktime(0, 0, 0, 12, 13, 2018);
  178. $date2=dol_mktime(0, 0, 0, 12, 13, 2018);
  179. $result=num_public_holiday($date1,$date2,'YY',1);
  180. print __METHOD__." result=".$result."\n";
  181. $this->assertEquals(1,$result,'NumPublicHoliday for YY the 2018-12-13'); // 0 opened day, 1 closed days (even if country unknown)
  182. $date1=dol_mktime(0, 0, 0, 12, 14, 2018);
  183. $date2=dol_mktime(0, 0, 0, 12, 14, 2018);
  184. $result=num_public_holiday($date1,$date2,'YY',1);
  185. print __METHOD__." result=".$result."\n";
  186. $this->assertEquals(0,$result,'NumPublicHoliday for YY the 2018-12-14'); // 1 opened day, 0 closed days (even if country unknown)
  187. $date1=dol_mktime(0, 0, 0, 12, 14, 2019);
  188. $date2=dol_mktime(0, 0, 0, 12, 14, 2019);
  189. $result=num_public_holiday($date1,$date2,'YY',1);
  190. print __METHOD__." result=".$result."\n";
  191. $this->assertEquals(1,$result,'NumPublicHoliday for YY the 2019-12-14'); // 0 opened day, 1 closed days (even if country unknown)
  192. }
  193. /**
  194. * testNumOpenDay
  195. *
  196. * @return void
  197. */
  198. public function testNumOpenDay()
  199. {
  200. global $conf,$user,$langs,$db;
  201. $conf=$this->savconf;
  202. $user=$this->savuser;
  203. $langs=$this->savlangs;
  204. $db=$this->savdb;
  205. // With same hours - Tuesday/Wednesday jan 2013
  206. $date1=dol_mktime(0, 0, 0, 1, 1, 2013);
  207. $date2=dol_mktime(0, 0, 0, 1, 2, 2013);
  208. $result=num_open_day($date1,$date2,0,1,0,'FR');
  209. print __METHOD__." result=".$result."\n";
  210. $this->assertEquals(1,$result,'NumOpenDay Tuesday/Wednesday jan 2013 for FR'); // 1 opened days
  211. $result=num_open_day($date1,$date2,0,1,0,'XX');
  212. print __METHOD__." result=".$result."\n";
  213. $this->assertEquals(2,$result,'NumOpenDay Tuesday/Wednesday jan 2013 for XX'); // 2 opened days (country unknown)
  214. // With same hours - Friday/Sunday jan 2013
  215. $date1=dol_mktime(0, 0, 0, 1, 4, 2013);
  216. $date2=dol_mktime(0, 0, 0, 1, 6, 2013);
  217. $result=num_open_day($date1,$date2,0,1,0,'FR');
  218. print __METHOD__." result=".$result."\n";
  219. $this->assertEquals(1,$result,'NumOpenDay for FR'); // 1 opened day, 2 closed
  220. $result=num_open_day($date1,$date2,'XX',1);
  221. print __METHOD__." result=".$result."\n";
  222. $this->assertEquals(1,$result,'NumOpenDay for XX'); // 1 opened day, 2 closes (even if country unknown)
  223. }
  224. /**
  225. * testConvertTime2Seconds
  226. *
  227. * @return void
  228. */
  229. public function testConvertTime2Seconds()
  230. {
  231. global $conf,$user,$langs,$db;
  232. $conf=$this->savconf;
  233. $user=$this->savuser;
  234. $langs=$this->savlangs;
  235. $db=$this->savdb;
  236. $result=convertTime2Seconds(1,1,2);
  237. print __METHOD__." result=".$result."\n";
  238. $this->assertEquals(3662,$result);
  239. return $result;
  240. }
  241. /**
  242. * testConvertSecondToTime
  243. *
  244. * @return void
  245. */
  246. public function testConvertSecondToTime()
  247. {
  248. global $conf,$user,$langs,$db;
  249. $conf=$this->savconf;
  250. $user=$this->savuser;
  251. $langs=$this->savlangs;
  252. $db=$this->savdb;
  253. $result=convertSecondToTime(0,'all',86400);
  254. print __METHOD__." result=".$result."\n";
  255. $this->assertEquals('0',$result);
  256. $result=convertSecondToTime(86400,'all',86400);
  257. print __METHOD__." result=".$result."\n";
  258. $this->assertSame('1 '.$langs->trans("Day"),$result);
  259. return $result;
  260. }
  261. /**
  262. * testDolPrintDate
  263. *
  264. * @return void
  265. */
  266. public function testDolPrintDate()
  267. {
  268. global $conf,$user,$langs,$db;
  269. $conf=$this->savconf;
  270. $user=$this->savuser;
  271. $langs=$this->savlangs;
  272. $db=$this->savdb;
  273. // Check %Y-%m-%d %H:%M:%S format
  274. $result=dol_print_date(0,'%Y-%m-%d %H:%M:%S',true);
  275. print __METHOD__." result=".$result."\n";
  276. $this->assertEquals('1970-01-01 00:00:00',$result);
  277. // Check %Y-%m-%d %H:%M:%S format
  278. $result=dol_print_date(16725225600,'%Y-%m-%d %H:%M:%S',true); // http://www.epochconverter.com/
  279. print __METHOD__." result=".$result."\n";
  280. $this->assertEquals('2500-01-01 00:00:00',$result);
  281. // Check %Y-%m-%d %H:%M:%S format
  282. $result=dol_print_date(-1830384000,'%Y-%m-%d %H:%M:%S',true); // http://www.epochconverter.com/
  283. print __METHOD__." result=".$result."\n";
  284. $this->assertEquals('1912-01-01 00:00:00',$result); // dol_print_date use TZ (good) but epoch converter does not use it.
  285. // Check %Y-%m-%d %H:%M:%S format
  286. $result=dol_print_date(-11676096000,'%Y-%m-%d %H:%M:%S',true); // http://www.epochconverter.com/
  287. print __METHOD__." result=".$result."\n";
  288. $this->assertEquals('1600-01-01 00:00:00',$result);
  289. // test with negative timezone
  290. $result=dol_print_date(-1,'%Y-%m-%d %H:%M:%S',true); // http://www.epochconverter.com/
  291. print __METHOD__." result=".$result."\n";
  292. $this->assertEquals('1969-12-31 23:59:59',$result);
  293. // Check dayhour format for fr_FR
  294. $outputlangs=new Translate('',$conf);
  295. $outputlangs->setDefaultLang('fr_FR');
  296. $outputlangs->load("main");
  297. $result=dol_print_date(0+24*3600,'dayhour',true,$outputlangs);
  298. print __METHOD__." result=".$result."\n";
  299. $this->assertEquals('02/01/1970 00:00',$result);
  300. // Check %a and %b format for fr_FR
  301. $result=dol_print_date(0,'%a %b %B',true,$outputlangs);
  302. print __METHOD__." result=".$result."\n";
  303. $this->assertEquals('Jeu Jan. Janvier',$result);
  304. // Check day format for en_US
  305. $outputlangs=new Translate('',$conf);
  306. $outputlangs->setDefaultLang('en_US');
  307. $outputlangs->load("main");
  308. $result=dol_print_date(0+24*3600,'day',true,$outputlangs);
  309. print __METHOD__." result=".$result."\n";
  310. $this->assertEquals('01/02/1970',$result);
  311. // Check %a and %b format for en_US
  312. $result=dol_print_date(0,'%a %b %B',true,$outputlangs);
  313. print __METHOD__." result=".$result."\n";
  314. $this->assertEquals('Thu Jan January',$result);
  315. return $result;
  316. }
  317. /**
  318. * testDolTimePlusDuree
  319. *
  320. * @return int
  321. */
  322. public function testDolTimePlusDuree()
  323. {
  324. global $conf,$user,$langs,$db;
  325. $conf=$this->savconf;
  326. $user=$this->savuser;
  327. $langs=$this->savlangs;
  328. $db=$this->savdb;
  329. // Check dayhour format for fr_FR
  330. $outputlangs=new Translate('',$conf);
  331. $outputlangs->setDefaultLang('fr_FR');
  332. $outputlangs->load("main");
  333. $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);
  334. print __METHOD__." result=".$result."\n";
  335. $this->assertEquals('02/02/1971 00:00',$result);
  336. return $result;
  337. }
  338. /**
  339. * testDolStringToTime
  340. *
  341. * @return int
  342. */
  343. public function testDolStringToTime()
  344. {
  345. global $conf,$user,$langs,$db;
  346. $conf=$this->savconf;
  347. $user=$this->savuser;
  348. $langs=$this->savlangs;
  349. $db=$this->savdb;
  350. $stime='19700102';
  351. $result=dol_stringtotime($stime);
  352. print __METHOD__." result=".$result."\n";
  353. $this->assertEquals(86400,$result);
  354. $stime='1970-01-01T02:00:00Z';
  355. $result=dol_stringtotime($stime);
  356. print __METHOD__." result=".$result."\n";
  357. $this->assertEquals(7200,$result);
  358. $stime='1970-01-01 02:00:00';
  359. $result=dol_stringtotime($stime);
  360. print __METHOD__." result=".$result."\n";
  361. $this->assertEquals(7200,$result);
  362. $stime='19700101T020000Z';
  363. $result=dol_stringtotime($stime);
  364. print __METHOD__." result=".$result."\n";
  365. $this->assertEquals(7200,$result);
  366. $stime='19700101020000';
  367. $result=dol_stringtotime($stime);
  368. print __METHOD__." result=".$result."\n";
  369. $this->assertEquals(7200,$result);
  370. return $result;
  371. }
  372. /**
  373. * testDolGetFirstDayWeek
  374. *
  375. * @return int
  376. */
  377. public function testDolGetFirstDayWeek()
  378. {
  379. global $conf;
  380. $day=3; $month=2; $year=2015;
  381. $conf->global->MAIN_START_WEEK = 1; // start on monday
  382. $prev = dol_get_first_day_week($day, $month, $year);
  383. $this->assertEquals(2, (int) $prev['first_day']); // monday for month 2, year 2014 is the 2
  384. $day=3; $month=2; $year=2015;
  385. $conf->global->MAIN_START_WEEK = 0; // start on sunday
  386. $prev = dol_get_first_day_week($day, $month, $year);
  387. $this->assertEquals(1, (int) $prev['first_day']); // sunday for month 2, year 2015 is the 1st
  388. }
  389. }