Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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 qbank_customfields;
18
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
global $CFG;
22
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
23
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
24
 
25
/**
26
 * Class qbank_customfields_customfield_testcase
27
 *
28
 * @package     qbank_customfields
29
 * @copyright   2021 Catalyst IT Australia Pty Ltd
30
 * @author      Matt Porritt <mattp@catalyst-au.net>
31
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
1441 ariadna 33
final class customfield_test extends \advanced_testcase {
1 efrain 34
 
35
    /**
36
     * @var array Data object for generating a question.
37
     */
38
    protected $question1data;
39
 
40
    /**
41
     * @var array Data object for generating a question.
42
     */
43
    protected $question2data;
44
 
45
    /**
46
     * @var component_generator_base Question Generator.
47
     */
48
    protected $qgen;
49
 
50
    /**
51
     * @var core_course_category Course category.
52
     */
53
    protected $category;
54
 
55
    /**
56
     * @var stdClass Course object.
57
     */
58
    protected $course;
59
 
60
    /**
61
     * @var int Timestamp to use in tests.
62
     */
63
    protected $testnow = 1632278491;
64
 
65
    /**
66
     * Helper to assist with setting up custom fields.
67
     * This is creating custom field category and the fields, not adding instance field data.
68
     */
69
    protected function setup_custom_fields(): void {
70
 
71
        $dg = self::getDataGenerator();
72
        $data = new \stdClass();
73
        $data->component = 'qbank_customfields';
74
        $data->area = 'question';
75
 
76
        $catid = $dg->create_custom_field_category($data)->get('id');
77
        $dg->create_custom_field(['categoryid' => $catid, 'type' => 'text', 'shortname' => 'f1']);
78
        $dg->create_custom_field(['categoryid' => $catid, 'type' => 'checkbox', 'shortname' => 'f2']);
79
        $dg->create_custom_field(['categoryid' => $catid, 'type' => 'date', 'shortname' => 'f3',
80
                'configdata' => ['startyear' => 2000, 'endyear' => 3000, 'includetime' => 1]]);
81
        $dg->create_custom_field(['categoryid' => $catid, 'type' => 'select', 'shortname' => 'f4',
82
                'configdata' => ['options' => "a\nb\nc"]]);
83
        $dg->create_custom_field(['categoryid' => $catid, 'type' => 'textarea', 'shortname' => 'f5']);
84
 
85
    }
86
 
87
    /**
88
     * Helper to assist with setting up questions used in tests.
89
     */
90
    protected function setup_questions(): void {
91
        // Question initial set up.
92
        $this->category = $this->getDataGenerator()->create_category();
93
        $this->course = $this->getDataGenerator()->create_course(['category' => $this->category->id]);
1441 ariadna 94
        $qbank = self::getDataGenerator()->create_module('qbank', ['course' => $this->course->id]);
95
        $context = \context_module::instance($qbank->cmid);
1 efrain 96
        $this->qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
97
        $qcat = $this->qgen->create_question_category(['contextid' => $context->id]);
98
 
99
        $this->question1data = [
100
                'category' => $qcat->id, 'idnumber' => 'q1',
101
                'customfield_f1' => 'some text', 'customfield_f2' => 1,
102
                'customfield_f3' => $this->testnow, 'customfield_f4' => 2,
103
                'customfield_f5_editor' => ['text' => 'test', 'format' => FORMAT_HTML]];
104
 
105
        $this->question2data = [
106
                'category' => $qcat->id, 'idnumber' => 'q2',
107
                'customfield_f1' => 'some more text', 'customfield_f2' => 0,
108
                'customfield_f3' => $this->testnow, 'customfield_f4' => 1,
109
                'customfield_f5_editor' => ['text' => 'test text', 'format' => FORMAT_HTML]];
110
    }
111
 
112
    /**
113
     * Makes a backup of the course.
114
     *
115
     * @param \stdClass $course The course object.
116
     * @return string Unique identifier for this backup.
117
     */
118
    protected function backup_course(\stdClass $course): string {
119
        global $CFG, $USER;
120
 
121
        // Turn off file logging, otherwise it can't delete the file (Windows).
122
        $CFG->backup_file_logger_level = \backup::LOG_NONE;
123
 
124
        // Do backup with default settings. MODE_IMPORT means it will just
125
        // create the directory and not zip it.
126
        $bc = new \backup_controller(\backup::TYPE_1COURSE, $course->id,
127
                \backup::FORMAT_MOODLE, \backup::INTERACTIVE_NO, \backup::MODE_IMPORT,
128
                $USER->id);
129
        $backupid = $bc->get_backupid();
130
        $bc->execute_plan();
131
        $bc->destroy();
132
 
133
        return $backupid;
134
    }
135
 
136
    /**
137
     * Restores a backup that has been made earlier.
138
     *
139
     * @param string $backupid The unique identifier of the backup.
140
     * @param string $fullname Full name of the new course that is going to be created.
141
     * @param string $shortname Short name of the new course that is going to be created.
142
     * @param int $categoryid The course category the backup is going to be restored in.
143
     * @return int The new course id.
144
     */
145
    protected function restore_course(string $backupid, string $fullname, string $shortname, int $categoryid): int {
146
        global $CFG, $USER;
147
 
148
        // Turn off file logging, otherwise it can't delete the file (Windows).
149
        $CFG->backup_file_logger_level = \backup::LOG_NONE;
150
 
151
        // Do restore to new course with default settings.
152
        $newcourseid = \restore_dbops::create_new_course($fullname, $shortname, $categoryid);
153
        $rc = new \restore_controller($backupid, $newcourseid,
154
                \backup::INTERACTIVE_NO, \backup::MODE_GENERAL, $USER->id,
155
                \backup::TARGET_NEW_COURSE);
156
 
157
        $rc->execute_precheck();
158
        $rc->execute_plan();
159
        $rc->destroy();
160
 
161
        return $newcourseid;
162
    }
163
 
164
    /**
165
     * Test creating questions with custom fields.
166
     */
167
    public function test_create_question(): void {
168
        $this->resetAfterTest();
169
        $this->setAdminUser();
170
        $this->setup_custom_fields();
171
        $this->setup_questions();
172
 
173
        // Create 2 questions.
174
        $question1 = $this->qgen->create_question('shortanswer', null, $this->question1data);
175
        $question2 = $this->qgen->create_question('shortanswer', null, $this->question2data);
176
 
177
        // Explicitly save the custom field data for the questions, like a form would.
178
        $customfieldhandler = \qbank_customfields\customfield\question_handler::create();
179
        $this->question1data['id'] = $question1->id;
180
        $this->question2data['id'] = $question2->id;
181
        $customfieldhandler->instance_form_save((object)$this->question1data);
182
        $customfieldhandler->instance_form_save((object)$this->question2data);
183
 
184
        // Get the custom field data associated with these question ids.
185
        $q1cfdata = $customfieldhandler->export_instance_data_object($question1->id);
186
        $q2cfdata = $customfieldhandler->export_instance_data_object($question2->id);
187
 
188
        $this->assertEquals('some text', $q1cfdata->f1);
189
        $this->assertEquals('Yes', $q1cfdata->f2);
190
        $this->assertEquals(userdate($this->testnow, get_string('strftimedaydatetime')), $q1cfdata->f3);
191
        $this->assertEquals('b', $q1cfdata->f4);
192
        $this->assertEquals('test', $q1cfdata->f5);
193
 
194
        $this->assertEquals('some more text', $q2cfdata->f1);
195
        $this->assertEquals('No', $q2cfdata->f2);
196
        $this->assertEquals(userdate($this->testnow, get_string('strftimedaydatetime')), $q2cfdata->f3);
197
        $this->assertEquals('a', $q2cfdata->f4);
198
        $this->assertEquals('test text', $q2cfdata->f5);
199
    }
200
 
201
    /**
202
     * Test deleting questions with custom fields.
203
     */
204
    public function test_delete_question(): void {
205
        $this->resetAfterTest();
206
        $this->setAdminUser();
207
        $this->setup_custom_fields();
208
        $this->setup_questions();
209
 
210
        // Create 2 questions.
211
        $question1 = $this->qgen->create_question('shortanswer', null, $this->question1data);
212
        $question2 = $this->qgen->create_question('shortanswer', null, $this->question2data);
213
 
214
        // Explicitly save the custom field data for the questions, like a form would.
215
        $customfieldhandler = \qbank_customfields\customfield\question_handler::create();
216
        $this->question1data['id'] = $question1->id;
217
        $this->question2data['id'] = $question2->id;
218
        $customfieldhandler->instance_form_save((object)$this->question1data);
219
        $customfieldhandler->instance_form_save((object)$this->question2data);
220
 
221
        // Get the custom field data associated with these question ids.
222
        $q1cfdata = $customfieldhandler->export_instance_data_object($question1->id);
223
        $q2cfdata = $customfieldhandler->export_instance_data_object($question2->id);
224
 
225
        // Quick check that we have data for the custom fields.
226
        $this->assertEquals('some text', $q1cfdata->f1);
227
        $this->assertEquals('some more text', $q2cfdata->f1);
228
 
229
        // Delete the questions.
230
        question_delete_question($question1->id);
231
        question_delete_question($question2->id);
232
 
233
        // Check the custom field data for the questions has also gone.
234
        $q1cfdata = $customfieldhandler->export_instance_data_object($question1->id);
235
        $q2cfdata = $customfieldhandler->export_instance_data_object($question2->id);
236
 
237
        $this->assertEmpty($q1cfdata->f1);
238
        $this->assertEmpty($q2cfdata->f1);
239
    }
240
 
241
    /**
242
     * Test custom fields attached to questions persist
243
     * across the backup and restore process.
244
     */
245
    public function test_backup_restore(): void {
246
        global $DB;
247
 
248
        $this->resetAfterTest();
249
        $this->setAdminUser();
250
        $this->setup_custom_fields();
251
        $this->setup_questions();
252
 
253
        $courseshortname = $this->course->shortname;
254
        $coursefullname = $this->course->fullname;
255
 
256
        // Create 2 questions.
257
        $question1 = $this->qgen->create_question('shortanswer', null, $this->question1data);
258
        $question2 = $this->qgen->create_question('shortanswer', null, $this->question2data);
259
 
260
        // Explicitly save the custom field data for the questions, like a form would.
261
        $customfieldhandler = \qbank_customfields\customfield\question_handler::create();
262
        $this->question1data['id'] = $question1->id;
263
        $this->question2data['id'] = $question2->id;
264
        $customfieldhandler->instance_form_save((object)$this->question1data);
265
        $customfieldhandler->instance_form_save((object)$this->question2data);
266
 
267
        // Create a quiz and the questions to that.
268
        $quiz = $this->getDataGenerator()->create_module(
269
                'quiz', ['course' => $this->course->id, 'name' => 'restored_quiz']);
270
        quiz_add_quiz_question($question1->id, $quiz);
271
        quiz_add_quiz_question($question2->id, $quiz);
272
 
273
        // Backup the course.
274
        $backupid = $this->backup_course($this->course);
275
 
276
        // Now delete everything.
277
        delete_course($this->course, false);
278
        question_delete_question($question1->id);
279
        question_delete_question($question2->id);
280
 
281
        // Check the custom field data for the questions has also gone.
282
        $q1cfdata = $customfieldhandler->export_instance_data_object($question1->id);
283
        $q2cfdata = $customfieldhandler->export_instance_data_object($question2->id);
284
 
285
        $this->assertEmpty($q1cfdata->f1);
286
        $this->assertEmpty($q2cfdata->f1);
287
 
288
        // Restore the backup we had made earlier into a new course.
289
        $newcategory = $this->getDataGenerator()->create_category();
290
        $this->restore_course($backupid, $coursefullname, $courseshortname . '_2', $newcategory->id);
291
 
292
        // The questions and their associated custom fields should have been restored.
293
        $sql = 'SELECT q.*
294
                 FROM {question} q
295
                 JOIN {question_versions} qv ON qv.questionid = q.id
296
                 JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
297
                WHERE qbe.idnumber = ?';
298
        $newquestion1 = $DB->get_record_sql($sql, ['q1']);
299
        $newquestion1cfdata = $customfieldhandler->export_instance_data_object($newquestion1->id);
300
        $this->assertEquals('some text', $newquestion1cfdata->f1);
301
        $this->assertEquals('Yes', $newquestion1cfdata->f2);
302
        $this->assertEquals(userdate($this->testnow, get_string('strftimedaydatetime')), $newquestion1cfdata->f3);
303
        $this->assertEquals('b', $newquestion1cfdata->f4);
304
        $this->assertEquals('test', $newquestion1cfdata->f5);
305
 
306
        $newquestion2 = $DB->get_record_sql($sql, ['q2']);
307
        $newquestion2cfdata = $customfieldhandler->export_instance_data_object($newquestion2->id);
308
        $this->assertEquals('some more text', $newquestion2cfdata->f1);
309
        $this->assertEquals('No', $newquestion2cfdata->f2);
310
        $this->assertEquals(userdate($this->testnow, get_string('strftimedaydatetime')), $newquestion2cfdata->f3);
311
        $this->assertEquals('a', $newquestion2cfdata->f4);
312
        $this->assertEquals('test text', $newquestion2cfdata->f5);
313
    }
314
}