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