<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/>.

defined('MOODLE_INTERNAL') || die();

/**
 * Automated unit testing. This tests the 'make large course' backend,
 * using the 'XS' option so that it completes quickly.
 *
 * @package tool_generator
 * @copyright 2013 The Open University
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class tool_generator_maketestcourse_testcase extends advanced_testcase {
    /**
     * Creates a small test course and checks all the components have been put in place.
     */
    public function test_make_xs_course() {
        global $DB;

        $this->resetAfterTest();
        $this->setAdminUser();

        // Create the XS course.
        $backend = new tool_generator_backend('TOOL_MAKELARGECOURSE_XS', 0, false, false);
        $courseid = $backend->make();

        // Get course details.
        $course = get_course($courseid);
        $context = context_course::instance($courseid);
        $modinfo = get_fast_modinfo($course);

        // Check sections (just section 0 plus one other).
        $this->assertEquals(2, count($modinfo->get_section_info_all()));

        // Check user is enrolled.
        $users = get_enrolled_users($context);
        $this->assertEquals(1, count($users));
        $this->assertEquals('tool_generator_000001', reset($users)->username);

        // Check there's a page on the course.
        $pages = $modinfo->get_instances_of('page');
        $this->assertEquals(1, count($pages));

        // Check there are small files.
        $resources = $modinfo->get_instances_of('resource');
        $ok = false;
        foreach ($resources as $resource) {
            if ($resource->sectionnum == 0) {
                // The one in section 0 is the 'small files' resource.
                $ok = true;
                break;
            }
        }
        $this->assertTrue($ok);

        // Check it contains 2 files (the default txt and a dat file).
        $fs = get_file_storage();
        $resourcecontext = context_module::instance($resource->id);
        $files = $fs->get_area_files($resourcecontext->id, 'mod_resource', 'content', false, 'filename', false);
        $files = array_values($files);
        $this->assertEquals(2, count($files));
        $this->assertEquals('resource1.txt', $files[0]->get_filename());
        $this->assertEquals('smallfile0.dat', $files[1]->get_filename());

        // Check there's a single 'big' file (it's actually only 8KB).
        $ok = false;
        foreach ($resources as $resource) {
            if ($resource->sectionnum == 1) {
                $ok = true;
                break;
            }
        }
        $this->assertTrue($ok);

        // Check it contains 2 files.
        $resourcecontext = context_module::instance($resource->id);
        $files = $fs->get_area_files($resourcecontext->id, 'mod_resource', 'content', false, 'filename', false);
        $files = array_values($files);
        $this->assertEquals(2, count($files));
        $this->assertEquals('bigfile0.dat', $files[0]->get_filename());
        $this->assertEquals('resource2.txt', $files[1]->get_filename());

        // Get forum and count the number of posts on it.
        $forums = $modinfo->get_instances_of('forum');
        $forum = reset($forums);
        $posts = $DB->count_records_sql("
                SELECT
                    COUNT(1)
                FROM
                    {forum_posts} fp
                    JOIN {forum_discussions} fd ON fd.id = fp.discussion
                WHERE
                    fd.forum = ?", array($forum->instance));
        $this->assertEquals(2, $posts);
    }
}
<commit_msg>MDL-41399 tool_generator: Unit test for fixeddataset<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/>.

defined('MOODLE_INTERNAL') || die();

/**
 * Automated unit testing. This tests the 'make large course' backend,
 * using the 'XS' option so that it completes quickly.
 *
 * @package tool_generator
 * @copyright 2013 The Open University
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class tool_generator_maketestcourse_testcase extends advanced_testcase {
    /**
     * Creates a small test course and checks all the components have been put in place.
     */
    public function test_make_xs_course() {
        global $DB;

        $this->resetAfterTest();
        $this->setAdminUser();

        // Create the XS course.
        $backend = new tool_generator_backend('TOOL_MAKELARGECOURSE_XS', 0, false, false);
        $courseid = $backend->make();

        // Get course details.
        $course = get_course($courseid);
        $context = context_course::instance($courseid);
        $modinfo = get_fast_modinfo($course);

        // Check sections (just section 0 plus one other).
        $this->assertEquals(2, count($modinfo->get_section_info_all()));

        // Check user is enrolled.
        $users = get_enrolled_users($context);
        $this->assertEquals(1, count($users));
        $this->assertEquals('tool_generator_000001', reset($users)->username);

        // Check there's a page on the course.
        $pages = $modinfo->get_instances_of('page');
        $this->assertEquals(1, count($pages));

        // Check there are small files.
        $resources = $modinfo->get_instances_of('resource');
        $ok = false;
        foreach ($resources as $resource) {
            if ($resource->sectionnum == 0) {
                // The one in section 0 is the 'small files' resource.
                $ok = true;
                break;
            }
        }
        $this->assertTrue($ok);

        // Check it contains 2 files (the default txt and a dat file).
        $fs = get_file_storage();
        $resourcecontext = context_module::instance($resource->id);
        $files = $fs->get_area_files($resourcecontext->id, 'mod_resource', 'content', false, 'filename', false);
        $files = array_values($files);
        $this->assertEquals(2, count($files));
        $this->assertEquals('resource1.txt', $files[0]->get_filename());
        $this->assertEquals('smallfile0.dat', $files[1]->get_filename());

        // Check there's a single 'big' file (it's actually only 8KB).
        $ok = false;
        foreach ($resources as $resource) {
            if ($resource->sectionnum == 1) {
                $ok = true;
                break;
            }
        }
        $this->assertTrue($ok);

        // Check it contains 2 files.
        $resourcecontext = context_module::instance($resource->id);
        $files = $fs->get_area_files($resourcecontext->id, 'mod_resource', 'content', false, 'filename', false);
        $files = array_values($files);
        $this->assertEquals(2, count($files));
        $this->assertEquals('bigfile0.dat', $files[0]->get_filename());
        $this->assertEquals('resource2.txt', $files[1]->get_filename());

        // Get forum and count the number of posts on it.
        $forums = $modinfo->get_instances_of('forum');
        $forum = reset($forums);
        $posts = $DB->count_records_sql("
                SELECT
                    COUNT(1)
                FROM
                    {forum_posts} fp
                    JOIN {forum_discussions} fd ON fd.id = fp.discussion
                WHERE
                    fd.forum = ?", array($forum->instance));
        $this->assertEquals(2, $posts);
    }

    /**
     * Creates an small test course with fixed data set and checks the used sections and users.
     */
    public function test_fixed_data_set() {
        global $DB;

        $this->resetAfterTest();
        $this->setAdminUser();

        // Create the S course (more sections and activities than XS).
        $backend = new tool_generator_backend('TOOL_S_COURSE_1', 1, true, false);
        $courseid = $backend->make();

        // Get course details.
        $course = get_course($courseid);
        $modinfo = get_fast_modinfo($course);

        // Check module instances belongs to section 1.
        $instances = $modinfo->get_instances_of('page');
        $npageinstances = count($instances);
        foreach ($instances as $instance) {
            $this->assertEquals(1, $instance->sectionnum);
        }

        // Users that started discussions are the same.
        $forums = $modinfo->get_instances_of('forum');
        $nforuminstances = count($forums);
        $discussions = forum_get_discussions(reset($forums), 'd.timemodified ASC');
        $lastusernumber = 0;
        $discussionstarters = array();
        foreach ($discussions as $discussion) {
            $usernumber = intval($discussion->lastname);

            // Checks that the users are odd numbers.
            $this->assertEquals(1, $usernumber % 2);

            // Checks that the users follows an increasing order.
            $this->assertGreaterThan($lastusernumber, $usernumber);
            $lastusernumber = $usernumber;
            $discussionstarters[$discussion->userid] = $discussion->subject;
        }

        // Resetting data generators internal counters. Courses are created in individual
        // processes so the counters are restarted before each course creation.
        $this->resetAllData();
        $this->setAdminUser();

        // Create another S course to check that the same data was generated.
        $backend = new tool_generator_backend('TOOL_S_COURSE_2', 1, true, false);
        $courseid = $backend->make();
        $course = get_course($courseid);
        $modinfo = get_fast_modinfo($course);

        // Check it generates the same number of page activities.
        $this->assertEquals(count($modinfo->get_instances_of('page')), $npageinstances);

        // Check it generates the same number of forum activities.
        $forums = $modinfo->get_instances_of('forum');
        $this->assertEquals(count($forums), $nforuminstances);

        // Check same discussions have been started by the same users.
        $discussions = forum_get_discussions(reset($forums), 'd.timemodified ASC');
        reset($discussionstarters);
        foreach ($discussions as $discussion) {
            $previousdiscussionsubject = current($discussionstarters);
            $previoususer = key($discussionstarters);
            $this->assertEquals($discussion->userid, $previoususer);
            $this->assertEquals($discussion->subject, $previousdiscussionsubject);
            next($discussionstarters);
        }

    }
}

{'fraction_non_alphanumeric': 0.12614150379790048, 'fraction_numerical': 0.007169070581206793, 'mean_word_length': 2.4658385093167703, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 98, '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': '2884635', 'n_tokens_mistral': 3309, 'n_tokens_neox': 3122, 'n_words': 927}