weekly release 3.7dev
[moodle.git] / question / type / gapselect / tests / edit_form_test.php
CommitLineData
8d6fb0c6
TH
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/**
4889d6ac 18 * Unit tests for the select missing words question edit form.
8d6fb0c6 19 *
bbe27b2d
TH
20 * @package qtype_gapselect
21 * @copyright 2012 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
8d6fb0c6
TH
23 */
24
25
26defined('MOODLE_INTERNAL') || die();
27global $CFG;
28
29require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
30require_once($CFG->dirroot . '/question/type/edit_question_form.php');
4889d6ac 31require_once($CFG->dirroot . '/question/type/gapselect/edit_gapselect_form.php');
8d6fb0c6
TH
32
33
34/**
35 * Subclass of qtype_gapselect_edit_form_base that is easier to used in unit tests.
36 *
bbe27b2d
TH
37 * @copyright 2012 The Open University
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
8d6fb0c6
TH
39 */
40class qtype_gapselect_edit_form_base_testable extends qtype_gapselect_edit_form_base {
8d6fb0c6
TH
41 public function get_illegal_tag_error($text) {
42 return parent::get_illegal_tag_error($text);
43 }
44
45 /**
46 * Set the list of allowed tags.
47 * @param array $allowed
48 */
49 public function set_allowed_tags(array $allowed) {
50 $this->allowedhtmltags = $allowed;
51 }
52}
53
54
55/**
4889d6ac 56 * Unit tests for select missing words question edit form.
8d6fb0c6
TH
57 *
58 * @copyright 2012 The Open University
59 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
60 */
61class qtype_gapselect_edit_form_test extends advanced_testcase {
62
647d9373
TH
63 /**
64 * Helper method.
4889d6ac
HN
65 *
66 * @param string $classname the question form class to instantiate.
67 *
68 * @return question_edit_form great a question form instance that can be tested.
647d9373 69 */
4889d6ac 70 protected function get_form($classname) {
8d6fb0c6
TH
71 $this->setAdminUser();
72 $this->resetAfterTest();
73
4889d6ac
HN
74 $syscontext = context_system::instance();
75 $category = question_make_default_categories(array($syscontext));
76 $fakequestion = new stdClass();
77 $fakequestion->qtype = 'stack';
78 $fakequestion->contextid = $syscontext->id;
79 $fakequestion->createdby = 2;
80 $fakequestion->category = $category->id;
81 $fakequestion->questiontext = 'Test [[1]] question [[2]]';
82 $fakequestion->options = new stdClass();
83 $fakequestion->options->answers = array();
84 $fakequestion->formoptions = new stdClass();
85 $fakequestion->formoptions->movecontext = null;
86 $fakequestion->formoptions->repeatelements = true;
87 $fakequestion->inputs = null;
88 return new $classname(new moodle_url('/'), $fakequestion, $category,
89 new question_edit_contexts($syscontext));
8d6fb0c6
TH
90 }
91
92 public function test_get_illegal_tag_error() {
4889d6ac 93 $form = $this->get_form('qtype_gapselect_edit_form_base_testable');
8d6fb0c6
TH
94
95 $this->assertEquals('', $form->get_illegal_tag_error('frog'));
96 $this->assertEquals('', $form->get_illegal_tag_error('<i>toad</i>'));
97
98 $a = new stdClass();
99 $a->tag = '&lt;ijk&gt;';
100 $a->allowed = '&lt;sub&gt;, &lt;sup&gt;, &lt;b&gt;, &lt;i&gt;, &lt;em&gt;, &lt;strong&gt;';
101 $this->assertEquals(get_string('tagsnotallowed', 'qtype_gapselect', $a), $form->get_illegal_tag_error('<ijk>'));
102
103 $a->tag = '&lt;/cat&gt;';
104 $this->assertEquals(get_string('tagsnotallowed', 'qtype_gapselect', $a), $form->get_illegal_tag_error('</cat>'));
105
106 $a->tag = '&lt;br /&gt;';
107 $this->assertEquals(get_string('tagsnotallowed', 'qtype_gapselect', $a), $form->get_illegal_tag_error('<i><br /></i>'));
108
109 $form->set_allowed_tags(array());
110
111 $this->assertEquals('', $form->get_illegal_tag_error('frog'));
112
113 $a->tag = '&lt;i&gt;';
114 $this->assertEquals(get_string('tagsnotallowedatall', 'qtype_gapselect', $a),
115 $form->get_illegal_tag_error('<i>toad</i>'));
116
117 $a->tag = '&lt;ijk&gt;';
118 $this->assertEquals(get_string('tagsnotallowedatall', 'qtype_gapselect', $a),
119 $form->get_illegal_tag_error('<ijk>'));
120
121 $a->tag = '&lt;/cat&gt;';
122 $this->assertEquals(get_string('tagsnotallowedatall', 'qtype_gapselect', $a),
123 $form->get_illegal_tag_error('</cat>'));
124
125 $a->tag = '&lt;i&gt;';
126 $this->assertEquals(get_string('tagsnotallowedatall', 'qtype_gapselect', $a),
127 $form->get_illegal_tag_error('<i><br /></i>'));
128 }
4889d6ac
HN
129
130 /**
131 * Test the form shows the right number of groups of choices.
132 */
133 public function test_number_of_choice_groups() {
134 $form = $this->get_form('qtype_gapselect_edit_form');
135 // Use reflection to get the protected property we need.
136 $property = new ReflectionProperty('qtype_gapselect_edit_form', '_form');
137 $property->setAccessible(true);
138 $mform = $property->getValue($form);
139 $choices = $mform->getElement('choices[0]');
140 $groupoptions = $choices->_elements[1];
141 $this->assertCount(20, $groupoptions->_options);
142 }
8d6fb0c6 143}