weekly release 3.6dev
[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
8d6fb0c6
TH
39 */
40class qtype_gapselect_edit_form_base_testable extends qtype_gapselect_edit_form_base {
41 public function __construct() {
42 $syscontext = context_system::instance();
43 $category = question_make_default_categories(array($syscontext));
44 $fakequestion = new stdClass();
45 $fakequestion->qtype = 'stack';
2cf7bde8
SL
46 $fakequestion->contextid = $syscontext->id;
47 $fakequestion->createdby = 2;
8d6fb0c6
TH
48 $fakequestion->category = $category->id;
49 $fakequestion->questiontext = 'Test [[1]] question [[2]]';
50 $fakequestion->options = new stdClass();
51 $fakequestion->options->answers = array();
52 $fakequestion->formoptions = new stdClass();
53 $fakequestion->formoptions->movecontext = null;
54 $fakequestion->formoptions->repeatelements = true;
55 $fakequestion->inputs = null;
56 parent::__construct(new moodle_url('/'), $fakequestion, $category,
57 new question_edit_contexts($syscontext));
58 }
59
8d6fb0c6
TH
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
647d9373
TH
82 /**
83 * Helper method.
84 * @return qtype_gapselect_edit_form_base_testable a new form instance that can be tested.
85 */
8d6fb0c6
TH
86 protected function get_form() {
87 $this->setAdminUser();
88 $this->resetAfterTest();
89
90 return new qtype_gapselect_edit_form_base_testable();
91 }
92
93 public function test_get_illegal_tag_error() {
94 $form = $this->get_form();
95
96 $this->assertEquals('', $form->get_illegal_tag_error('frog'));
97 $this->assertEquals('', $form->get_illegal_tag_error('<i>toad</i>'));
98
99 $a = new stdClass();
100 $a->tag = '&lt;ijk&gt;';
101 $a->allowed = '&lt;sub&gt;, &lt;sup&gt;, &lt;b&gt;, &lt;i&gt;, &lt;em&gt;, &lt;strong&gt;';
102 $this->assertEquals(get_string('tagsnotallowed', 'qtype_gapselect', $a), $form->get_illegal_tag_error('<ijk>'));
103
104 $a->tag = '&lt;/cat&gt;';
105 $this->assertEquals(get_string('tagsnotallowed', 'qtype_gapselect', $a), $form->get_illegal_tag_error('</cat>'));
106
107 $a->tag = '&lt;br /&gt;';
108 $this->assertEquals(get_string('tagsnotallowed', 'qtype_gapselect', $a), $form->get_illegal_tag_error('<i><br /></i>'));
109
110 $form->set_allowed_tags(array());
111
112 $this->assertEquals('', $form->get_illegal_tag_error('frog'));
113
114 $a->tag = '&lt;i&gt;';
115 $this->assertEquals(get_string('tagsnotallowedatall', 'qtype_gapselect', $a),
116 $form->get_illegal_tag_error('<i>toad</i>'));
117
118 $a->tag = '&lt;ijk&gt;';
119 $this->assertEquals(get_string('tagsnotallowedatall', 'qtype_gapselect', $a),
120 $form->get_illegal_tag_error('<ijk>'));
121
122 $a->tag = '&lt;/cat&gt;';
123 $this->assertEquals(get_string('tagsnotallowedatall', 'qtype_gapselect', $a),
124 $form->get_illegal_tag_error('</cat>'));
125
126 $a->tag = '&lt;i&gt;';
127 $this->assertEquals(get_string('tagsnotallowedatall', 'qtype_gapselect', $a),
128 $form->get_illegal_tag_error('<i><br /></i>'));
129 }
130}