WebservicesInvoicesTest.php 11 KB

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