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_ddwtos\form;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  global $CFG;
  21  
  22  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  23  require_once($CFG->dirroot . '/question/type/edit_question_form.php');
  24  require_once($CFG->dirroot . '/question/type/ddwtos/edit_ddwtos_form.php');
  25  
  26  /**
  27   * Unit tests for the drag-and-drop words into sentences edit form.
  28   *
  29   * @package    qtype_ddwtos
  30   * @copyright  2012 The Open University
  31   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class edit_form_test extends \advanced_testcase {
  34      /**
  35       * Helper method.
  36       *
  37       * @param string $classname the question form class to instantiate.
  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($classname) {
  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 = 'ddwtos'; // Does not actually matter if this is wrong.
  51          $fakequestion->contextid = $syscontext->id;
  52          $fakequestion->createdby = 2;
  53          $fakequestion->category = $category->id;
  54          $fakequestion->questiontext = 'Test [[1]] question [[2]]';
  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 $classname(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 shows the right number of groups of choices.
  70       */
  71      public function test_number_of_choice_groups() {
  72          list($form) = $this->get_form('qtype_ddwtos_edit_form');
  73          // Use reflection to get the protected property we need.
  74          $property = new \ReflectionProperty('qtype_ddwtos_edit_form', '_form');
  75          $property->setAccessible(true);
  76          $mform = $property->getValue($form);
  77          $choices = $mform->getElement('choices[0]');
  78          $groupoptions = $choices->_elements[1];
  79          $this->assertCount(8, $groupoptions->_options);
  80      }
  81  
  82      /**
  83       * Test the form correctly validates the HTML allowed in choices.
  84       */
  85      public function test_choices_validation() {
  86          list($form, $category) = $this->get_form('qtype_ddwtos_edit_form');
  87  
  88          $submitteddata = [
  89                  'category' => $category->id,
  90                  'questiontext' => ['text' => 'Test [[1]] question [[2]]', 'format' => FORMAT_HTML],
  91                  'choices' => [
  92                          ['answer' => 'frog'],
  93                          ['answer' => '<b>toad</b>'],
  94                          ['answer' => '<span lang="fr"><em>chien</em></span>'],
  95                          ['answer' => '<textarea>evil!</textarea>'],
  96                  ],
  97          ];
  98  
  99          $errors = $form->validation($submitteddata, []);
 100  
 101          $this->assertArrayNotHasKey('choices[0]', $errors);
 102          $this->assertArrayNotHasKey('choices[1]', $errors);
 103          $this->assertArrayNotHasKey('choices[2]', $errors);
 104          $this->assertEquals('&lt;textarea&gt; is not allowed. ' .
 105                  '(Only &lt;sub&gt;, &lt;sup&gt;, &lt;b&gt;, &lt;i&gt;, ' .
 106                  '&lt;em&gt;, &lt;strong&gt;, &lt;span&gt; are permitted.)',
 107                  $errors['choices[3]']);
 108      }
 109  }