RestAPIDocumentTest.php 7.4 KB

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