1 |
efrain |
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 |
namespace mod_quiz;
|
|
|
18 |
|
|
|
19 |
use core_question_generator;
|
|
|
20 |
use mod_quiz_generator;
|
|
|
21 |
use restore_date_testcase;
|
|
|
22 |
|
|
|
23 |
defined('MOODLE_INTERNAL') || die();
|
|
|
24 |
|
|
|
25 |
global $CFG;
|
|
|
26 |
require_once($CFG->libdir . "/phpunit/classes/restore_date_testcase.php");
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Test of backup and restore of quiz grade items.
|
|
|
30 |
*
|
|
|
31 |
* @package mod_quiz
|
|
|
32 |
* @copyright 2023 The Open University
|
|
|
33 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
34 |
* @covers \backup_quiz_activity_structure_step
|
|
|
35 |
* @covers \restore_quiz_activity_structure_step
|
|
|
36 |
*/
|
|
|
37 |
final class restore_quiz_grade_items_test extends restore_date_testcase {
|
|
|
38 |
|
|
|
39 |
public function test_restore_quiz_grade_items(): void {
|
|
|
40 |
global $DB;
|
|
|
41 |
$this->resetAfterTest();
|
|
|
42 |
|
|
|
43 |
$generator = $this->getDataGenerator();
|
|
|
44 |
/** @var mod_quiz_generator $quizgen */
|
|
|
45 |
$quizgen = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
|
|
|
46 |
/** @var core_question_generator $questiongenerator */
|
|
|
47 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
48 |
|
|
|
49 |
// Create a quiz with two grade items.
|
|
|
50 |
$course = $generator->create_course();
|
|
|
51 |
$quiz = $quizgen->create_instance(['course' => $course->id]);
|
|
|
52 |
$listeninggrade = $quizgen->create_grade_item(['quizid' => $quiz->id, 'name' => 'Listening']);
|
|
|
53 |
$readinggrade = $quizgen->create_grade_item(['quizid' => $quiz->id, 'name' => 'Reading']);
|
|
|
54 |
|
|
|
55 |
// Add two questions to the quiz.
|
|
|
56 |
$cat = $questiongenerator->create_question_category();
|
|
|
57 |
$saq1 = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
|
|
|
58 |
$saq2 = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
|
|
|
59 |
quiz_add_quiz_question($saq1->id, $quiz, 0, 1);
|
|
|
60 |
quiz_add_quiz_question($saq2->id, $quiz, 0, 1);
|
|
|
61 |
|
|
|
62 |
// Set one of the question to use a grade item.
|
|
|
63 |
$quizobj = quiz_settings::create($quiz->id);
|
|
|
64 |
$structure = $quizobj->get_structure();
|
|
|
65 |
$structure->update_slot_grade_item($structure->get_slot_by_number(2), $readinggrade->id);
|
|
|
66 |
$quizobj->get_grade_calculator()->recompute_quiz_sumgrades();
|
|
|
67 |
|
|
|
68 |
// Back up and restore the course.
|
|
|
69 |
$newcourseid = $this->backup_and_restore($course);
|
|
|
70 |
|
|
|
71 |
// Verify the grade items were copied over.
|
|
|
72 |
$newquiz = $DB->get_record('quiz', ['course' => $newcourseid]);
|
|
|
73 |
$quizobj = quiz_settings::create($newquiz->id);
|
|
|
74 |
$structure = $quizobj->get_structure();
|
|
|
75 |
$quizgradeitems = array_values($structure->get_grade_items());
|
|
|
76 |
|
|
|
77 |
// Check the grade items are right in the restored quiz.
|
|
|
78 |
$this->assertEquals(
|
|
|
79 |
[
|
|
|
80 |
(object) ['id' => reset($quizgradeitems)->id, 'quizid' => $newquiz->id, 'sortorder' => 1, 'name' => 'Listening'],
|
|
|
81 |
(object) ['id' => end($quizgradeitems)->id, 'quizid' => $newquiz->id, 'sortorder' => 2, 'name' => 'Reading'],
|
|
|
82 |
],
|
|
|
83 |
array_values($quizgradeitems),
|
|
|
84 |
);
|
|
|
85 |
|
|
|
86 |
// Verify that each slot uses the right grade item.
|
|
|
87 |
$this->assertNull($structure->get_slot_by_number(1)->quizgradeitemid);
|
|
|
88 |
$this->assertEquals(end($quizgradeitems)->id, $structure->get_slot_by_number(2)->quizgradeitemid);
|
|
|
89 |
}
|
|
|
90 |
}
|