SupplierProposalTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. /* Copyright (C) 2017 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/SupplierProposalTest.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/supplier_proposal/class/supplier_proposal.class.php';
  30. if (empty($user->id)) {
  31. print "Load permissions for user nb 1 (that should be admin)\n";
  32. $user->fetch(1);
  33. //$user->addrights(0, 'supplier_proposal');
  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 SupplierProposalTest 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 SupplierProposalTest
  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. if (empty($conf->global->MAIN_MODULE_SUPPLIERPROPOSAL)) {
  80. print "\n".__METHOD__." module Supplier proposal must be enabled.\n"; die(1);
  81. }
  82. print __METHOD__."\n";
  83. }
  84. /**
  85. * tearDownAfterClass
  86. *
  87. * @return void
  88. */
  89. public static function tearDownAfterClass(): void
  90. {
  91. global $conf,$user,$langs,$db;
  92. $db->rollback();
  93. print __METHOD__."\n";
  94. }
  95. /**
  96. * Init phpunit tests
  97. *
  98. * @return void
  99. */
  100. protected function setUp(): void
  101. {
  102. global $conf,$user,$langs,$db;
  103. $conf=$this->savconf;
  104. $user=$this->savuser;
  105. $langs=$this->savlangs;
  106. $db=$this->savdb;
  107. print __METHOD__."\n";
  108. //print $db->getVersion()."\n";
  109. // Set permission not set by default sql sample
  110. $user->addrights(0, 'supplier_proposal');
  111. $user->getrights('supplier_proposal', 1);
  112. }
  113. /**
  114. * End phpunit tests
  115. *
  116. * @return void
  117. */
  118. protected function tearDown(): void
  119. {
  120. print __METHOD__."\n";
  121. }
  122. /**
  123. * testSupplierProposalCreate
  124. *
  125. * @return void
  126. */
  127. public function testSupplierProposalCreate()
  128. {
  129. global $conf,$user,$langs,$db;
  130. $conf=$this->savconf;
  131. $user=$this->savuser;
  132. $langs=$this->savlangs;
  133. $db=$this->savdb;
  134. $localobject=new SupplierProposal($db);
  135. $localobject->initAsSpecimen();
  136. $result=$localobject->create($user);
  137. $this->assertLessThan($result, 0);
  138. print __METHOD__." result=".$result."\n";
  139. return $result;
  140. }
  141. /**
  142. * testSupplierProposalFetch
  143. *
  144. * @param int $id Id of object
  145. * @return void
  146. *
  147. * @depends testSupplierProposalCreate
  148. * The depends says test is run only if previous is ok
  149. */
  150. public function testSupplierProposalFetch($id)
  151. {
  152. global $conf,$user,$langs,$db;
  153. $conf=$this->savconf;
  154. $user=$this->savuser;
  155. $langs=$this->savlangs;
  156. $db=$this->savdb;
  157. $localobject=new SupplierProposal($db);
  158. $result=$localobject->fetch($id);
  159. $this->assertLessThan($result, 0);
  160. print __METHOD__." id=".$id." result=".$result."\n";
  161. return $localobject;
  162. }
  163. /**
  164. * testSupplierProposalAddLine
  165. *
  166. * @param SupplierProposal $localobject Proposal
  167. * @return void
  168. *
  169. * @depends testSupplierProposalFetch
  170. * The depends says test is run only if previous is ok
  171. */
  172. public function testSupplierProposalAddLine($localobject)
  173. {
  174. global $conf,$user,$langs,$db;
  175. $conf=$this->savconf;
  176. $user=$this->savuser;
  177. $langs=$this->savlangs;
  178. $db=$this->savdb;
  179. $localobject->fetch_thirdparty();
  180. $result=$localobject->addline('Added line', 10, 2, 19.6);
  181. $this->assertLessThan($result, 0);
  182. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  183. return $localobject;
  184. }
  185. /**
  186. * testSupplierProposalValid
  187. *
  188. * @param SupplierProposal $localobject Proposal
  189. * @return SupplierProposal
  190. *
  191. * @depends testSupplierProposalAddLine
  192. * The depends says test is run only if previous is ok
  193. */
  194. public function testSupplierProposalValid($localobject)
  195. {
  196. global $conf,$user,$langs,$db;
  197. $conf=$this->savconf;
  198. $user=$this->savuser;
  199. $langs=$this->savlangs;
  200. $db=$this->savdb;
  201. $result = $user->addrights(0, 'supplier_proposal');
  202. $this->assertLessThan($result, 0);
  203. $result = $user->getrights('supplier_proposal', 1);
  204. //$this->assertLessThan($result, 0);
  205. $result=$localobject->valid($user);
  206. $this->assertLessThan($result, 0);
  207. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  208. $this->assertLessThan($result, 0);
  209. return $localobject;
  210. }
  211. /**
  212. * testSupplierProposalOther
  213. *
  214. * @param SupplierProposal $localobject Proposal
  215. * @return int
  216. *
  217. * @depends testSupplierProposalValid
  218. * The depends says test is run only if previous is ok
  219. */
  220. public function testSupplierProposalOther($localobject)
  221. {
  222. global $conf,$user,$langs,$db;
  223. $conf=$this->savconf;
  224. $user=$this->savuser;
  225. $langs=$this->savlangs;
  226. $db=$this->savdb;
  227. /*$result=$localobject->setstatus(0);
  228. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  229. $this->assertLessThan($result, 0);
  230. */
  231. $localobject->info($localobject->id);
  232. print __METHOD__." localobject->date_creation=".$localobject->date_creation."\n";
  233. $this->assertNotEquals($localobject->date_creation, '');
  234. return $localobject->id;
  235. }
  236. /**
  237. * testSupplierProposalDelete
  238. *
  239. * @param int $id Id of proposal
  240. * @return void
  241. *
  242. * @depends testSupplierProposalOther
  243. * The depends says test is run only if previous is ok
  244. */
  245. public function testSupplierProposalDelete($id)
  246. {
  247. global $conf,$user,$langs,$db;
  248. $conf=$this->savconf;
  249. $user=$this->savuser;
  250. $langs=$this->savlangs;
  251. $db=$this->savdb;
  252. $localobject=new SupplierProposal($db);
  253. $result=$localobject->fetch($id);
  254. $result=$localobject->delete($user);
  255. print __METHOD__." id=".$id." result=".$result."\n";
  256. $this->assertLessThan($result, 0);
  257. return $result;
  258. }
  259. }