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_essay\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/essay/edit_essay_form.php');
  27  
  28  /**
  29   * Unit tests for the essay edit form.
  30   *
  31   * @package   qtype_essay
  32   * @copyright  2021 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          global $USER;
  47          $this->setAdminUser();
  48          $this->resetAfterTest();
  49  
  50          $syscontext = \context_system::instance();
  51          $category = question_make_default_categories(array($syscontext));
  52          $fakequestion = new \stdClass();
  53          $fakequestion->qtype = 'essay';
  54          $fakequestion->contextid = $syscontext->id;
  55          $fakequestion->createdby = $USER->id;
  56          $fakequestion->category = $category->id;
  57          $fakequestion->questiontext = 'please writer an assay about ...';
  58          $fakequestion->responseformat = 'editorfilepicker';
  59          $fakequestion->responserequired = 1;
  60          $fakequestion->responsefieldlines = 10;
  61          $fakequestion->attachments = -1;
  62          $fakequestion->attachmentsrequired = 3;
  63          $fakequestion->filetypeslist = '';
  64  
  65          $form = new $classname(
  66              new \moodle_url('/'),
  67              $fakequestion,
  68              $category,
  69              new question_edit_contexts($syscontext)
  70          );
  71  
  72          return [$form, $category];
  73      }
  74  
  75      /**
  76       * Test the form for correct validation of attachments options.
  77       *
  78       * @dataProvider user_preference_provider
  79       * @param int $allowed
  80       * @param int $required
  81       * @param array $expected
  82       */
  83      public function test_attachments_validation(int $allowed, int $required, array $expected): void {
  84          list($form, $category) = $this->get_form('qtype_essay_edit_form');
  85          $submitteddata = [
  86              'category' => $category->id,
  87              'questiontext' => [
  88                  'text' => 'please writer an assay about ...',
  89                  'format' => FORMAT_HTML,
  90              ],
  91              'responseformat' => 'editorfilepicker',
  92              'responserequired' => '1',
  93              'attachments' => $allowed,
  94              'attachmentsrequired' => $required,
  95          ];
  96          $errors = $form->validation($submitteddata, []);
  97          $this->assertArrayNotHasKey('attachments', $errors);
  98          $this->assertEquals($expected, $errors);
  99      }
 100  
 101      /**
 102       * Return an array of all possible allowed and required attachments,
 103       * and the expected results from the form validation method.
 104       *
 105       * @return array, an array of all possible options.
 106       */
 107      public function user_preference_provider(): array {
 108          $valid = [];
 109          $invalid = ['attachmentsrequired' => get_string('mustrequirefewer', 'qtype_essay')];
 110          return [
 111              'Attachments allowed=0, required=0, valid' => [0, 0, $valid],
 112              'Attachments allowed=0, required=1, invalid, so required is set to 0 when saving' => [0, 1, $valid],
 113              'Attachments allowed=0, required=2, invalid, so required is set to 0 when saving' => [0, 2, $valid],
 114              'Attachments allowed=0, required=3, invalid, so required is set to 0 when saving' => [0, 3, $valid],
 115  
 116              'Attachments allowed=1, required=0, valid' => [1, 0, $valid],
 117              'Attachments allowed=1, required=1, valid' => [1, 1, $valid],
 118              'Attachments allowed=1, required=2, invalid' => [1, 2, $invalid],
 119  
 120              'Attachments allowed=2, required=3, invalid' => [2, 3, $invalid],
 121  
 122              'Attachments allowed=3, required=4, invalid' => [3, 4, $invalid],
 123  
 124              'Attachments allowed=-1, required=4, valid' => [-1, 4, $valid],
 125          ];
 126      }
 127  }