CommandeFournisseurTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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"; die(1);
  136. }
  137. $quantity=10;
  138. $ref_fourn='SUPPLIER_REF_PHPUNIT';
  139. $tva_tx=19.6;
  140. // Delete existing supplier prices
  141. // TODO
  142. // Create 1 supplier price with min qty = 10;
  143. $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
  144. print __METHOD__." add_fournisseur result=".$result."\n";
  145. $this->assertGreaterThanOrEqual(0, $result, 'Create 1 supplier price with min qty = 10 if not exists');
  146. $result=$product->update_buyprice($quantity, 20, $user, 'HT', $societe, '', $ref_fourn, $tva_tx, 0, 0);
  147. print __METHOD__." update_buyprice result=".$result."\n";
  148. $this->assertGreaterThanOrEqual(0, $result, 'Update buyprice');
  149. // Create purchase order with a too low quantity and option SUPPLIER_ORDER_WITH_PREDEFINED_PRICES_ONLY is on
  150. $conf->global->SUPPLIER_ORDER_WITH_PREDEFINED_PRICES_ONLY = 1;
  151. $localobject=new CommandeFournisseur($db);
  152. $localobject->initAsSpecimen();
  153. $localobject->lines=array(); // Overwrite lines of order
  154. $line=new CommandeFournisseurLigne($db);
  155. $line->desc=$langs->trans("Description")." specimen line with qty too low";
  156. $line->qty=1; // So lower than $quantity
  157. $line->subprice=100;
  158. $line->fk_product=$product->id;
  159. $line->ref_fourn=$ref_fourn;
  160. $localobject->lines[]=$line;
  161. $result=$localobject->create($user);
  162. print __METHOD__." result=".$result."\n";
  163. $this->assertEquals(-1, $result, 'Creation of too low quantity'); // must be -1 because quantity is lower than minimum of supplier price
  164. $sql="DELETE FROM ".MAIN_DB_PREFIX."commande_fournisseur where ref IN ('', '(PROV)')";
  165. $db->query($sql);
  166. // Create purchase order
  167. $localobject2=new CommandeFournisseur($db);
  168. $localobject2->initAsSpecimen(); // This create 5 lines of first product found for socid 1
  169. $localobject2->lines=array(); // Overwrite lines of order
  170. $line=new CommandeFournisseurLigne($db);
  171. $line->desc=$langs->trans("Description")." specimen line ok";
  172. $line->qty=10; // So enough quantity
  173. $line->subprice=100;
  174. $line->fk_product=$product->id;
  175. $line->ref_fourn=$ref_fourn;
  176. $localobject2->lines[]=$line;
  177. $result=$localobject2->create($user);
  178. print __METHOD__." result=".$result."\n";
  179. $this->assertGreaterThan(0, $result);
  180. // Create purchase order with a too low quantity but option SUPPLIER_ORDER_WITH_PREDEFINED_PRICES_ONLY is off
  181. $conf->global->SUPPLIER_ORDER_WITH_PREDEFINED_PRICES_ONLY = 0;
  182. $localobject3=new CommandeFournisseur($db);
  183. $localobject3->initAsSpecimen();
  184. $localobject3->lines=array(); // Overwrite lines of order
  185. $line=new CommandeFournisseurLigne($db);
  186. $line->desc=$langs->trans("Description")." specimen line with qty too low";
  187. $line->qty=1; // So lower than $quantity
  188. $line->subprice=100;
  189. $line->fk_product=$product->id;
  190. $line->ref_fourn=$ref_fourn;
  191. $localobject3->lines[]=$line;
  192. $result=$localobject3->create($user);
  193. print __METHOD__." result=".$result."\n";
  194. $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
  195. $sql="DELETE FROM ".MAIN_DB_PREFIX."commande_fournisseur where ref=''";
  196. $db->query($sql);
  197. // Create purchase order
  198. $localobject4=new CommandeFournisseur($db);
  199. $localobject4->initAsSpecimen(); // This create 5 lines of first product found for socid 1
  200. $localobject4->lines=array(); // Overwrite lines of order
  201. $line=new CommandeFournisseurLigne($db);
  202. $line->desc=$langs->trans("Description")." specimen line ok";
  203. $line->qty=10; // So enough quantity
  204. $line->subprice=100;
  205. $line->fk_product=$product->id;
  206. $line->ref_fourn=$ref_fourn;
  207. $localobject4->lines[]=$line;
  208. $result=$localobject4->create($user);
  209. print __METHOD__." id for purchase order created by testCommandeFournisseurCreate = ".$result."\n";
  210. $this->assertGreaterThan(0, $result, 'Test to create a purchase order by testCommandeFournisseurCreate');
  211. return $result;
  212. }
  213. /**
  214. * testCommandeFournisseurFetch
  215. *
  216. * @param int $id Id of purchase order
  217. * @return CommandeFournisseur Purchase order
  218. *
  219. * @depends testCommandeFournisseurCreate
  220. * The depends says test is run only if previous is ok
  221. */
  222. public function testCommandeFournisseurFetch($id)
  223. {
  224. global $conf,$user,$langs,$db;
  225. $conf=$this->savconf;
  226. $user=$this->savuser;
  227. $langs=$this->savlangs;
  228. $db=$this->savdb;
  229. $localobject=new CommandeFournisseur($db);
  230. $result=$localobject->fetch($id);
  231. print __METHOD__." id=".$id." result=".$result."\n";
  232. $this->assertLessThan($result, 0, 'Failed to fetch supplier order with id '.$id);
  233. return $localobject;
  234. }
  235. /**
  236. * testCommandeFournisseurValid
  237. *
  238. * @param CommandeFournisseur $localobject Supplier order
  239. * @return CommandeFournisseur Supplier order
  240. *
  241. * @depends testCommandeFournisseurFetch
  242. * The depends says test is run only if previous is ok
  243. */
  244. public function testCommandeFournisseurValid($localobject)
  245. {
  246. global $conf,$user,$langs,$db;
  247. $conf=$this->savconf;
  248. $user=$this->savuser;
  249. $langs=$this->savlangs;
  250. $db=$this->savdb;
  251. $result=$localobject->valid($user);
  252. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  253. $this->assertLessThan($result, 0);
  254. return $localobject;
  255. }
  256. /**
  257. * testCommandeFournisseurApprove
  258. *
  259. * @param CommandeFournisseur $localobject Supplier order
  260. * @return CommandeFournisseur Supplier order
  261. *
  262. * @depends testCommandeFournisseurValid
  263. * The depends says test is run only if previous is ok
  264. */
  265. public function testCommandeFournisseurApprove($localobject)
  266. {
  267. global $conf,$user,$langs,$db;
  268. $conf=$this->savconf;
  269. $user=$this->savuser;
  270. $langs=$this->savlangs;
  271. $db=$this->savdb;
  272. $result=$localobject->approve($user);
  273. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  274. $this->assertLessThan($result, 0);
  275. return $localobject;
  276. }
  277. /**
  278. * testCommandeFournisseurCancel
  279. *
  280. * @param CommandeFournisseur $localobject Supplier order
  281. * @return CommandeFournisseur Supplier order
  282. *
  283. * @depends testCommandeFournisseurApprove
  284. * The depends says test is run only if previous is ok
  285. */
  286. public function testCommandeFournisseurCancel($localobject)
  287. {
  288. global $conf,$user,$langs,$db;
  289. $conf=$this->savconf;
  290. $user=$this->savuser;
  291. $langs=$this->savlangs;
  292. $db=$this->savdb;
  293. $result=$localobject->cancel($user);
  294. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  295. $this->assertLessThan($result, 0);
  296. return $localobject;
  297. }
  298. /**
  299. * testCommandeFournisseurOther
  300. *
  301. * @param CommandeFournisseur $localobject Supplier order
  302. * @return int Id of purchase order
  303. *
  304. * @depends testCommandeFournisseurCancel
  305. * The depends says test is run only if previous is ok
  306. */
  307. public function testCommandeFournisseurOther($localobject)
  308. {
  309. global $conf,$user,$langs,$db;
  310. $conf=$this->savconf;
  311. $user=$this->savuser;
  312. $langs=$this->savlangs;
  313. $db=$this->savdb;
  314. /*$result=$localobject->setstatus(0);
  315. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  316. $this->assertLessThan($result, 0);
  317. */
  318. /*$localobject->info($localobject->id);
  319. print __METHOD__." localobject->date_creation=".$localobject->date_creation."\n";
  320. $this->assertNotEquals($localobject->date_creation, '');
  321. */
  322. $this->assertEquals(1, 1);
  323. return $localobject->id;
  324. }
  325. /**
  326. * testCommandeFournisseurDelete
  327. *
  328. * @param int $id Id of order
  329. * @return int Result of delete
  330. *
  331. * @depends testCommandeFournisseurOther
  332. * The depends says test is run only if previous is ok
  333. */
  334. public function testCommandeFournisseurDelete($id)
  335. {
  336. global $conf,$user,$langs,$db;
  337. $conf=$this->savconf;
  338. $user=$this->savuser;
  339. $langs=$this->savlangs;
  340. $db=$this->savdb;
  341. $localobject=new CommandeFournisseur($db);
  342. $result=$localobject->fetch($id);
  343. $result=$localobject->delete($user);
  344. print __METHOD__." id=".$id." result=".$result."\n";
  345. $this->assertLessThan($result, 0);
  346. return $result;
  347. }
  348. }