WebservicesProductsTest.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 <http://www.gnu.org/licenses/>.
  16. * or see http://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. {
  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. * @return DateLibTest
  62. */
  63. function __construct()
  64. {
  65. parent::__construct();
  66. //$this->sharedFixture
  67. global $conf,$user,$langs,$db;
  68. $this->savconf=$conf;
  69. $this->savuser=$user;
  70. $this->savlangs=$langs;
  71. $this->savdb=$db;
  72. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  73. //print " - db ".$db->db;
  74. print "\n";
  75. }
  76. // Static methods
  77. public static function setUpBeforeClass()
  78. {
  79. global $conf,$user,$langs,$db;
  80. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  81. print __METHOD__."\n";
  82. }
  83. // tear down after class
  84. public static function tearDownAfterClass()
  85. {
  86. global $conf,$user,$langs,$db;
  87. $db->rollback();
  88. print __METHOD__."\n";
  89. }
  90. /**
  91. * Init phpunit tests
  92. *
  93. * @return void
  94. */
  95. protected function setUp()
  96. {
  97. global $conf,$user,$langs,$db;
  98. $conf=$this->savconf;
  99. $user=$this->savuser;
  100. $langs=$this->savlangs;
  101. $db=$this->savdb;
  102. print __METHOD__."\n";
  103. }
  104. /**
  105. * End phpunit tests
  106. *
  107. * @return void
  108. */
  109. protected function tearDown()
  110. {
  111. print __METHOD__."\n";
  112. }
  113. /**
  114. * testWSProductsCreateProductOrService
  115. *
  116. * @return int
  117. */
  118. public function testWSProductsCreateProductOrService()
  119. {
  120. global $conf,$user,$langs,$db;
  121. $conf=$this->savconf;
  122. $user=$this->savuser;
  123. $langs=$this->savlangs;
  124. $db=$this->savdb;
  125. $datestring=dol_print_date(dol_now(),'dayhourlog');
  126. $WS_DOL_URL = DOL_MAIN_URL_ROOT.'/webservices/server_productorservice.php';
  127. $WS_METHOD = 'createProductOrService';
  128. $ns='http://www.dolibarr.org/ns/';
  129. // Set the WebService URL
  130. print __METHOD__." create nusoap_client for URL=".$WS_DOL_URL."\n";
  131. $soapclient = new nusoap_client($WS_DOL_URL);
  132. if ($soapclient) {
  133. $soapclient->soap_defencoding='UTF-8';
  134. $soapclient->decodeUTF8(false);
  135. }
  136. // Call the WebService method and store its result in $result.
  137. $authentication=array(
  138. 'dolibarrkey'=>$conf->global->WEBSERVICES_KEY,
  139. 'sourceapplication'=>'DEMO',
  140. 'login'=>'admin',
  141. 'password'=>'admin',
  142. 'entity'=>''
  143. );
  144. // Test URL
  145. $result='';
  146. $parameters = array(
  147. 'authentication'=>$authentication,'product'=>array(
  148. 'ref'=>'NewProductFromWS'.$datestring,
  149. 'label'=>'New Product From WS '.$datestring,
  150. 'type'=>1,
  151. 'description'=>'This is a new product created from WS PHPUnit test case',
  152. 'barcode'=>'123456789012',
  153. 'barcode_type'=>2
  154. )
  155. );
  156. print __METHOD__." call method ".$WS_METHOD."\n";
  157. try {
  158. $result = $soapclient->call($WS_METHOD,$parameters,$ns,'');
  159. } catch(SoapFault $exception) {
  160. echo $exception;
  161. $result=0;
  162. }
  163. if (! $result || ! empty($result['faultstring']) || $result['result']['result_code'] != 'OK') {
  164. //var_dump($soapclient);
  165. print $soapclient->error_str;
  166. print "\n<br>\n";
  167. print $soapclient->request;
  168. print "\n<br>\n";
  169. print $soapclient->response;
  170. print "\n";
  171. }
  172. print __METHOD__." result=".$result."\n";
  173. $this->assertEquals('OK',$result['result']['result_code']);
  174. return $result['id'];
  175. }
  176. /**
  177. * testWSProductsGetProductOrService
  178. *
  179. * @param int $id Id of product or service
  180. * @return int Id of product or service
  181. *
  182. * @depends testWSProductsCreateProductOrService
  183. */
  184. public function testWSProductsGetProductOrService($id)
  185. {
  186. global $conf,$user,$langs,$db;
  187. $conf=$this->savconf;
  188. $user=$this->savuser;
  189. $langs=$this->savlangs;
  190. $db=$this->savdb;
  191. $WS_DOL_URL = DOL_MAIN_URL_ROOT.'/webservices/server_productorservice.php';
  192. $WS_METHOD = 'getProductOrService';
  193. $ns='http://www.dolibarr.org/ns/';
  194. // Set the WebService URL
  195. print __METHOD__." create nusoap_client for URL=".$WS_DOL_URL."\n";
  196. $soapclient = new nusoap_client($WS_DOL_URL);
  197. if ($soapclient) {
  198. $soapclient->soap_defencoding='UTF-8';
  199. $soapclient->decodeUTF8(false);
  200. }
  201. // Call the WebService method and store its result in $result.
  202. $authentication=array(
  203. 'dolibarrkey'=>$conf->global->WEBSERVICES_KEY,
  204. 'sourceapplication'=>'DEMO',
  205. 'login'=>'admin',
  206. 'password'=>'admin',
  207. 'entity'=>''
  208. );
  209. // Test URL
  210. $result='';
  211. $parameters = array('authentication'=>$authentication,'id'=>$id,'ref'=>'');
  212. print __METHOD__." call method ".$WS_METHOD."\n";
  213. try {
  214. $result = $soapclient->call($WS_METHOD,$parameters,$ns,'');
  215. } catch(SoapFault $exception) {
  216. echo $exception;
  217. $result=0;
  218. }
  219. if (! $result || ! empty($result['faultstring'])) {
  220. //var_dump($soapclient);
  221. print $soapclient->error_str;
  222. print "\n<br>\n";
  223. print $soapclient->request;
  224. print "\n<br>\n";
  225. print $soapclient->response;
  226. print "\n";
  227. }
  228. print __METHOD__." result=".$result."\n";
  229. $this->assertEquals('OK',$result['result']['result_code']);
  230. return $id;
  231. }
  232. /**
  233. * testWSProductsDeleteProductOrService
  234. *
  235. * @param int $id Id of product or service
  236. * @return int 0
  237. *
  238. * @depends testWSProductsGetProductOrService
  239. */
  240. public function testWSProductsDeleteProductOrService($id)
  241. {
  242. global $conf,$user,$langs,$db;
  243. $conf=$this->savconf;
  244. $user=$this->savuser;
  245. $langs=$this->savlangs;
  246. $db=$this->savdb;
  247. $WS_DOL_URL = DOL_MAIN_URL_ROOT.'/webservices/server_productorservice.php';
  248. $WS_METHOD = 'deleteProductOrService';
  249. $ns='http://www.dolibarr.org/ns/';
  250. // Set the WebService URL
  251. print __METHOD__." create nusoap_client for URL=".$WS_DOL_URL."\n";
  252. $soapclient = new nusoap_client($WS_DOL_URL);
  253. if ($soapclient) {
  254. $soapclient->soap_defencoding='UTF-8';
  255. $soapclient->decodeUTF8(false);
  256. }
  257. // Call the WebService method and store its result in $result.
  258. $authentication=array(
  259. 'dolibarrkey'=>$conf->global->WEBSERVICES_KEY,
  260. 'sourceapplication'=>'DEMO',
  261. 'login'=>'admin',
  262. 'password'=>'admin',
  263. 'entity'=>''
  264. );
  265. // Test URL
  266. $result='';
  267. $parameters = array('authentication'=>$authentication,'listofid'=>$id);
  268. print __METHOD__." call method ".$WS_METHOD."\n";
  269. try {
  270. $result = $soapclient->call($WS_METHOD,$parameters,$ns,'');
  271. } catch(SoapFault $exception) {
  272. echo $exception;
  273. $result=0;
  274. }
  275. if (! $result || ! empty($result['faultstring']) || $result['result']['result_code'] != 'OK') {
  276. //var_dump($soapclient);
  277. print $soapclient->error_str;
  278. print "\n<br>\n";
  279. print $soapclient->request;
  280. print "\n<br>\n";
  281. print $soapclient->response;
  282. print "\n";
  283. }
  284. print __METHOD__." result=".$result."\n";
  285. $this->assertEquals('OK',$result['result']['result_code']);
  286. return 0;
  287. }
  288. }