This repository has been archived by the owner on Dec 13, 2024. It is now read-only.
forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_controller_test.php
181 lines (156 loc) · 8.1 KB
/
data_controller_test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Tests for class data_controller.
*
* @package core_customfield
* @category test
* @copyright 2018 Toni Barbera <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
use core_customfield\data_controller;
/**
* Functional test for class data_controller.
* @copyright 2018 Toni Barbera <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_customfield_data_controller_testcase extends advanced_testcase {
/**
* Get generator.
*
* @return core_customfield_generator
*/
protected function get_generator(): core_customfield_generator {
return $this->getDataGenerator()->get_plugin_generator('core_customfield');
}
/**
* Test for function data_controller::create()
*/
public function test_constructor() {
global $DB;
$this->resetAfterTest();
// Create a course, fields category and fields.
$course = $this->getDataGenerator()->create_course();
$category0 = $this->get_generator()->create_category(['name' => 'aaaa']);
// Add fields to this category.
$fielddata = new stdClass();
$fielddata->categoryid = $category0->get('id');
$fielddata->configdata = "{\"required\":\"0\",\"uniquevalues\":\"0\",\"locked\":\"0\",\"visibility\":\"0\",
\"defaultvalue\":\"\",\"displaysize\":0,\"maxlength\":0,\"ispassword\":\"0\",
\"link\":\"\",\"linktarget\":\"\"}";
$fielddata->type = 'checkbox';
$field0 = $this->get_generator()->create_field($fielddata);
$fielddata->type = 'date';
$field1 = $this->get_generator()->create_field($fielddata);
$fielddata->type = 'select';
$field2 = $this->get_generator()->create_field($fielddata);
$fielddata->type = 'text';
$field3 = $this->get_generator()->create_field($fielddata);
$fielddata->type = 'textarea';
$field4 = $this->get_generator()->create_field($fielddata);
$params = ['instanceid' => $course->id, 'contextid' => context_course::instance($course->id)->id];
// Generate new data_controller records for these fields, specifying field controller or fieldid or both.
$data0 = data_controller::create(0, (object)$params, $field0);
$this->assertInstanceOf(customfield_checkbox\data_controller::class, $data0);
$data1 = data_controller::create(0,
(object)($params + ['fieldid' => $field1->get('id')]), $field1);
$this->assertInstanceOf(customfield_date\data_controller::class, $data1);
$data2 = data_controller::create(0,
(object)($params + ['fieldid' => $field2->get('id')]));
$this->assertInstanceOf(customfield_select\data_controller::class, $data2);
$data3 = data_controller::create(0, (object)$params, $field3);
$this->assertInstanceOf(customfield_text\data_controller::class, $data3);
$data4 = data_controller::create(0, (object)$params, $field4);
$this->assertInstanceOf(customfield_textarea\data_controller::class, $data4);
// Save data so we can have ids.
$data0->save();
$data1->save();
$data2->save();
$data3->save();
$data4->save();
// Retrieve data by id.
$this->assertInstanceOf(customfield_checkbox\data_controller::class, data_controller::create($data0->get('id')));
$this->assertInstanceOf(customfield_date\data_controller::class, data_controller::create($data1->get('id')));
// Retrieve data by id and field.
$this->assertInstanceOf(customfield_select\data_controller::class,
data_controller::create($data2->get('id'), null, $field2));
// Retrieve data by record without field.
$datarecord = $DB->get_record(\core_customfield\data::TABLE, ['id' => $data3->get('id')], '*', MUST_EXIST);
$this->assertInstanceOf(customfield_text\data_controller::class, data_controller::create(0, $datarecord));
// Retrieve data by record with field.
$datarecord = $DB->get_record(\core_customfield\data::TABLE, ['id' => $data4->get('id')], '*', MUST_EXIST);
$this->assertInstanceOf(customfield_textarea\data_controller::class, data_controller::create(0, $datarecord, $field4));
}
/**
* Test for function \core_customfield\field_controller::create() in case of wrong parameters
*/
public function test_constructor_errors() {
global $DB;
$this->resetAfterTest();
// Create a category, field and data.
$category = $this->get_generator()->create_category();
$field = $this->get_generator()->create_field(['categoryid' => $category->get('id')]);
$course = $this->getDataGenerator()->create_course();
$data = data_controller::create(0, (object)['instanceid' => $course->id,
'contextid' => context_course::instance($course->id)->id], $field);
$data->save();
$datarecord = $DB->get_record(\core_customfield\data::TABLE, ['id' => $data->get('id')], '*', MUST_EXIST);
// Both id and record give warning.
$d = data_controller::create($datarecord->id, $datarecord);
$debugging = $this->getDebuggingMessages();
$this->assertEquals(1, count($debugging));
$this->assertEquals('Too many parameters, either id need to be specified or a record, but not both.',
$debugging[0]->message);
$this->resetDebugging();
$this->assertInstanceOf(customfield_text\data_controller::class, $d);
// Retrieve non-existing data.
try {
data_controller::create($datarecord->id + 1);
$this->fail('Expected exception');
} catch (dml_missing_record_exception $e) {
$this->assertStringMatchesFormat('Can\'t find data record in database table customfield_data%a', $e->getMessage());
$this->assertEquals(dml_missing_record_exception::class, get_class($e));
}
// Missing field id.
try {
data_controller::create(0, (object)['instanceid' => $course->id]);
$this->fail('Expected exception');
} catch (coding_exception $e) {
$this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters to ' .
'initialise data_controller - unknown field', $e->getMessage());
$this->assertEquals(coding_exception::class, get_class($e));
}
// Mismatching field id.
try {
data_controller::create(0, (object)['instanceid' => $course->id, 'fieldid' => $field->get('id') + 1], $field);
$this->fail('Expected exception');
} catch (coding_exception $e) {
$this->assertEquals('Coding error detected, it must be fixed by a programmer: Field id from the record ' .
'does not match field from the parameter', $e->getMessage());
$this->assertEquals(coding_exception::class, get_class($e));
}
// Nonexisting class.
try {
$field->set('type', 'invalid');
data_controller::create(0, (object)['instanceid' => $course->id], $field);
$this->fail('Expected exception');
} catch (moodle_exception $e) {
$this->assertEquals('Field type invalid not found', $e->getMessage());
$this->assertEquals(moodle_exception::class, get_class($e));
}
}
}