Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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_ddmarker\form;
  18  
  19  use qtype_ddmarker_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/ddmarker/edit_ddmarker_form.php');
  28  
  29  /**
  30   * Unit tests for the drag-and-drop markers edit form.
  31   *
  32   * @package    qtype_ddmarker
  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 = 'ddmarker';
  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_ddmarker_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' => 0,
  78              'nodropzone' => 0,
  79              'noitems' => 4,
  80              'drags' => [
  81                  ['label' => 'frog'],
  82                  ['label' => '<b>toad</b>'],
  83                  ['label' => '<span lang="fr"><em>chien</em></span>'],
  84                  ['label' => '<textarea>evil!</textarea>'],
  85              ],
  86          ];
  87  
  88          $errors = $form->validation($submitteddata, []);
  89  
  90          $this->assertArrayNotHasKey('drags[0]', $errors);
  91          $this->assertArrayNotHasKey('drags[1]', $errors);
  92          $this->assertArrayNotHasKey('drags[2]', $errors);
  93          $this->assertEquals('Only "&lt;br&gt;&lt;i&gt;&lt;em&gt;&lt;b&gt;&lt;strong&gt;' .
  94                  '&lt;sup&gt;&lt;sub&gt;&lt;u&gt;&lt;span&gt;" tags are allowed in the label for a marker.',
  95                  $errors['drags[3]']);
  96      }
  97  }