WebservicesThirdpartyTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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/WebservicesThirdpartyTest.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 WebservicesThirdpartyTest extends PHPUnit\Framework\TestCase
  46. {
  47. protected $savconf;
  48. protected $savuser;
  49. protected $savlangs;
  50. protected $savdb;
  51. protected $soapclient;
  52. private $ns='http://www.dolibarr.org/ns/';
  53. /**
  54. * Constructor
  55. * We save global variables into local variables
  56. *
  57. * @param string $name Name
  58. * @return WebservicesThirdpartyTest
  59. */
  60. public function __construct($name = '')
  61. {
  62. parent::__construct($name);
  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. $WS_DOL_URL = DOL_MAIN_URL_ROOT.'/webservices/server_thirdparty.php';
  70. // Set the WebService URL
  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(): void
  87. {
  88. global $conf,$user,$langs,$db;
  89. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  90. print __METHOD__."\n";
  91. }
  92. /**
  93. * tearDownAfterClass
  94. *
  95. * @return void
  96. */
  97. public static function tearDownAfterClass(): void
  98. {
  99. global $conf,$user,$langs,$db;
  100. $db->rollback();
  101. print __METHOD__."\n";
  102. }
  103. /**
  104. * Init phpunit tests
  105. *
  106. * @return void
  107. */
  108. protected function setUp(): void
  109. {
  110. global $conf,$user,$langs,$db;
  111. $conf=$this->savconf;
  112. $user=$this->savuser;
  113. $langs=$this->savlangs;
  114. $db=$this->savdb;
  115. print __METHOD__."\n";
  116. }
  117. /**
  118. * End phpunit tests
  119. *
  120. * @return void
  121. */
  122. protected function tearDown(): void
  123. {
  124. print __METHOD__."\n";
  125. }
  126. /**
  127. * testWSThirdpartycreateThirdParty
  128. *
  129. * @return array thirdparty created
  130. */
  131. public function testWSThirdpartycreateThirdParty()
  132. {
  133. global $conf,$user,$langs,$db;
  134. $conf=$this->savconf;
  135. $user=$this->savuser;
  136. $langs=$this->savlangs;
  137. $db=$this->savdb;
  138. $WS_METHOD = 'createThirdParty';
  139. // Call the WebService method and store its result in $result.
  140. $authentication=array(
  141. 'dolibarrkey'=>$conf->global->WEBSERVICES_KEY,
  142. 'sourceapplication'=>'DEMO',
  143. 'login'=>'admin',
  144. 'password'=>'admin',
  145. 'entity'=>'');
  146. $body = array(
  147. "id" => null,
  148. "ref" => "name",
  149. "ref_ext" => "12",
  150. "fk_user_author" => null,
  151. "status" => null,
  152. "client" => 1,
  153. "supplier" => 0,
  154. "customer_code" => "CU0901-5678",
  155. "supplier_code" => "SU0901-5678",
  156. "customer_code_accountancy" => "",
  157. "supplier_code_accountancy" => "",
  158. "date_creation" => "", // dateTime
  159. "date_modification" => "", // dateTime
  160. "note_private" => "",
  161. "note_public" => "",
  162. "address" => "",
  163. "zip" => "",
  164. "town" => "",
  165. "province_id" => "",
  166. "country_id" => "",
  167. "country_code" => "",
  168. "country" => "",
  169. "phone" => "",
  170. "fax" => "",
  171. "email" => "",
  172. "url" => "",
  173. "profid1" => "",
  174. "profid2" => "",
  175. "profid3" => "",
  176. "profid4" => "",
  177. "profid5" => "",
  178. "profid6" => "",
  179. "capital" => "",
  180. "vat_used" => "",
  181. "vat_number" => ""
  182. );
  183. // Test URL
  184. $result='';
  185. $parameters = array('authentication'=>$authentication, 'thirdparty'=>$body);
  186. print __METHOD__." call method ".$WS_METHOD."\n";
  187. try {
  188. $result = $this->soapclient->call($WS_METHOD, $parameters, $this->ns, '');
  189. } catch (SoapFault $exception) {
  190. echo $exception;
  191. $result=0;
  192. }
  193. if (! $result || !empty($result['faultstring'])) {
  194. //var_dump($soapclient);
  195. print $this->soapclient->error_str;
  196. print "\n<br>\n";
  197. print $this->soapclient->request;
  198. print "\n<br>\n";
  199. print $this->soapclient->response;
  200. print "\n";
  201. }
  202. print __METHOD__." result=".$result['result']['result_code']."\n";
  203. $this->assertEquals('OK', $result['result']['result_code']);
  204. $this->assertEquals('name', $result['ref']);
  205. return $result;
  206. }
  207. /**
  208. * testWSThirdpartygetThirdPartyById
  209. *
  210. * Use id to retrieve thirdparty
  211. * @depends testWSThirdpartycreateThirdParty
  212. *
  213. * @param array $result thirdparty created by create method
  214. * @return array thirpdarty updated
  215. */
  216. public function testWSThirdpartygetThirdPartyById($result)
  217. {
  218. global $conf,$user,$langs,$db;
  219. $conf=$this->savconf;
  220. $user=$this->savuser;
  221. $langs=$this->savlangs;
  222. $db=$this->savdb;
  223. $id = $result['id'];
  224. $WS_METHOD = 'getThirdParty';
  225. // Call the WebService method and store its result in $result.
  226. $authentication=array(
  227. 'dolibarrkey'=>$conf->global->WEBSERVICES_KEY,
  228. 'sourceapplication'=>'DEMO',
  229. 'login'=>'admin',
  230. 'password'=>'admin',
  231. 'entity'=>'');
  232. $result='';
  233. $parameters = array('authentication'=>$authentication, 'id'=>$id);
  234. print __METHOD__." call method ".$WS_METHOD."\n";
  235. try {
  236. $result = $this->soapclient->call($WS_METHOD, $parameters, $this->ns, '');
  237. } catch (SoapFault $exception) {
  238. echo $exception;
  239. $result=0;
  240. }
  241. if (! $result || !empty($result['faultstring'])) {
  242. //var_dump($soapclient);
  243. print $this->soapclient->error_str;
  244. print "\n<br>\n";
  245. print $this->soapclient->request;
  246. print "\n<br>\n";
  247. print $this->soapclient->response;
  248. print "\n";
  249. }
  250. print __METHOD__." result=".$result['result']['result_code']."\n";
  251. $this->assertEquals('OK', $result['result']['result_code']);
  252. $this->assertEquals($id, $result['thirdparty']['id']);
  253. $this->assertEquals('name', $result['thirdparty']['ref']);
  254. $this->assertEquals('12', $result['thirdparty']['ref_ext']);
  255. $this->assertEquals('0', $result['thirdparty']['status']);
  256. $this->assertEquals('1', $result['thirdparty']['client']);
  257. $this->assertEquals('0', $result['thirdparty']['supplier']);
  258. return $result;
  259. }
  260. /**
  261. * testWSThirdpartygetThirdPartyByRefExt
  262. *
  263. * Use ref_ext to retrieve thirdparty
  264. *
  265. * @depends testWSThirdpartycreateThirdParty
  266. *
  267. * @param array $result thirdparty created by create method
  268. * @return array thirdparty
  269. */
  270. public function testWSThirdpartygetThirdPartyByRefExt($result)
  271. {
  272. global $conf,$user,$langs,$db;
  273. $conf=$this->savconf;
  274. $user=$this->savuser;
  275. $langs=$this->savlangs;
  276. $db=$this->savdb;
  277. $id = $result['id'];
  278. $WS_METHOD = 'getThirdParty';
  279. // Call the WebService method and store its result in $result.
  280. $authentication=array(
  281. 'dolibarrkey'=>$conf->global->WEBSERVICES_KEY,
  282. 'sourceapplication'=>'DEMO',
  283. 'login'=>'admin',
  284. 'password'=>'admin',
  285. 'entity'=>'');
  286. // Test URL
  287. $result='';
  288. $parameters = array('authentication'=>$authentication, 'id'=>'', 'ref'=>'', 'ref_ext'=>'12');
  289. print __METHOD__." call method ".$WS_METHOD."\n";
  290. try {
  291. $result = $this->soapclient->call($WS_METHOD, $parameters, $this->ns, '');
  292. } catch (SoapFault $exception) {
  293. echo $exception;
  294. $result=0;
  295. }
  296. print $this->soapclient->response;
  297. if (! $result || !empty($result['faultstring'])) {
  298. //var_dump($soapclient);
  299. print $this->soapclient->error_str;
  300. print "\n<br>\n";
  301. print $this->soapclient->request;
  302. print "\n<br>\n";
  303. print $this->soapclient->response;
  304. print "\n";
  305. }
  306. print __METHOD__." result=".$result['result']['result_code']."\n";
  307. $this->assertEquals('OK', $result['result']['result_code']);
  308. $this->assertEquals($id, $result['thirdparty']['id']);
  309. $this->assertEquals('name', $result['thirdparty']['ref']);
  310. $this->assertEquals('12', $result['thirdparty']['ref_ext']);
  311. $this->assertEquals('0', $result['thirdparty']['status']);
  312. $this->assertEquals('1', $result['thirdparty']['client']);
  313. $this->assertEquals('0', $result['thirdparty']['supplier']);
  314. return $result;
  315. }
  316. /**
  317. * testWSThirdpartydeleteThirdParty
  318. *
  319. * @depends testWSThirdpartycreateThirdParty
  320. *
  321. * @param array $result thirdparty created by create method
  322. * @return array thirdparty
  323. */
  324. public function testWSThirdpartydeleteThirdPartyById($result)
  325. {
  326. global $conf,$user,$langs,$db;
  327. $conf=$this->savconf;
  328. $user=$this->savuser;
  329. $langs=$this->savlangs;
  330. $db=$this->savdb;
  331. $id = $result['id'];
  332. $WS_METHOD = 'deleteThirdParty';
  333. // Call the WebService method and store its result in $result.
  334. $authentication=array(
  335. 'dolibarrkey'=>$conf->global->WEBSERVICES_KEY,
  336. 'sourceapplication'=>'DEMO',
  337. 'login'=>'admin',
  338. 'password'=>'admin',
  339. 'entity'=>'');
  340. $result='';
  341. $parameters = array('authentication'=>$authentication, 'id'=>$id, 'ref'=>'', 'ref_ext'=>'');
  342. print __METHOD__." call method ".$WS_METHOD."\n";
  343. try {
  344. $result = $this->soapclient->call($WS_METHOD, $parameters, $this->ns, '');
  345. } catch (SoapFault $exception) {
  346. echo $exception;
  347. $result=0;
  348. }
  349. if (! $result || !empty($result['faultstring'])) {
  350. print 'Error: '.$this->soapclient->error_str;
  351. print "\n<br>\n";
  352. print $this->soapclient->request;
  353. print "\n<br>\n";
  354. print $this->soapclient->response;
  355. print "\n";
  356. }
  357. print __METHOD__." count(result)=".(is_array($result) ? count($result) : 0)."\n";
  358. $this->assertEquals('OK', $result['result']['result_code']);
  359. return $result;
  360. }
  361. }