MDL-71036 phpunit: Remove custom autoloader
[moodle.git] / customfield / tests / field_controller_test.php
bloba642919e2624a48e36223584aed80175a1dbad46
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.
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 /**
18 * Tests for class \core_customfield\field_controller.
20 * @package core_customfield
21 * @category test
22 * @copyright 2018 Ruslan Kabalin
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 use \core_customfield\category_controller;
29 use \core_customfield\field_controller;
31 /**
32 * Functional test for class \core_customfield\field_controller.
34 * @package core_customfield
35 * @category test
36 * @copyright 2018 Ruslan Kabalin
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class core_customfield_field_controller_testcase extends advanced_testcase {
41 /**
42 * Get generator.
44 * @return core_customfield_generator
46 protected function get_generator(): core_customfield_generator {
47 return $this->getDataGenerator()->get_plugin_generator('core_customfield');
50 /**
51 * Test for function \core_customfield\field_controller::create()
53 public function test_constructor() {
54 global $DB;
55 $this->resetAfterTest();
57 // Create the category.
58 $category0 = $this->get_generator()->create_category();
60 // Initiate objects without id, try with the category object or with category id or with both.
61 $field0 = field_controller::create(0, (object)['type' => 'checkbox'], $category0);
62 $this->assertInstanceOf(customfield_checkbox\field_controller::class, $field0);
63 $field1 = field_controller::create(0, (object)['type' => 'date', 'categoryid' => $category0->get('id')]);
64 $this->assertInstanceOf(customfield_date\field_controller::class, $field1);
65 $field2 = field_controller::create(0, (object)['type' => 'select', 'categoryid' => $category0->get('id')], $category0);
66 $this->assertInstanceOf(customfield_select\field_controller::class, $field2);
67 $field3 = field_controller::create(0, (object)['type' => 'text'], $category0);
68 $this->assertInstanceOf(customfield_text\field_controller::class, $field3);
69 $field4 = field_controller::create(0, (object)['type' => 'textarea'], $category0);
70 $this->assertInstanceOf(customfield_textarea\field_controller::class, $field4);
72 // Save fields to the db so we have ids.
73 \core_customfield\api::save_field_configuration($field0, (object)['name' => 'a', 'shortname' => 'a']);
74 \core_customfield\api::save_field_configuration($field1, (object)['name' => 'b', 'shortname' => 'b']);
75 \core_customfield\api::save_field_configuration($field2, (object)['name' => 'c', 'shortname' => 'c']);
76 \core_customfield\api::save_field_configuration($field3, (object)['name' => 'd', 'shortname' => 'd']);
77 \core_customfield\api::save_field_configuration($field4, (object)['name' => 'e', 'shortname' => 'e']);
79 // Retrieve fields by id.
80 $this->assertInstanceOf(customfield_checkbox\field_controller::class, field_controller::create($field0->get('id')));
81 $this->assertInstanceOf(customfield_date\field_controller::class, field_controller::create($field1->get('id')));
83 // Retrieve field by id and category.
84 $this->assertInstanceOf(customfield_select\field_controller::class,
85 field_controller::create($field2->get('id'), null, $category0));
87 // Retrieve fields by record without category.
88 $fieldrecord = $DB->get_record(\core_customfield\field::TABLE, ['id' => $field3->get('id')], '*', MUST_EXIST);
89 $this->assertInstanceOf(customfield_text\field_controller::class, field_controller::create(0, $fieldrecord));
91 // Retrieve fields by record with category.
92 $fieldrecord = $DB->get_record(\core_customfield\field::TABLE, ['id' => $field4->get('id')], '*', MUST_EXIST);
93 $this->assertInstanceOf(customfield_textarea\field_controller::class,
94 field_controller::create(0, $fieldrecord, $category0));
97 /**
98 * Test for function \core_customfield\field_controller::create() in case of wrong parameters
100 public function test_constructor_errors() {
101 global $DB;
102 $this->resetAfterTest();
104 // Create a category and a field.
105 $category = $this->get_generator()->create_category();
106 $field = $this->get_generator()->create_field(['categoryid' => $category->get('id')]);
108 $fieldrecord = $DB->get_record(\core_customfield\field::TABLE, ['id' => $field->get('id')], '*', MUST_EXIST);
110 // Both id and record give warning.
111 $field = field_controller::create($fieldrecord->id, $fieldrecord);
112 $debugging = $this->getDebuggingMessages();
113 $this->assertEquals(1, count($debugging));
114 $this->assertEquals('Too many parameters, either id need to be specified or a record, but not both.',
115 $debugging[0]->message);
116 $this->resetDebugging();
117 $this->assertInstanceOf(customfield_text\field_controller::class, $field);
119 // Retrieve non-existing field.
120 try {
121 field_controller::create($fieldrecord->id + 1);
122 $this->fail('Expected exception');
123 } catch (moodle_exception $e) {
124 $this->assertEquals('Field not found', $e->getMessage());
125 $this->assertEquals(moodle_exception::class, get_class($e));
128 // Retrieve without id and without type.
129 try {
130 field_controller::create(0, (object)['name' => 'a'], $category);
131 $this->fail('Expected exception');
132 } catch (coding_exception $e) {
133 $this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters to ' .
134 'initialise field_controller - unknown field type', $e->getMessage());
135 $this->assertEquals(coding_exception::class, get_class($e));
138 // Missing category id.
139 try {
140 field_controller::create(0, (object)['type' => 'text']);
141 $this->fail('Expected exception');
142 } catch (coding_exception $e) {
143 $this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters ' .
144 'to initialise field_controller - unknown category', $e->getMessage());
145 $this->assertEquals(coding_exception::class, get_class($e));
148 // Mismatching category id.
149 try {
150 field_controller::create(0, (object)['type' => 'text', 'categoryid' => $category->get('id') + 1], $category);
151 $this->fail('Expected exception');
152 } catch (coding_exception $e) {
153 $this->assertEquals('Coding error detected, it must be fixed by a programmer: Category of the field ' .
154 'does not match category from the parameter', $e->getMessage());
155 $this->assertEquals(coding_exception::class, get_class($e));
158 // Non-existing type.
159 try {
160 field_controller::create(0, (object)['type' => 'nonexisting'], $category);
161 $this->fail('Expected exception');
162 } catch (moodle_exception $e) {
163 $this->assertEquals('Field type nonexisting not found', $e->getMessage());
164 $this->assertEquals(moodle_exception::class, get_class($e));
169 * Tests for behaviour of:
170 * \core_customfield\field_controller::save()
171 * \core_customfield\field_controller::get()
172 * \core_customfield\field_controller::get_category()
174 public function test_create_field() {
175 global $DB;
176 $this->resetAfterTest();
178 $lpg = $this->get_generator();
179 $category = $lpg->create_category();
180 $fields = $DB->get_records(\core_customfield\field::TABLE, ['categoryid' => $category->get('id')]);
181 $this->assertCount(0, $fields);
183 // Create field.
184 $fielddata = new stdClass();
185 $fielddata->name = 'Field';
186 $fielddata->shortname = 'field';
187 $fielddata->type = 'text';
188 $fielddata->categoryid = $category->get('id');
189 $field = field_controller::create(0, $fielddata);
190 $field->save();
192 $fields = $DB->get_records(\core_customfield\field::TABLE, ['categoryid' => $category->get('id')]);
193 $this->assertCount(1, $fields);
194 $this->assertTrue(\core_customfield\field::record_exists($field->get('id')));
195 $this->assertInstanceOf(\customfield_text\field_controller::class, $field);
196 $this->assertSame($field->get('name'), $fielddata->name);
197 $this->assertSame($field->get('type'), $fielddata->type);
198 $this->assertEquals($field->get_category()->get('id'), $category->get('id'));
202 * Tests for \core_customfield\field_controller::delete() behaviour.
204 public function test_delete_field() {
205 global $DB;
206 $this->resetAfterTest();
208 $lpg = $this->get_generator();
209 $category = $lpg->create_category();
210 $fields = $DB->get_records(\core_customfield\field::TABLE, ['categoryid' => $category->get('id')]);
211 $this->assertCount(0, $fields);
213 // Create field using generator.
214 $field1 = $lpg->create_field(array('categoryid' => $category->get('id')));
215 $field2 = $lpg->create_field(array('categoryid' => $category->get('id')));
216 $fields = $DB->get_records(\core_customfield\field::TABLE, ['categoryid' => $category->get('id')]);
217 $this->assertCount(2, $fields);
219 // Delete fields.
220 $this->assertTrue($field1->delete());
221 $this->assertTrue($field2->delete());
223 // Check that the fields have been deleted.
224 $fields = $DB->get_records(\core_customfield\field::TABLE, ['categoryid' => $category->get('id')]);
225 $this->assertCount(0, $fields);
226 $this->assertFalse(\core_customfield\field::record_exists($field1->get('id')));
227 $this->assertFalse(\core_customfield\field::record_exists($field2->get('id')));
231 * Tests for \core_customfield\field_controller::get_configdata_property() behaviour.
233 public function test_get_configdata_property() {
234 $this->resetAfterTest();
236 $lpg = $this->get_generator();
237 $category = $lpg->create_category();
238 $configdata = ['a' => 'b', 'c' => ['d', 'e']];
239 $field = field_controller::create(0, (object)['type' => 'text',
240 'configdata' => json_encode($configdata), 'shortname' => 'a', 'name' => 'a'], $category);
241 $field->save();
243 // Retrieve field and check configdata.
244 $field = field_controller::create($field->get('id'));
245 $this->assertEquals($configdata, $field->get('configdata'));
246 $this->assertEquals('b', $field->get_configdata_property('a'));
247 $this->assertEquals(['d', 'e'], $field->get_configdata_property('c'));
248 $this->assertEquals(null, $field->get_configdata_property('x'));