Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   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  /**
  18   * Unit tests for the drag-and-drop words into sentences edit form.
  19   *
  20   * @package   qtype_ddwtos
  21   * @copyright 2018 The Open University
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  global $CFG;
  27  
  28  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  29  require_once($CFG->dirroot . '/question/type/edit_question_form.php');
  30  require_once($CFG->dirroot . '/question/type/ddwtos/edit_ddwtos_form.php');
  31  
  32  /**
  33   * Unit tests for the drag-and-drop words into sentences edit form.
  34   *
  35   * @copyright  2012 The Open University
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class qtype_ddwtos_edit_form_test extends advanced_testcase {
  39      /**
  40       * Helper method.
  41       *
  42       * @param string $classname the question form class to instantiate.
  43       *
  44       * @return array with two elements:
  45       *      question_edit_form great a question form instance that can be tested.
  46       *      stdClass the question category.
  47       */
  48      protected function get_form($classname) {
  49          $this->setAdminUser();
  50          $this->resetAfterTest();
  51  
  52          $syscontext = context_system::instance();
  53          $category = question_make_default_categories(array($syscontext));
  54          $fakequestion = new stdClass();
  55          $fakequestion->qtype = 'ddwtos'; // Does not actually matter if this is wrong.
  56          $fakequestion->contextid = $syscontext->id;
  57          $fakequestion->createdby = 2;
  58          $fakequestion->category = $category->id;
  59          $fakequestion->questiontext = 'Test [[1]] question [[2]]';
  60          $fakequestion->options = new stdClass();
  61          $fakequestion->options->answers = array();
  62          $fakequestion->formoptions = new stdClass();
  63          $fakequestion->formoptions->movecontext = null;
  64          $fakequestion->formoptions->repeatelements = true;
  65          $fakequestion->inputs = null;
  66  
  67          $form = new $classname(new moodle_url('/'), $fakequestion, $category,
  68                  new question_edit_contexts($syscontext));
  69  
  70          return [$form, $category];
  71      }
  72  
  73      /**
  74       * Test the form shows the right number of groups of choices.
  75       */
  76      public function test_number_of_choice_groups() {
  77          list($form) = $this->get_form('qtype_ddwtos_edit_form');
  78          // Use reflection to get the protected property we need.
  79          $property = new ReflectionProperty('qtype_ddwtos_edit_form', '_form');
  80          $property->setAccessible(true);
  81          $mform = $property->getValue($form);
  82          $choices = $mform->getElement('choices[0]');
  83          $groupoptions = $choices->_elements[1];
  84          $this->assertCount(8, $groupoptions->_options);
  85      }
  86  
  87      /**
  88       * Test the form correctly validates the HTML allowed in choices.
  89       */
  90      public function test_choices_validation() {
  91          list($form, $category) = $this->get_form('qtype_ddwtos_edit_form');
  92  
  93          $submitteddata = [
  94                  'category' => $category->id,
  95                  'questiontext' => ['text' => 'Test [[1]] question [[2]]', 'format' => FORMAT_HTML],
  96                  'choices' => [
  97                          ['answer' => 'frog'],
  98                          ['answer' => '<b>toad</b>'],
  99                          ['answer' => '<span lang="fr"><em>chien</em></span>'],
 100                          ['answer' => '<textarea>evil!</textarea>'],
 101                  ],
 102          ];
 103  
 104          $errors = $form->validation($submitteddata, []);
 105  
 106          $this->assertArrayNotHasKey('choices[0]', $errors);
 107          $this->assertArrayNotHasKey('choices[1]', $errors);
 108          $this->assertArrayNotHasKey('choices[2]', $errors);
 109          $this->assertEquals('&lt;textarea&gt; is not allowed. ' .
 110                  '(Only &lt;sub&gt;, &lt;sup&gt;, &lt;b&gt;, &lt;i&gt;, ' .
 111                  '&lt;em&gt;, &lt;strong&gt;, &lt;span&gt; are permitted.)',
 112                  $errors['choices[3]']);
 113      }
 114  }