on-demand release 4.0dev+
[moodle.git] / question / type / ddimageortext / tests / edit_form_test.php
CommitLineData
096a5b99
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 drag-and-drop onto image edit form.
19 *
20 * @package qtype_ddimageortext
21 * @copyright 2019 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26global $CFG;
27
28require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
29require_once($CFG->dirroot . '/question/type/edit_question_form.php');
30require_once($CFG->dirroot . '/question/type/ddimageortext/edit_ddimageortext_form.php');
31
32/**
33 * Unit tests for the drag-and-drop onto image edit form.
34 *
35 * @copyright 2019 The Open University
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38class qtype_ddimageortext_edit_form_test extends advanced_testcase {
39 /**
40 * Helper method.
41 *
42 * @return array with two elements:
43 * question_edit_form great a question form instance that can be tested.
44 * stdClass the question category.
45 */
46 protected function get_form() {
47 $this->setAdminUser();
48 $this->resetAfterTest();
49
50 $syscontext = context_system::instance();
51 $category = question_make_default_categories(array($syscontext));
52 $fakequestion = new stdClass();
53 $fakequestion->qtype = 'ddimageortext';
54 $fakequestion->contextid = $syscontext->id;
55 $fakequestion->createdby = 2;
56 $fakequestion->category = $category->id;
57 $fakequestion->questiontext = 'Test question';
58 $fakequestion->options = new stdClass();
59 $fakequestion->options->answers = array();
60 $fakequestion->formoptions = new stdClass();
61 $fakequestion->formoptions->movecontext = null;
62 $fakequestion->formoptions->repeatelements = true;
63 $fakequestion->inputs = null;
64
65 $form = new qtype_ddimageortext_edit_form(new moodle_url('/'), $fakequestion, $category,
66 new question_edit_contexts($syscontext));
67
68 return [$form, $category];
69 }
70
71 /**
72 * Test the form correctly validates the HTML allowed in items.
73 */
74 public function test_item_validation() {
75 list($form, $category) = $this->get_form();
76
77 $submitteddata = [
78 'category' => $category->id,
79 'bgimage' => '',
80 'nodropzone' => 0,
81 'noitems' => 5,
82 'drags' => [
83 ['dragitemtype' => 'image'],
84 ['dragitemtype' => 'image'],
85 ['dragitemtype' => 'word'],
86 ['dragitemtype' => 'word'],
87 ['dragitemtype' => 'word'],
88 ],
89 'draglabel' => [
90 'frog',
91 '<b>toad</b>',
92 'cat',
93 '<span lang="fr"><b>chien</b></span>',
94 '<textarea>evil!</textarea>',
95 ],
96 ];
97
98 $errors = $form->validation($submitteddata, []);
99
100 $this->assertArrayNotHasKey('drags[0]', $errors);
101 $this->assertEquals('HTML tags are not allowed in this text which is the alt text for a draggable image.',
102 $errors['drags[1]']);
103 $this->assertArrayNotHasKey('drags[2]', $errors);
104 $this->assertArrayNotHasKey('drags[3]', $errors);
105 $this->assertEquals('Only "&lt;br&gt;&lt;sub&gt;&lt;sup&gt;&lt;b&gt;&lt;i&gt;&lt;strong&gt;&lt;em&gt;&lt;span&gt;" ' .
106 'tags are allowed in this draggable text.', $errors['drags[4]']);
107 }
108}