WebservicesThirdpartyTest.php 12 KB

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