SupplierProposalTest.php 7.2 KB

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