CommandeFournisseurTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <?php
  2. /* Copyright (C) 2010 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2023 Alexandre Janniaux <alexandre.janniaux@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. * or see https://www.gnu.org/
  18. */
  19. /**
  20. * \file test/phpunit/CommandeFournisseurTest.php
  21. * \ingroup test
  22. * \brief PHPUnit test
  23. * \remarks To run this script as CLI: phpunit filename.php
  24. */
  25. global $conf,$user,$langs,$db;
  26. //define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
  27. //require_once 'PHPUnit/Autoload.php';
  28. require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
  29. require_once dirname(__FILE__).'/../../htdocs/fourn/class/fournisseur.commande.class.php';
  30. require_once dirname(__FILE__).'/../../htdocs/fourn/class/fournisseur.product.class.php';
  31. if (empty($user->id)) {
  32. print "Load permissions for admin user nb 1\n";
  33. $user->fetch(1);
  34. $user->getrights();
  35. }
  36. $conf->global->MAIN_DISABLE_ALL_MAILS=1;
  37. /**
  38. * Class for PHPUnit tests
  39. *
  40. * @backupGlobals disabled
  41. * @backupStaticAttributes enabled
  42. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  43. */
  44. class CommandeFournisseurTest extends PHPUnit\Framework\TestCase
  45. {
  46. protected $savconf;
  47. protected $savuser;
  48. protected $savlangs;
  49. protected $savdb;
  50. /**
  51. * Constructor
  52. * We save global variables into local variables
  53. *
  54. * @param string $name Name
  55. * @return CommandeFournisseurTest
  56. */
  57. public function __construct($name = '')
  58. {
  59. parent::__construct($name);
  60. //$this->sharedFixture
  61. global $conf,$user,$langs,$db;
  62. $this->savconf=$conf;
  63. $this->savuser=$user;
  64. $this->savlangs=$langs;
  65. $this->savdb=$db;
  66. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  67. //print " - db ".$db->db;
  68. print "\n";
  69. }
  70. /**
  71. * setUpBeforeClass
  72. *
  73. * @return void
  74. */
  75. public static function setUpBeforeClass(): void
  76. {
  77. global $conf,$user,$langs,$db;
  78. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  79. print __METHOD__."\n";
  80. }
  81. /**
  82. * tearDownAfterClass
  83. *
  84. * @return void
  85. */
  86. public static function tearDownAfterClass(): void
  87. {
  88. global $conf,$user,$langs,$db;
  89. $db->rollback();
  90. print __METHOD__."\n";
  91. }
  92. /**
  93. * Init phpunit tests
  94. *
  95. * @return void
  96. */
  97. protected function setUp(): void
  98. {
  99. global $conf,$user,$langs,$db;
  100. $conf=$this->savconf;
  101. $user=$this->savuser;
  102. $langs=$this->savlangs;
  103. $db=$this->savdb;
  104. print __METHOD__."\n";
  105. //print $db->getVersion()."\n";
  106. }
  107. /**
  108. * End phpunit tests
  109. *
  110. * @return void
  111. */
  112. protected function tearDown(): void
  113. {
  114. print __METHOD__."\n";
  115. }
  116. /**
  117. * testCommandeFournisseurCreate
  118. *
  119. * @return int ID of purchase order
  120. */
  121. public function testCommandeFournisseurCreate()
  122. {
  123. global $conf,$user,$langs,$db;
  124. $conf=$this->savconf;
  125. $user=$this->savuser;
  126. $langs=$this->savlangs;
  127. $db=$this->savdb;
  128. // Set supplier and product to use
  129. $socid=1;
  130. $societe=new Societe($db);
  131. $societe->fetch($socid);
  132. $product=new ProductFournisseur($db);
  133. $product->fetch(0, 'PINKDRESS');
  134. if ($product->id <= 0) {
  135. print "\n".__METHOD__." A product with ref PINKDRESS must exists into database";
  136. die(1);
  137. }
  138. $quantity=10;
  139. $ref_fourn='SUPPLIER_REF_PHPUNIT';
  140. $tva_tx=19.6;
  141. // Delete existing supplier prices
  142. // TODO
  143. // Create 1 supplier price with min qty = 10;
  144. $result=$product->add_fournisseur($user, $societe->id, $ref_fourn, $quantity); // This insert record with no value for price. Values are update later with update_buyprice
  145. print __METHOD__." add_fournisseur result=".$result."\n";
  146. $this->assertGreaterThanOrEqual(0, $result, 'Create 1 supplier price with min qty = 10 if not exists');
  147. $result=$product->update_buyprice($quantity, 20, $user, 'HT', $societe, '', $ref_fourn, $tva_tx, 0, 0);
  148. print __METHOD__." update_buyprice result=".$result."\n";
  149. $this->assertGreaterThanOrEqual(0, $result, 'Update buyprice');
  150. // Create purchase order with a too low quantity and option SUPPLIER_ORDER_WITH_PREDEFINED_PRICES_ONLY is on
  151. $conf->global->SUPPLIER_ORDER_WITH_PREDEFINED_PRICES_ONLY = 1;
  152. $localobject=new CommandeFournisseur($db);
  153. $localobject->initAsSpecimen();
  154. $localobject->lines=array(); // Overwrite lines of order
  155. $line=new CommandeFournisseurLigne($db);
  156. $line->desc=$langs->trans("Description")." specimen line with qty too low";
  157. $line->qty=1; // So lower than $quantity
  158. $line->subprice=100;
  159. $line->fk_product=$product->id;
  160. $line->ref_fourn=$ref_fourn;
  161. $localobject->lines[]=$line;
  162. $result=$localobject->create($user);
  163. print __METHOD__." result=".$result."\n";
  164. $this->assertEquals(-1, $result, 'Creation of too low quantity'); // must be -1 because quantity is lower than minimum of supplier price
  165. $sql="DELETE FROM ".MAIN_DB_PREFIX."commande_fournisseur where ref IN ('', '(PROV)')";
  166. $db->query($sql);
  167. // Create purchase order
  168. $localobject2=new CommandeFournisseur($db);
  169. $localobject2->initAsSpecimen(); // This create 5 lines of first product found for socid 1
  170. $localobject2->lines=array(); // Overwrite lines of order
  171. $line=new CommandeFournisseurLigne($db);
  172. $line->desc=$langs->trans("Description")." specimen line ok";
  173. $line->qty=10; // So enough quantity
  174. $line->subprice=100;
  175. $line->fk_product=$product->id;
  176. $line->ref_fourn=$ref_fourn;
  177. $localobject2->lines[]=$line;
  178. $result=$localobject2->create($user);
  179. print __METHOD__." result=".$result."\n";
  180. $this->assertGreaterThan(0, $result);
  181. // Create purchase order with a too low quantity but option SUPPLIER_ORDER_WITH_PREDEFINED_PRICES_ONLY is off
  182. $conf->global->SUPPLIER_ORDER_WITH_PREDEFINED_PRICES_ONLY = 0;
  183. $localobject3=new CommandeFournisseur($db);
  184. $localobject3->initAsSpecimen();
  185. $localobject3->lines=array(); // Overwrite lines of order
  186. $line=new CommandeFournisseurLigne($db);
  187. $line->desc=$langs->trans("Description")." specimen line with qty too low";
  188. $line->qty=1; // So lower than $quantity
  189. $line->subprice=100;
  190. $line->fk_product=$product->id;
  191. $line->ref_fourn=$ref_fourn;
  192. $localobject3->lines[]=$line;
  193. $result=$localobject3->create($user);
  194. print __METHOD__." result=".$result."\n";
  195. $this->assertGreaterThan(0, $result, 'Creation of too low quantity should be ok'); // must be id of line because there is no test on minimum quantity
  196. $sql="DELETE FROM ".MAIN_DB_PREFIX."commande_fournisseur where ref=''";
  197. $db->query($sql);
  198. // Create purchase order
  199. $localobject4=new CommandeFournisseur($db);
  200. $localobject4->initAsSpecimen(); // This create 5 lines of first product found for socid 1
  201. $localobject4->lines=array(); // Overwrite lines of order
  202. $line=new CommandeFournisseurLigne($db);
  203. $line->desc=$langs->trans("Description")." specimen line ok";
  204. $line->qty=10; // So enough quantity
  205. $line->subprice=100;
  206. $line->fk_product=$product->id;
  207. $line->ref_fourn=$ref_fourn;
  208. $localobject4->lines[]=$line;
  209. $result=$localobject4->create($user);
  210. print __METHOD__." id for purchase order created by testCommandeFournisseurCreate = ".$result."\n";
  211. $this->assertGreaterThan(0, $result, 'Test to create a purchase order by testCommandeFournisseurCreate');
  212. return $result;
  213. }
  214. /**
  215. * testCommandeFournisseurFetch
  216. *
  217. * @param int $id Id of purchase order
  218. * @return CommandeFournisseur Purchase order
  219. *
  220. * @depends testCommandeFournisseurCreate
  221. * The depends says test is run only if previous is ok
  222. */
  223. public function testCommandeFournisseurFetch($id)
  224. {
  225. global $conf,$user,$langs,$db;
  226. $conf=$this->savconf;
  227. $user=$this->savuser;
  228. $langs=$this->savlangs;
  229. $db=$this->savdb;
  230. $localobject=new CommandeFournisseur($db);
  231. $result=$localobject->fetch($id);
  232. print __METHOD__." id=".$id." result=".$result."\n";
  233. $this->assertLessThan($result, 0, 'Failed to fetch supplier order with id '.$id);
  234. return $localobject;
  235. }
  236. /**
  237. * testCommandeFournisseurValid
  238. *
  239. * @param CommandeFournisseur $localobject Supplier order
  240. * @return CommandeFournisseur Supplier order
  241. *
  242. * @depends testCommandeFournisseurFetch
  243. * The depends says test is run only if previous is ok
  244. */
  245. public function testCommandeFournisseurValid($localobject)
  246. {
  247. global $conf,$user,$langs,$db;
  248. $conf=$this->savconf;
  249. $user=$this->savuser;
  250. $langs=$this->savlangs;
  251. $db=$this->savdb;
  252. $result=$localobject->valid($user);
  253. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  254. $this->assertLessThan($result, 0);
  255. return $localobject;
  256. }
  257. /**
  258. * testCommandeFournisseurApprove
  259. *
  260. * @param CommandeFournisseur $localobject Supplier order
  261. * @return CommandeFournisseur Supplier order
  262. *
  263. * @depends testCommandeFournisseurValid
  264. * The depends says test is run only if previous is ok
  265. */
  266. public function testCommandeFournisseurApprove($localobject)
  267. {
  268. global $conf,$user,$langs,$db;
  269. $conf=$this->savconf;
  270. $user=$this->savuser;
  271. $langs=$this->savlangs;
  272. $db=$this->savdb;
  273. $result=$localobject->approve($user);
  274. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  275. $this->assertLessThan($result, 0);
  276. return $localobject;
  277. }
  278. /**
  279. * testCommandeFournisseurCancel
  280. *
  281. * @param CommandeFournisseur $localobject Supplier order
  282. * @return CommandeFournisseur Supplier order
  283. *
  284. * @depends testCommandeFournisseurApprove
  285. * The depends says test is run only if previous is ok
  286. */
  287. public function testCommandeFournisseurCancel($localobject)
  288. {
  289. global $conf,$user,$langs,$db;
  290. $conf=$this->savconf;
  291. $user=$this->savuser;
  292. $langs=$this->savlangs;
  293. $db=$this->savdb;
  294. $result=$localobject->cancel($user);
  295. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  296. $this->assertLessThan($result, 0);
  297. return $localobject;
  298. }
  299. /**
  300. * testCommandeFournisseurOther
  301. *
  302. * @param CommandeFournisseur $localobject Supplier order
  303. * @return int Id of purchase order
  304. *
  305. * @depends testCommandeFournisseurCancel
  306. * The depends says test is run only if previous is ok
  307. */
  308. public function testCommandeFournisseurOther($localobject)
  309. {
  310. global $conf,$user,$langs,$db;
  311. $conf=$this->savconf;
  312. $user=$this->savuser;
  313. $langs=$this->savlangs;
  314. $db=$this->savdb;
  315. /*$result=$localobject->setstatus(0);
  316. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  317. $this->assertLessThan($result, 0);
  318. */
  319. /*$localobject->info($localobject->id);
  320. print __METHOD__." localobject->date_creation=".$localobject->date_creation."\n";
  321. $this->assertNotEquals($localobject->date_creation, '');
  322. */
  323. $this->assertEquals(1, 1);
  324. return $localobject->id;
  325. }
  326. /**
  327. * testCommandeFournisseurDelete
  328. *
  329. * @param int $id Id of order
  330. * @return int Result of delete
  331. *
  332. * @depends testCommandeFournisseurOther
  333. * The depends says test is run only if previous is ok
  334. */
  335. public function testCommandeFournisseurDelete($id)
  336. {
  337. global $conf,$user,$langs,$db;
  338. $conf=$this->savconf;
  339. $user=$this->savuser;
  340. $langs=$this->savlangs;
  341. $db=$this->savdb;
  342. $localobject=new CommandeFournisseur($db);
  343. $result=$localobject->fetch($id);
  344. $result=$localobject->delete($user);
  345. print __METHOD__." id=".$id." result=".$result."\n";
  346. $this->assertLessThan($result, 0);
  347. return $result;
  348. }
  349. }