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