HolidayTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <?php
  2. /* Copyright (C) 2012 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/HolidayTest.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/holiday/class/holiday.class.php';
  29. $langs->load("dict");
  30. if (empty($user->id)) {
  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 HolidayTest 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 HolidayTest
  54. */
  55. public 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. /**
  69. * setUpBeforeClass
  70. *
  71. * @return void
  72. */
  73. public static function setUpBeforeClass()
  74. {
  75. global $conf,$user,$langs,$db;
  76. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  77. print __METHOD__."\n";
  78. }
  79. /**
  80. * tearDownAfterClass
  81. *
  82. * @return void
  83. */
  84. public static function tearDownAfterClass()
  85. {
  86. global $conf,$user,$langs,$db;
  87. $db->rollback();
  88. print __METHOD__."\n";
  89. }
  90. /**
  91. * Init phpunit tests
  92. *
  93. * @return void
  94. */
  95. protected function setUp()
  96. {
  97. global $conf,$user,$langs,$db;
  98. $conf=$this->savconf;
  99. $user=$this->savuser;
  100. $langs=$this->savlangs;
  101. $db=$this->savdb;
  102. print __METHOD__."\n";
  103. }
  104. /**
  105. * End phpunit tests
  106. *
  107. * @return void
  108. */
  109. protected function tearDown()
  110. {
  111. print __METHOD__."\n";
  112. }
  113. /**
  114. * testHolidayCreate
  115. *
  116. * @return int
  117. */
  118. public function testHolidayCreate()
  119. {
  120. global $conf,$user,$langs,$db;
  121. $conf=$this->savconf;
  122. $user=$this->savuser;
  123. $langs=$this->savlangs;
  124. $db=$this->savdb;
  125. $localobject=new Holiday($this->savdb);
  126. $localobject->initAsSpecimen();
  127. $result=$localobject->create($user);
  128. print __METHOD__." result=".$result."\n";
  129. $this->assertLessThan($result, 0);
  130. return $result;
  131. }
  132. /**
  133. * testHolidayFetch
  134. *
  135. * @param int $id Id of Holiday
  136. * @return int
  137. * @depends testHolidayCreate
  138. * The depends says test is run only if previous is ok
  139. */
  140. public function testHolidayFetch($id)
  141. {
  142. global $conf,$user,$langs,$db;
  143. $conf=$this->savconf;
  144. $user=$this->savuser;
  145. $langs=$this->savlangs;
  146. $db=$this->savdb;
  147. $localobject=new Holiday($this->savdb);
  148. $result=$localobject->fetch($id);
  149. print __METHOD__." id=".$id." result=".$result."\n";
  150. $this->assertLessThan($result, 0);
  151. return $localobject;
  152. }
  153. /**
  154. * testHolidayUpdate
  155. *
  156. * @param Holiday $localobject Holiday
  157. * @return int
  158. *
  159. * @depends testHolidayFetch
  160. * The depends says test is run only if previous is ok
  161. */
  162. public function testHolidayUpdate($localobject)
  163. {
  164. global $conf,$user,$langs,$db;
  165. $conf=$this->savconf;
  166. $user=$this->savuser;
  167. $langs=$this->savlangs;
  168. $db=$this->savdb;
  169. $localobject->oldcopy = clone $localobject;
  170. $localobject->note_private='New private note after update';
  171. $localobject->note_public='New public note after update';
  172. $localobject->lastname='New name';
  173. $localobject->firstname='New firstname';
  174. $localobject->address='New address';
  175. $localobject->zip='New zip';
  176. $localobject->town='New town';
  177. $localobject->country_id=2;
  178. //$localobject->status=0;
  179. $localobject->phone_pro='New tel pro';
  180. $localobject->phone_perso='New tel perso';
  181. $localobject->phone_mobile='New tel mobile';
  182. $localobject->fax='New fax';
  183. $localobject->email='newemail@newemail.com';
  184. $localobject->jabberid='New im id';
  185. $localobject->default_lang='es_ES';
  186. $result=$localobject->update($localobject->id, $user);
  187. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  188. $this->assertLessThan($result, 0, 'Holiday::update error');
  189. $result=$localobject->update_note($localobject->note_private, '_private');
  190. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  191. $this->assertLessThan($result, 0, 'Holiday::update_note (private) error');
  192. $result=$localobject->update_note($localobject->note_public, '_public');
  193. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  194. $this->assertLessThan($result, 0, 'Holiday::update_note (public) error');
  195. $newobject=new Holiday($this->savdb);
  196. $result=$newobject->fetch($localobject->id);
  197. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  198. $this->assertLessThan($result, 0, 'Holiday::fetch error');
  199. print __METHOD__." old=".$localobject->note." new=".$newobject->note."\n";
  200. $this->assertEquals($localobject->note, $newobject->note, 'Holiday::update_note error compare note');
  201. //print __METHOD__." old=".$localobject->note_public." new=".$newobject->note_public."\n";
  202. //$this->assertEquals($localobject->note_public, $newobject->note_public);
  203. return $localobject;
  204. }
  205. /**
  206. * testHolidayOther
  207. *
  208. * @param Holiday $localobject Holiday
  209. * @return void
  210. *
  211. * @depends testHolidayUpdate
  212. * The depends says test is run only if previous is ok
  213. */
  214. public function testHolidayOther($localobject)
  215. {
  216. global $conf,$user,$langs,$db;
  217. $conf=$this->savconf;
  218. $user=$this->savuser;
  219. $langs=$this->savlangs;
  220. $db=$this->savdb;
  221. $result = $localobject->fetchUsers(true, true, '');
  222. $this->assertNotEquals($result, -1);
  223. $result = $localobject->fetchUsers(true, false, '');
  224. $this->assertNotEquals($result, -1);
  225. $result = $localobject->fetchUsers(false, true, '');
  226. $this->assertNotEquals($result, -1);
  227. $result = $localobject->fetchUsers(false, false, '');
  228. $this->assertNotEquals($result, -1);
  229. return $localobject->id;
  230. }
  231. /**
  232. * testHolidayDelete
  233. *
  234. * @param int $id Id of Holiday
  235. * @return void
  236. *
  237. * @depends testHolidayOther
  238. * The depends says test is run only if previous is ok
  239. */
  240. public function testHolidayDelete($id)
  241. {
  242. global $conf,$user,$langs,$db;
  243. $conf=$this->savconf;
  244. $user=$this->savuser;
  245. $langs=$this->savlangs;
  246. $db=$this->savdb;
  247. $localobject=new Holiday($this->savdb);
  248. $result=$localobject->fetch($id);
  249. $result=$localobject->delete(0);
  250. print __METHOD__." id=".$id." result=".$result."\n";
  251. $this->assertLessThan($result, 0);
  252. return $result;
  253. }
  254. /**
  255. * testVerifDateHolidayCP
  256. *
  257. * @return void
  258. */
  259. public function testVerifDateHolidayCP()
  260. {
  261. global $conf,$user,$langs,$db;
  262. $conf=$this->savconf;
  263. $user=$this->savuser;
  264. $langs=$this->savlangs;
  265. $db=$this->savdb;
  266. // Create a leave request the 1st morning only
  267. $localobjecta=new Holiday($this->savdb);
  268. $localobjecta->initAsSpecimen();
  269. $localobjecta->date_debut = dol_mktime(0, 0, 0, 1, 1, 2020);
  270. $localobjecta->date_fin = dol_mktime(0, 0, 0, 1, 1, 2020);
  271. $localobjecta->halfday = 1;
  272. $result=$localobjecta->create($user);
  273. // Create a leave request the 2 afternoon only
  274. $localobjectb=new Holiday($this->savdb);
  275. $localobjectb->initAsSpecimen();
  276. $localobjectb->date_debut = dol_mktime(0, 0, 0, 1, 2, 2020);
  277. $localobjectb->date_fin = dol_mktime(0, 0, 0, 1, 2, 2020);
  278. $localobjectb->halfday = -1;
  279. $result=$localobjectb->create($user);
  280. $date_debut = dol_mktime(0, 0, 0, 1, 1, 2020);
  281. $date_fin = dol_mktime(0, 0, 0, 1, 2, 2020);
  282. $localobjectc=new Holiday($this->savdb);
  283. $result=$localobjectc->verifDateHolidayCP($user->id, $date_debut, $date_debut, 0);
  284. $this->assertFalse($result, 'result should be false, there is overlapping, full day is not available.');
  285. $result=$localobjectc->verifDateHolidayCP($user->id, $date_debut, $date_fin, 0);
  286. $this->assertFalse($result, 'result should be false, there is overlapping, full day is not available.');
  287. $result=$localobjectc->verifDateHolidayCP($user->id, $date_fin, $date_fin, 0);
  288. $this->assertFalse($result, 'result should be false, there is overlapping, full day is not available.');
  289. $result=$localobjectc->verifDateHolidayCP($user->id, $date_debut, $date_debut, 1);
  290. $this->assertFalse($result, 'result should be false, there is overlapping, morning of first day is not available.');
  291. $result=$localobjectc->verifDateHolidayCP($user->id, $date_debut, $date_fin, 1);
  292. $this->assertFalse($result, 'result should be false, there is overlapping, morning of first day is not available.');
  293. $result=$localobjectc->verifDateHolidayCP($user->id, $date_fin, $date_fin, 1);
  294. $this->assertTrue($result, 'result should be true, there is no overlapping');
  295. $result=$localobjectc->verifDateHolidayCP($user->id, $date_debut, $date_debut, -1);
  296. $this->assertTrue($result, 'result should be true, there is no overlapping');
  297. $result=$localobjectc->verifDateHolidayCP($user->id, $date_debut, $date_fin, -1);
  298. $this->assertFalse($result, 'result should be false, there is overlapping, afternoon of second day is not available');
  299. $result=$localobjectc->verifDateHolidayCP($user->id, $date_fin, $date_fin, -1);
  300. $this->assertFalse($result, 'result should be false, there is overlapping, afternoon of second day is not available');
  301. $result=$localobjectc->verifDateHolidayCP($user->id, $date_debut, $date_fin, 2); // start afternoon and end morning
  302. $this->assertTrue($result, 'result should be true, there is no overlapping');
  303. }
  304. /**
  305. * testUpdateBalance
  306. *
  307. * @return void
  308. */
  309. public function testUpdateBalance()
  310. {
  311. $localobjecta=new Holiday($this->savdb);
  312. $localobjecta->updateConfCP('lastUpdate', '20100101120000');
  313. $result = $localobjecta->updateBalance();
  314. $this->assertEquals($result, 0);
  315. }
  316. }