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_customfield
;
19 use core_customfield_generator
;
20 use customfield_checkbox
;
22 use customfield_select
;
24 use customfield_textarea
;
27 * Functional test for class data_controller.
29 * @package core_customfield
31 * @copyright 2018 Toni Barbera <toni@moodle.com>
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class data_controller_test
extends \advanced_testcase
{
39 * @return core_customfield_generator
41 protected function get_generator(): core_customfield_generator
{
42 return $this->getDataGenerator()->get_plugin_generator('core_customfield');
46 * Test for function data_controller::create()
48 public function test_constructor() {
50 $this->resetAfterTest();
52 // Create a course, fields category and fields.
53 $course = $this->getDataGenerator()->create_course();
54 $category0 = $this->get_generator()->create_category(['name' => 'aaaa']);
56 // Add fields to this category.
57 $fielddata = new \
stdClass();
58 $fielddata->categoryid
= $category0->get('id');
59 $fielddata->configdata
= "{\"required\":\"0\",\"uniquevalues\":\"0\",\"locked\":\"0\",\"visibility\":\"0\",
60 \"defaultvalue\":\"\",\"displaysize\":0,\"maxlength\":0,\"ispassword\":\"0\",
61 \"link\":\"\",\"linktarget\":\"\"}";
63 $fielddata->type
= 'checkbox';
64 $field0 = $this->get_generator()->create_field($fielddata);
65 $fielddata->type
= 'date';
66 $field1 = $this->get_generator()->create_field($fielddata);
67 $fielddata->type
= 'select';
68 $field2 = $this->get_generator()->create_field($fielddata);
69 $fielddata->type
= 'text';
70 $field3 = $this->get_generator()->create_field($fielddata);
71 $fielddata->type
= 'textarea';
72 $field4 = $this->get_generator()->create_field($fielddata);
74 $params = ['instanceid' => $course->id
, 'contextid' => \context_course
::instance($course->id
)->id
];
76 // Generate new data_controller records for these fields, specifying field controller or fieldid or both.
77 $data0 = data_controller
::create(0, (object)$params, $field0);
78 $this->assertInstanceOf(customfield_checkbox\data_controller
::class, $data0);
79 $data1 = data_controller
::create(0,
80 (object)($params +
['fieldid' => $field1->get('id')]), $field1);
81 $this->assertInstanceOf(customfield_date\data_controller
::class, $data1);
82 $data2 = data_controller
::create(0,
83 (object)($params +
['fieldid' => $field2->get('id')]));
84 $this->assertInstanceOf(customfield_select\data_controller
::class, $data2);
85 $data3 = data_controller
::create(0, (object)$params, $field3);
86 $this->assertInstanceOf(customfield_text\data_controller
::class, $data3);
87 $data4 = data_controller
::create(0, (object)$params, $field4);
88 $this->assertInstanceOf(customfield_textarea\data_controller
::class, $data4);
90 // Save data so we can have ids.
97 // Retrieve data by id.
98 $this->assertInstanceOf(customfield_checkbox\data_controller
::class, data_controller
::create($data0->get('id')));
99 $this->assertInstanceOf(customfield_date\data_controller
::class, data_controller
::create($data1->get('id')));
101 // Retrieve data by id and field.
102 $this->assertInstanceOf(customfield_select\data_controller
::class,
103 data_controller
::create($data2->get('id'), null, $field2));
105 // Retrieve data by record without field.
106 $datarecord = $DB->get_record(\core_customfield\data
::TABLE
, ['id' => $data3->get('id')], '*', MUST_EXIST
);
107 $this->assertInstanceOf(customfield_text\data_controller
::class, data_controller
::create(0, $datarecord));
109 // Retrieve data by record with field.
110 $datarecord = $DB->get_record(\core_customfield\data
::TABLE
, ['id' => $data4->get('id')], '*', MUST_EXIST
);
111 $this->assertInstanceOf(customfield_textarea\data_controller
::class, data_controller
::create(0, $datarecord, $field4));
116 * Test for function \core_customfield\field_controller::create() in case of wrong parameters
118 public function test_constructor_errors() {
120 $this->resetAfterTest();
122 // Create a category, field and data.
123 $category = $this->get_generator()->create_category();
124 $field = $this->get_generator()->create_field(['categoryid' => $category->get('id')]);
125 $course = $this->getDataGenerator()->create_course();
126 $data = data_controller
::create(0, (object)['instanceid' => $course->id
,
127 'contextid' => \context_course
::instance($course->id
)->id
], $field);
130 $datarecord = $DB->get_record(\core_customfield\data
::TABLE
, ['id' => $data->get('id')], '*', MUST_EXIST
);
132 // Both id and record give warning.
133 $d = data_controller
::create($datarecord->id
, $datarecord);
134 $debugging = $this->getDebuggingMessages();
135 $this->assertEquals(1, count($debugging));
136 $this->assertEquals('Too many parameters, either id need to be specified or a record, but not both.',
137 $debugging[0]->message
);
138 $this->resetDebugging();
139 $this->assertInstanceOf(customfield_text\data_controller
::class, $d);
141 // Retrieve non-existing data.
143 data_controller
::create($datarecord->id +
1);
144 $this->fail('Expected exception');
145 } catch (\dml_missing_record_exception
$e) {
146 $this->assertStringMatchesFormat('Can\'t find data record in database table customfield_data%a', $e->getMessage());
147 $this->assertEquals(\dml_missing_record_exception
::class, get_class($e));
152 data_controller
::create(0, (object)['instanceid' => $course->id
]);
153 $this->fail('Expected exception');
154 } catch (\coding_exception
$e) {
155 $this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters to ' .
156 'initialise data_controller - unknown field', $e->getMessage());
157 $this->assertEquals(\coding_exception
::class, get_class($e));
160 // Mismatching field id.
162 data_controller
::create(0, (object)['instanceid' => $course->id
, 'fieldid' => $field->get('id') +
1], $field);
163 $this->fail('Expected exception');
164 } catch (\coding_exception
$e) {
165 $this->assertEquals('Coding error detected, it must be fixed by a programmer: Field id from the record ' .
166 'does not match field from the parameter', $e->getMessage());
167 $this->assertEquals(\coding_exception
::class, get_class($e));
170 // Nonexisting class.
172 $field->set('type', 'invalid');
173 data_controller
::create(0, (object)['instanceid' => $course->id
], $field);
174 $this->fail('Expected exception');
175 } catch (\moodle_exception
$e) {
176 $this->assertEquals('Field type invalid not found', $e->getMessage());
177 $this->assertEquals(\moodle_exception
::class, get_class($e));