/**
  * Test add_user_private_files
  */
 public function test_add_user_private_files()
 {
     global $USER, $CFG, $DB;
     $this->resetAfterTest(true);
     $context = context_system::instance();
     $roleid = $this->assignUserCapability('moodle/user:manageownfiles', $context->id);
     $context = context_user::instance($USER->id);
     $contextid = $context->id;
     $component = "user";
     $filearea = "draft";
     $itemid = 0;
     $filepath = "/";
     $filename = "Simple.txt";
     $filecontent = base64_encode("Let us create a nice simple file");
     $contextlevel = null;
     $instanceid = null;
     $browser = get_file_browser();
     // Call the files api to create a file.
     $draftfile = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent, $contextlevel, $instanceid);
     $draftid = $draftfile['itemid'];
     // Make sure the file was created.
     $file = $browser->get_file_info($context, $component, $filearea, $draftid, $filepath, $filename);
     $this->assertNotEmpty($file);
     // Make sure the file does not exist in the user private files.
     $file = $browser->get_file_info($context, $component, 'private', 0, $filepath, $filename);
     $this->assertEmpty($file);
     // Call the external function.
     core_user_external::add_user_private_files($draftid);
     // Make sure the file was added to the user private files.
     $file = $browser->get_file_info($context, $component, 'private', 0, $filepath, $filename);
     $this->assertNotEmpty($file);
 }
 public function test_upload_param_area()
 {
     global $USER;
     $this->resetAfterTest();
     $this->setAdminUser();
     $context = context_user::instance($USER->id);
     $contextid = $context->id;
     $component = "user";
     $filearea = "draft";
     $itemid = 0;
     $filepath = "/";
     $filename = "Simple4.txt";
     $filecontent = base64_encode("Let us create a nice simple file");
     // Make sure exception is thrown.
     $this->setExpectedException("coding_exception");
     core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent);
 }
 /**
  * Returns description of upload returns
  *
  * @return external_single_structure
  * @since Moodle 2.0
  * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
  * @see core_files_external::upload_returns()
  */
 public static function upload_returns()
 {
     return core_files_external::upload_returns();
 }
 /**
  * Test getting a list of files with and without a context ID.
  */
 public function test_get_files()
 {
     global $USER, $DB;
     $this->resetAfterTest();
     // Set the current user to be the administrator.
     $this->setAdminUser();
     $USER->email = '*****@*****.**';
     // Create a course.
     $course = $this->getDataGenerator()->create_course();
     $record = new stdClass();
     $record->course = $course->id;
     $record->name = "Mod data upload test";
     $record->intro = "Some intro of some sort";
     // Create a database module.
     $module = $this->getDataGenerator()->create_module('data', $record);
     // Create a new field in the database activity.
     $field = data_get_field_new('file', $module);
     // Add more detail about the field.
     $fielddetail = new stdClass();
     $fielddetail->d = $module->id;
     $fielddetail->mode = 'add';
     $fielddetail->type = 'file';
     $fielddetail->sesskey = sesskey();
     $fielddetail->name = 'Upload file';
     $fielddetail->description = 'Some description';
     $fielddetail->param3 = '0';
     $field->define_field($fielddetail);
     $field->insert_field();
     $recordid = data_add_record($module);
     // File information for the database module record.
     $datacontent = array();
     $datacontent['fieldid'] = $field->field->id;
     $datacontent['recordid'] = $recordid;
     $datacontent['content'] = 'Simple4.txt';
     // Insert the information about the file.
     $contentid = $DB->insert_record('data_content', $datacontent);
     // Required information for uploading a file.
     $context = context_module::instance($module->cmid);
     $usercontext = context_user::instance($USER->id);
     $component = 'mod_data';
     $filearea = 'content';
     $itemid = $contentid;
     $filename = $datacontent['content'];
     $filecontent = base64_encode("Let us create a nice simple file.");
     $filerecord = array();
     $filerecord['contextid'] = $context->id;
     $filerecord['component'] = $component;
     $filerecord['filearea'] = $filearea;
     $filerecord['itemid'] = $itemid;
     $filerecord['filepath'] = '/';
     $filerecord['filename'] = $filename;
     // Create an area to upload the file.
     $fs = get_file_storage();
     // Create a file from the string that we made earlier.
     $file = $fs->create_file_from_string($filerecord, $filecontent);
     $timemodified = $file->get_timemodified();
     $timecreated = $file->get_timemodified();
     $filesize = $file->get_filesize();
     // Use the web service function to return the information about the file that we just uploaded.
     // The first time is with a valid context ID.
     $filename = '';
     $testfilelisting = core_files_external::get_files($context->id, $component, $filearea, $itemid, '/', $filename);
     $testfilelisting = external_api::clean_returnvalue(core_files_external::get_files_returns(), $testfilelisting);
     // With the information that we have provided we should get an object exactly like the one below.
     $coursecontext = context_course::instance($course->id);
     $testdata = array();
     $testdata['parents'] = array();
     $testdata['parents']['0'] = array('contextid' => 1, 'component' => null, 'filearea' => null, 'itemid' => null, 'filepath' => null, 'filename' => 'System');
     $testdata['parents']['1'] = array('contextid' => 3, 'component' => null, 'filearea' => null, 'itemid' => null, 'filepath' => null, 'filename' => 'Miscellaneous');
     $testdata['parents']['2'] = array('contextid' => $coursecontext->id, 'component' => null, 'filearea' => null, 'itemid' => null, 'filepath' => null, 'filename' => 'Test course 1');
     $testdata['parents']['3'] = array('contextid' => $context->id, 'component' => null, 'filearea' => null, 'itemid' => null, 'filepath' => null, 'filename' => 'Mod data upload test (Database)');
     $testdata['parents']['4'] = array('contextid' => $context->id, 'component' => 'mod_data', 'filearea' => 'content', 'itemid' => null, 'filepath' => null, 'filename' => 'Fields');
     $testdata['files'] = array();
     $testdata['files']['0'] = array('contextid' => $context->id, 'component' => 'mod_data', 'filearea' => 'content', 'itemid' => $itemid, 'filepath' => '/', 'filename' => 'Simple4.txt', 'url' => 'http://www.example.com/moodle/pluginfile.php/' . $context->id . '/mod_data/content/' . $itemid . '/Simple4.txt', 'isdir' => false, 'timemodified' => $timemodified, 'timecreated' => $timecreated, 'filesize' => $filesize, 'author' => null, 'license' => null);
     // Make sure that they are the same.
     $this->assertEquals($testdata, $testfilelisting);
     // Try again but without the context. Minus one signals the function to use other variables to obtain the context.
     $nocontext = -1;
     $modified = 0;
     // Context level and instance ID are used to determine what the context is.
     $contextlevel = 'module';
     $instanceid = $module->cmid;
     $testfilelisting = core_files_external::get_files($nocontext, $component, $filearea, $itemid, '/', $filename, $modified, $contextlevel, $instanceid);
     $testfilelisting = external_api::clean_returnvalue(core_files_external::get_files_returns(), $testfilelisting);
     $this->assertEquals($testfilelisting, $testdata);
 }
Beispiel #5
0
 /**
  * Test update_picture
  */
 public function test_update_picture()
 {
     global $DB, $USER;
     $this->resetAfterTest(true);
     $user = self::getDataGenerator()->create_user();
     self::setUser($user);
     $context = context_user::instance($USER->id);
     $contextid = $context->id;
     $filename = "reddot.png";
     $filecontent = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38" . "GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";
     // Call the files api to create a file.
     $draftfile = core_files_external::upload($contextid, 'user', 'draft', 0, '/', $filename, $filecontent, null, null);
     $draftid = $draftfile['itemid'];
     // Change user profile image.
     $result = core_user_external::update_picture($draftid);
     $result = external_api::clean_returnvalue(core_user_external::update_picture_returns(), $result);
     $picture = $DB->get_field('user', 'picture', array('id' => $user->id));
     // The new revision is in the url for the user.
     $this->assertContains($picture, $result['profileimageurl']);
     // Check expected URL for serving the image.
     $this->assertContains("/{$contextid}/user/icon", $result['profileimageurl']);
     // Delete image.
     $result = core_user_external::update_picture(0, true);
     $result = external_api::clean_returnvalue(core_user_external::update_picture_returns(), $result);
     $picture = $DB->get_field('user', 'picture', array('id' => $user->id));
     // No picture.
     $this->assertEquals(0, $picture);
     // Add again the user profile image (as admin).
     $this->setAdminUser();
     $context = context_user::instance($USER->id);
     $admincontextid = $context->id;
     $draftfile = core_files_external::upload($admincontextid, 'user', 'draft', 0, '/', $filename, $filecontent, null, null);
     $draftid = $draftfile['itemid'];
     $result = core_user_external::update_picture($draftid, false, $user->id);
     $result = external_api::clean_returnvalue(core_user_external::update_picture_returns(), $result);
     // The new revision is in the url for the user.
     $picture = $DB->get_field('user', 'picture', array('id' => $user->id));
     $this->assertContains($picture, $result['profileimageurl']);
     $this->assertContains("/{$contextid}/user/icon", $result['profileimageurl']);
 }