WebservicesProductsTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. * Path to WSDL is: http://localhost/dolibarr/webservices/server_productorservice.php?wsdl
  20. */
  21. /**
  22. * \file test/phpunit/WebservicesProductsTest.php
  23. * \ingroup test
  24. * \brief PHPUnit test
  25. * \remarks To run this script as CLI: phpunit filename.php
  26. */
  27. global $conf,$user,$langs,$db;
  28. //define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
  29. //require_once 'PHPUnit/Autoload.php';
  30. require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
  31. require_once dirname(__FILE__).'/../../htdocs/core/lib/date.lib.php';
  32. require_once NUSOAP_PATH.'/nusoap.php'; // Include SOAP
  33. if (empty($user->id)) {
  34. print "Load permissions for admin user nb 1\n";
  35. $user->fetch(1);
  36. $user->getrights();
  37. }
  38. $conf->global->MAIN_DISABLE_ALL_MAILS=1;
  39. $conf->global->MAIN_UMASK='0666';
  40. if (!isModEnabled('service')) {
  41. print "Error: Module service must be enabled.\n";
  42. exit(1);
  43. }
  44. /**
  45. * Class for PHPUnit tests
  46. *
  47. * @backupGlobals disabled
  48. * @backupStaticAttributes enabled
  49. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  50. */
  51. class WebservicesProductsTest extends PHPUnit\Framework\TestCase
  52. {
  53. protected $savconf;
  54. protected $savuser;
  55. protected $savlangs;
  56. protected $savdb;
  57. /**
  58. * Constructor
  59. * We save global variables into local variables
  60. *
  61. * @param string $name Name
  62. * @return WebservicesProductsTest
  63. */
  64. public function __construct($name = '')
  65. {
  66. parent::__construct($name);
  67. //$this->sharedFixture
  68. global $conf,$user,$langs,$db;
  69. $this->savconf=$conf;
  70. $this->savuser=$user;
  71. $this->savlangs=$langs;
  72. $this->savdb=$db;
  73. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  74. //print " - db ".$db->db;
  75. print "\n";
  76. }
  77. /**
  78. * setUpBeforeClass
  79. *
  80. * @return void
  81. */
  82. public static function setUpBeforeClass(): void
  83. {
  84. global $conf,$user,$langs,$db;
  85. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  86. print __METHOD__."\n";
  87. }
  88. /**
  89. * tearDownAfterClass
  90. *
  91. * @return void
  92. */
  93. public static function tearDownAfterClass(): void
  94. {
  95. global $conf,$user,$langs,$db;
  96. $db->rollback();
  97. print __METHOD__."\n";
  98. }
  99. /**
  100. * Init phpunit tests
  101. *
  102. * @return void
  103. */
  104. protected function setUp(): void
  105. {
  106. global $conf,$user,$langs,$db;
  107. $conf=$this->savconf;
  108. $user=$this->savuser;
  109. $langs=$this->savlangs;
  110. $db=$this->savdb;
  111. print __METHOD__."\n";
  112. }
  113. /**
  114. * End phpunit tests
  115. *
  116. * @return void
  117. */
  118. protected function tearDown(): void
  119. {
  120. print __METHOD__."\n";
  121. }
  122. /**
  123. * testWSProductsCreateProductOrService
  124. *
  125. * @return int
  126. */
  127. public function testWSProductsCreateProductOrService()
  128. {
  129. global $conf,$user,$langs,$db;
  130. $conf=$this->savconf;
  131. $user=$this->savuser;
  132. $langs=$this->savlangs;
  133. $db=$this->savdb;
  134. $datestring=dol_print_date(dol_now(), 'dayhourlog');
  135. $WS_DOL_URL = DOL_MAIN_URL_ROOT.'/webservices/server_productorservice.php';
  136. $WS_METHOD = 'createProductOrService';
  137. $ns='http://www.dolibarr.org/ns/';
  138. // Set the WebService URL
  139. print __METHOD__." create nusoap_client for URL=".$WS_DOL_URL."\n";
  140. $soapclient = new nusoap_client($WS_DOL_URL);
  141. if ($soapclient) {
  142. $soapclient->soap_defencoding='UTF-8';
  143. $soapclient->decodeUTF8(false);
  144. }
  145. // Call the WebService method and store its result in $result.
  146. $authentication=array(
  147. 'dolibarrkey'=>$conf->global->WEBSERVICES_KEY,
  148. 'sourceapplication'=>'DEMO',
  149. 'login'=>'admin',
  150. 'password'=>'admin',
  151. 'entity'=>''
  152. );
  153. // Test URL
  154. $result='';
  155. $parameters = array(
  156. 'authentication'=>$authentication,'product'=>array(
  157. 'ref'=>'NewProductFromWS'.$datestring,
  158. 'label'=>'New Product From WS '.$datestring,
  159. 'type'=>1,
  160. 'description'=>'This is a new product created from WS PHPUnit test case',
  161. 'barcode'=>'123456789012',
  162. 'barcode_type'=>2,
  163. 'price_net'=>10,
  164. 'status_tosell'=>1,
  165. 'status_tobuy'=>1
  166. )
  167. );
  168. print __METHOD__." call method ".$WS_METHOD."\n";
  169. try {
  170. $result = $soapclient->call($WS_METHOD, $parameters, $ns, '');
  171. } catch (SoapFault $exception) {
  172. echo $exception;
  173. $result=0;
  174. }
  175. if (! $result || !empty($result['faultstring']) || $result['result']['result_code'] != 'OK') {
  176. //var_dump($soapclient);
  177. print $soapclient->error_str;
  178. print "\n<br>\n";
  179. print $soapclient->request;
  180. print "\n<br>\n";
  181. print $soapclient->response;
  182. print "\n";
  183. }
  184. print var_export($result, true);
  185. print __METHOD__." count(result)=".(is_array($result) ? count($result) : '')."\n";
  186. $this->assertEquals('OK', $result['result']['result_code']);
  187. return $result['id'];
  188. }
  189. /**
  190. * testWSProductsGetProductOrService
  191. *
  192. * @param int $id Id of product or service
  193. * @return int Id of product or service
  194. *
  195. * @depends testWSProductsCreateProductOrService
  196. */
  197. public function testWSProductsGetProductOrService($id)
  198. {
  199. global $conf,$user,$langs,$db;
  200. $conf=$this->savconf;
  201. $user=$this->savuser;
  202. $langs=$this->savlangs;
  203. $db=$this->savdb;
  204. $WS_DOL_URL = DOL_MAIN_URL_ROOT.'/webservices/server_productorservice.php';
  205. $WS_METHOD = 'getProductOrService';
  206. $ns='http://www.dolibarr.org/ns/';
  207. // Set the WebService URL
  208. print __METHOD__." create nusoap_client for URL=".$WS_DOL_URL."\n";
  209. $soapclient = new nusoap_client($WS_DOL_URL);
  210. if ($soapclient) {
  211. $soapclient->soap_defencoding='UTF-8';
  212. $soapclient->decodeUTF8(false);
  213. }
  214. // Call the WebService method and store its result in $result.
  215. $authentication=array(
  216. 'dolibarrkey'=>$conf->global->WEBSERVICES_KEY,
  217. 'sourceapplication'=>'DEMO',
  218. 'login'=>'admin',
  219. 'password'=>'admin',
  220. 'entity'=>''
  221. );
  222. // Test URL
  223. $result='';
  224. $parameters = array('authentication'=>$authentication,'id'=>$id,'ref'=>'');
  225. print __METHOD__." call method ".$WS_METHOD."\n";
  226. try {
  227. $result = $soapclient->call($WS_METHOD, $parameters, $ns, '');
  228. } catch (SoapFault $exception) {
  229. echo $exception;
  230. $result=0;
  231. }
  232. if (! $result || !empty($result['faultstring'])) {
  233. //var_dump($soapclient);
  234. print $soapclient->error_str;
  235. print "\n<br>\n";
  236. print $soapclient->request;
  237. print "\n<br>\n";
  238. print $soapclient->response;
  239. print "\n";
  240. }
  241. print __METHOD__." count(result)=".count($result)."\n";
  242. $this->assertEquals('OK', $result['result']['result_code']);
  243. return $id;
  244. }
  245. /**
  246. * testWSProductsDeleteProductOrService
  247. *
  248. * @param int $id Id of product or service
  249. * @return int 0
  250. *
  251. * @depends testWSProductsGetProductOrService
  252. */
  253. public function testWSProductsDeleteProductOrService($id)
  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_DOL_URL = DOL_MAIN_URL_ROOT.'/webservices/server_productorservice.php';
  261. $WS_METHOD = 'deleteProductOrService';
  262. $ns='http://www.dolibarr.org/ns/';
  263. // Set the WebService URL
  264. print __METHOD__." create nusoap_client for URL=".$WS_DOL_URL."\n";
  265. $soapclient = new nusoap_client($WS_DOL_URL);
  266. if ($soapclient) {
  267. $soapclient->soap_defencoding='UTF-8';
  268. $soapclient->decodeUTF8(false);
  269. }
  270. // Call the WebService method and store its result in $result.
  271. $authentication=array(
  272. 'dolibarrkey'=>$conf->global->WEBSERVICES_KEY,
  273. 'sourceapplication'=>'DEMO',
  274. 'login'=>'admin',
  275. 'password'=>'admin',
  276. 'entity'=>''
  277. );
  278. // Test URL
  279. $result='';
  280. $parameters = array('authentication'=>$authentication,'listofid'=>$id);
  281. print __METHOD__." call method ".$WS_METHOD."\n";
  282. try {
  283. $result = $soapclient->call($WS_METHOD, $parameters, $ns, '');
  284. } catch (SoapFault $exception) {
  285. echo $exception;
  286. $result=0;
  287. }
  288. if (! $result || !empty($result['faultstring']) || $result['result']['result_code'] != 'OK') {
  289. //var_dump($soapclient);
  290. print 'Error: '.$soapclient->error_str;
  291. print "\n<br>\n";
  292. print $soapclient->request;
  293. print "\n<br>\n";
  294. print $soapclient->response;
  295. print "\n";
  296. }
  297. print __METHOD__." count(result)=".(is_array($result) ? count($result) : 0)."\n";
  298. $this->assertEquals('OK', $result['result']['result_code']);
  299. return 0;
  300. }
  301. }