FactureTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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"; die(1);
  80. }
  81. if (isModEnabled('ecotaxdeee')) {
  82. print __METHOD__." ecotaxdeee module must not be enabled.\n"; die(1);
  83. }
  84. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  85. print __METHOD__."\n";
  86. }
  87. /**
  88. * tearDownAfterClass
  89. *
  90. * @return void
  91. */
  92. public static function tearDownAfterClass(): void
  93. {
  94. global $conf,$user,$langs,$db;
  95. $db->rollback();
  96. print __METHOD__."\n";
  97. }
  98. /**
  99. * Init phpunit tests
  100. *
  101. * @return void
  102. */
  103. protected function setUp(): void
  104. {
  105. global $conf,$user,$langs,$db;
  106. $conf=$this->savconf;
  107. $user=$this->savuser;
  108. $langs=$this->savlangs;
  109. $db=$this->savdb;
  110. print __METHOD__."\n";
  111. }
  112. /**
  113. * End phpunit tests
  114. *
  115. * @return void
  116. */
  117. protected function tearDown(): void
  118. {
  119. print __METHOD__."\n";
  120. }
  121. /**
  122. * testFactureCreate
  123. *
  124. * @return int
  125. */
  126. public function testFactureCreate()
  127. {
  128. global $conf,$user,$langs,$db;
  129. $conf=$this->savconf;
  130. $user=$this->savuser;
  131. $langs=$this->savlangs;
  132. $db=$this->savdb;
  133. $localobject=new Facture($db);
  134. $localobject->initAsSpecimen();
  135. $result=$localobject->create($user);
  136. $this->assertLessThan($result, 0);
  137. print __METHOD__." result=".$result."\n";
  138. return $result;
  139. }
  140. /**
  141. * testFactureFetch
  142. *
  143. * @param int $id Id invoice
  144. * @return int
  145. *
  146. * @depends testFactureCreate
  147. * The depends says test is run only if previous is ok
  148. */
  149. public function testFactureFetch($id)
  150. {
  151. global $conf,$user,$langs,$db;
  152. $conf=$this->savconf;
  153. $user=$this->savuser;
  154. $langs=$this->savlangs;
  155. $db=$this->savdb;
  156. $localobject=new Facture($db);
  157. $result=$localobject->fetch($id);
  158. $this->assertLessThan($result, 0);
  159. print __METHOD__." id=".$id." result=".$result."\n";
  160. return $localobject;
  161. }
  162. /**
  163. * testFactureFetch
  164. *
  165. * @param Facture $localobject Invoice
  166. * @return int
  167. *
  168. * @depends testFactureFetch
  169. * The depends says test is run only if previous is ok
  170. */
  171. public function testFactureUpdate($localobject)
  172. {
  173. global $conf,$user,$langs,$db;
  174. $conf=$this->savconf;
  175. $user=$this->savuser;
  176. $langs=$this->savlangs;
  177. $db=$this->savdb;
  178. $this->changeProperties($localobject);
  179. $result=$localobject->update($user);
  180. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  181. $this->assertLessThan($result, 0);
  182. return $localobject;
  183. }
  184. /**
  185. * testFactureValid
  186. *
  187. * @param Facture $localobject Invoice
  188. * @return void
  189. *
  190. * @depends testFactureUpdate
  191. * The depends says test is run only if previous is ok
  192. */
  193. public function testFactureValid($localobject)
  194. {
  195. global $conf,$user,$langs,$db;
  196. $conf=$this->savconf;
  197. $user=$this->savuser;
  198. $langs=$this->savlangs;
  199. $db=$this->savdb;
  200. $result=$localobject->validate($user);
  201. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  202. $this->assertLessThan($result, 0);
  203. // Test everything are still same than specimen
  204. $newlocalobject=new Facture($db);
  205. $newlocalobject->initAsSpecimen();
  206. $this->changeProperties($newlocalobject);
  207. // Hack to avoid test to be wrong when module sellyoursaas is on
  208. unset($localobject->array_options['options_commission']);
  209. unset($localobject->array_options['options_reseller']);
  210. $arraywithdiff = $this->objCompare(
  211. $localobject,
  212. $newlocalobject,
  213. true,
  214. array(
  215. 'newref','oldref','id','lines','client','thirdparty','brouillon','user_author','date_creation','date_validation','datem','date_modification',
  216. 'ref','statut','status','paye','specimen','ref','actiontypecode','actionmsg2','actionmsg','mode_reglement','cond_reglement',
  217. 'cond_reglement_doc', 'modelpdf',
  218. 'multicurrency_total_ht','multicurrency_total_tva', 'multicurrency_total_ttc','fk_multicurrency','multicurrency_code','multicurrency_tx',
  219. 'retained_warranty' ,'retained_warranty_date_limit', 'retained_warranty_fk_cond_reglement', 'specimen', 'situation_cycle_ref', 'situation_counter', 'situation_final',
  220. 'trackid','user_creat','user_valid'
  221. )
  222. );
  223. $this->assertEquals($arraywithdiff, array()); // Actual, Expected
  224. return $localobject;
  225. }
  226. /**
  227. * testFactureOther
  228. *
  229. * @param Facture $localobject Invoice
  230. * @return int
  231. *
  232. * @depends testFactureValid
  233. * The depends says test is run only if previous is ok
  234. */
  235. public function testFactureOther($localobject)
  236. {
  237. global $conf,$user,$langs,$db;
  238. $conf=$this->savconf;
  239. $user=$this->savuser;
  240. $langs=$this->savlangs;
  241. $db=$this->savdb;
  242. /*$result=$localobject->setstatus(0);
  243. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  244. $this->assertLessThan($result, 0);
  245. */
  246. $localobject->info($localobject->id);
  247. print __METHOD__." localobject->date_creation=".$localobject->date_creation."\n";
  248. $this->assertNotEquals($localobject->date_creation, '');
  249. $result=$localobject->demande_prelevement($user);
  250. print __METHOD__." result=".$result."\n";
  251. $this->assertLessThan($result, 0);
  252. return $localobject->id;
  253. }
  254. /**
  255. * testFactureDelete
  256. *
  257. * @param int $id Id of invoice
  258. * @return int
  259. *
  260. * @depends testFactureOther
  261. * The depends says test is run only if previous is ok
  262. */
  263. public function testFactureDelete($id)
  264. {
  265. global $conf,$user,$langs,$db;
  266. $conf=$this->savconf;
  267. $user=$this->savuser;
  268. $langs=$this->savlangs;
  269. $db=$this->savdb;
  270. // Force default setup
  271. unset($conf->global->INVOICE_CAN_ALWAYS_BE_REMOVED);
  272. unset($conf->global->INVOICE_CAN_NEVER_BE_REMOVED);
  273. $localobject=new Facture($db);
  274. $result=$localobject->fetch($id);
  275. // Create another invoice and validate it after $localobject
  276. $localobject2=new Facture($db);
  277. $result=$localobject2->initAsSpecimen();
  278. $result=$localobject2->create($user);
  279. $result=$localobject2->validate($user);
  280. print 'Invoice $localobject ref = '.$localobject->ref."\n";
  281. print 'Invoice $localobject2 created with ref = '.$localobject2->ref."\n";
  282. $conf->global->INVOICE_CAN_NEVER_BE_REMOVED = 1;
  283. $result=$localobject2->delete($user); // Deletion is KO, option INVOICE_CAN_NEVER_BE_REMOVED is on
  284. print __METHOD__." id=".$localobject2->id." ref=".$localobject2->ref." result=".$result."\n";
  285. $this->assertEquals(0, $result, 'Deletion should fail, option INVOICE_CAN_NEVER_BE_REMOVED is on');
  286. unset($conf->global->INVOICE_CAN_NEVER_BE_REMOVED);
  287. $result=$localobject->delete($user); // Deletion is KO, it is not last invoice
  288. print __METHOD__." id=".$localobject->id." ref=".$localobject->ref." result=".$result."\n";
  289. $this->assertEquals(0, $result, 'Deletion should fail, it is not last invoice');
  290. $result=$localobject2->delete($user); // Deletion is OK, it is last invoice
  291. print __METHOD__." id=".$localobject2->id." ref=".$localobject2->ref." result=".$result."\n";
  292. $this->assertGreaterThan(0, $result, 'Deletion should work, it is last invoice');
  293. $result=$localobject->delete($user); // Deletion is KO, it is not last invoice
  294. print __METHOD__." id=".$localobject->id." ref=".$localobject->ref." result=".$result."\n";
  295. $this->assertGreaterThan(0, $result, 'Deletion should work, it is again last invoice');
  296. return $result;
  297. }
  298. /**
  299. * Edit an object to test updates
  300. *
  301. * @param Facture $localobject Object Facture
  302. * @return void
  303. */
  304. public function changeProperties(&$localobject)
  305. {
  306. $localobject->note_private='New note';
  307. //$localobject->note='New note after update';
  308. }
  309. /**
  310. * Compare all public properties values of 2 objects
  311. *
  312. * @param Object $oA Object operand 1
  313. * @param Object $oB Object operand 2
  314. * @param boolean $ignoretype False will not report diff if type of value differs
  315. * @param array $fieldstoignorearray Array of fields to ignore in diff
  316. * @return array Array with differences
  317. */
  318. public function objCompare($oA, $oB, $ignoretype = true, $fieldstoignorearray = array('id'))
  319. {
  320. $retAr=array();
  321. if (get_class($oA) !== get_class($oB)) {
  322. $retAr[]="Supplied objects are not of same class.";
  323. } else {
  324. $oVarsA=get_object_vars($oA);
  325. $oVarsB=get_object_vars($oB);
  326. $aKeys=array_keys($oVarsA);
  327. foreach ($aKeys as $sKey) {
  328. if (in_array($sKey, $fieldstoignorearray)) {
  329. continue;
  330. }
  331. if (! $ignoretype && ($oVarsA[$sKey] !== $oVarsB[$sKey])) {
  332. $retAr[]=$sKey.' : '.(is_object($oVarsA[$sKey])?get_class($oVarsA[$sKey]):$oVarsA[$sKey]).' <> '.(is_object($oVarsB[$sKey])?get_class($oVarsB[$sKey]):$oVarsB[$sKey]);
  333. }
  334. if ($ignoretype && ($oVarsA[$sKey] != $oVarsB[$sKey])) {
  335. $retAr[]=$sKey.' : '.(is_object($oVarsA[$sKey])?get_class($oVarsA[$sKey]):$oVarsA[$sKey]).' <> '.(is_object($oVarsB[$sKey])?get_class($oVarsB[$sKey]):$oVarsB[$sKey]);
  336. }
  337. }
  338. }
  339. return $retAr;
  340. }
  341. }