Merge branch 'MDL-81256' of https://github.com/marinaglancy/moodle
[moodle.git] / customfield / tests / category_controller_test.php
blob5179a4135ffc126456153f84043c025863e3c49b
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 namespace core_customfield;
19 use core_customfield_generator;
21 /**
22 * Functional test for class \core_customfield\category_controller.
24 * @package core_customfield
25 * @category test
26 * @copyright 2018 Toni Barbera <toni@moodle.com>
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 class category_controller_test extends \advanced_testcase {
31 /**
32 * Get generator.
34 * @return core_customfield_generator
36 protected function get_generator(): core_customfield_generator {
37 return $this->getDataGenerator()->get_plugin_generator('core_customfield');
40 /**
41 * Test for the field_controller::__construct function.
43 public function test_constructor() {
44 $this->resetAfterTest();
46 $c = category_controller::create(0, (object)['component' => 'core_course', 'area' => 'course', 'itemid' => 0]);
47 $handler = $c->get_handler();
48 $this->assertTrue($c instanceof category_controller);
50 $cat = $this->get_generator()->create_category();
51 $c = category_controller::create($cat->get('id'));
52 $this->assertTrue($c instanceof category_controller);
54 $c = category_controller::create($cat->get('id'), null, $handler);
55 $this->assertTrue($c instanceof category_controller);
57 $c = category_controller::create(0, $cat->to_record());
58 $this->assertTrue($c instanceof category_controller);
60 $c = category_controller::create(0, $cat->to_record(), $handler);
61 $this->assertTrue($c instanceof category_controller);
64 /**
65 * Test for function \core_customfield\field_controller::create() in case of wrong parameters
67 public function test_constructor_errors() {
68 global $DB;
69 $this->resetAfterTest();
71 $cat = $this->get_generator()->create_category();
72 $catrecord = $cat->to_record();
74 // Both id and record give warning.
75 $c = category_controller::create($catrecord->id, $catrecord);
76 $debugging = $this->getDebuggingMessages();
77 $this->assertEquals(1, count($debugging));
78 $this->assertEquals('Too many parameters, either id need to be specified or a record, but not both.',
79 $debugging[0]->message);
80 $this->resetDebugging();
81 $this->assertTrue($c instanceof category_controller);
83 // Retrieve non-existing data.
84 try {
85 category_controller::create($catrecord->id + 1);
86 $this->fail('Expected exception');
87 } catch (\moodle_exception $e) {
88 $this->assertEquals('Category not found', $e->getMessage());
89 $this->assertEquals(\moodle_exception::class, get_class($e));
92 // Missing required elements.
93 try {
94 category_controller::create(0, (object)['area' => 'course', 'itemid' => 0]);
95 $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 ' .
98 'to initialise category_controller - unknown component', $e->getMessage());
99 $this->assertEquals(\coding_exception::class, get_class($e));
102 // Missing required elements.
103 try {
104 category_controller::create(0, (object)['component' => 'core_course', 'itemid' => 0]);
105 $this->fail('Expected exception');
106 } catch (\coding_exception $e) {
107 $this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters ' .
108 'to initialise category_controller - unknown area', $e->getMessage());
109 $this->assertEquals(\coding_exception::class, get_class($e));
112 // Missing required elements.
113 try {
114 category_controller::create(0, (object)['component' => 'core_course', 'area' => 'course']);
115 $this->fail('Expected exception');
116 } catch (\coding_exception $e) {
117 $this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters ' .
118 'to initialise category_controller - unknown itemid', $e->getMessage());
119 $this->assertEquals(\coding_exception::class, get_class($e));
122 $handler = \core_course\customfield\course_handler::create();
123 // Missing required elements.
124 try {
125 category_controller::create(0, (object)['component' => 'x', 'area' => 'course', 'itemid' => 0], $handler);
126 $this->fail('Expected exception');
127 } catch (\coding_exception $e) {
128 $this->assertEquals('Coding error detected, it must be fixed by a programmer: Component of the handler ' .
129 'does not match the one from the record', $e->getMessage());
130 $this->assertEquals(\coding_exception::class, get_class($e));
133 try {
134 category_controller::create(0, (object)['component' => 'core_course', 'area' => 'x', 'itemid' => 0], $handler);
135 $this->fail('Expected exception');
136 } catch (\coding_exception $e) {
137 $this->assertEquals('Coding error detected, it must be fixed by a programmer: Area of the handler ' .
138 'does not match the one from the record', $e->getMessage());
139 $this->assertEquals(\coding_exception::class, get_class($e));
142 try {
143 category_controller::create(0, (object)['component' => 'core_course', 'area' => 'course', 'itemid' => 1], $handler);
144 $this->fail('Expected exception');
145 } catch (\coding_exception $e) {
146 $this->assertEquals('Coding error detected, it must be fixed by a programmer: Itemid of the ' .
147 'handler does not match the one from the record', $e->getMessage());
148 $this->assertEquals(\coding_exception::class, get_class($e));
151 try {
152 $user = $this->getDataGenerator()->create_user();
153 category_controller::create(0, (object)['component' => 'core_course', 'area' => 'course', 'itemid' => 0,
154 'contextid' => \context_user::instance($user->id)->id], $handler);
155 $this->fail('Expected exception');
156 } catch (\coding_exception $e) {
157 $this->assertEquals('Coding error detected, it must be fixed by a programmer: Context of the ' .
158 'handler does not match the one from the record', $e->getMessage());
159 $this->assertEquals(\coding_exception::class, get_class($e));
164 * Tests for behaviour of:
165 * \core_customfield\category_controller::save()
166 * \core_customfield\category_controller::get()
168 public function test_create_category() {
169 $this->resetAfterTest();
171 // Create the category.
172 $lpg = $this->get_generator();
173 $categorydata = new \stdClass();
174 $categorydata->name = 'Category1';
175 $categorydata->component = 'core_course';
176 $categorydata->area = 'course';
177 $categorydata->itemid = 0;
178 $categorydata->contextid = \context_system::instance()->id;
179 $category = category_controller::create(0, $categorydata);
180 $category->save();
181 $this->assertNotEmpty($category->get('id'));
183 // Confirm record exists.
184 $this->assertTrue(\core_customfield\category::record_exists($category->get('id')));
186 // Confirm that base data was inserted correctly.
187 $category = category_controller::create($category->get('id'));
188 $this->assertSame($category->get('name'), $categorydata->name);
189 $this->assertSame($category->get('component'), $categorydata->component);
190 $this->assertSame($category->get('area'), $categorydata->area);
191 $this->assertSame((int)$category->get('itemid'), $categorydata->itemid);
195 * Tests for \core_customfield\category_controller::set() behaviour.
197 public function test_rename_category() {
198 $this->resetAfterTest();
200 // Create the category.
201 $params = ['component' => 'core_course', 'area' => 'course', 'itemid' => 0, 'name' => 'Cat1',
202 'contextid' => \context_system::instance()->id];
203 $c1 = category_controller::create(0, (object)$params);
204 $c1->save();
205 $this->assertNotEmpty($c1->get('id'));
207 // Checking new name are correct updated.
208 $category = category_controller::create($c1->get('id'));
209 $category->set('name', 'Cat2');
210 $this->assertSame('Cat2', $category->get('name'));
212 // Checking new name are correct updated after save.
213 $category->save();
215 $category = category_controller::create($c1->get('id'));
216 $this->assertSame('Cat2', $category->get('name'));
220 * Tests for \core_customfield\category_controller::delete() behaviour.
222 public function test_delete_category() {
223 $this->resetAfterTest();
225 // Create the category.
226 $lpg = $this->get_generator();
227 $category0 = $lpg->create_category();
228 $id0 = $category0->get('id');
230 $category1 = $lpg->create_category();
231 $id1 = $category1->get('id');
233 $category2 = $lpg->create_category();
234 $id2 = $category2->get('id');
236 // Confirm that exist in the database.
237 $this->assertTrue(\core_customfield\category::record_exists($id0));
239 // Delete and confirm that is deleted.
240 $category0->delete();
241 $this->assertFalse(\core_customfield\category::record_exists($id0));
243 // Confirm correct order after delete.
244 // Check order after re-fetch.
245 $category1 = category_controller::create($id1);
246 $category2 = category_controller::create($id2);
248 $this->assertSame((int) $category1->get('sortorder'), 1);
249 $this->assertSame((int) $category2->get('sortorder'), 2);