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