FactureTest.php 12 KB

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