DateLibTzFranceTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /* Copyright (C) 2013 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 <https://www.gnu.org/licenses/>.
  16. * or see https://www.gnu.org/
  17. */
  18. /**
  19. * \file test/phpunit/DateLibTzFranceTest.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. print "Load permissions for admin user nb 1\n";
  31. $user->fetch(1);
  32. $user->getrights();
  33. }
  34. $conf->global->MAIN_DISABLE_ALL_MAILS=1;
  35. /**
  36. * Class for PHPUnit tests
  37. *
  38. * @backupGlobals disabled
  39. * @backupStaticAttributes enabled
  40. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  41. */
  42. class DateLibTzFranceTest extends PHPUnit\Framework\TestCase
  43. {
  44. protected $savconf;
  45. protected $savuser;
  46. protected $savlangs;
  47. protected $savdb;
  48. /**
  49. * Constructor
  50. * We save global variables into local variables
  51. *
  52. * @return DateLibTest
  53. */
  54. public function __construct()
  55. {
  56. parent::__construct();
  57. //$this->sharedFixture
  58. global $conf,$user,$langs,$db;
  59. $this->savconf=$conf;
  60. $this->savuser=$user;
  61. $this->savlangs=$langs;
  62. $this->savdb=$db;
  63. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  64. //print " - db ".$db->db;
  65. print "\n";
  66. }
  67. /**
  68. * setUpBeforeClass
  69. *
  70. * @return void
  71. */
  72. public static function setUpBeforeClass()
  73. {
  74. global $conf,$user,$langs,$db;
  75. if (getServerTimeZoneString() != 'Europe/Paris' && getServerTimeZoneString() != 'Europe/Berlin') {
  76. print "\n".__METHOD__." This PHPUnit test can be launched manually only onto a server with PHP timezone set to TZ=Europe/Paris, not a TZ=".getServerTimeZoneString().".\n";
  77. print "You can launch the test from command line with:\n";
  78. print "php -d date.timezone='Europe/Paris' phpunit DateLibTzFranceTest.php\n";
  79. die(1);
  80. }
  81. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  82. print __METHOD__."\n";
  83. }
  84. /**
  85. * tearDownAfterClass
  86. *
  87. * @return void
  88. */
  89. public static function tearDownAfterClass()
  90. {
  91. global $conf,$user,$langs,$db;
  92. $db->rollback();
  93. print __METHOD__."\n";
  94. }
  95. /**
  96. * Init phpunit tests
  97. *
  98. * @return void
  99. */
  100. protected function setUp()
  101. {
  102. global $conf,$user,$langs,$db;
  103. $conf=$this->savconf;
  104. $user=$this->savuser;
  105. $langs=$this->savlangs;
  106. $db=$this->savdb;
  107. print __METHOD__."\n";
  108. }
  109. /**
  110. * End phpunit tests
  111. *
  112. * @return void
  113. */
  114. protected function tearDown()
  115. {
  116. print __METHOD__."\n";
  117. }
  118. /**
  119. * testDolPrintDateTzFrance
  120. * Same than official testDolPrintDate but with parameter tzoutput that is false='tzserver'.
  121. * This test works only onto a server using TZ+1 Europe/Paris.
  122. *
  123. * You can use http://www.epochconverter.com/ to generate more tests.
  124. *
  125. * @return void
  126. */
  127. public function testDolPrintDateTzFrance()
  128. {
  129. global $conf,$user,$langs,$db;
  130. $conf=$this->savconf;
  131. $user=$this->savuser;
  132. $langs=$this->savlangs;
  133. $db=$this->savdb;
  134. // Check %Y-%m-%d %H:%M:%S format
  135. $result=dol_print_date(0, '%Y-%m-%d %H:%M:%S', false);
  136. print __METHOD__." result=".$result."\n";
  137. $this->assertEquals('1970-01-01 01:00:00', $result);
  138. // Check %Y-%m-%d %H:%M:%S format
  139. $result=dol_print_date(16725225600, '%Y-%m-%d %H:%M:%S', false);
  140. print __METHOD__." result=".$result."\n";
  141. $this->assertEquals('2500-01-01 01:00:00', $result);
  142. // Check %Y-%m-%d %H:%M:%S format
  143. $result=dol_print_date(-1830384000, '%Y-%m-%d %H:%M:%S', false); // http://www.epochconverter.com/
  144. print __METHOD__." result=".$result."\n";
  145. $this->assertEquals('1912-01-01 01:00:00', $result); // dol_print_date use a timezone, not epoch converter as it did not exists this year
  146. // Specific cas during war
  147. // 1940, no timezone
  148. $result=dol_print_date(-946771200, '%Y-%m-%d %H:%M:%S', false); // http://www.epochconverter.com/
  149. print __METHOD__." result=".$result."\n";
  150. $this->assertEquals('1940-01-01 01:00:00', $result); // dol_print_date use a modern timezone, not epoch converter as it did not exists this year
  151. // 1941, timezone is added by germany to +2 (same for 1942)
  152. $result=dol_print_date(-915148800, '%Y-%m-%d %H:%M:%S', false); // http://www.epochconverter.com/
  153. print __METHOD__." result=".$result."\n";
  154. $this->assertEquals('1941-01-01 01:00:00', $result); // dol_print_date use a modern timezone, epoch converter use historic timezone
  155. // 1943, timezone is +1
  156. $result=dol_print_date(-852076800, '%Y-%m-%d %H:%M:%S', false); // http://www.epochconverter.com/
  157. print __METHOD__." result=".$result."\n";
  158. $this->assertEquals('1943-01-01 01:00:00', $result);
  159. // test with negative timezone
  160. $result=dol_print_date(-1, '%Y-%m-%d %H:%M:%S', false); // http://www.epochconverter.com/
  161. print __METHOD__." result=".$result."\n";
  162. $this->assertEquals('1970-01-01 00:59:59', $result);
  163. // Check dayhour format for fr_FR
  164. $outputlangs=new Translate('', $conf);
  165. $outputlangs->setDefaultLang('fr_FR');
  166. $outputlangs->load("main");
  167. $result=dol_print_date(0+24*3600, 'dayhour', false, $outputlangs);
  168. print __METHOD__." result=".$result."\n";
  169. $this->assertEquals('02/01/1970 01:00', $result);
  170. // Check day format for en_US
  171. $outputlangs=new Translate('', $conf);
  172. $outputlangs->setDefaultLang('en_US');
  173. $outputlangs->load("main");
  174. $result=dol_print_date(0+24*3600, 'day', false, $outputlangs);
  175. print __METHOD__." result=".$result."\n";
  176. $this->assertEquals('01/02/1970', $result);
  177. // Check %a and %b format for en_US
  178. $result=dol_print_date(0, '%a %b', false, $outputlangs);
  179. print __METHOD__." result=".$result."\n";
  180. $this->assertEquals('Thu Jan', $result);
  181. return $result;
  182. }
  183. }