<commit_before><?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.


/**
 * PHPunit tests for external files API.
 *
 * @package    core_files
 * @category   external
 * @copyright  2013 <NAME>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 * @since Moodle 2.6
 */
defined('MOODLE_INTERNAL') || die();

global $CFG;

require_once($CFG->dirroot . '/webservice/tests/helpers.php');
require_once($CFG->dirroot . '/files/externallib.php');

class core_files_externallib_testcase extends advanced_testcase {

    /*
     * Test core_files_external::upload().
     */

    public function test_upload() {
        global $USER;

        $this->resetAfterTest();
        $this->setAdminUser();
        $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();

        // Make sure no file exists.
        $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
        $this->assertEmpty($file);

        // Call the api to create a file.
        $fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath,
                $filename, $filecontent, $contextlevel, $instanceid);
        // Get the created draft item id.
        $itemid = $fileinfo['itemid'];

        // Make sure the file was created.
        $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
        $this->assertNotEmpty($file);

        // Make sure no file exists.
        $filename = "Simple2.txt";
        $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
        $this->assertEmpty($file);

        // Call the api to create a file.
        $fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid,
                $filepath, $filename, $filecontent, $contextlevel, $instanceid);
        $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
        $this->assertNotEmpty($file);

        // Let us try creating a file using contextlevel and instance id.
        $filename = "Simple5.txt";
        $contextid = 0;
        $contextlevel = "user";
        $instanceid = $USER->id;
        $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
        $this->assertEmpty($file);
        $fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath,
                $filename, $filecontent, $contextlevel, $instanceid);
        $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
        $this->assertNotEmpty($file);

        // Make sure the same file cannot be created again.
        $this->setExpectedException("moodle_exception");
        core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath,
                $filename, $filecontent, $contextlevel, $instanceid);
    }

    /*
     * Make sure only user component is allowed in  core_files_external::upload().
     */
    public function test_upload_param_component() {
        global $USER;

        $this->resetAfterTest();
        $this->setAdminUser();
        $context = context_user::instance($USER->id);
        $contextid = $context->id;
        $component = "backup";
        $filearea = "private";
        $itemid = 0;
        $filepath = "/";
        $filename = "Simple3.txt";
        $filecontent = base64_encode("Let us create a nice simple file");
        $contextlevel = null;
        $instanceid = null;

        // Make sure exception is thrown.
        $this->setExpectedException("coding_exception");
        core_files_external::upload($contextid, $component, $filearea, $itemid,
                $filepath, $filename, $filecontent, $contextlevel, $instanceid);
    }

    /*
     * Make sure only private or draft areas are allowed in  core_files_external::upload().
     */
    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 = file_get_unused_draft_itemid();
        $filepath = "/";
        $filename = "Simple4.txt";
        $filecontent = base64_encode("Let us create a nice simple file");
        $contextlevel = null;
        $instanceid = null;

        // Make sure the file is created.
        @core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent);
        $browser = get_file_browser();
        $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
        $this->assertNotEmpty($file);
    }

    /*
     * Make sure core_files_external::upload() works without new parameters.
     */
    public function test_upload_without_new_param() {
        global $USER;

        $this->resetAfterTest();
        $this->setAdminUser();
        $context = context_user::instance($USER->id);
        $contextid = $context->id;
        $component = "user";
        $filearea = "private";
        $itemid = 0;
        $filepath = "/";
        $filename = "Simple4.txt";
        $filecontent = base64_encode("Let us create a nice simple file");

        @core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent);

        // Assert debugging called (deprecation warning).
        $this->assertDebuggingCalled();

        // Make sure the file is created.
        $browser = get_file_browser();
        $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
        $this->assertNotEmpty($file);
    }

    public function test_get_files() {
        global $USER, $DB;

        $this->resetAfterTest();

        $this->setAdminUser();
        $USER->email = '<EMAIL>';
        // print_object($USER);

        $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";

        $module = $this->getDataGenerator()->create_module('data', $record);

        $field = data_get_field_new('file', $module);

        $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);

        $timemodified = $DB->get_field('data_records', 'timemodified', array('id' => $recordid));

        $datacontent = array();
        $datacontent['fieldid'] = $field->field->id;
        $datacontent['recordid'] = $recordid;
        $datacontent['content'] = 'Simple4.txt';<commit_msg>MDL-40038 files: Removed commented code from unit test<commit_after><?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.


/**
 * PHPunit tests for external files API.
 *
 * @package    core_files
 * @category   external
 * @copyright  2013 <NAME>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 * @since Moodle 2.6
 */
defined('MOODLE_INTERNAL') || die();

global $CFG;

require_once($CFG->dirroot . '/webservice/tests/helpers.php');
require_once($CFG->dirroot . '/files/externallib.php');

class core_files_externallib_testcase extends advanced_testcase {

    /*
     * Test core_files_external::upload().
     */

    public function test_upload() {
        global $USER;

        $this->resetAfterTest();
        $this->setAdminUser();
        $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();

        // Make sure no file exists.
        $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
        $this->assertEmpty($file);

        // Call the api to create a file.
        $fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath,
                $filename, $filecontent, $contextlevel, $instanceid);
        // Get the created draft item id.
        $itemid = $fileinfo['itemid'];

        // Make sure the file was created.
        $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
        $this->assertNotEmpty($file);

        // Make sure no file exists.
        $filename = "Simple2.txt";
        $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
        $this->assertEmpty($file);

        // Call the api to create a file.
        $fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid,
                $filepath, $filename, $filecontent, $contextlevel, $instanceid);
        $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
        $this->assertNotEmpty($file);

        // Let us try creating a file using contextlevel and instance id.
        $filename = "Simple5.txt";
        $contextid = 0;
        $contextlevel = "user";
        $instanceid = $USER->id;
        $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
        $this->assertEmpty($file);
        $fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath,
                $filename, $filecontent, $contextlevel, $instanceid);
        $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
        $this->assertNotEmpty($file);

        // Make sure the same file cannot be created again.
        $this->setExpectedException("moodle_exception");
        core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath,
                $filename, $filecontent, $contextlevel, $instanceid);
    }

    /*
     * Make sure only user component is allowed in  core_files_external::upload().
     */
    public function test_upload_param_component() {
        global $USER;

        $this->resetAfterTest();
        $this->setAdminUser();
        $context = context_user::instance($USER->id);
        $contextid = $context->id;
        $component = "backup";
        $filearea = "private";
        $itemid = 0;
        $filepath = "/";
        $filename = "Simple3.txt";
        $filecontent = base64_encode("Let us create a nice simple file");
        $contextlevel = null;
        $instanceid = null;

        // Make sure exception is thrown.
        $this->setExpectedException("coding_exception");
        core_files_external::upload($contextid, $component, $filearea, $itemid,
                $filepath, $filename, $filecontent, $contextlevel, $instanceid);
    }

    /*
     * Make sure only private or draft areas are allowed in  core_files_external::upload().
     */
    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 = file_get_unused_draft_itemid();
        $filepath = "/";
        $filename = "Simple4.txt";
        $filecontent = base64_encode("Let us create a nice simple file");
        $contextlevel = null;
        $instanceid = null;

        // Make sure the file is created.
        @core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent);
        $browser = get_file_browser();
        $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
        $this->assertNotEmpty($file);
    }

    /*
     * Make sure core_files_external::upload() works without new parameters.
     */
    public function test_upload_without_new_param() {
        global $USER;

        $this->resetAfterTest();
        $this->setAdminUser();
        $context = context_user::instance($USER->id);
        $contextid = $context->id;
        $component = "user";
        $filearea = "private";
        $itemid = 0;
        $filepath = "/";
        $filename = "Simple4.txt";
        $filecontent = base64_encode("Let us create a nice simple file");

        @core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent);

        // Assert debugging called (deprecation warning).
        $this->assertDebuggingCalled();

        // Make sure the file is created.
        $browser = get_file_browser();
        $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
        $this->assertNotEmpty($file);
    }

    public function test_get_files() {
        global $USER, $DB;

        $this->resetAfterTest();

        $this->setAdminUser();
        $USER->email = '<EMAIL>';

        $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";

        $module = $this->getDataGenerator()->create_module('data', $record);

        $field = data_get_field_new('file', $module);

        $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);

        $timemodified = $DB->get_field('data_records', 'timemodified', array('id' => $recordid));

        $datacontent = array();
        $datacontent['fieldid'] = $field->field->id;
        $datacontent['recordid'] = $recordid;
        $datacontent['content'] = 'Simple4.txt';
{'fraction_non_alphanumeric': 0.142282211627763, 'fraction_numerical': 0.0037768559222339172, 'mean_word_length': 2.755405719600093, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 135, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2341232', 'n_tokens_mistral': 4760, 'n_tokens_neox': 4638, 'n_words': 1183}