RestAPIDocumentTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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/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. //$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. echo __METHOD__.' db->type='.$db->type.' user->id='.$user->id;
  68. //print " - db ".$db->db;
  69. echo "\n";
  70. }
  71. // Static methods
  72. public static function setUpBeforeClass()
  73. {
  74. global $conf,$user,$langs,$db;
  75. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  76. echo __METHOD__."\n";
  77. }
  78. // tear down after class
  79. public static function tearDownAfterClass()
  80. {
  81. global $conf,$user,$langs,$db;
  82. $db->rollback();
  83. echo __METHOD__."\n";
  84. }
  85. /**
  86. * Init phpunit tests.
  87. */
  88. protected function setUp()
  89. {
  90. global $conf,$user,$langs,$db;
  91. $conf = $this->savconf;
  92. $user = $this->savuser;
  93. $langs = $this->savlangs;
  94. $db = $this->savdb;
  95. $this->api_url = DOL_MAIN_URL_ROOT.'/api/index.php';
  96. $login = 'admin';
  97. $password = 'admin';
  98. $url = $this->api_url.'/login?login='.$login.'&password='.$password;
  99. // Call the API login method to save api_key for this test class
  100. $result = getURLContent($url, 'GET', '', 1, array());
  101. echo __METHOD__.' result = '.var_export($result, true)."\n";
  102. echo __METHOD__.' curl_error_no: '.$result['curl_error_no']."\n";
  103. $this->assertEquals($result['curl_error_no'], '');
  104. $object = json_decode($result['content'], true);
  105. $this->assertNotNull($object, 'Parsing of json result must no be null');
  106. $this->assertEquals('200', $object['success']['code']);
  107. $this->api_key = $object['success']['token'];
  108. echo __METHOD__." api_key: $this->api_key \n";
  109. echo __METHOD__."\n";
  110. }
  111. /**
  112. * End phpunit tests.
  113. */
  114. protected function tearDown()
  115. {
  116. echo __METHOD__."\n";
  117. }
  118. /**
  119. * testPushDocument.
  120. *
  121. * @return int
  122. */
  123. public function testPushDocument()
  124. {
  125. global $conf,$user,$langs,$db;
  126. $url = $this->api_url.'/documents/?api_key='.$this->api_key;
  127. echo __METHOD__.' Request POST url='.$url."\n";
  128. // Send to non existant directory
  129. dol_delete_dir_recursive(DOL_DATA_ROOT.'/medias/tmpphpunit');
  130. //$data = '{ "filename": "mynewfile.txt", "modulepart": "medias", "ref": "", "subdir": "mysubdir1/mysubdir2", "filecontent": "content text", "fileencoding": "" }';
  131. $data = array(
  132. 'filename'=>"mynewfile.txt",
  133. 'modulepart'=>"medias",
  134. 'ref'=>"",
  135. 'subdir'=>"tmpphpunit/tmpphpunit2",
  136. 'filecontent'=>"content text",
  137. 'fileencoding'=>""
  138. );
  139. $result = getURLContent($url, 'POST', $data, 1);
  140. echo __METHOD__.' Result for sending document: '.var_export($result, true)."\n";
  141. echo __METHOD__.' curl_error_no: '.$result['curl_error_no']."\n";
  142. $object = json_decode($result['content'], true);
  143. $this->assertNotNull($object, 'Parsing of json result must no be null');
  144. $this->assertEquals('401', $object['error']['code']);
  145. // Send to existant directory
  146. dol_mkdir(DOL_DATA_ROOT.'/medias/tmpphpunit/tmpphpunit2');
  147. $data = array(
  148. 'filename'=>"mynewfile.txt",
  149. 'modulepart'=>"medias",
  150. 'ref'=>"",
  151. 'subdir'=>"tmpphpunit/tmpphpunit2",
  152. 'filecontent'=>"content text",
  153. 'fileencoding'=>""
  154. );
  155. $result2 = getURLContent($url, 'POST', $data, 1);
  156. echo __METHOD__.' Result for sending document: '.var_export($result2, true)."\n";
  157. echo __METHOD__.' curl_error_no: '.$result2['curl_error_no']."\n";
  158. $object2 = json_decode($result2['content'], true);
  159. $this->assertNotNull($object2, 'Parsing of json result must no be null');
  160. $this->assertEquals($result2['curl_error_no'], '');
  161. $this->assertEquals($result2['content'], 'true');
  162. }
  163. }