WebservicesInvoicesTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <?php
  2. /* Copyright (C) 2010 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2023 Alexandre Janniaux <alexandre.janniaux@gmail.com>
  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/WebservicesInvoicesTest.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/core/lib/date.lib.php';
  30. require_once NUSOAP_PATH.'/nusoap.php'; // Include SOAP
  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. $conf->global->MAIN_UMASK='0666';
  38. /**
  39. * Class for PHPUnit tests
  40. *
  41. * @backupGlobals disabled
  42. * @backupStaticAttributes enabled
  43. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  44. */
  45. class WebservicesInvoicesTest extends PHPUnit\Framework\TestCase
  46. {
  47. protected $savconf;
  48. protected $savuser;
  49. protected $savlangs;
  50. protected $savdb;
  51. protected $soapclient;
  52. private static $socid;
  53. protected $ns = 'http://www.dolibarr.org/ns/';
  54. protected $pass = 'admin';
  55. /**
  56. * Constructor
  57. * We save global variables into local variables
  58. *
  59. * @param string $name Name
  60. * @return WebservicesInvoicesTest
  61. */
  62. public function __construct($name = '')
  63. {
  64. parent::__construct($name);
  65. //$this->sharedFixture
  66. global $conf,$user,$langs,$db;
  67. $this->savconf=$conf;
  68. $this->savuser=$user;
  69. $this->savlangs=$langs;
  70. $this->savdb=$db;
  71. // Set the WebService URL
  72. $WS_DOL_URL = DOL_MAIN_URL_ROOT.'/webservices/server_invoice.php';
  73. print __METHOD__." create nusoap_client for URL=".$WS_DOL_URL."\n";
  74. $this->soapclient = new nusoap_client($WS_DOL_URL);
  75. if ($this->soapclient) {
  76. $this->soapclient->soap_defencoding='UTF-8';
  77. $this->soapclient->decodeUTF8(false);
  78. }
  79. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  80. //print " - db ".$db->db;
  81. print "\n";
  82. }
  83. /**
  84. * setUpBeforeClass
  85. *
  86. * @return void
  87. */
  88. public static function setUpBeforeClass(): void
  89. {
  90. global $conf,$user,$langs,$db;
  91. $now = dol_now();
  92. // create a third_party, needed to create an invoice
  93. //
  94. // The third party is created in setUpBeforeClass() and not in the
  95. // constructor to avoid creating several objects (the constructor is
  96. // called for each test).
  97. //
  98. // The third party must be created before beginning the DB transaction
  99. // because there is a foreign key constraint between invoices and third
  100. // parties (tables: lx_facture and llx_societe) and with MySQL,
  101. // constraints are checked immediately, they are not deferred to
  102. // transaction commit. So if the invoice is created in the same
  103. // transaction than the third party, the FK constraint fails.
  104. // See this post for more detail: http://stackoverflow.com/a/5014744/5187108
  105. $societe=new Societe($db);
  106. $societe->ref='';
  107. $societe->name='name';
  108. $societe->ref_ext='ref-phpunit';
  109. $societe->status=1;
  110. $societe->client=1;
  111. $societe->code_client='CU0901-1234';
  112. $societe->code_fournisseur='SU0901-1234';
  113. $societe->fournisseur=0;
  114. $societe->date_creation=$now;
  115. $societe->tva_assuj=0;
  116. $societe->particulier=0;
  117. $societe->create($user);
  118. if (empty($societe->id)) {
  119. // Create failed, may be the thirdparty already exists, we fetch it
  120. $societe->fetch(0, 'name');
  121. }
  122. self::$socid = $societe->id;
  123. print __METHOD__." societe created or found with id=".$societe->id."\n";
  124. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  125. print __METHOD__."\n";
  126. }
  127. /**
  128. * tearDownAfterClass
  129. *
  130. * @return void
  131. */
  132. public static function tearDownAfterClass(): void
  133. {
  134. global $conf,$user,$langs,$db;
  135. $db->rollback();
  136. print __METHOD__."\n";
  137. }
  138. /**
  139. * Init phpunit tests
  140. *
  141. * @return void
  142. */
  143. protected function setUp(): void
  144. {
  145. global $conf,$user,$langs,$db;
  146. $conf=$this->savconf;
  147. $user=$this->savuser;
  148. $langs=$this->savlangs;
  149. $db=$this->savdb;
  150. print __METHOD__."\n";
  151. }
  152. /**
  153. * End phpunit tests
  154. *
  155. * @return void
  156. */
  157. protected function tearDown(): void
  158. {
  159. print __METHOD__."\n";
  160. }
  161. /**
  162. * testWSInvoicesCreateInvoice
  163. *
  164. * @return int invoice created
  165. */
  166. public function testWSInvoicesCreateInvoice()
  167. {
  168. global $conf,$user,$langs,$db;
  169. $conf=$this->savconf;
  170. $user=$this->savuser;
  171. $langs=$this->savlangs;
  172. $db=$this->savdb;
  173. $WS_METHOD = 'createInvoice';
  174. $body = array(
  175. "id" => null,
  176. "ref" => null,
  177. "ref_ext" => "ref-phpunit-2",
  178. "thirdparty_id" => self::$socid,
  179. "fk_user_author" => null,
  180. "fk_user_valid" => null,
  181. "date" => "2015-04-19 20:16:53",
  182. "date_due" => "",
  183. "date_creation" => "",
  184. "date_validation" => "",
  185. "date_modification" => "",
  186. "type" => "",
  187. "total_net" => "36.30",
  188. "total_vat" => "6.00",
  189. "total" => "42.30",
  190. "payment_mode_id" => 50,
  191. "note_private" => "Synchronised from Prestashop",
  192. "note_public" => "",
  193. "status" => "1",
  194. "close_code" => null ,
  195. "close_note" => null,
  196. "project_id" => null,
  197. "lines" => array(
  198. array("id" => null,
  199. "type" => 0,
  200. "desc" => "Horloge Vinyle Serge",
  201. "vat_rate" => 20,
  202. "qty" => 1,
  203. "unitprice" => "30.000000",
  204. "total_net" => "30.000000",
  205. "total_vat" => "6.00",
  206. "total" => "36.000000",
  207. "date_start" => "",
  208. "date_end" => "",
  209. "payment_mode_id" => "",
  210. "product_id" => "",
  211. "product_ref" => "",
  212. "product_label" => "",
  213. "product_desc" => "" ))
  214. );
  215. // Call the WebService method and store its result in $result.
  216. $authentication=array(
  217. 'dolibarrkey'=>$conf->global->WEBSERVICES_KEY,
  218. 'sourceapplication'=>'DEMO',
  219. 'login'=>'admin',
  220. 'password'=>$this->pass,
  221. 'entity'=>'');
  222. // Test URL
  223. $result='';
  224. $parameters = array('authentication'=>$authentication,'invoice'=>$body);
  225. print __METHOD__." call method ".$WS_METHOD."\n";
  226. try {
  227. $result = $this->soapclient->call($WS_METHOD, $parameters, $this->ns, '');
  228. } catch (SoapFault $exception) {
  229. echo $exception;
  230. $result=0;
  231. }
  232. if (! $result || !empty($result['faultstring'])) {
  233. //var_dump($soapclient);
  234. print $this->soapclient->error_str;
  235. print "\n<br>\n";
  236. print $this->soapclient->request;
  237. print "\n<br>\n";
  238. print $this->soapclient->response;
  239. print "\n";
  240. }
  241. print __METHOD__." result=".$result['result']['result_code']." ".$result['result']['result_label']."\n";
  242. $this->assertEquals('OK', $result['result']['result_code']);
  243. $this->assertEquals('ref-phpunit-2', $result['ref_ext']);
  244. return $result;
  245. }
  246. /**
  247. * testWSInvoicesGetInvoiceByRefExt
  248. *
  249. * Retrieve an invoice using ref_ext
  250. * @depends testWSInvoicesCreateInvoice
  251. *
  252. * @param array $result Invoice created by create method
  253. * @return array Invoice
  254. */
  255. public function testWSInvoicesGetInvoiceByRefExt($result)
  256. {
  257. global $conf,$user,$langs,$db;
  258. $conf=$this->savconf;
  259. $user=$this->savuser;
  260. $langs=$this->savlangs;
  261. $db=$this->savdb;
  262. $WS_METHOD = 'getInvoice';
  263. // Call the WebService method and store its result in $result.
  264. $authentication=array(
  265. 'dolibarrkey'=>$conf->global->WEBSERVICES_KEY,
  266. 'sourceapplication'=>'DEMO',
  267. 'login'=>'admin',
  268. 'password'=>$this->pass,
  269. 'entity'=>'');
  270. // Test URL
  271. $result='';
  272. $parameters = array('authentication'=>$authentication, 'id'=>null, 'ref'=>null, 'ref_ext'=>'ref-phpunit-2');
  273. print __METHOD__." call method ".$WS_METHOD."\n";
  274. try {
  275. $result = $this->soapclient->call($WS_METHOD, $parameters, $this->ns, '');
  276. } catch (SoapFault $exception) {
  277. echo $exception;
  278. $result=0;
  279. }
  280. if (! $result || !empty($result['faultstring'])) {
  281. print $this->soapclient->error_str;
  282. print "\n<br>\n";
  283. print $this->soapclient->request;
  284. print "\n<br>\n";
  285. print $this->soapclient->response;
  286. print "\n";
  287. }
  288. print __METHOD__." result=".$result['result']['result_code']."\n";
  289. $this->assertEquals('OK', $result['result']['result_code']);
  290. $this->assertEquals('ref-phpunit-2', $result['invoice']['ref_ext']);
  291. return $result;
  292. }
  293. /**
  294. * testWSInvoicesUpdateInvoiceByRefExt
  295. *
  296. * Update an invoice using ref_ext
  297. * @depends testWSInvoicesCreateInvoice
  298. *
  299. * @param array $result invoice created by create method
  300. * @return array Invoice
  301. */
  302. public function testWSInvoicesUpdateInvoiceByRefExt($result)
  303. {
  304. global $conf,$user,$langs,$db;
  305. $conf=$this->savconf;
  306. $user=$this->savuser;
  307. $langs=$this->savlangs;
  308. $db=$this->savdb;
  309. $WS_METHOD = 'updateInvoice';
  310. // update status to 2
  311. $body = array(
  312. "id" => null,
  313. "ref" => null,
  314. "ref_ext" => "ref-phpunit-2",
  315. "thirdparty_id" => self::$socid,
  316. "fk_user_author" => null,
  317. "fk_user_valid" => null,
  318. "date" => "2015-04-19 20:16:53",
  319. "date_due" => "",
  320. "date_creation" => "",
  321. "date_validation" => "",
  322. "date_modification" => "",
  323. "type" => "",
  324. "total_net" => "36.30",
  325. "total_vat" => "6.00",
  326. "total" => "42.30",
  327. "payment_mode_id" => 50,
  328. "note_private" => "Synchronised from Prestashop",
  329. "note_public" => "",
  330. "status" => "2",
  331. "close_code" => null ,
  332. "close_note" => null,
  333. "project_id" => null,
  334. "lines" => array(
  335. array(
  336. "id" => null,
  337. "type" => 0,
  338. "desc" => "Horloge Vinyle Serge",
  339. "vat_rate" => 20,
  340. "qty" => "1",
  341. "unitprice" => "30.000000",
  342. "total_net" => "30.000000",
  343. "total_vat" => "6.00",
  344. "total" => "36.000000",
  345. "date_start" => "",
  346. "date_end" => "",
  347. "payment_mode_id" => "",
  348. "product_id" => "",
  349. "product_ref" => "",
  350. "product_label" => "",
  351. "product_desc" => "" ))
  352. );
  353. // Call the WebService method and store its result in $result.
  354. $authentication=array(
  355. 'dolibarrkey'=>$conf->global->WEBSERVICES_KEY,
  356. 'sourceapplication'=>'DEMO',
  357. 'login'=>'admin',
  358. 'password'=>$this->pass,
  359. 'entity'=>'');
  360. // Test URL
  361. $result='';
  362. $parameters = array('authentication'=>$authentication,'invoice'=>$body);
  363. print __METHOD__." call method ".$WS_METHOD."\n";
  364. try {
  365. $result = $this->soapclient->call($WS_METHOD, $parameters, $this->ns, '');
  366. } catch (SoapFault $exception) {
  367. echo $exception;
  368. $result=0;
  369. }
  370. if (! $result || !empty($result['faultstring'])) {
  371. print 'Error: '.$this->soapclient->error_str;
  372. print "\n<br>\n";
  373. print $this->soapclient->request;
  374. print "\n<br>\n";
  375. print $this->soapclient->response;
  376. print "\n";
  377. }
  378. print __METHOD__." count(result)=".(is_array($result) ? count($result) : 0)."\n";
  379. print __METHOD__." result=".$result['result']['result_code'].$result['result']['result_label']."\n";
  380. $this->assertEquals('OK', $result['result']['result_code']);
  381. $this->assertEquals('ref-phpunit-2', $result['ref_ext']);
  382. return $result;
  383. }
  384. }