FactureTest.php 11 KB

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