RestAPIDocumentTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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/RestAPIDocumentTest.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 dirname(__FILE__).'/../../htdocs/core/lib/geturl.lib.php';
  31. require_once dirname(__FILE__).'/../../htdocs/core/lib/files.lib.php';
  32. if (empty($user->id)) {
  33. echo "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. /**
  40. * Class for PHPUnit tests.
  41. *
  42. * @backupGlobals disabled
  43. * @backupStaticAttributes enabled
  44. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  45. */
  46. class RestAPIDocumentTest extends PHPUnit\Framework\TestCase
  47. {
  48. protected $savconf;
  49. protected $savuser;
  50. protected $savlangs;
  51. protected $savdb;
  52. protected $api_url;
  53. protected $api_key;
  54. /**
  55. * Constructor
  56. * We save global variables into local variables.
  57. *
  58. * @param string $name Name
  59. * @return RestAPIDocumentTest
  60. */
  61. public function __construct($name = '')
  62. {
  63. parent::__construct($name);
  64. //$this->sharedFixture
  65. global $conf,$user,$langs,$db;
  66. $this->savconf = $conf;
  67. $this->savuser = $user;
  68. $this->savlangs = $langs;
  69. $this->savdb = $db;
  70. echo __METHOD__.' db->type='.$db->type.' user->id='.$user->id;
  71. //print " - db ".$db->db;
  72. echo "\n";
  73. }
  74. /**
  75. * setUpBeforeClass
  76. *
  77. * @return void
  78. */
  79. public static function setUpBeforeClass(): void
  80. {
  81. global $conf,$user,$langs,$db;
  82. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  83. echo __METHOD__."\n";
  84. }
  85. /**
  86. * tearDownAfterClass
  87. *
  88. * @return void
  89. */
  90. public static function tearDownAfterClass(): void
  91. {
  92. global $conf,$user,$langs,$db;
  93. $db->rollback();
  94. echo __METHOD__."\n";
  95. }
  96. /**
  97. * Init phpunit tests.
  98. * @return void
  99. */
  100. protected function setUp(): void
  101. {
  102. global $conf,$user,$langs,$db;
  103. $conf = $this->savconf;
  104. $user = $this->savuser;
  105. $langs = $this->savlangs;
  106. $db = $this->savdb;
  107. $this->api_url = DOL_MAIN_URL_ROOT.'/api/index.php';
  108. $login = 'admin';
  109. $password = 'admin';
  110. $url = $this->api_url.'/login?login='.$login.'&password='.$password;
  111. // Call the API login method to save api_key for this test class
  112. $result = getURLContent($url, 'GET', '', 1, array(), array('http', 'https'), 2);
  113. echo __METHOD__.' result = '.var_export($result, true)."\n";
  114. echo __METHOD__.' curl_error_no: '.$result['curl_error_no']."\n";
  115. $this->assertEquals($result['curl_error_no'], '');
  116. $object = json_decode($result['content'], true);
  117. $this->assertNotNull($object, 'Parsing of json result must no be null');
  118. $this->assertEquals('200', $object['success']['code']);
  119. $this->api_key = $object['success']['token'];
  120. echo __METHOD__." api_key: $this->api_key \n";
  121. echo __METHOD__."\n";
  122. }
  123. /**
  124. * End phpunit tests.
  125. * @return void
  126. */
  127. protected function tearDown(): void
  128. {
  129. echo __METHOD__."\n";
  130. }
  131. /**
  132. * testPushDocument.
  133. *
  134. * @return int
  135. */
  136. public function testPushDocument()
  137. {
  138. global $conf,$user,$langs,$db;
  139. $url = $this->api_url.'/documents/upload?api_key='.$this->api_key;
  140. echo __METHOD__.' Request POST url='.$url."\n";
  141. // Send to non existent directory
  142. dol_delete_dir_recursive(DOL_DATA_ROOT.'/medias/tmpphpunit/tmpphpunit1');
  143. //$data = '{ "filename": "mynewfile.txt", "modulepart": "medias", "ref": "", "subdir": "mysubdir1/mysubdir2", "filecontent": "content text", "fileencoding": "" }';
  144. $data = array(
  145. 'filename'=>"mynewfile.txt",
  146. 'modulepart'=>"medias",
  147. 'subdir'=>"tmpphpunit/tmpphpunit1",
  148. 'filecontent'=>"content text",
  149. 'fileencoding'=>"",
  150. 'overwriteifexists'=>0,
  151. 'createdirifnotexists'=>0
  152. );
  153. $param = '';
  154. foreach ($data as $key => $val) {
  155. $param .= '&'.$key.'='.urlencode($val);
  156. }
  157. $result = getURLContent($url, 'POST', $param, 1, array(), array('http', 'https'), 2);
  158. echo __METHOD__.' Result for sending document: '.var_export($result, true)."\n";
  159. echo __METHOD__.' curl_error_no: '.$result['curl_error_no']."\n";
  160. $object = json_decode($result['content'], true);
  161. $this->assertNotNull($object, 'Parsing of json result must not be null');
  162. $this->assertEquals('401', $result['http_code'], 'Return code is not 401');
  163. $this->assertEquals('401', empty($object['error']['code']) ? '' : $object['error']['code'], 'Error code is not 401');
  164. // Send to existent directory
  165. dol_delete_dir_recursive(DOL_DATA_ROOT.'/medias/tmpphpunit/tmpphpunit2');
  166. dol_mkdir(DOL_DATA_ROOT.'/medias/tmpphpunit/tmpphpunit2');
  167. $data = array(
  168. 'filename'=>"mynewfile.txt",
  169. 'modulepart'=>"medias",
  170. 'ref'=>"",
  171. 'subdir'=>"tmpphpunit/tmpphpunit2",
  172. 'filecontent'=>"content text",
  173. 'fileencoding'=>"",
  174. 'overwriteifexists'=>0,
  175. 'createdirifnotexists'=>0
  176. );
  177. $param = '';
  178. foreach ($data as $key => $val) {
  179. $param .= '&'.$key.'='.urlencode($val);
  180. }
  181. $result2 = getURLContent($url, 'POST', $param, 1, array(), array('http', 'https'), 2);
  182. echo __METHOD__.' Result for sending document: '.var_export($result2, true)."\n";
  183. echo __METHOD__.' curl_error_no: '.$result2['curl_error_no']."\n";
  184. $object2 = json_decode($result2['content'], true);
  185. //$this->assertNotNull($object2, 'Parsing of json result must not be null');
  186. $this->assertEquals('200', $result2['http_code'], 'Return code must be 200');
  187. $this->assertEquals($result2['curl_error_no'], '');
  188. $this->assertEquals($object2, 'mynewfile.txt', 'Must contains basename of file');
  189. dol_delete_dir_recursive(DOL_DATA_ROOT.'/medias/tmpphpunit/tmpphpunit3');
  190. $data = array(
  191. 'filename'=>"mynewfile.txt",
  192. 'modulepart'=>"medias",
  193. 'ref'=>"",
  194. 'subdir'=>"tmpphpunit/tmpphpunit3",
  195. 'filecontent'=>"content text",
  196. 'fileencoding'=>"",
  197. 'overwriteifexists'=>0,
  198. 'createdirifnotexists'=>1
  199. );
  200. $param = '';
  201. foreach ($data as $key => $val) {
  202. $param .= '&'.$key.'='.urlencode($val);
  203. }
  204. $result3 = getURLContent($url, 'POST', $param, 1, array(), array('http', 'https'), 2);
  205. echo __METHOD__.' Result for sending document: '.var_export($result3, true)."\n";
  206. echo __METHOD__.' curl_error_no: '.$result3['curl_error_no']."\n";
  207. $object3 = json_decode($result3['content'], true);
  208. //$this->assertNotNull($object2, 'Parsing of json result must not be null');
  209. $this->assertEquals('200', $result3['http_code'], 'Return code must be 200');
  210. $this->assertEquals($result3['curl_error_no'], '');
  211. $this->assertEquals($object3, 'mynewfile.txt', 'Must contains basename of file');
  212. dol_delete_dir_recursive(DOL_DATA_ROOT.'/medias/tmpphpunit');
  213. }
  214. }