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/**
18 * Unit tests for usage of tags in quizzes.
19 *
20 * @package    mod_quiz
21 * @copyright  2018 Shamim Rezaie <shamim@moodle.com>
22 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27/**
28 * Class mod_quiz_tags_testcase
29 * Class for tests related to usage of question tags in quizzes.
30 *
31 * @copyright  2018 Shamim Rezaie <shamim@moodle.com>
32 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
34class mod_quiz_tags_testcase extends advanced_testcase {
35    public function test_restore_random_question_by_tag() {
36        global $CFG, $USER, $DB;
37
38        require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
39        require_once($CFG->dirroot . '/mod/quiz/locallib.php');
40
41        $this->resetAfterTest();
42        $this->setAdminUser();
43
44        $backupid = 'abc';
45        $backuppath = make_backup_temp_directory($backupid);
46        get_file_packer('application/vnd.moodle.backup')->extract_to_pathname(
47                __DIR__ . "/fixtures/random_by_tag_quiz.mbz", $backuppath);
48
49        // Do the restore to new course with default settings.
50        $categoryid = $DB->get_field_sql("SELECT MIN(id) FROM {course_categories}");
51        $newcourseid = restore_dbops::create_new_course('Test fullname', 'Test shortname', $categoryid);
52        $rc = new restore_controller($backupid, $newcourseid, backup::INTERACTIVE_NO, backup::MODE_GENERAL, $USER->id,
53                backup::TARGET_NEW_COURSE);
54
55        $this->assertTrue($rc->execute_precheck());
56        $rc->execute_plan();
57        $rc->destroy();
58
59        // Get the information about the resulting course and check that it is set up correctly.
60        $modinfo = get_fast_modinfo($newcourseid);
61        $quiz = array_values($modinfo->get_instances_of('quiz'))[0];
62        $quizobj = quiz::create($quiz->instance);
63        $structure = \mod_quiz\structure::create_for_quiz($quizobj);
64
65        // Are the correct slots returned?
66        $slots = $structure->get_slots();
67        $this->assertCount(1, $slots);
68
69        $quizobj->preload_questions();
70        $quizobj->load_questions();
71        $questions = $quizobj->get_questions();
72
73        $this->assertCount(1, $questions);
74
75        $question = array_values($questions)[0];
76
77        $tag1 = core_tag_tag::get_by_name(0, 't1', 'id, name');
78        $this->assertNotFalse($tag1);
79
80        $tag2 = core_tag_tag::get_by_name(0, 't2', 'id, name');
81        $this->assertNotFalse($tag2);
82
83        $tag3 = core_tag_tag::get_by_name(0, 't3', 'id, name');
84        $this->assertNotFalse($tag3);
85
86        $slottags = quiz_retrieve_slot_tags($question->slotid);
87        $this->assertEqualsCanonicalizing(
88                [
89                    ['tagid' => $tag2->id, 'tagname' => $tag2->name]
90                ],
91                array_map(function($tag) {
92                    return ['tagid' => $tag->tagid, 'tagname' => $tag->tagname];
93                }, $slottags)
94        );
95
96        $defaultcategory = question_get_default_category(context_course::instance($newcourseid)->id);
97        $this->assertEquals($defaultcategory->id, $question->randomfromcategory);
98        $this->assertEquals(0, $question->randomincludingsubcategories);
99    }
100}
101