WebservicesThirdpartyTest.php 11 KB

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