Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 311 and 401]

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