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_ddwtos\form;
|
|
|
18 |
|
|
|
19 |
defined('MOODLE_INTERNAL') || die();
|
|
|
20 |
global $CFG;
|
|
|
21 |
|
|
|
22 |
require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
|
|
|
23 |
require_once($CFG->dirroot . '/question/type/edit_question_form.php');
|
|
|
24 |
require_once($CFG->dirroot . '/question/type/ddwtos/edit_ddwtos_form.php');
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* Unit tests for the drag-and-drop words into sentences edit form.
|
|
|
28 |
*
|
|
|
29 |
* @package qtype_ddwtos
|
|
|
30 |
* @copyright 2012 The Open University
|
|
|
31 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
32 |
*/
|
1441 |
ariadna |
33 |
final class edit_form_test extends \advanced_testcase {
|
1 |
efrain |
34 |
/**
|
|
|
35 |
* Helper method.
|
|
|
36 |
*
|
|
|
37 |
* @param string $classname the question form class to instantiate.
|
|
|
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($classname) {
|
|
|
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 = 'ddwtos'; // Does not actually matter if this is wrong.
|
1441 |
ariadna |
53 |
$fakequestion->contextid = $bankcontext->id;
|
1 |
efrain |
54 |
$fakequestion->createdby = 2;
|
|
|
55 |
$fakequestion->category = $category->id;
|
|
|
56 |
$fakequestion->questiontext = 'Test [[1]] question [[2]]';
|
|
|
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 $classname(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 shows the right number of groups of choices.
|
|
|
72 |
*/
|
11 |
efrain |
73 |
public function test_number_of_choice_groups(): void {
|
1 |
efrain |
74 |
list($form) = $this->get_form('qtype_ddwtos_edit_form');
|
|
|
75 |
// Use reflection to get the protected property we need.
|
|
|
76 |
$property = new \ReflectionProperty('qtype_ddwtos_edit_form', '_form');
|
|
|
77 |
$mform = $property->getValue($form);
|
|
|
78 |
$choices = $mform->getElement('choices[0]');
|
|
|
79 |
$groupoptions = $choices->_elements[1];
|
|
|
80 |
$this->assertCount(8, $groupoptions->_options);
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* Test the form correctly validates the HTML allowed in choices.
|
|
|
85 |
*/
|
11 |
efrain |
86 |
public function test_choices_validation(): void {
|
1 |
efrain |
87 |
list($form, $category) = $this->get_form('qtype_ddwtos_edit_form');
|
|
|
88 |
|
|
|
89 |
$submitteddata = [
|
|
|
90 |
'category' => $category->id,
|
|
|
91 |
'questiontext' => ['text' => 'Test [[1]] question [[2]]', 'format' => FORMAT_HTML],
|
|
|
92 |
'choices' => [
|
|
|
93 |
['answer' => 'frog'],
|
|
|
94 |
['answer' => '<b>toad</b>'],
|
|
|
95 |
['answer' => '<span lang="fr"><em>chien</em></span>'],
|
|
|
96 |
['answer' => '<textarea>evil!</textarea>'],
|
|
|
97 |
],
|
|
|
98 |
];
|
|
|
99 |
|
|
|
100 |
$errors = $form->validation($submitteddata, []);
|
|
|
101 |
|
|
|
102 |
$this->assertArrayNotHasKey('choices[0]', $errors);
|
|
|
103 |
$this->assertArrayNotHasKey('choices[1]', $errors);
|
|
|
104 |
$this->assertArrayNotHasKey('choices[2]', $errors);
|
|
|
105 |
$this->assertEquals('<textarea> is not allowed. ' .
|
|
|
106 |
'(Only <sub>, <sup>, <b>, <i>, ' .
|
|
|
107 |
'<em>, <strong>, <span> are permitted.)',
|
|
|
108 |
$errors['choices[3]']);
|
|
|
109 |
}
|
|
|
110 |
}
|