TicketTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 <https://www.gnu.org/licenses/>.
  16. * or see https://www.gnu.org/
  17. */
  18. /**
  19. * \file test/unit/TicketTest.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/user/class/usergroup.class.php';
  29. require_once dirname(__FILE__).'/../../htdocs/ticket/class/ticket.class.php';
  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 TicketTest 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 ContratTest
  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. * testTicketCreate
  115. *
  116. * @return int
  117. */
  118. public function testTicketCreate()
  119. {
  120. global $conf,$user,$langs,$db;
  121. $conf=$this->savconf;
  122. $user=$this->savuser;
  123. $langs=$this->savlangs;
  124. $db=$this->savdb;
  125. // Try to create one with bad values
  126. $localobject=new Ticket($this->savdb);
  127. $localobject->initAsSpecimen();
  128. $localobject->ref = '';
  129. $result=$localobject->create($user);
  130. print __METHOD__." result=".$result."\n";
  131. $this->assertEquals(-3, $result, $localobject->error.join(',', $localobject->errors));
  132. // Try to create one with correct values
  133. $localobject=new Ticket($this->savdb);
  134. $localobject->initAsSpecimen();
  135. $result=$localobject->create($user);
  136. print __METHOD__." result=".$result."\n";
  137. $this->assertGreaterThan(0, $result, $localobject->error.join(',', $localobject->errors));
  138. return $result;
  139. }
  140. /**
  141. * testTicketFetch
  142. *
  143. * @param int $id Id of ticket
  144. * @return int
  145. *
  146. * @depends testTicketCreate
  147. * The depends says test is run only if previous is ok
  148. */
  149. public function testTicketFetch($id)
  150. {
  151. global $conf,$user,$langs,$db;
  152. $conf=$this->savconf;
  153. $user=$this->savuser;
  154. $langs=$this->savlangs;
  155. $db=$this->savdb;
  156. $localobject=new Ticket($this->savdb);
  157. $result=$localobject->fetch($id);
  158. print __METHOD__." id=".$id." result=".$result."\n";
  159. $this->assertGreaterThan(0, $result);
  160. return $localobject;
  161. }
  162. /**
  163. * testTicketmarkAsRead
  164. *
  165. * @param Ticket $localobject Ticket
  166. * @return int
  167. *
  168. * @depends testTicketFetch
  169. * The depends says test is run only if previous is ok
  170. */
  171. public function testTicketmarkAsRead($localobject)
  172. {
  173. global $conf,$user,$langs,$db;
  174. $conf=$this->savconf;
  175. $user=$this->savuser;
  176. $langs=$this->savlangs;
  177. $db=$this->savdb;
  178. $result=$localobject->markAsRead($user);
  179. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  180. $this->assertGreaterThan(0, $result);
  181. return $localobject;
  182. }
  183. /**
  184. * testTicketsetProject
  185. *
  186. * @param Ticket $localobject Ticket
  187. * @return int
  188. *
  189. * @depends testTicketFetch
  190. * The depends says test is run only if previous is ok
  191. */
  192. public function testTicketsetProject($localobject)
  193. {
  194. global $conf,$user,$langs,$db;
  195. $conf=$this->savconf;
  196. $user=$this->savuser;
  197. $langs=$this->savlangs;
  198. $db=$this->savdb;
  199. $project_id = 1;
  200. $result=$localobject->setProject($project_id);
  201. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  202. $this->assertGreaterThan(0, $result);
  203. return $localobject;
  204. }
  205. /**
  206. * testTicketsetContract
  207. *
  208. * @param Ticket $localobject Ticket
  209. * @return int
  210. *
  211. * @depends testTicketFetch
  212. * The depends says test is run only if previous is ok
  213. */
  214. public function testTicketsetContract($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. $contract_id = 1;
  222. $result=$localobject->setContract($contract_id);
  223. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  224. $this->assertGreaterThan(0, $result);
  225. return $localobject;
  226. }
  227. /**
  228. * testTicketsetProgression
  229. *
  230. * @param Ticket $localobject Ticket
  231. * @return int
  232. *
  233. * @depends testTicketFetch
  234. * The depends says test is run only if previous is ok
  235. */
  236. public function testTicketsetProgression($localobject)
  237. {
  238. global $conf,$user,$langs,$db;
  239. $conf=$this->savconf;
  240. $user=$this->savuser;
  241. $langs=$this->savlangs;
  242. $db=$this->savdb;
  243. $percent = 80;
  244. $result=$localobject->setProgression($percent);
  245. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  246. $this->assertGreaterThan(0, $result);
  247. return $localobject;
  248. }
  249. /**
  250. * testTicketassignUser
  251. *
  252. * @param Ticket $localobject Ticket
  253. * @return int
  254. *
  255. * @depends testTicketFetch
  256. * The depends says test is run only if previous is ok
  257. */
  258. public function testTicketassignUser($localobject)
  259. {
  260. global $conf,$user,$langs,$db;
  261. $conf=$this->savconf;
  262. $user=$this->savuser;
  263. $langs=$this->savlangs;
  264. $db=$this->savdb;
  265. $user_id_to_assign = 1;
  266. $result=$localobject->assignUser($user, $user_id_to_assign);
  267. ;
  268. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  269. $this->assertGreaterThan(0, $result);
  270. return $localobject;
  271. }
  272. /**
  273. * testTicketassignUserOther
  274. *
  275. * @param Ticket $localobject Ticket
  276. * @return int
  277. *
  278. * @depends testTicketFetch
  279. * The depends says test is run only if previous is ok
  280. */
  281. public function testTicketassignUserOther($localobject)
  282. {
  283. global $conf,$user,$langs,$db;
  284. $conf=$this->savconf;
  285. $user=$this->savuser;
  286. $langs=$this->savlangs;
  287. $db=$this->savdb;
  288. $user_id_to_assign = 2;
  289. $result=$localobject->assignUser($user, $user_id_to_assign);
  290. ;
  291. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  292. $this->assertGreaterThan(0, $result);
  293. return $localobject;
  294. }
  295. /**
  296. * testTicketclose
  297. *
  298. * @param Ticket $localobject Ticket
  299. * @return int
  300. *
  301. * @depends testTicketFetch
  302. * The depends says test is run only if previous is ok
  303. */
  304. public function testTicketclose($localobject)
  305. {
  306. global $conf,$user,$langs,$db;
  307. $conf=$this->savconf;
  308. $user=$this->savuser;
  309. $langs=$this->savlangs;
  310. $db=$this->savdb;
  311. $result=$localobject->close($user);
  312. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  313. $this->assertGreaterThan(0, $result);
  314. return $localobject->id;
  315. }
  316. /**
  317. * testTicketDelete
  318. *
  319. * @param int $id Id of ticket
  320. * @return int
  321. *
  322. * @depends testTicketclose
  323. * The depends says test is run only if previous is ok
  324. */
  325. public function testTicketDelete($id)
  326. {
  327. global $conf,$user,$langs,$db;
  328. $conf=$this->savconf;
  329. $user=$this->savuser;
  330. $langs=$this->savlangs;
  331. $db=$this->savdb;
  332. $localobject=new Ticket($this->savdb);
  333. $result=$localobject->fetch($id);
  334. $result=$localobject->delete($user);
  335. print __METHOD__." id=".$id." result=".$result."\n";
  336. $this->assertGreaterThan(0, $result);
  337. return $result;
  338. }
  339. }