TicketTest.php 9.3 KB

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