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_ddimageortext\form;
|
|
|
18 |
|
|
|
19 |
use qtype_ddimageortext_edit_form;
|
|
|
20 |
|
|
|
21 |
defined('MOODLE_INTERNAL') || die();
|
|
|
22 |
global $CFG;
|
|
|
23 |
|
|
|
24 |
require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
|
|
|
25 |
require_once($CFG->dirroot . '/question/type/edit_question_form.php');
|
|
|
26 |
require_once($CFG->dirroot . '/question/type/ddimageortext/edit_ddimageortext_form.php');
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Unit tests for the drag-and-drop onto image edit form.
|
|
|
30 |
*
|
|
|
31 |
* @package qtype_ddimageortext
|
|
|
32 |
* @copyright 2019 The Open University
|
|
|
33 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
34 |
*/
|
1441 |
ariadna |
35 |
final class edit_form_test extends \advanced_testcase {
|
1 |
efrain |
36 |
/**
|
|
|
37 |
* Helper method.
|
|
|
38 |
*
|
|
|
39 |
* @return array with two elements:
|
|
|
40 |
* question_edit_form great a question form instance that can be tested.
|
|
|
41 |
* stdClass the question category.
|
|
|
42 |
*/
|
|
|
43 |
protected function get_form() {
|
|
|
44 |
$this->setAdminUser();
|
|
|
45 |
$this->resetAfterTest();
|
|
|
46 |
|
1441 |
ariadna |
47 |
$course = self::getDataGenerator()->create_course();
|
|
|
48 |
$qbank = self::getDataGenerator()->create_module('qbank', ['course' => $course->id]);
|
|
|
49 |
$bankcontext = \context_module::instance($qbank->cmid);
|
|
|
50 |
$category = question_get_default_category($bankcontext->id, true);
|
1 |
efrain |
51 |
$fakequestion = new \stdClass();
|
|
|
52 |
$fakequestion->qtype = 'ddimageortext';
|
1441 |
ariadna |
53 |
$fakequestion->contextid = $bankcontext->id;
|
1 |
efrain |
54 |
$fakequestion->createdby = 2;
|
|
|
55 |
$fakequestion->category = $category->id;
|
|
|
56 |
$fakequestion->questiontext = 'Test question';
|
|
|
57 |
$fakequestion->options = new \stdClass();
|
|
|
58 |
$fakequestion->options->answers = array();
|
|
|
59 |
$fakequestion->formoptions = new \stdClass();
|
|
|
60 |
$fakequestion->formoptions->movecontext = null;
|
|
|
61 |
$fakequestion->formoptions->repeatelements = true;
|
|
|
62 |
$fakequestion->inputs = null;
|
|
|
63 |
|
|
|
64 |
$form = new qtype_ddimageortext_edit_form(new \moodle_url('/'), $fakequestion, $category,
|
1441 |
ariadna |
65 |
new \core_question\local\bank\question_edit_contexts($bankcontext));
|
1 |
efrain |
66 |
|
|
|
67 |
return [$form, $category];
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* Test the form correctly validates the HTML allowed in items.
|
|
|
72 |
*/
|
11 |
efrain |
73 |
public function test_item_validation(): void {
|
1 |
efrain |
74 |
list($form, $category) = $this->get_form();
|
|
|
75 |
|
|
|
76 |
$submitteddata = [
|
|
|
77 |
'category' => $category->id,
|
|
|
78 |
'bgimage' => '',
|
|
|
79 |
'nodropzone' => 0,
|
|
|
80 |
'noitems' => 5,
|
|
|
81 |
'drags' => [
|
|
|
82 |
['dragitemtype' => 'image'],
|
|
|
83 |
['dragitemtype' => 'image'],
|
|
|
84 |
['dragitemtype' => 'word'],
|
|
|
85 |
['dragitemtype' => 'word'],
|
|
|
86 |
['dragitemtype' => 'word'],
|
|
|
87 |
],
|
|
|
88 |
'dragitem' => [
|
|
|
89 |
0,
|
|
|
90 |
0,
|
|
|
91 |
0,
|
|
|
92 |
0,
|
|
|
93 |
0,
|
|
|
94 |
],
|
|
|
95 |
'draglabel' => [
|
|
|
96 |
'frog',
|
|
|
97 |
'<b>toad</b>',
|
|
|
98 |
'cat',
|
|
|
99 |
'<span lang="fr"><b>chien</b></span>',
|
|
|
100 |
'<textarea>evil!</textarea>',
|
|
|
101 |
],
|
|
|
102 |
];
|
|
|
103 |
|
|
|
104 |
$errors = $form->validation($submitteddata, []);
|
|
|
105 |
|
|
|
106 |
$this->assertArrayNotHasKey('draglabel[0]', $errors);
|
|
|
107 |
$this->assertEquals('HTML tags are not allowed in this text which is the alt text for a draggable image.',
|
|
|
108 |
$errors['draglabel[1]']);
|
|
|
109 |
$this->assertArrayNotHasKey('draglabel[2]', $errors);
|
|
|
110 |
$this->assertArrayNotHasKey('draglabel[3]', $errors);
|
|
|
111 |
$this->assertEquals('Only "<br><sub><sup><b><i><strong><em><span>" ' .
|
|
|
112 |
'tags are allowed in this draggable text.', $errors['draglabel[4]']);
|
|
|
113 |
}
|
|
|
114 |
}
|