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