2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 namespace core_course
;
19 defined('MOODLE_INTERNAL') ||
die();
22 require_once($CFG->dirroot
. '/backup/util/includes/backup_includes.php');
23 require_once($CFG->dirroot
. '/backup/util/includes/restore_includes.php');
26 * Tests for customfields in courses
28 * @package core_course
29 * @copyright 2018 Marina Glancy
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 class customfield_test
extends \advanced_testcase
{
37 protected function setUp(): void
{
39 $this->resetAfterTest();
40 $this->setAdminUser();
42 $dg = self
::getDataGenerator();
43 $catid = $dg->create_custom_field_category([])->get('id');
44 $dg->create_custom_field(['categoryid' => $catid, 'type' => 'text', 'shortname' => 'f1']);
45 $dg->create_custom_field(['categoryid' => $catid, 'type' => 'checkbox', 'shortname' => 'f2']);
46 $dg->create_custom_field(['categoryid' => $catid, 'type' => 'date', 'shortname' => 'f3',
47 'configdata' => ['startyear' => 2000, 'endyear' => 3000, 'includetime' => 1]]);
48 $dg->create_custom_field(['categoryid' => $catid, 'type' => 'select', 'shortname' => 'f4',
49 'configdata' => ['options' => "a\nb\nc"]]);
50 $dg->create_custom_field(['categoryid' => $catid, 'type' => 'textarea', 'shortname' => 'f5']);
54 * Test creating course with customfields and retrieving them
56 public function test_create_course() {
58 $dg = $this->getDataGenerator();
61 $c1 = $dg->create_course(['shortname' => 'SN', 'fullname' => 'FN',
62 'summary' => 'DESC', 'summaryformat' => FORMAT_MOODLE
,
63 'customfield_f1' => 'some text', 'customfield_f2' => 1,
64 'customfield_f3' => $now, 'customfield_f4' => 2,
65 'customfield_f5_editor' => ['text' => 'test', 'format' => FORMAT_HTML
]]);
67 $data = \core_course\customfield\course_handler
::create()->export_instance_data_object($c1->id
);
69 $this->assertEquals('some text', $data->f1
);
70 $this->assertEquals('Yes', $data->f2
);
71 $this->assertEquals(userdate($now, get_string('strftimedaydatetime')), $data->f3
);
72 $this->assertEquals('b', $data->f4
);
73 $this->assertEquals('test', $data->f5
);
75 $this->assertEquals(5, count($DB->get_records('customfield_data')));
77 delete_course($c1->id
, false);
79 $this->assertEquals(0, count($DB->get_records('customfield_data')));
83 * Backup a course and return its backup ID.
85 * @param int $courseid The course ID.
86 * @param int $userid The user doing the backup.
89 protected function backup_course($courseid, $userid = 2) {
90 $backuptempdir = make_backup_temp_directory('');
91 $packer = get_file_packer('application/vnd.moodle.backup');
93 $bc = new \backup_controller
(\backup
::TYPE_1COURSE
, $courseid, \backup
::FORMAT_MOODLE
, \backup
::INTERACTIVE_NO
,
94 \backup
::MODE_GENERAL
, $userid);
97 $results = $bc->get_results();
98 $results['backup_destination']->extract_to_pathname($packer, "$backuptempdir/core_course_testcase");
102 return 'core_course_testcase';
108 * @param int $backupid The backup ID.
109 * @param int $courseid The course ID to restore in, or 0.
110 * @param int $userid The ID of the user performing the restore.
111 * @return stdClass The updated course object.
113 protected function restore_course($backupid, $courseid, $userid) {
116 $target = \backup
::TARGET_CURRENT_ADDING
;
118 $target = \backup
::TARGET_NEW_COURSE
;
119 $categoryid = $DB->get_field_sql("SELECT MIN(id) FROM {course_categories}");
120 $courseid = \restore_dbops
::create_new_course('Tmp', 'tmp', $categoryid);
123 $rc = new \restore_controller
($backupid, $courseid, \backup
::INTERACTIVE_NO
, \backup
::MODE_GENERAL
, $userid, $target);
124 $target == \backup
::TARGET_NEW_COURSE ?
: $rc->get_plan()->get_setting('overwrite_conf')->set_value(true);
125 $this->assertTrue($rc->execute_precheck());
128 $course = $DB->get_record('course', array('id' => $rc->get_courseid()));
136 * Test backup and restore of custom fields
138 public function test_restore_customfields() {
140 $dg = $this->getDataGenerator();
142 $c1 = $dg->create_course(['shortname' => 'SN', 'fullname' => 'FN',
143 'summary' => 'DESC', 'summaryformat' => FORMAT_MOODLE
,
144 'customfield_f1' => 'some text', 'customfield_f2' => 1]);
145 $backupid = $this->backup_course($c1->id
);
147 // The information is restored but adapted because names are already taken.
148 $c2 = $this->restore_course($backupid, 0, $USER->id
);
150 $data = \core_course\customfield\course_handler
::create()->export_instance_data_object($c1->id
);
151 $this->assertEquals('some text', $data->f1
);
152 $this->assertEquals('Yes', $data->f2
);
156 * Delete a category that has fields and the fields have data.
158 public function test_delete_category() {
160 $dg = $this->getDataGenerator();
163 $c1 = $dg->create_course(['shortname' => 'SN', 'fullname' => 'FN',
164 'summary' => 'DESC', 'summaryformat' => FORMAT_MOODLE
,
165 'customfield_f1' => 'some text', 'customfield_f2' => 1,
166 'customfield_f3' => $now, 'customfield_f4' => 2,
167 'customfield_f5_editor' => ['text' => 'test', 'format' => FORMAT_HTML
]]);
169 // Find the category and delete it.
170 $cats = \core_course\customfield\course_handler
::create()->get_categories_with_fields();
172 $cat->get_handler()->delete_category($cat);
174 // Course no longer has the customfield properties.
175 $course = course_get_format($c1->id
)->get_course();
176 $keys = array_keys((array)$course);
177 $this->assertEmpty(array_intersect($keys, ['customfield_f1', 'customfield_f2',
178 'customfield_f3', 'customfield_f4', 'customfield_f5']));
180 // Nothing in customfield tables either.
181 $this->assertEquals(0, count($DB->get_records('customfield_field')));
182 $this->assertEquals(0, count($DB->get_records('customfield_data')));