FactureTest.php 12 KB

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