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 |
/**
|
|
|
18 |
* Unit tests for the select missing words question edit form.
|
|
|
19 |
*
|
|
|
20 |
* @package qtype_gapselect
|
|
|
21 |
* @copyright 2012 The Open University
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace qtype_gapselect\form;
|
|
|
26 |
|
|
|
27 |
defined('MOODLE_INTERNAL') || die();
|
|
|
28 |
global $CFG;
|
|
|
29 |
|
|
|
30 |
require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
|
|
|
31 |
require_once($CFG->dirroot . '/question/type/edit_question_form.php');
|
|
|
32 |
require_once($CFG->dirroot . '/question/type/gapselect/edit_gapselect_form.php');
|
|
|
33 |
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Subclass of qtype_gapselect_edit_form_base that is easier to used in unit tests.
|
|
|
37 |
*
|
|
|
38 |
* @copyright 2012 The Open University
|
|
|
39 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
40 |
*/
|
|
|
41 |
class qtype_gapselect_edit_form_base_testable extends \qtype_gapselect_edit_form_base {
|
|
|
42 |
public function get_illegal_tag_error($text) {
|
|
|
43 |
return parent::get_illegal_tag_error($text);
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Set the list of allowed tags.
|
|
|
48 |
* @param array $allowed
|
|
|
49 |
*/
|
|
|
50 |
public function set_allowed_tags(array $allowed) {
|
|
|
51 |
$this->allowedhtmltags = $allowed;
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
public function qtype() {
|
|
|
55 |
return 'gapselect';
|
|
|
56 |
}
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Unit tests for select missing words question edit form.
|
|
|
62 |
*
|
|
|
63 |
* @package qtype_gapselect
|
|
|
64 |
* @copyright 2012 The Open University
|
|
|
65 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
66 |
*/
|
1441 |
ariadna |
67 |
final class edit_form_test extends \advanced_testcase {
|
1 |
efrain |
68 |
|
|
|
69 |
/**
|
|
|
70 |
* Helper method.
|
|
|
71 |
*
|
|
|
72 |
* @param string $classname the question form class to instantiate.
|
|
|
73 |
*
|
|
|
74 |
*
|
|
|
75 |
* @return array with two elements:
|
|
|
76 |
* question_edit_form great a question form instance that can be tested.
|
|
|
77 |
* stdClass the question category.
|
|
|
78 |
*/
|
|
|
79 |
protected function get_form($classname) {
|
|
|
80 |
$this->setAdminUser();
|
|
|
81 |
$this->resetAfterTest();
|
|
|
82 |
|
1441 |
ariadna |
83 |
$course = self::getDataGenerator()->create_course();
|
|
|
84 |
$qbank = self::getDataGenerator()->create_module('qbank', ['course' => $course->id]);
|
|
|
85 |
$bankcontext = \context_module::instance($qbank->cmid);
|
|
|
86 |
$category = question_get_default_category($bankcontext->id, true);
|
1 |
efrain |
87 |
$fakequestion = new \stdClass();
|
|
|
88 |
$fakequestion->qtype = 'gapselect'; // Does not actually matter if this is wrong.
|
1441 |
ariadna |
89 |
$fakequestion->contextid = $bankcontext->id;
|
1 |
efrain |
90 |
$fakequestion->createdby = 2;
|
|
|
91 |
$fakequestion->category = $category->id;
|
|
|
92 |
$fakequestion->questiontext = 'Test [[1]] question [[2]]';
|
|
|
93 |
$fakequestion->options = new \stdClass();
|
|
|
94 |
$fakequestion->options->answers = array();
|
|
|
95 |
$fakequestion->formoptions = new \stdClass();
|
|
|
96 |
$fakequestion->formoptions->movecontext = null;
|
|
|
97 |
$fakequestion->formoptions->repeatelements = true;
|
|
|
98 |
$fakequestion->inputs = null;
|
|
|
99 |
|
|
|
100 |
$form = new $classname(new \moodle_url('/'), $fakequestion, $category,
|
1441 |
ariadna |
101 |
new \core_question\local\bank\question_edit_contexts($bankcontext));
|
1 |
efrain |
102 |
|
|
|
103 |
return [$form, $category];
|
|
|
104 |
}
|
|
|
105 |
|
11 |
efrain |
106 |
public function test_get_illegal_tag_error(): void {
|
1 |
efrain |
107 |
list($form) = $this->get_form('\qtype_gapselect\form\qtype_gapselect_edit_form_base_testable');
|
|
|
108 |
|
|
|
109 |
$this->assertEquals('', $form->get_illegal_tag_error('frog'));
|
|
|
110 |
$this->assertEquals('', $form->get_illegal_tag_error('<i>toad</i>'));
|
|
|
111 |
|
|
|
112 |
$a = new \stdClass();
|
|
|
113 |
$a->tag = '<ijk>';
|
|
|
114 |
$a->allowed = '<sub>, <sup>, <b>, <i>, <em>, <strong>, <span>';
|
|
|
115 |
$this->assertEquals(get_string('tagsnotallowed', 'qtype_gapselect', $a), $form->get_illegal_tag_error('<ijk>'));
|
|
|
116 |
|
|
|
117 |
$a->tag = '</cat>';
|
|
|
118 |
$this->assertEquals(get_string('tagsnotallowed', 'qtype_gapselect', $a), $form->get_illegal_tag_error('</cat>'));
|
|
|
119 |
|
|
|
120 |
$a->tag = '<br />';
|
|
|
121 |
$this->assertEquals(get_string('tagsnotallowed', 'qtype_gapselect', $a), $form->get_illegal_tag_error('<i><br /></i>'));
|
|
|
122 |
|
|
|
123 |
$form->set_allowed_tags(array());
|
|
|
124 |
|
|
|
125 |
$this->assertEquals('', $form->get_illegal_tag_error('frog'));
|
|
|
126 |
|
|
|
127 |
$a->tag = '<i>';
|
|
|
128 |
$this->assertEquals(get_string('tagsnotallowedatall', 'qtype_gapselect', $a),
|
|
|
129 |
$form->get_illegal_tag_error('<i>toad</i>'));
|
|
|
130 |
|
|
|
131 |
$a->tag = '<ijk>';
|
|
|
132 |
$this->assertEquals(get_string('tagsnotallowedatall', 'qtype_gapselect', $a),
|
|
|
133 |
$form->get_illegal_tag_error('<ijk>'));
|
|
|
134 |
|
|
|
135 |
$a->tag = '</cat>';
|
|
|
136 |
$this->assertEquals(get_string('tagsnotallowedatall', 'qtype_gapselect', $a),
|
|
|
137 |
$form->get_illegal_tag_error('</cat>'));
|
|
|
138 |
|
|
|
139 |
$a->tag = '<i>';
|
|
|
140 |
$this->assertEquals(get_string('tagsnotallowedatall', 'qtype_gapselect', $a),
|
|
|
141 |
$form->get_illegal_tag_error('<i><br /></i>'));
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
/**
|
|
|
145 |
* Test the form shows the right number of groups of choices.
|
|
|
146 |
*/
|
11 |
efrain |
147 |
public function test_number_of_choice_groups(): void {
|
1 |
efrain |
148 |
list($form) = $this->get_form('qtype_gapselect_edit_form');
|
|
|
149 |
// Use reflection to get the protected property we need.
|
|
|
150 |
$property = new \ReflectionProperty('qtype_gapselect_edit_form', '_form');
|
|
|
151 |
$mform = $property->getValue($form);
|
|
|
152 |
$choices = $mform->getElement('choices[0]');
|
|
|
153 |
$groupoptions = $choices->_elements[1];
|
|
|
154 |
$this->assertCount(20, $groupoptions->_options);
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
/**
|
|
|
158 |
* Test the form correctly validates the HTML allowed in choices.
|
|
|
159 |
*/
|
11 |
efrain |
160 |
public function test_choices_validation(): void {
|
1 |
efrain |
161 |
list($form, $category) = $this->get_form('qtype_gapselect_edit_form');
|
|
|
162 |
|
|
|
163 |
$submitteddata = [
|
|
|
164 |
'category' => $category->id,
|
|
|
165 |
'questiontext' => ['text' => 'Test [[1]] question [[2]]', 'format' => FORMAT_HTML],
|
|
|
166 |
'choices' => [
|
|
|
167 |
['answer' => 'frog'],
|
|
|
168 |
['answer' => '<b>toad</b>'],
|
|
|
169 |
],
|
|
|
170 |
];
|
|
|
171 |
|
|
|
172 |
$errors = $form->validation($submitteddata, []);
|
|
|
173 |
|
|
|
174 |
$this->assertArrayNotHasKey('choices[0]', $errors);
|
|
|
175 |
$this->assertEquals('<b> is not allowed. (No HTML is allowed here.)',
|
|
|
176 |
$errors['choices[1]']);
|
|
|
177 |
}
|
|
|
178 |
}
|