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 qtype_essay;
|
|
|
18 |
|
|
|
19 |
defined('MOODLE_INTERNAL') || die();
|
|
|
20 |
|
|
|
21 |
global $CFG;
|
|
|
22 |
require_once($CFG->libdir . "/phpunit/classes/restore_date_testcase.php");
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* Test restore logic.
|
|
|
26 |
*
|
|
|
27 |
* @package qtype_essay
|
|
|
28 |
* @copyright 2019 The Open University
|
|
|
29 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
30 |
*/
|
1441 |
ariadna |
31 |
final class restore_test extends \restore_date_testcase {
|
1 |
efrain |
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Test missing qtype_essay_options creation.
|
|
|
35 |
*
|
|
|
36 |
* Old backup files may contain essays with no qtype_essay_options record.
|
|
|
37 |
* During restore, we add default options for any questions like that.
|
|
|
38 |
* That is what is tested in this file.
|
|
|
39 |
*/
|
11 |
efrain |
40 |
public function test_restore_create_missing_qtype_essay_options(): void {
|
1 |
efrain |
41 |
global $DB;
|
|
|
42 |
|
|
|
43 |
// Create a course with one essay question in its question bank.
|
|
|
44 |
$generator = $this->getDataGenerator();
|
|
|
45 |
$course = $generator->create_course();
|
1441 |
ariadna |
46 |
$qbank = $generator->create_module('qbank', ['course' => $course->id]);
|
|
|
47 |
$context = \context_module::instance($qbank->cmid);
|
|
|
48 |
$category = question_get_default_category($context->id, true);
|
1 |
efrain |
49 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
50 |
$essay = $questiongenerator->create_question('essay', null, array('category' => $category->id));
|
|
|
51 |
|
|
|
52 |
// Remove the options record, which means that the backup will look like a backup made in an old Moodle.
|
|
|
53 |
$DB->delete_records('qtype_essay_options', ['questionid' => $essay->id]);
|
|
|
54 |
|
|
|
55 |
// Do backup and restore.
|
|
|
56 |
$newcourseid = $this->backup_and_restore($course);
|
|
|
57 |
|
1441 |
ariadna |
58 |
$modinfo = get_fast_modinfo($newcourseid);
|
|
|
59 |
$newqbanks = array_filter(
|
|
|
60 |
$modinfo->get_instances_of('qbank'),
|
|
|
61 |
static fn($qbank) => $qbank->get_name() === 'Question bank 1'
|
|
|
62 |
);
|
|
|
63 |
$newqbank = reset($newqbanks);
|
|
|
64 |
|
1 |
efrain |
65 |
// Verify that the restored question has options.
|
1441 |
ariadna |
66 |
$newcategory = question_get_default_category(\context_module::instance($newqbank->id)->id, true);
|
1 |
efrain |
67 |
$newessay = $DB->get_record_sql('SELECT q.*
|
|
|
68 |
FROM {question} q
|
|
|
69 |
JOIN {question_versions} qv ON qv.questionid = q.id
|
|
|
70 |
JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
|
|
|
71 |
WHERE qbe.questioncategoryid = ?
|
|
|
72 |
AND q.qtype = ?', [$newcategory->id, 'essay']);
|
|
|
73 |
$this->assertTrue($DB->record_exists('qtype_essay_options', ['questionid' => $newessay->id]));
|
|
|
74 |
}
|
|
|
75 |
}
|