WebservicesOtherTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. /**
  20. * \file test/phpunit/WebservicesOtherTest.php
  21. * \ingroup test
  22. * \brief PHPUnit test
  23. * \remarks To run this script as CLI: phpunit filename.php
  24. */
  25. global $conf,$user,$langs,$db;
  26. //define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
  27. //require_once 'PHPUnit/Autoload.php';
  28. require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
  29. require_once dirname(__FILE__).'/../../htdocs/core/lib/date.lib.php';
  30. require_once NUSOAP_PATH.'/nusoap.php'; // Include SOAP
  31. if (empty($user->id)) {
  32. print "Load permissions for admin user nb 1\n";
  33. $user->fetch(1);
  34. $user->getrights();
  35. }
  36. $conf->global->MAIN_DISABLE_ALL_MAILS=1;
  37. $conf->global->MAIN_UMASK='0666';
  38. /**
  39. * Class for PHPUnit tests
  40. *
  41. * @backupGlobals disabled
  42. * @backupStaticAttributes enabled
  43. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  44. */
  45. class WebservicesOtherTest extends PHPUnit\Framework\TestCase
  46. {
  47. protected $savconf;
  48. protected $savuser;
  49. protected $savlangs;
  50. protected $savdb;
  51. /**
  52. * Constructor
  53. * We save global variables into local variables
  54. *
  55. * @param string $name Name
  56. * @return WebservicesOtherTest
  57. */
  58. public function __construct($name = '')
  59. {
  60. parent::__construct($name);
  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. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  68. //print " - db ".$db->db;
  69. print "\n";
  70. }
  71. /**
  72. * setUpBeforeClass
  73. *
  74. * @return void
  75. */
  76. public static function setUpBeforeClass(): void
  77. {
  78. global $conf,$user,$langs,$db;
  79. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  80. print __METHOD__."\n";
  81. }
  82. /**
  83. * tearDownAfterClass
  84. *
  85. * @return void
  86. */
  87. public static function tearDownAfterClass(): void
  88. {
  89. global $conf,$user,$langs,$db;
  90. $db->rollback();
  91. print __METHOD__."\n";
  92. }
  93. /**
  94. * Init phpunit tests
  95. *
  96. * @return void
  97. */
  98. protected function setUp(): void
  99. {
  100. global $conf,$user,$langs,$db;
  101. $conf=$this->savconf;
  102. $user=$this->savuser;
  103. $langs=$this->savlangs;
  104. $db=$this->savdb;
  105. print __METHOD__."\n";
  106. }
  107. /**
  108. * End phpunit tests
  109. *
  110. * @return void
  111. */
  112. protected function tearDown(): void
  113. {
  114. print __METHOD__."\n";
  115. }
  116. /**
  117. * testWSOtherGetVersions
  118. *
  119. * @return int
  120. */
  121. public function testWSOtherGetVersions()
  122. {
  123. global $conf,$user,$langs,$db;
  124. $conf=$this->savconf;
  125. $user=$this->savuser;
  126. $langs=$this->savlangs;
  127. $db=$this->savdb;
  128. $WS_DOL_URL = DOL_MAIN_URL_ROOT.'/webservices/server_other.php';
  129. $WS_METHOD = 'getVersions';
  130. $ns='http://www.dolibarr.org/ns/';
  131. // Set the WebService URL
  132. print __METHOD__." create nusoap_client for URL=".$WS_DOL_URL."\n";
  133. $soapclient = new nusoap_client($WS_DOL_URL);
  134. if ($soapclient) {
  135. $soapclient->soap_defencoding='UTF-8';
  136. $soapclient->decodeUTF8(false);
  137. }
  138. // Call the WebService method and store its result in $result.
  139. $authentication=array(
  140. 'dolibarrkey'=>$conf->global->WEBSERVICES_KEY,
  141. 'sourceapplication'=>'DEMO',
  142. 'login'=>'admin',
  143. 'password'=>'admin',
  144. 'entity'=>''
  145. );
  146. // Test URL
  147. $result='';
  148. $parameters = array('authentication'=>$authentication);
  149. print __METHOD__." call method ".$WS_METHOD."\n";
  150. try {
  151. $result = $soapclient->call($WS_METHOD, $parameters, $ns, '');
  152. } catch (SoapFault $exception) {
  153. echo $exception;
  154. $result=0;
  155. }
  156. if (!empty($result['faultstring'])) {
  157. print $result['faultstring']."\n";
  158. $result=0;
  159. }
  160. if (! $result) {
  161. //var_dump($soapclient);
  162. print 'Error: '.$soapclient->error_str;
  163. print "\n<br>\n";
  164. print $soapclient->request;
  165. print "\n<br>\n";
  166. print $soapclient->response;
  167. print "\n";
  168. }
  169. print __METHOD__." count(result)=".count($result)."\n";
  170. $this->assertEquals('OK', $result['result']['result_code']);
  171. // Test method that does not exists
  172. $WS_METHOD='methodthatdoesnotexists';
  173. $result='';
  174. $parameters = array('authentication'=>$authentication);
  175. print __METHOD__." call method ".$WS_METHOD."\n";
  176. try {
  177. $result = $soapclient->call($WS_METHOD, $parameters, $ns, '');
  178. } catch (SoapFault $exception) {
  179. echo $exception;
  180. $result=0;
  181. }
  182. if (! $result || !empty($result['faultstring'])) {
  183. //var_dump($soapclient);
  184. print $soapclient->error_str;
  185. print "\n<br>\n";
  186. print $soapclient->request;
  187. print "\n<br>\n";
  188. print $soapclient->response;
  189. print "\n";
  190. }
  191. print __METHOD__." count(result)=".(is_array($result) ? count($result) : 0)."\n";
  192. $this->assertEquals("SOAP-ENV:Client: Operation 'methodthatdoesnotexists' is not defined in the WSDL for this service", $soapclient->error_str);
  193. return $result;
  194. }
  195. }