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 select missing words question edit form.
  19   *
  20   * @package   qtype_gapselect
  21   * @copyright 2012 The Open University
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  global $CFG;
  28  
  29  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  30  require_once($CFG->dirroot . '/question/type/edit_question_form.php');
  31  require_once($CFG->dirroot . '/question/type/gapselect/edit_gapselect_form.php');
  32  
  33  
  34  /**
  35   * Subclass of qtype_gapselect_edit_form_base that is easier to used in unit tests.
  36   *
  37   * @copyright 2012 The Open University
  38   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class qtype_gapselect_edit_form_base_testable extends qtype_gapselect_edit_form_base {
  41      public function get_illegal_tag_error($text) {
  42          return parent::get_illegal_tag_error($text);
  43      }
  44  
  45      /**
  46       * Set the list of allowed tags.
  47       * @param array $allowed
  48       */
  49      public function set_allowed_tags(array $allowed) {
  50          $this->allowedhtmltags = $allowed;
  51      }
  52  }
  53  
  54  
  55  /**
  56   * Unit tests for select missing words question edit form.
  57   *
  58   * @copyright  2012 The Open University
  59   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  60   */
  61  class qtype_gapselect_edit_form_test extends advanced_testcase {
  62  
  63      /**
  64       * Helper method.
  65       *
  66       * @param string $classname the question form class to instantiate.
  67       *
  68       *
  69       * @return array with two elements:
  70       *      question_edit_form great a question form instance that can be tested.
  71       *      stdClass the question category.
  72       */
  73      protected function get_form($classname) {
  74          $this->setAdminUser();
  75          $this->resetAfterTest();
  76  
  77          $syscontext = context_system::instance();
  78          $category = question_make_default_categories(array($syscontext));
  79          $fakequestion = new stdClass();
  80          $fakequestion->qtype = 'gapselect'; // Does not actually matter if this is wrong.
  81          $fakequestion->contextid = $syscontext->id;
  82          $fakequestion->createdby = 2;
  83          $fakequestion->category = $category->id;
  84          $fakequestion->questiontext = 'Test [[1]] question [[2]]';
  85          $fakequestion->options = new stdClass();
  86          $fakequestion->options->answers = array();
  87          $fakequestion->formoptions = new stdClass();
  88          $fakequestion->formoptions->movecontext = null;
  89          $fakequestion->formoptions->repeatelements = true;
  90          $fakequestion->inputs = null;
  91  
  92          $form = new $classname(new moodle_url('/'), $fakequestion, $category,
  93                  new question_edit_contexts($syscontext));
  94  
  95          return [$form, $category];
  96      }
  97  
  98      public function test_get_illegal_tag_error() {
  99          list($form) = $this->get_form('qtype_gapselect_edit_form_base_testable');
 100  
 101          $this->assertEquals('', $form->get_illegal_tag_error('frog'));
 102          $this->assertEquals('', $form->get_illegal_tag_error('<i>toad</i>'));
 103  
 104          $a = new stdClass();
 105          $a->tag = '&lt;ijk&gt;';
 106          $a->allowed = '&lt;sub&gt;, &lt;sup&gt;, &lt;b&gt;, &lt;i&gt;, &lt;em&gt;, &lt;strong&gt;, &lt;span&gt;';
 107          $this->assertEquals(get_string('tagsnotallowed', 'qtype_gapselect', $a), $form->get_illegal_tag_error('<ijk>'));
 108  
 109          $a->tag = '&lt;/cat&gt;';
 110          $this->assertEquals(get_string('tagsnotallowed', 'qtype_gapselect', $a), $form->get_illegal_tag_error('</cat>'));
 111  
 112          $a->tag = '&lt;br /&gt;';
 113          $this->assertEquals(get_string('tagsnotallowed', 'qtype_gapselect', $a), $form->get_illegal_tag_error('<i><br /></i>'));
 114  
 115          $form->set_allowed_tags(array());
 116  
 117          $this->assertEquals('', $form->get_illegal_tag_error('frog'));
 118  
 119          $a->tag = '&lt;i&gt;';
 120          $this->assertEquals(get_string('tagsnotallowedatall', 'qtype_gapselect', $a),
 121                  $form->get_illegal_tag_error('<i>toad</i>'));
 122  
 123          $a->tag = '&lt;ijk&gt;';
 124          $this->assertEquals(get_string('tagsnotallowedatall', 'qtype_gapselect', $a),
 125                  $form->get_illegal_tag_error('<ijk>'));
 126  
 127          $a->tag = '&lt;/cat&gt;';
 128          $this->assertEquals(get_string('tagsnotallowedatall', 'qtype_gapselect', $a),
 129                  $form->get_illegal_tag_error('</cat>'));
 130  
 131          $a->tag = '&lt;i&gt;';
 132          $this->assertEquals(get_string('tagsnotallowedatall', 'qtype_gapselect', $a),
 133                  $form->get_illegal_tag_error('<i><br /></i>'));
 134      }
 135  
 136      /**
 137       * Test the form shows the right number of groups of choices.
 138       */
 139      public function test_number_of_choice_groups() {
 140          list($form) = $this->get_form('qtype_gapselect_edit_form');
 141          // Use reflection to get the protected property we need.
 142          $property = new ReflectionProperty('qtype_gapselect_edit_form', '_form');
 143          $property->setAccessible(true);
 144          $mform = $property->getValue($form);
 145          $choices = $mform->getElement('choices[0]');
 146          $groupoptions = $choices->_elements[1];
 147          $this->assertCount(20, $groupoptions->_options);
 148      }
 149  
 150      /**
 151       * Test the form correctly validates the HTML allowed in choices.
 152       */
 153      public function test_choices_validation() {
 154          list($form, $category) = $this->get_form('qtype_gapselect_edit_form');
 155  
 156          $submitteddata = [
 157                  'category' => $category->id,
 158                  'questiontext' => ['text' => 'Test [[1]] question [[2]]', 'format' => FORMAT_HTML],
 159                  'choices' => [
 160                          ['answer' => 'frog'],
 161                          ['answer' => '<b>toad</b>'],
 162                  ],
 163          ];
 164  
 165          $errors = $form->validation($submitteddata, []);
 166  
 167          $this->assertArrayNotHasKey('choices[0]', $errors);
 168          $this->assertEquals('&lt;b&gt; is not allowed. (No HTML is allowed here.)',
 169                  $errors['choices[1]']);
 170      }
 171  }