MDL-47494 gapselect: Bump the version number for the 1.6 release.
[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/**
18 * Unit tests for the select missing words question definition class.
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');
31require_once($CFG->dirroot . '/question/type/gapselect/edit_form_base.php');
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
39 * @group qtype_gapselect
8d6fb0c6
TH
40 */
41class qtype_gapselect_edit_form_base_testable extends qtype_gapselect_edit_form_base {
42 public function __construct() {
43 $syscontext = context_system::instance();
44 $category = question_make_default_categories(array($syscontext));
45 $fakequestion = new stdClass();
46 $fakequestion->qtype = 'stack';
47 $fakequestion->category = $category->id;
48 $fakequestion->questiontext = 'Test [[1]] question [[2]]';
49 $fakequestion->options = new stdClass();
50 $fakequestion->options->answers = array();
51 $fakequestion->formoptions = new stdClass();
52 $fakequestion->formoptions->movecontext = null;
53 $fakequestion->formoptions->repeatelements = true;
54 $fakequestion->inputs = null;
55 parent::__construct(new moodle_url('/'), $fakequestion, $category,
56 new question_edit_contexts($syscontext));
57 }
58
59 // Make this public so we can test it.
60 public function get_illegal_tag_error($text) {
61 return parent::get_illegal_tag_error($text);
62 }
63
64 /**
65 * Set the list of allowed tags.
66 * @param array $allowed
67 */
68 public function set_allowed_tags(array $allowed) {
69 $this->allowedhtmltags = $allowed;
70 }
71}
72
73
74/**
75 * Unit tests for Stack question editing form.
76 *
77 * @copyright 2012 The Open University
78 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
79 */
80class qtype_gapselect_edit_form_test extends advanced_testcase {
81
82 protected function get_form() {
83 $this->setAdminUser();
84 $this->resetAfterTest();
85
86 return new qtype_gapselect_edit_form_base_testable();
87 }
88
89 public function test_get_illegal_tag_error() {
90 $form = $this->get_form();
91
92 $this->assertEquals('', $form->get_illegal_tag_error('frog'));
93 $this->assertEquals('', $form->get_illegal_tag_error('<i>toad</i>'));
94
95 $a = new stdClass();
96 $a->tag = '&lt;ijk&gt;';
97 $a->allowed = '&lt;sub&gt;, &lt;sup&gt;, &lt;b&gt;, &lt;i&gt;, &lt;em&gt;, &lt;strong&gt;';
98 $this->assertEquals(get_string('tagsnotallowed', 'qtype_gapselect', $a), $form->get_illegal_tag_error('<ijk>'));
99
100 $a->tag = '&lt;/cat&gt;';
101 $this->assertEquals(get_string('tagsnotallowed', 'qtype_gapselect', $a), $form->get_illegal_tag_error('</cat>'));
102
103 $a->tag = '&lt;br /&gt;';
104 $this->assertEquals(get_string('tagsnotallowed', 'qtype_gapselect', $a), $form->get_illegal_tag_error('<i><br /></i>'));
105
106 $form->set_allowed_tags(array());
107
108 $this->assertEquals('', $form->get_illegal_tag_error('frog'));
109
110 $a->tag = '&lt;i&gt;';
111 $this->assertEquals(get_string('tagsnotallowedatall', 'qtype_gapselect', $a),
112 $form->get_illegal_tag_error('<i>toad</i>'));
113
114 $a->tag = '&lt;ijk&gt;';
115 $this->assertEquals(get_string('tagsnotallowedatall', 'qtype_gapselect', $a),
116 $form->get_illegal_tag_error('<ijk>'));
117
118 $a->tag = '&lt;/cat&gt;';
119 $this->assertEquals(get_string('tagsnotallowedatall', 'qtype_gapselect', $a),
120 $form->get_illegal_tag_error('</cat>'));
121
122 $a->tag = '&lt;i&gt;';
123 $this->assertEquals(get_string('tagsnotallowedatall', 'qtype_gapselect', $a),
124 $form->get_illegal_tag_error('<i><br /></i>'));
125 }
126}