CategorieTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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/CategorieTest.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/categories/class/categorie.class.php';
  29. require_once dirname(__FILE__).'/../../htdocs/product/class/product.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 CategorieTest 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 CategorieTest
  54. */
  55. function __construct()
  56. {
  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. // Static methods
  68. public static function setUpBeforeClass()
  69. {
  70. global $conf,$user,$langs,$db;
  71. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  72. print __METHOD__."\n";
  73. }
  74. // tear down after class
  75. public static function tearDownAfterClass()
  76. {
  77. global $conf,$user,$langs,$db;
  78. $db->rollback();
  79. print __METHOD__."\n";
  80. }
  81. /**
  82. * Init phpunit tests
  83. *
  84. * @return void
  85. */
  86. protected function setUp()
  87. {
  88. global $conf,$user,$langs,$db;
  89. $conf=$this->savconf;
  90. $user=$this->savuser;
  91. $langs=$this->savlangs;
  92. $db=$this->savdb;
  93. print __METHOD__."\n";
  94. }
  95. /**
  96. * End phpunit tests
  97. *
  98. * @return void
  99. */
  100. protected function tearDown()
  101. {
  102. print __METHOD__."\n";
  103. }
  104. /**
  105. * testCategorieCreate
  106. *
  107. * @return int
  108. */
  109. public function testCategorieCreate()
  110. {
  111. global $conf,$user,$langs,$db;
  112. $conf=$this->savconf;
  113. $user=$this->savuser;
  114. $langs=$this->savlangs;
  115. $db=$this->savdb;
  116. // We create a category
  117. $localobject=new Categorie($this->savdb);
  118. $localobject->initAsSpecimen();
  119. // Check it does not exist (return 0)
  120. $resultCheck=$localobject->already_exists();
  121. print __METHOD__." resultCheck=".$resultCheck."\n";
  122. $this->assertEquals(0, $resultCheck);
  123. // Create
  124. $resultFirstCreate=$localobject->create($user);
  125. print __METHOD__." resultFirstCreate=".$resultFirstCreate."\n";
  126. $this->assertGreaterThan(0, $resultFirstCreate);
  127. // We try to create another one with same ref
  128. $localobject2=new Categorie($this->savdb);
  129. $localobject2->initAsSpecimen();
  130. // Check it does exist (return 1)
  131. $resultCheck=$localobject2->already_exists();
  132. print __METHOD__." resultCheck=".$resultCheck."\n";
  133. $this->assertGreaterThan(0, $resultCheck);
  134. $resultSecondCreate=$localobject2->create($user);
  135. print __METHOD__." result=".$resultSecondCreate."\n";
  136. $this->assertEquals(-4, $resultSecondCreate);
  137. return $resultFirstCreate;
  138. }
  139. /**
  140. * testCategorieProduct
  141. *
  142. * @param int $id Id of category
  143. * @return int
  144. *
  145. * @depends testCategorieCreate
  146. * The depends says test is run only if previous is ok
  147. */
  148. public function testCategorieProduct($id)
  149. {
  150. global $conf,$user,$langs,$db;
  151. $conf=$this->savconf;
  152. $user=$this->savuser;
  153. $langs=$this->savlangs;
  154. $db=$this->savdb;
  155. $localobjecttmp=new Categorie($this->savdb);
  156. $localobjecttmp->initAsSpecimen();
  157. $localobjecttmp->label='Specimen Category for product';
  158. $localobjecttmp->type=0; // product category
  159. $catid=$localobjecttmp->create($user);
  160. print __METHOD__." catid=".$catid."\n";
  161. $this->assertGreaterThan(0, $catid);
  162. // Try to create product linked to category
  163. $localobject2=new Product($this->savdb);
  164. $localobject2->initAsSpecimen();
  165. $localobject2->ref.='-CATEG';
  166. $localobject2->tva_npr=1;
  167. $result=$localobject2->create($user);
  168. $cat = new Categorie($this->savdb);
  169. $cat->id = $catid;
  170. $result=$cat->add_type($localobject2,"product");
  171. print __METHOD__." result=".$result."\n";
  172. $this->assertGreaterThan(0, $result);
  173. // Get list of categories for product
  174. $localcateg=new Categorie($this->savdb);
  175. $listofcateg=$localcateg->containing($localobject2->id, Categorie::TYPE_PRODUCT, 'label');
  176. $this->assertTrue(in_array('Specimen Category for product',$listofcateg), 'Categ not found linked to product when it should');
  177. return $id;
  178. }
  179. /**
  180. * testCategorieFetch
  181. *
  182. * @param int $id Id of category
  183. * @return int
  184. *
  185. * @depends testCategorieProduct
  186. * The depends says test is run only if previous is ok
  187. */
  188. public function testCategorieFetch($id)
  189. {
  190. global $conf,$user,$langs,$db;
  191. $conf=$this->savconf;
  192. $user=$this->savuser;
  193. $langs=$this->savlangs;
  194. $db=$this->savdb;
  195. $localobject=new Categorie($this->savdb);
  196. $result=$localobject->fetch($id);
  197. print __METHOD__." id=".$id." result=".$result."\n";
  198. $this->assertGreaterThan(0, $result);
  199. return $localobject;
  200. }
  201. /**
  202. * testCategorieUpdate
  203. *
  204. * @param Category $localobject Category
  205. * @return int
  206. * @depends testCategorieFetch
  207. * The depends says test is run only if previous is ok
  208. */
  209. public function testCategorieUpdate($localobject)
  210. {
  211. global $conf,$user,$langs,$db;
  212. $conf=$this->savconf;
  213. $user=$this->savuser;
  214. $langs=$this->savlangs;
  215. $db=$this->savdb;
  216. $localobject->note='New note after update';
  217. $result=$localobject->update($user);
  218. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  219. $this->assertGreaterThan(0, $result);
  220. return $localobject;
  221. }
  222. /**
  223. * testCategorieOther
  224. *
  225. * @param Category $localobject Category
  226. * @return int
  227. *
  228. * @depends testCategorieUpdate
  229. * The depends says test is run only if previous is ok
  230. */
  231. public function testCategorieOther($localobject)
  232. {
  233. global $conf,$user,$langs,$db;
  234. $conf=$this->savconf;
  235. $user=$this->savuser;
  236. $langs=$this->savlangs;
  237. $db=$this->savdb;
  238. /*$result=$localobject->setstatus(0);
  239. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  240. $this->assertLessThan($result, 0);
  241. */
  242. $localobject2=new Categorie($db);
  243. $localobject2->initAsSpecimen();
  244. $retarray=$localobject->liste_photos('/');
  245. print __METHOD__." retarray size=".count($retarray)."\n";
  246. $this->assertTrue(is_array($retarray));
  247. return $localobject->id;
  248. }
  249. /**
  250. * testCategorieDelete
  251. *
  252. * @param int $id Id of category
  253. * @return int
  254. *
  255. * @depends testCategorieOther
  256. * The depends says test is run only if previous is ok
  257. */
  258. public function testCategorieDelete($id)
  259. {
  260. global $conf,$user,$langs,$db;
  261. $conf=$this->savconf;
  262. $user=$this->savuser;
  263. $langs=$this->savlangs;
  264. $db=$this->savdb;
  265. $localobject=new Categorie($this->savdb);
  266. $result=$localobject->fetch($id);
  267. $result=$localobject->delete($user);
  268. print __METHOD__." id=".$id." result=".$result."\n";
  269. $this->assertGreaterThan(0, $result);
  270. return $result;
  271. }
  272. /**
  273. * testCategorieStatic
  274. *
  275. * @return void
  276. *
  277. * @depends testCategorieDelete
  278. */
  279. public function testCategorieStatic()
  280. {
  281. global $conf,$user,$langs,$db;
  282. $conf=$this->savconf;
  283. $user=$this->savuser;
  284. $langs=$this->savlangs;
  285. $db=$this->savdb;
  286. $localobject=new Categorie($this->savdb);
  287. $retarray=$localobject->get_full_arbo(3);
  288. print __METHOD__." retarray size=".count($retarray)."\n";
  289. $this->assertTrue(is_array($retarray));
  290. return $retarray;
  291. }
  292. }