WebservicesProductsTest.php 8.6 KB

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