FactureTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. /* Copyright (C) 2010 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  4. * Copyright (C) 2023 Alexandre Janniaux <alexandre.janniaux@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. * or see https://www.gnu.org/
  19. */
  20. /**
  21. * \file test/phpunit/FactureTest.php
  22. * \ingroup test
  23. * \brief PHPUnit test
  24. * \remarks To run this script as CLI: phpunit filename.php
  25. */
  26. global $conf,$user,$langs,$db;
  27. //define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
  28. //require_once 'PHPUnit/Autoload.php';
  29. require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
  30. require_once dirname(__FILE__).'/../../htdocs/compta/facture/class/facture.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 FactureTest 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 FactureTest
  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. if (!isModEnabled('facture')) {
  79. print __METHOD__." module customer invoice must be enabled.\n";
  80. die(1);
  81. }
  82. if (isModEnabled('ecotaxdeee')) {
  83. print __METHOD__." ecotaxdeee module must not be enabled.\n";
  84. die(1);
  85. }
  86. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  87. print __METHOD__."\n";
  88. }
  89. /**
  90. * tearDownAfterClass
  91. *
  92. * @return void
  93. */
  94. public static function tearDownAfterClass(): void
  95. {
  96. global $conf,$user,$langs,$db;
  97. $db->rollback();
  98. print __METHOD__."\n";
  99. }
  100. /**
  101. * Init phpunit tests
  102. *
  103. * @return void
  104. */
  105. protected function setUp(): void
  106. {
  107. global $conf,$user,$langs,$db;
  108. $conf=$this->savconf;
  109. $user=$this->savuser;
  110. $langs=$this->savlangs;
  111. $db=$this->savdb;
  112. print __METHOD__."\n";
  113. }
  114. /**
  115. * End phpunit tests
  116. *
  117. * @return void
  118. */
  119. protected function tearDown(): void
  120. {
  121. print __METHOD__."\n";
  122. }
  123. /**
  124. * testFactureCreate
  125. *
  126. * @return int
  127. */
  128. public function testFactureCreate()
  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 Facture($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. * testFactureFetch
  144. *
  145. * @param int $id Id invoice
  146. * @return int
  147. *
  148. * @depends testFactureCreate
  149. * The depends says test is run only if previous is ok
  150. */
  151. public function testFactureFetch($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 Facture($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. * testFactureFetch
  166. *
  167. * @param Facture $localobject Invoice
  168. * @return int
  169. *
  170. * @depends testFactureFetch
  171. * The depends says test is run only if previous is ok
  172. */
  173. public function testFactureUpdate($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. $this->changeProperties($localobject);
  181. $result=$localobject->update($user);
  182. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  183. $this->assertLessThan($result, 0);
  184. return $localobject;
  185. }
  186. /**
  187. * testFactureValid
  188. *
  189. * @param Facture $localobject Invoice
  190. * @return void
  191. *
  192. * @depends testFactureUpdate
  193. * The depends says test is run only if previous is ok
  194. */
  195. public function testFactureValid($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=$localobject->validate($user);
  203. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  204. $this->assertLessThan($result, 0);
  205. // Test everything are still same than specimen
  206. $newlocalobject=new Facture($db);
  207. $newlocalobject->initAsSpecimen();
  208. $this->changeProperties($newlocalobject);
  209. // Hack to avoid test to be wrong when module sellyoursaas is on
  210. unset($localobject->array_options['options_commission']);
  211. unset($localobject->array_options['options_reseller']);
  212. $arraywithdiff = $this->objCompare(
  213. $localobject,
  214. $newlocalobject,
  215. true,
  216. array(
  217. 'newref','oldref','id','lines','client','thirdparty','brouillon','user_creation_id','date_creation','date_validation','datem','date_modification',
  218. 'ref','statut','status','paye','specimen','ref','actiontypecode','actionmsg2','actionmsg','mode_reglement','cond_reglement',
  219. 'cond_reglement_doc', 'modelpdf',
  220. 'multicurrency_total_ht','multicurrency_total_tva', 'multicurrency_total_ttc','fk_multicurrency','multicurrency_code','multicurrency_tx',
  221. 'retained_warranty' ,'retained_warranty_date_limit', 'retained_warranty_fk_cond_reglement', 'specimen', 'situation_cycle_ref', 'situation_counter', 'situation_final',
  222. 'trackid','user_creat','user_valid'
  223. )
  224. );
  225. $this->assertEquals($arraywithdiff, array()); // Actual, Expected
  226. return $localobject;
  227. }
  228. /**
  229. * testFactureOther
  230. *
  231. * @param Facture $localobject Invoice
  232. * @return int
  233. *
  234. * @depends testFactureValid
  235. * The depends says test is run only if previous is ok
  236. */
  237. public function testFactureOther($localobject)
  238. {
  239. global $conf,$user,$langs,$db;
  240. $conf=$this->savconf;
  241. $user=$this->savuser;
  242. $langs=$this->savlangs;
  243. $db=$this->savdb;
  244. /*$result=$localobject->setstatus(0);
  245. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  246. $this->assertLessThan($result, 0);
  247. */
  248. $localobject->info($localobject->id);
  249. print __METHOD__." localobject->date_creation=".$localobject->date_creation."\n";
  250. $this->assertNotEquals($localobject->date_creation, '');
  251. $result=$localobject->demande_prelevement($user);
  252. print __METHOD__." result=".$result."\n";
  253. $this->assertLessThan($result, 0);
  254. return $localobject->id;
  255. }
  256. /**
  257. * testFactureDelete
  258. *
  259. * @param int $id Id of invoice
  260. * @return int
  261. *
  262. * @depends testFactureOther
  263. * The depends says test is run only if previous is ok
  264. */
  265. public function testFactureDelete($id)
  266. {
  267. global $conf,$user,$langs,$db;
  268. $conf=$this->savconf;
  269. $user=$this->savuser;
  270. $langs=$this->savlangs;
  271. $db=$this->savdb;
  272. // Force default setup
  273. unset($conf->global->INVOICE_CAN_ALWAYS_BE_REMOVED);
  274. unset($conf->global->INVOICE_CAN_NEVER_BE_REMOVED);
  275. $localobject=new Facture($db);
  276. $result=$localobject->fetch($id);
  277. // Create another invoice and validate it after $localobject
  278. $localobject2=new Facture($db);
  279. $result=$localobject2->initAsSpecimen();
  280. $result=$localobject2->create($user);
  281. $result=$localobject2->validate($user);
  282. print 'Invoice $localobject ref = '.$localobject->ref."\n";
  283. print 'Invoice $localobject2 created with ref = '.$localobject2->ref."\n";
  284. $conf->global->INVOICE_CAN_NEVER_BE_REMOVED = 1;
  285. $result=$localobject2->delete($user); // Deletion is KO, option INVOICE_CAN_NEVER_BE_REMOVED is on
  286. print __METHOD__." id=".$localobject2->id." ref=".$localobject2->ref." result=".$result."\n";
  287. $this->assertEquals(0, $result, 'Deletion should fail, option INVOICE_CAN_NEVER_BE_REMOVED is on');
  288. unset($conf->global->INVOICE_CAN_NEVER_BE_REMOVED);
  289. $result=$localobject->delete($user); // Deletion is KO, it is not last invoice
  290. print __METHOD__." id=".$localobject->id." ref=".$localobject->ref." result=".$result."\n";
  291. $this->assertEquals(0, $result, 'Deletion should fail, it is not last invoice');
  292. $result=$localobject2->delete($user); // Deletion is OK, it is last invoice
  293. print __METHOD__." id=".$localobject2->id." ref=".$localobject2->ref." result=".$result."\n";
  294. $this->assertGreaterThan(0, $result, 'Deletion should work, it is last invoice');
  295. $result=$localobject->delete($user); // Deletion is KO, it is not last invoice
  296. print __METHOD__." id=".$localobject->id." ref=".$localobject->ref." result=".$result."\n";
  297. $this->assertGreaterThan(0, $result, 'Deletion should work, it is again last invoice');
  298. return $result;
  299. }
  300. /**
  301. * Edit an object to test updates
  302. *
  303. * @param Facture $localobject Object Facture
  304. * @return void
  305. */
  306. public function changeProperties(&$localobject)
  307. {
  308. $localobject->note_private='New note';
  309. //$localobject->note='New note after update';
  310. }
  311. /**
  312. * Compare all public properties values of 2 objects
  313. *
  314. * @param Object $oA Object operand 1
  315. * @param Object $oB Object operand 2
  316. * @param boolean $ignoretype False will not report diff if type of value differs
  317. * @param array $fieldstoignorearray Array of fields to ignore in diff
  318. * @return array Array with differences
  319. */
  320. public function objCompare($oA, $oB, $ignoretype = true, $fieldstoignorearray = array('id'))
  321. {
  322. $retAr=array();
  323. if (get_class($oA) !== get_class($oB)) {
  324. $retAr[]="Supplied objects are not of same class.";
  325. } else {
  326. $oVarsA=get_object_vars($oA);
  327. $oVarsB=get_object_vars($oB);
  328. $aKeys=array_keys($oVarsA);
  329. foreach ($aKeys as $sKey) {
  330. if (in_array($sKey, $fieldstoignorearray)) {
  331. continue;
  332. }
  333. if (! $ignoretype && ($oVarsA[$sKey] !== $oVarsB[$sKey])) {
  334. $retAr[]=$sKey.' : '.(is_object($oVarsA[$sKey]) ? get_class($oVarsA[$sKey]) : $oVarsA[$sKey]).' <> '.(is_object($oVarsB[$sKey]) ? get_class($oVarsB[$sKey]) : $oVarsB[$sKey]);
  335. }
  336. if ($ignoretype && ($oVarsA[$sKey] != $oVarsB[$sKey])) {
  337. $retAr[]=$sKey.' : '.(is_object($oVarsA[$sKey]) ? get_class($oVarsA[$sKey]) : $oVarsA[$sKey]).' <> '.(is_object($oVarsB[$sKey]) ? get_class($oVarsB[$sKey]) : $oVarsB[$sKey]);
  338. }
  339. }
  340. }
  341. return $retAr;
  342. }
  343. }