1 |
efrain |
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 customfield_textarea;
|
|
|
18 |
|
|
|
19 |
use core_customfield_generator;
|
|
|
20 |
use core_customfield_test_instance_form;
|
|
|
21 |
use context_user;
|
|
|
22 |
use context_course;
|
|
|
23 |
use context_system;
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Functional test for customfield_textarea
|
|
|
27 |
*
|
|
|
28 |
* @package customfield_textarea
|
|
|
29 |
* @copyright 2019 Marina Glancy
|
|
|
30 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
31 |
* @covers \customfield_textarea\field_controller
|
|
|
32 |
* @covers \customfield_textarea\data_controller
|
|
|
33 |
*/
|
|
|
34 |
class plugin_test extends \advanced_testcase {
|
|
|
35 |
|
|
|
36 |
/** @var \stdClass[] */
|
|
|
37 |
private $courses = [];
|
|
|
38 |
/** @var \core_customfield\category_controller */
|
|
|
39 |
private $cfcat;
|
|
|
40 |
/** @var \core_customfield\field_controller[] */
|
|
|
41 |
private $cfields;
|
|
|
42 |
/** @var \core_customfield\data_controller[] */
|
|
|
43 |
private $cfdata;
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Tests set up.
|
|
|
47 |
*/
|
|
|
48 |
public function setUp(): void {
|
|
|
49 |
$this->resetAfterTest();
|
|
|
50 |
|
|
|
51 |
$this->cfcat = $this->get_generator()->create_category();
|
|
|
52 |
|
|
|
53 |
$this->cfields[1] = $this->get_generator()->create_field(
|
|
|
54 |
['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield1', 'type' => 'textarea']);
|
|
|
55 |
$this->cfields[2] = $this->get_generator()->create_field(
|
|
|
56 |
['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield2', 'type' => 'textarea',
|
|
|
57 |
'configdata' => ['required' => 1]]);
|
|
|
58 |
$this->cfields[3] = $this->get_generator()->create_field(
|
|
|
59 |
['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield3', 'type' => 'textarea',
|
|
|
60 |
'configdata' => ['defaultvalue' => 'Value3', 'defaultvalueformat' => FORMAT_MOODLE]]);
|
|
|
61 |
|
|
|
62 |
$this->courses[1] = $this->getDataGenerator()->create_course();
|
|
|
63 |
$this->courses[2] = $this->getDataGenerator()->create_course();
|
|
|
64 |
$this->courses[3] = $this->getDataGenerator()->create_course();
|
|
|
65 |
|
|
|
66 |
$this->cfdata[1] = $this->get_generator()->add_instance_data($this->cfields[1], $this->courses[1]->id,
|
|
|
67 |
['text' => 'Value1', 'format' => FORMAT_MOODLE]);
|
|
|
68 |
$this->cfdata[2] = $this->get_generator()->add_instance_data($this->cfields[1], $this->courses[2]->id,
|
|
|
69 |
['text' => '<br />', 'format' => FORMAT_MOODLE]);
|
|
|
70 |
|
|
|
71 |
$this->setUser($this->getDataGenerator()->create_user());
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
* Get generator
|
|
|
76 |
* @return core_customfield_generator
|
|
|
77 |
*/
|
|
|
78 |
protected function get_generator(): core_customfield_generator {
|
|
|
79 |
return $this->getDataGenerator()->get_plugin_generator('core_customfield');
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* Test for initialising field and data controllers
|
|
|
84 |
*/
|
|
|
85 |
public function test_initialise() {
|
|
|
86 |
$f = \core_customfield\field_controller::create($this->cfields[1]->get('id'));
|
|
|
87 |
$this->assertTrue($f instanceof field_controller);
|
|
|
88 |
|
|
|
89 |
$f = \core_customfield\field_controller::create(0, (object)['type' => 'textarea'], $this->cfcat);
|
|
|
90 |
$this->assertTrue($f instanceof field_controller);
|
|
|
91 |
|
|
|
92 |
$d = \core_customfield\data_controller::create($this->cfdata[1]->get('id'));
|
|
|
93 |
$this->assertTrue($d instanceof data_controller);
|
|
|
94 |
|
|
|
95 |
$d = \core_customfield\data_controller::create(0, null, $this->cfields[1]);
|
|
|
96 |
$this->assertTrue($d instanceof data_controller);
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* Test for configuration form functions
|
|
|
101 |
*
|
|
|
102 |
* Create a configuration form and submit it with the same values as in the field
|
|
|
103 |
*/
|
|
|
104 |
public function test_config_form() {
|
|
|
105 |
$this->setAdminUser();
|
|
|
106 |
$submitdata = (array)$this->cfields[3]->to_record();
|
|
|
107 |
$submitdata['configdata'] = $this->cfields[3]->get('configdata');
|
|
|
108 |
|
|
|
109 |
$submitdata = \core_customfield\field_config_form::mock_ajax_submit($submitdata);
|
|
|
110 |
$form = new \core_customfield\field_config_form(null, null, 'post', '', null, true,
|
|
|
111 |
$submitdata, true);
|
|
|
112 |
$form->set_data_for_dynamic_submission();
|
|
|
113 |
$this->assertTrue($form->is_validated());
|
|
|
114 |
$form->process_dynamic_submission();
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
/**
|
|
|
118 |
* Test for instance form functions
|
|
|
119 |
*/
|
|
|
120 |
public function test_instance_form() {
|
|
|
121 |
global $CFG;
|
|
|
122 |
require_once($CFG->dirroot . '/customfield/tests/fixtures/test_instance_form.php');
|
|
|
123 |
$this->setAdminUser();
|
|
|
124 |
$handler = $this->cfcat->get_handler();
|
|
|
125 |
|
|
|
126 |
// First try to submit without required field.
|
|
|
127 |
$submitdata = (array)$this->courses[1];
|
|
|
128 |
core_customfield_test_instance_form::mock_submit($submitdata, []);
|
|
|
129 |
$form = new core_customfield_test_instance_form('POST',
|
|
|
130 |
['handler' => $handler, 'instance' => $this->courses[1]]);
|
|
|
131 |
$this->assertFalse($form->is_validated());
|
|
|
132 |
|
|
|
133 |
// Now with required field.
|
|
|
134 |
$submitdata['customfield_myfield2_editor'] = ['text' => 'Some text', 'format' => FORMAT_HTML];
|
|
|
135 |
core_customfield_test_instance_form::mock_submit($submitdata, []);
|
|
|
136 |
$form = new core_customfield_test_instance_form('POST',
|
|
|
137 |
['handler' => $handler, 'instance' => $this->courses[1]]);
|
|
|
138 |
$this->assertTrue($form->is_validated());
|
|
|
139 |
|
|
|
140 |
$data = $form->get_data();
|
|
|
141 |
$this->assertNotEmpty($data->customfield_myfield1_editor);
|
|
|
142 |
$this->assertNotEmpty($data->customfield_myfield2_editor);
|
|
|
143 |
$handler->instance_form_save($data);
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
/**
|
|
|
147 |
* Test that instance form save empties the field content for blank values
|
|
|
148 |
*/
|
|
|
149 |
public function test_instance_form_save_clear(): void {
|
|
|
150 |
global $CFG;
|
|
|
151 |
|
|
|
152 |
require_once("{$CFG->dirroot}/customfield/tests/fixtures/test_instance_form.php");
|
|
|
153 |
|
|
|
154 |
$this->setAdminUser();
|
|
|
155 |
|
|
|
156 |
$handler = $this->cfcat->get_handler();
|
|
|
157 |
|
|
|
158 |
// Set our custom field to a known value.
|
|
|
159 |
$submitdata = (array) $this->courses[1] + [
|
|
|
160 |
'customfield_myfield1_editor' => ['text' => 'I can see it in your eyes', 'format' => FORMAT_HTML],
|
|
|
161 |
'customfield_myfield2_editor' => ['text' => 'I can see it in your smile', 'format' => FORMAT_HTML],
|
|
|
162 |
];
|
|
|
163 |
|
|
|
164 |
core_customfield_test_instance_form::mock_submit($submitdata, []);
|
|
|
165 |
$form = new core_customfield_test_instance_form('post', ['handler' => $handler, 'instance' => $this->courses[1]]);
|
|
|
166 |
$handler->instance_form_save($form->get_data());
|
|
|
167 |
|
|
|
168 |
$this->assertEquals($submitdata['customfield_myfield1_editor']['text'],
|
|
|
169 |
\core_customfield\data_controller::create($this->cfdata[1]->get('id'))->export_value());
|
|
|
170 |
|
|
|
171 |
// Now empty our non-required field.
|
|
|
172 |
$submitdata['customfield_myfield1_editor']['text'] = '';
|
|
|
173 |
|
|
|
174 |
core_customfield_test_instance_form::mock_submit($submitdata, []);
|
|
|
175 |
$form = new core_customfield_test_instance_form('post', ['handler' => $handler, 'instance' => $this->courses[1]]);
|
|
|
176 |
$handler->instance_form_save($form->get_data());
|
|
|
177 |
|
|
|
178 |
$this->assertNull(\core_customfield\data_controller::create($this->cfdata[1]->get('id'))->export_value());
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
/**
|
|
|
182 |
* Test for data_controller::get_value and export_value
|
|
|
183 |
*/
|
|
|
184 |
public function test_get_export_value() {
|
|
|
185 |
$this->assertEquals('Value1', $this->cfdata[1]->get_value());
|
|
|
186 |
$this->assertEquals('<div class="text_to_html">Value1</div>', $this->cfdata[1]->export_value());
|
|
|
187 |
|
|
|
188 |
// Field with empty data.
|
|
|
189 |
$this->assertNull($this->cfdata[2]->export_value());
|
|
|
190 |
|
|
|
191 |
// Field without data but with a default value.
|
|
|
192 |
$d = \core_customfield\data_controller::create(0, null, $this->cfields[3]);
|
|
|
193 |
$this->assertEquals('Value3', $d->get_value());
|
|
|
194 |
$this->assertEquals('<div class="text_to_html">Value3</div>', $d->export_value());
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
/**
|
|
|
198 |
* Deleting fields and data
|
|
|
199 |
*/
|
|
|
200 |
public function test_delete() {
|
|
|
201 |
$this->cfcat->get_handler()->delete_all();
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
/**
|
|
|
205 |
* Test embedded file backup and restore.
|
|
|
206 |
*
|
|
|
207 |
* @covers \customfield_textarea\data_controller::backup_define_structure
|
|
|
208 |
* @covers \customfield_textarea\data_controller::backup_restore_structure
|
|
|
209 |
*/
|
|
|
210 |
public function test_embedded_file_backup_and_restore(): void {
|
|
|
211 |
global $CFG, $USER, $DB;
|
|
|
212 |
require_once($CFG->dirroot . '/customfield/tests/fixtures/test_instance_form.php');
|
|
|
213 |
$this->setAdminUser();
|
|
|
214 |
$handler = $this->cfcat->get_handler();
|
|
|
215 |
|
|
|
216 |
// Create a file.
|
|
|
217 |
$fs = get_file_storage();
|
|
|
218 |
$filerecord = [
|
|
|
219 |
'contextid' => context_user::instance($USER->id)->id,
|
|
|
220 |
'component' => 'user',
|
|
|
221 |
'filearea' => 'draft',
|
|
|
222 |
'itemid' => file_get_unused_draft_itemid(),
|
|
|
223 |
'filepath' => '/',
|
|
|
224 |
'filename' => 'mytextfile.txt',
|
|
|
225 |
];
|
|
|
226 |
$fs->create_file_from_string($filerecord, 'Some text contents');
|
|
|
227 |
|
|
|
228 |
// Add the file to the custom field.
|
|
|
229 |
$submitdata = (array) $this->courses[1];
|
|
|
230 |
$submitdata['customfield_myfield1_editor'] = [
|
|
|
231 |
'text' => 'Here is a file: @@PLUGINFILE@@/mytextfile.txt',
|
|
|
232 |
'format' => FORMAT_HTML,
|
|
|
233 |
'itemid' => $filerecord['itemid'],
|
|
|
234 |
];
|
|
|
235 |
|
|
|
236 |
// Set the required field and submit.
|
|
|
237 |
$submitdata['customfield_myfield2_editor'] = ['text' => 'Some text', 'format' => FORMAT_HTML];
|
|
|
238 |
core_customfield_test_instance_form::mock_submit($submitdata, []);
|
|
|
239 |
$form = new core_customfield_test_instance_form('POST',
|
|
|
240 |
['handler' => $handler, 'instance' => $this->courses[1]]);
|
|
|
241 |
$this->assertTrue($form->is_validated());
|
|
|
242 |
|
|
|
243 |
$data = $form->get_data();
|
|
|
244 |
$this->assertNotEmpty($data->customfield_myfield1_editor);
|
|
|
245 |
$this->assertNotEmpty($data->customfield_myfield2_editor);
|
|
|
246 |
$handler->instance_form_save($data);
|
|
|
247 |
|
|
|
248 |
// Check if the draft file exists.
|
|
|
249 |
$context = context_course::instance($this->courses[1]->id);
|
|
|
250 |
$file = $fs->get_file($filerecord['contextid'], $filerecord['component'], $filerecord['filearea'], $filerecord['itemid'],
|
|
|
251 |
$filerecord['filepath'], $filerecord['filename']);
|
|
|
252 |
$this->assertNotEmpty($file);
|
|
|
253 |
|
|
|
254 |
// Check if the permanent file exists.
|
|
|
255 |
$file = $fs->get_file($context->id, 'customfield_textarea', 'value', $this->cfdata[1]->get('id'), '/', 'mytextfile.txt');
|
|
|
256 |
$this->assertNotEmpty($file);
|
|
|
257 |
|
|
|
258 |
// Backup and restore the course.
|
|
|
259 |
$backupid = $this->backup($this->courses[1]);
|
|
|
260 |
$newcourseid = $this->restore($backupid, $this->courses[1], '_copy');
|
|
|
261 |
|
|
|
262 |
$newcontext = context_course::instance($newcourseid);
|
|
|
263 |
|
|
|
264 |
$newcfdata = $DB->get_record('customfield_data', ['instanceid' => $newcourseid, 'fieldid' => $this->cfields[1]->get('id')]);
|
|
|
265 |
|
|
|
266 |
// Check if the permanent file exists in the new course after restore.
|
|
|
267 |
$file = $fs->get_file($newcontext->id, 'customfield_textarea', 'value', $newcfdata->id, '/', 'mytextfile.txt');
|
|
|
268 |
$this->assertNotEmpty($file);
|
|
|
269 |
}
|
|
|
270 |
|
|
|
271 |
/**
|
|
|
272 |
* Backs a course up to temp directory.
|
|
|
273 |
*
|
|
|
274 |
* @param \stdClass $course Course object to backup
|
|
|
275 |
* @return string ID of backup
|
|
|
276 |
*/
|
|
|
277 |
protected function backup($course): string {
|
|
|
278 |
global $USER, $CFG;
|
|
|
279 |
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
|
|
|
280 |
|
|
|
281 |
// Turn off file logging, otherwise it can't delete the file (Windows).
|
|
|
282 |
$CFG->backup_file_logger_level = \backup::LOG_NONE;
|
|
|
283 |
|
|
|
284 |
// Do backup with default settings. MODE_IMPORT means it will just
|
|
|
285 |
// create the directory and not zip it.
|
|
|
286 |
$bc = new \backup_controller(\backup::TYPE_1COURSE, $course->id,
|
|
|
287 |
\backup::FORMAT_MOODLE, \backup::INTERACTIVE_NO, \backup::MODE_IMPORT,
|
|
|
288 |
$USER->id);
|
|
|
289 |
$bc->get_plan()->get_setting('users')->set_status(\backup_setting::NOT_LOCKED);
|
|
|
290 |
$bc->get_plan()->get_setting('users')->set_value(true);
|
|
|
291 |
$bc->get_plan()->get_setting('logs')->set_value(true);
|
|
|
292 |
$backupid = $bc->get_backupid();
|
|
|
293 |
|
|
|
294 |
$bc->execute_plan();
|
|
|
295 |
$bc->destroy();
|
|
|
296 |
return $backupid;
|
|
|
297 |
}
|
|
|
298 |
|
|
|
299 |
/**
|
|
|
300 |
* Restores a course from temp directory.
|
|
|
301 |
*
|
|
|
302 |
* @param string $backupid Backup id
|
|
|
303 |
* @param \stdClass $course Original course object
|
|
|
304 |
* @param string $suffix Suffix to add after original course shortname and fullname
|
|
|
305 |
* @return int New course id
|
|
|
306 |
* @throws \restore_controller_exception
|
|
|
307 |
*/
|
|
|
308 |
protected function restore(string $backupid, $course, string $suffix): int {
|
|
|
309 |
global $USER, $CFG;
|
|
|
310 |
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
|
|
|
311 |
|
|
|
312 |
// Do restore to new course with default settings.
|
|
|
313 |
$newcourseid = \restore_dbops::create_new_course(
|
|
|
314 |
$course->fullname . $suffix, $course->shortname . $suffix, $course->category);
|
|
|
315 |
$rc = new \restore_controller($backupid, $newcourseid,
|
|
|
316 |
\backup::INTERACTIVE_NO, \backup::MODE_GENERAL, $USER->id,
|
|
|
317 |
\backup::TARGET_NEW_COURSE);
|
|
|
318 |
$rc->get_plan()->get_setting('logs')->set_value(true);
|
|
|
319 |
$rc->get_plan()->get_setting('users')->set_value(true);
|
|
|
320 |
|
|
|
321 |
$this->assertTrue($rc->execute_precheck());
|
|
|
322 |
$rc->execute_plan();
|
|
|
323 |
$rc->destroy();
|
|
|
324 |
|
|
|
325 |
return $newcourseid;
|
|
|
326 |
}
|
|
|
327 |
}
|