Línea 23... |
Línea 23... |
23 |
*
|
23 |
*
|
24 |
* @package core_customfield
|
24 |
* @package core_customfield
|
25 |
* @category test
|
25 |
* @category test
|
26 |
* @copyright 2018 Toni Barbera <toni@moodle.com>
|
26 |
* @copyright 2018 Toni Barbera <toni@moodle.com>
|
27 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
27 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
- |
|
28 |
* @covers \core_customfield\category_controller
|
28 |
*/
|
29 |
*/
|
29 |
class category_controller_test extends \advanced_testcase {
|
30 |
final class category_controller_test extends \advanced_testcase {
|
30 |
|
- |
|
31 |
/**
|
31 |
/**
|
32 |
* Get generator.
|
32 |
* Get generator.
|
33 |
*
|
33 |
*
|
34 |
* @return core_customfield_generator
|
34 |
* @return core_customfield_generator
|
35 |
*/
|
35 |
*/
|
Línea 60... |
Línea 60... |
60 |
$c = category_controller::create(0, $cat->to_record(), $handler);
|
60 |
$c = category_controller::create(0, $cat->to_record(), $handler);
|
61 |
$this->assertTrue($c instanceof category_controller);
|
61 |
$this->assertTrue($c instanceof category_controller);
|
62 |
}
|
62 |
}
|
Línea 63... |
Línea 63... |
63 |
|
63 |
|
- |
|
64 |
/**
|
- |
|
65 |
* Test creation of category instance from pre-defined object
|
- |
|
66 |
*/
|
- |
|
67 |
public function test_constructor_from_record(): void {
|
- |
|
68 |
$this->resetAfterTest();
|
- |
|
69 |
|
- |
|
70 |
// Create field object that matches the persistent/schema definition.
|
- |
|
71 |
$category = category_controller::create(0, (object) [
|
- |
|
72 |
'name' => 'Test',
|
- |
|
73 |
'description' => null,
|
- |
|
74 |
'descriptionformat' => null,
|
- |
|
75 |
'component' => 'core_course',
|
- |
|
76 |
'area' => 'course',
|
- |
|
77 |
'itemid' => 0,
|
- |
|
78 |
'sortorder' => null,
|
- |
|
79 |
]);
|
- |
|
80 |
|
- |
|
81 |
// Saving the category will validate the persistent internally.
|
- |
|
82 |
$category->save();
|
- |
|
83 |
|
- |
|
84 |
$this->assertInstanceOf(category_controller::class, $category);
|
- |
|
85 |
}
|
- |
|
86 |
|
64 |
/**
|
87 |
/**
|
65 |
* Test for function \core_customfield\field_controller::create() in case of wrong parameters
|
88 |
* Test for function \core_customfield\field_controller::create() in case of wrong parameters
|
66 |
*/
|
89 |
*/
|
67 |
public function test_constructor_errors(): void {
|
90 |
public function test_constructor_errors(): void {
|
68 |
global $DB;
|
91 |
global $DB;
|
Línea 73... |
Línea 96... |
73 |
|
96 |
|
74 |
// Both id and record give warning.
|
97 |
// Both id and record give warning.
|
75 |
$c = category_controller::create($catrecord->id, $catrecord);
|
98 |
$c = category_controller::create($catrecord->id, $catrecord);
|
76 |
$debugging = $this->getDebuggingMessages();
|
99 |
$debugging = $this->getDebuggingMessages();
|
- |
|
100 |
$this->assertEquals(1, count($debugging));
|
77 |
$this->assertEquals(1, count($debugging));
|
101 |
$this->assertEquals(
|
78 |
$this->assertEquals('Too many parameters, either id need to be specified or a record, but not both.',
|
102 |
'Too many parameters, either id need to be specified or a record, but not both.',
|
- |
|
103 |
$debugging[0]->message
|
79 |
$debugging[0]->message);
|
104 |
);
|
80 |
$this->resetDebugging();
|
105 |
$this->resetDebugging();
|
Línea 81... |
Línea 106... |
81 |
$this->assertTrue($c instanceof category_controller);
|
106 |
$this->assertTrue($c instanceof category_controller);
|
82 |
|
107 |
|
83 |
// Retrieve non-existing data.
|
108 |
// Retrieve non-existing data.
|
84 |
try {
|
109 |
try {
|
85 |
category_controller::create($catrecord->id + 1);
|
110 |
category_controller::create($catrecord->id + 1);
|
86 |
$this->fail('Expected exception');
|
111 |
$this->fail('Expected exception');
|
87 |
} catch (\moodle_exception $e) {
|
- |
|
88 |
$this->assertEquals('Category not found', $e->getMessage());
|
112 |
} catch (\moodle_exception $e) {
|
Línea 89... |
Línea 113... |
89 |
$this->assertEquals(\moodle_exception::class, get_class($e));
|
113 |
$this->assertEquals('Category not found', $e->getMessage());
|
90 |
}
|
114 |
}
|
91 |
|
115 |
|
92 |
// Missing required elements.
|
116 |
// Missing required elements.
|
93 |
try {
|
117 |
try {
|
94 |
category_controller::create(0, (object)['area' => 'course', 'itemid' => 0]);
|
118 |
category_controller::create(0, (object)['area' => 'course', 'itemid' => 0]);
|
95 |
$this->fail('Expected exception');
|
119 |
$this->fail('Expected exception');
|
96 |
} catch (\coding_exception $e) {
|
- |
|
97 |
$this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters ' .
|
120 |
} catch (\coding_exception $e) {
|
Línea 98... |
Línea 121... |
98 |
'to initialise category_controller - unknown component', $e->getMessage());
|
121 |
$this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters ' .
|
99 |
$this->assertEquals(\coding_exception::class, get_class($e));
|
122 |
'to initialise category_controller - unknown component', $e->getMessage());
|
100 |
}
|
123 |
}
|
101 |
|
124 |
|
102 |
// Missing required elements.
|
125 |
// Missing required elements.
|
103 |
try {
|
126 |
try {
|
104 |
category_controller::create(0, (object)['component' => 'core_course', 'itemid' => 0]);
|
127 |
category_controller::create(0, (object)['component' => 'core_course', 'itemid' => 0]);
|
105 |
$this->fail('Expected exception');
|
- |
|
106 |
} catch (\coding_exception $e) {
|
128 |
$this->fail('Expected exception');
|
Línea 107... |
Línea 129... |
107 |
$this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters ' .
|
129 |
} catch (\coding_exception $e) {
|
108 |
'to initialise category_controller - unknown area', $e->getMessage());
|
130 |
$this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters ' .
|
109 |
$this->assertEquals(\coding_exception::class, get_class($e));
|
131 |
'to initialise category_controller - unknown area', $e->getMessage());
|
110 |
}
|
132 |
}
|
111 |
|
133 |
|
112 |
// Missing required elements.
|
134 |
// Missing required elements.
|
113 |
try {
|
135 |
try {
|
114 |
category_controller::create(0, (object)['component' => 'core_course', 'area' => 'course']);
|
- |
|
115 |
$this->fail('Expected exception');
|
136 |
category_controller::create(0, (object)['component' => 'core_course', 'area' => 'course']);
|
Línea 116... |
Línea 137... |
116 |
} catch (\coding_exception $e) {
|
137 |
$this->fail('Expected exception');
|
117 |
$this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters ' .
|
138 |
} catch (\coding_exception $e) {
|
118 |
'to initialise category_controller - unknown itemid', $e->getMessage());
|
139 |
$this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters ' .
|
119 |
$this->assertEquals(\coding_exception::class, get_class($e));
|
140 |
'to initialise category_controller - unknown itemid', $e->getMessage());
|
120 |
}
|
141 |
}
|
121 |
|
142 |
|
122 |
$handler = \core_course\customfield\course_handler::create();
|
143 |
$handler = \core_course\customfield\course_handler::create();
|
123 |
// Missing required elements.
|
144 |
// Missing required elements.
|
124 |
try {
|
- |
|
125 |
category_controller::create(0, (object)['component' => 'x', 'area' => 'course', 'itemid' => 0], $handler);
|
145 |
try {
|
Línea 126... |
Línea 146... |
126 |
$this->fail('Expected exception');
|
146 |
category_controller::create(0, (object)['component' => 'x', 'area' => 'course', 'itemid' => 0], $handler);
|
127 |
} catch (\coding_exception $e) {
|
147 |
$this->fail('Expected exception');
|
128 |
$this->assertEquals('Coding error detected, it must be fixed by a programmer: Component of the handler ' .
|
148 |
} catch (\coding_exception $e) {
|
129 |
'does not match the one from the record', $e->getMessage());
|
149 |
$this->assertEquals('Coding error detected, it must be fixed by a programmer: Component of the handler ' .
|
130 |
$this->assertEquals(\coding_exception::class, get_class($e));
|
150 |
'does not match the one from the record', $e->getMessage());
|
131 |
}
|
151 |
}
|
132 |
|
- |
|
133 |
try {
|
152 |
|
Línea 134... |
Línea 153... |
134 |
category_controller::create(0, (object)['component' => 'core_course', 'area' => 'x', 'itemid' => 0], $handler);
|
153 |
try {
|
135 |
$this->fail('Expected exception');
|
154 |
category_controller::create(0, (object)['component' => 'core_course', 'area' => 'x', 'itemid' => 0], $handler);
|
136 |
} catch (\coding_exception $e) {
|
155 |
$this->fail('Expected exception');
|
137 |
$this->assertEquals('Coding error detected, it must be fixed by a programmer: Area of the handler ' .
|
156 |
} catch (\coding_exception $e) {
|
138 |
'does not match the one from the record', $e->getMessage());
|
157 |
$this->assertEquals('Coding error detected, it must be fixed by a programmer: Area of the handler ' .
|
139 |
$this->assertEquals(\coding_exception::class, get_class($e));
|
158 |
'does not match the one from the record', $e->getMessage());
|
140 |
}
|
- |
|
141 |
|
159 |
}
|
Línea 142... |
Línea 160... |
142 |
try {
|
160 |
|
143 |
category_controller::create(0, (object)['component' => 'core_course', 'area' => 'course', 'itemid' => 1], $handler);
|
161 |
try {
|
144 |
$this->fail('Expected exception');
|
162 |
category_controller::create(0, (object)['component' => 'core_course', 'area' => 'course', 'itemid' => 1], $handler);
|
- |
|
163 |
$this->fail('Expected exception');
|
- |
|
164 |
} catch (\coding_exception $e) {
|
- |
|
165 |
$this->assertEquals('Coding error detected, it must be fixed by a programmer: Itemid of the ' .
|
145 |
} catch (\coding_exception $e) {
|
166 |
'handler does not match the one from the record', $e->getMessage());
|
- |
|
167 |
}
|
146 |
$this->assertEquals('Coding error detected, it must be fixed by a programmer: Itemid of the ' .
|
168 |
|
147 |
'handler does not match the one from the record', $e->getMessage());
|
169 |
try {
|
148 |
$this->assertEquals(\coding_exception::class, get_class($e));
|
170 |
$user = $this->getDataGenerator()->create_user();
|
149 |
}
|
171 |
category_controller::create(0, (object) [
|
150 |
|
- |
|
151 |
try {
|
172 |
'component' => 'core_course',
|
152 |
$user = $this->getDataGenerator()->create_user();
|
173 |
'area' => 'course',
|
Línea 153... |
Línea 174... |
153 |
category_controller::create(0, (object)['component' => 'core_course', 'area' => 'course', 'itemid' => 0,
|
174 |
'itemid' => 0,
|
154 |
'contextid' => \context_user::instance($user->id)->id], $handler);
|
175 |
'contextid' => \context_user::instance($user->id)->id,
|