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_ddwtos\form;
  18  
  19  use question_edit_contexts;
  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/ddwtos/edit_ddwtos_form.php');
  27  
  28  /**
  29   * Unit tests for the drag-and-drop words into sentences edit form.
  30   *
  31   * @package    qtype_ddwtos
  32   * @copyright  2012 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       * @param string $classname the question form class to instantiate.
  40       *
  41       * @return array with two elements:
  42       *      question_edit_form great a question form instance that can be tested.
  43       *      stdClass the question category.
  44       */
  45      protected function get_form($classname) {
  46          $this->setAdminUser();
  47          $this->resetAfterTest();
  48  
  49          $syscontext = \context_system::instance();
  50          $category = question_make_default_categories(array($syscontext));
  51          $fakequestion = new \stdClass();
  52          $fakequestion->qtype = 'ddwtos'; // Does not actually matter if this is wrong.
  53          $fakequestion->contextid = $syscontext->id;
  54          $fakequestion->createdby = 2;
  55          $fakequestion->category = $category->id;
  56          $fakequestion->questiontext = 'Test [[1]] question [[2]]';
  57          $fakequestion->options = new \stdClass();
  58          $fakequestion->options->answers = array();
  59          $fakequestion->formoptions = new \stdClass();
  60          $fakequestion->formoptions->movecontext = null;
  61          $fakequestion->formoptions->repeatelements = true;
  62          $fakequestion->inputs = null;
  63  
  64          $form = new $classname(new \moodle_url('/'), $fakequestion, $category,
  65                  new question_edit_contexts($syscontext));
  66  
  67          return [$form, $category];
  68      }
  69  
  70      /**
  71       * Test the form shows the right number of groups of choices.
  72       */
  73      public function test_number_of_choice_groups() {
  74          list($form) = $this->get_form('qtype_ddwtos_edit_form');
  75          // Use reflection to get the protected property we need.
  76          $property = new \ReflectionProperty('qtype_ddwtos_edit_form', '_form');
  77          $property->setAccessible(true);
  78          $mform = $property->getValue($form);
  79          $choices = $mform->getElement('choices[0]');
  80          $groupoptions = $choices->_elements[1];
  81          $this->assertCount(8, $groupoptions->_options);
  82      }
  83  
  84      /**
  85       * Test the form correctly validates the HTML allowed in choices.
  86       */
  87      public function test_choices_validation() {
  88          list($form, $category) = $this->get_form('qtype_ddwtos_edit_form');
  89  
  90          $submitteddata = [
  91                  'category' => $category->id,
  92                  'questiontext' => ['text' => 'Test [[1]] question [[2]]', 'format' => FORMAT_HTML],
  93                  'choices' => [
  94                          ['answer' => 'frog'],
  95                          ['answer' => '<b>toad</b>'],
  96                          ['answer' => '<span lang="fr"><em>chien</em></span>'],
  97                          ['answer' => '<textarea>evil!</textarea>'],
  98                  ],
  99          ];
 100  
 101          $errors = $form->validation($submitteddata, []);
 102  
 103          $this->assertArrayNotHasKey('choices[0]', $errors);
 104          $this->assertArrayNotHasKey('choices[1]', $errors);
 105          $this->assertArrayNotHasKey('choices[2]', $errors);
 106          $this->assertEquals('&lt;textarea&gt; is not allowed. ' .
 107                  '(Only &lt;sub&gt;, &lt;sup&gt;, &lt;b&gt;, &lt;i&gt;, ' .
 108                  '&lt;em&gt;, &lt;strong&gt;, &lt;span&gt; are permitted.)',
 109                  $errors['choices[3]']);
 110      }
 111  }