externallib_test.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle 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. // Moodle 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 Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * PHPunit tests for external files API.
  18. *
  19. * @package core_files
  20. * @category external
  21. * @copyright 2013 Ankit Agarwal
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. * @since Moodle 2.6
  24. */
  25. defined('MOODLE_INTERNAL') || die();
  26. global $CFG;
  27. require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  28. require_once($CFG->dirroot . '/files/externallib.php');
  29. class core_files_externallib_testcase extends advanced_testcase {
  30. /*
  31. * Test core_files_external::upload().
  32. */
  33. public function test_upload() {
  34. global $USER;
  35. $this->resetAfterTest();
  36. $this->setAdminUser();
  37. $context = context_user::instance($USER->id);
  38. $contextid = $context->id;
  39. $component = "user";
  40. $filearea = "draft";
  41. $itemid = 0;
  42. $filepath = "/";
  43. $filename = "Simple.txt";
  44. $filecontent = base64_encode("Let us create a nice simple file");
  45. $contextlevel = null;
  46. $instanceid = null;
  47. $browser = get_file_browser();
  48. // Make sure no file exists.
  49. $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
  50. $this->assertEmpty($file);
  51. // Call the api to create a file.
  52. $fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath,
  53. $filename, $filecontent, $contextlevel, $instanceid);
  54. $fileinfo = external_api::clean_returnvalue(core_files_external::upload_returns(), $fileinfo);
  55. // Get the created draft item id.
  56. $itemid = $fileinfo['itemid'];
  57. // Make sure the file was created.
  58. $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
  59. $this->assertNotEmpty($file);
  60. // Make sure no file exists.
  61. $filename = "Simple2.txt";
  62. $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
  63. $this->assertEmpty($file);
  64. // Call the api to create a file.
  65. $fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid,
  66. $filepath, $filename, $filecontent, $contextlevel, $instanceid);
  67. $fileinfo = external_api::clean_returnvalue(core_files_external::upload_returns(), $fileinfo);
  68. $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
  69. $this->assertNotEmpty($file);
  70. // Let us try creating a file using contextlevel and instance id.
  71. $filename = "Simple5.txt";
  72. $contextid = 0;
  73. $contextlevel = "user";
  74. $instanceid = $USER->id;
  75. $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
  76. $this->assertEmpty($file);
  77. $fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath,
  78. $filename, $filecontent, $contextlevel, $instanceid);
  79. $fileinfo = external_api::clean_returnvalue(core_files_external::upload_returns(), $fileinfo);
  80. $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
  81. $this->assertNotEmpty($file);
  82. // Make sure the same file cannot be created again.
  83. $this->expectException("moodle_exception");
  84. core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath,
  85. $filename, $filecontent, $contextlevel, $instanceid);
  86. }
  87. /*
  88. * Make sure only user component is allowed in core_files_external::upload().
  89. */
  90. public function test_upload_param_component() {
  91. global $USER;
  92. $this->resetAfterTest();
  93. $this->setAdminUser();
  94. $context = context_user::instance($USER->id);
  95. $contextid = $context->id;
  96. $component = "backup";
  97. $filearea = "draft";
  98. $itemid = 0;
  99. $filepath = "/";
  100. $filename = "Simple3.txt";
  101. $filecontent = base64_encode("Let us create a nice simple file");
  102. $contextlevel = null;
  103. $instanceid = null;
  104. // Make sure exception is thrown.
  105. $this->expectException("coding_exception");
  106. core_files_external::upload($contextid, $component, $filearea, $itemid,
  107. $filepath, $filename, $filecontent, $contextlevel, $instanceid);
  108. }
  109. /*
  110. * Make sure only draft areas are allowed in core_files_external::upload().
  111. */
  112. public function test_upload_param_area() {
  113. global $USER;
  114. $this->resetAfterTest();
  115. $this->setAdminUser();
  116. $context = context_user::instance($USER->id);
  117. $contextid = $context->id;
  118. $component = "user";
  119. $filearea = "draft";
  120. $itemid = file_get_unused_draft_itemid();
  121. $filepath = "/";
  122. $filename = "Simple4.txt";
  123. $filecontent = base64_encode("Let us create a nice simple file");
  124. $contextlevel = null;
  125. $instanceid = null;
  126. // Make sure the file is created.
  127. $fileinfo = @core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent);
  128. $fileinfo = external_api::clean_returnvalue(core_files_external::upload_returns(), $fileinfo);
  129. $browser = get_file_browser();
  130. $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
  131. $this->assertNotEmpty($file);
  132. }
  133. /**
  134. * Test getting a list of files with and without a context ID.
  135. */
  136. public function test_get_files() {
  137. global $USER, $DB;
  138. $this->resetAfterTest();
  139. // Set the current user to be the administrator.
  140. $this->setAdminUser();
  141. $USER->email = 'test@example.com';
  142. // Create a course.
  143. $course = $this->getDataGenerator()->create_course();
  144. $record = new stdClass();
  145. $record->course = $course->id;
  146. $record->name = "Mod data upload test";
  147. $record->intro = "Some intro of some sort";
  148. // Create a database module.
  149. $module = $this->getDataGenerator()->create_module('data', $record);
  150. // Create a new field in the database activity.
  151. $field = data_get_field_new('file', $module);
  152. // Add more detail about the field.
  153. $fielddetail = new stdClass();
  154. $fielddetail->d = $module->id;
  155. $fielddetail->mode = 'add';
  156. $fielddetail->type = 'file';
  157. $fielddetail->sesskey = sesskey();
  158. $fielddetail->name = 'Upload file';
  159. $fielddetail->description = 'Some description';
  160. $fielddetail->param3 = '0';
  161. $field->define_field($fielddetail);
  162. $field->insert_field();
  163. $recordid = data_add_record($module);
  164. // File information for the database module record.
  165. $datacontent = array();
  166. $datacontent['fieldid'] = $field->field->id;
  167. $datacontent['recordid'] = $recordid;
  168. $datacontent['content'] = 'Simple4.txt';
  169. // Insert the information about the file.
  170. $contentid = $DB->insert_record('data_content', $datacontent);
  171. // Required information for uploading a file.
  172. $context = context_module::instance($module->cmid);
  173. $usercontext = context_user::instance($USER->id);
  174. $component = 'mod_data';
  175. $filearea = 'content';
  176. $itemid = $contentid;
  177. $filename = $datacontent['content'];
  178. $filecontent = base64_encode("Let us create a nice simple file.");
  179. $filerecord = array();
  180. $filerecord['contextid'] = $context->id;
  181. $filerecord['component'] = $component;
  182. $filerecord['filearea'] = $filearea;
  183. $filerecord['itemid'] = $itemid;
  184. $filerecord['filepath'] = '/';
  185. $filerecord['filename'] = $filename;
  186. // Create an area to upload the file.
  187. $fs = get_file_storage();
  188. // Create a file from the string that we made earlier.
  189. $file = $fs->create_file_from_string($filerecord, $filecontent);
  190. $timemodified = $file->get_timemodified();
  191. $timecreated = $file->get_timemodified();
  192. $filesize = $file->get_filesize();
  193. // Use the web service function to return the information about the file that we just uploaded.
  194. // The first time is with a valid context ID.
  195. $filename = '';
  196. $testfilelisting = core_files_external::get_files($context->id, $component, $filearea, $itemid, '/', $filename);
  197. $testfilelisting = external_api::clean_returnvalue(core_files_external::get_files_returns(), $testfilelisting);
  198. // With the information that we have provided we should get an object exactly like the one below.
  199. $coursecontext = context_course::instance($course->id);
  200. $testdata = array();
  201. $testdata['parents'] = array();
  202. $testdata['parents']['0'] = array('contextid' => 1,
  203. 'component' => null,
  204. 'filearea' => null,
  205. 'itemid' => null,
  206. 'filepath' => null,
  207. 'filename' => 'System');
  208. $testdata['parents']['1'] = array('contextid' => 3,
  209. 'component' => null,
  210. 'filearea' => null,
  211. 'itemid' => null,
  212. 'filepath' => null,
  213. 'filename' => 'Miscellaneous');
  214. $testdata['parents']['2'] = array('contextid' => $coursecontext->id,
  215. 'component' => null,
  216. 'filearea' => null,
  217. 'itemid' => null,
  218. 'filepath' => null,
  219. 'filename' => 'Test course 1');
  220. $testdata['parents']['3'] = array('contextid' => $context->id,
  221. 'component' => null,
  222. 'filearea' => null,
  223. 'itemid' => null,
  224. 'filepath' => null,
  225. 'filename' => 'Mod data upload test (Database)');
  226. $testdata['parents']['4'] = array('contextid' => $context->id,
  227. 'component' => 'mod_data',
  228. 'filearea' => 'content',
  229. 'itemid' => null,
  230. 'filepath' => null,
  231. 'filename' => 'Fields');
  232. $testdata['files'] = array();
  233. $testdata['files']['0'] = array('contextid' => $context->id,
  234. 'component' => 'mod_data',
  235. 'filearea' => 'content',
  236. 'itemid' => $itemid,
  237. 'filepath' => '/',
  238. 'filename' => 'Simple4.txt',
  239. 'url' => 'http://www.example.com/moodle/pluginfile.php/'.$context->id.'/mod_data/content/'.$itemid.'/Simple4.txt',
  240. 'isdir' => false,
  241. 'timemodified' => $timemodified,
  242. 'timecreated' => $timecreated,
  243. 'filesize' => $filesize,
  244. 'author' => null,
  245. 'license' => null
  246. );
  247. // Make sure that they are the same.
  248. $this->assertEquals($testdata, $testfilelisting);
  249. // Try again but without the context. Minus one signals the function to use other variables to obtain the context.
  250. $nocontext = -1;
  251. $modified = 0;
  252. // Context level and instance ID are used to determine what the context is.
  253. $contextlevel = 'module';
  254. $instanceid = $module->cmid;
  255. $testfilelisting = core_files_external::get_files($nocontext, $component, $filearea, $itemid, '/', $filename, $modified, $contextlevel, $instanceid);
  256. $testfilelisting = external_api::clean_returnvalue(core_files_external::get_files_returns(), $testfilelisting);
  257. $this->assertEquals($testfilelisting, $testdata);
  258. }
  259. }