See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]
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. 13 // 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/>. 16 17 /** 18 * Tests for class \core_customfield\category_controller. 19 * 20 * @package core_customfield 21 * @category test 22 * @copyright 2018 Toni Barbera <toni@moodle.com> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 use \core_customfield\category_controller; 29 use \core_customfield\field_controller; 30 31 /** 32 * Functional test for class \core_customfield\category_controller. 33 * @package core_customfield 34 * @copyright 2018 Toni Barbera <toni@moodle.com> 35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 36 */ 37 class core_customfield_category_controller_testcase extends advanced_testcase { 38 39 /** 40 * Get generator. 41 * 42 * @return core_customfield_generator 43 */ 44 protected function get_generator(): core_customfield_generator { 45 return $this->getDataGenerator()->get_plugin_generator('core_customfield'); 46 } 47 48 /** 49 * Test for the field_controller::__construct function. 50 */ 51 public function test_constructor() { 52 $this->resetAfterTest(); 53 54 $c = category_controller::create(0, (object)['component' => 'core_course', 'area' => 'course', 'itemid' => 0]); 55 $handler = $c->get_handler(); 56 $this->assertTrue($c instanceof category_controller); 57 58 $cat = $this->get_generator()->create_category(); 59 $c = category_controller::create($cat->get('id')); 60 $this->assertTrue($c instanceof category_controller); 61 62 $c = category_controller::create($cat->get('id'), null, $handler); 63 $this->assertTrue($c instanceof category_controller); 64 65 $c = category_controller::create(0, $cat->to_record()); 66 $this->assertTrue($c instanceof category_controller); 67 68 $c = category_controller::create(0, $cat->to_record(), $handler); 69 $this->assertTrue($c instanceof category_controller); 70 } 71 72 /** 73 * Test for function \core_customfield\field_controller::create() in case of wrong parameters 74 */ 75 public function test_constructor_errors() { 76 global $DB; 77 $this->resetAfterTest(); 78 79 $cat = $this->get_generator()->create_category(); 80 $catrecord = $cat->to_record(); 81 82 // Both id and record give warning. 83 $c = category_controller::create($catrecord->id, $catrecord); 84 $debugging = $this->getDebuggingMessages(); 85 $this->assertEquals(1, count($debugging)); 86 $this->assertEquals('Too many parameters, either id need to be specified or a record, but not both.', 87 $debugging[0]->message); 88 $this->resetDebugging(); 89 $this->assertTrue($c instanceof category_controller); 90 91 // Retrieve non-existing data. 92 try { 93 category_controller::create($catrecord->id + 1); 94 $this->fail('Expected exception'); 95 } catch (moodle_exception $e) { 96 $this->assertEquals('Category not found', $e->getMessage()); 97 $this->assertEquals(moodle_exception::class, get_class($e)); 98 } 99 100 // Missing required elements. 101 try { 102 category_controller::create(0, (object)['area' => 'course', 'itemid' => 0]); 103 $this->fail('Expected exception'); 104 } catch (coding_exception $e) { 105 $this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters ' . 106 'to initialise category_controller - unknown component', $e->getMessage()); 107 $this->assertEquals(coding_exception::class, get_class($e)); 108 } 109 110 // Missing required elements. 111 try { 112 category_controller::create(0, (object)['component' => 'core_course', 'itemid' => 0]); 113 $this->fail('Expected exception'); 114 } catch (coding_exception $e) { 115 $this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters ' . 116 'to initialise category_controller - unknown area', $e->getMessage()); 117 $this->assertEquals(coding_exception::class, get_class($e)); 118 } 119 120 // Missing required elements. 121 try { 122 category_controller::create(0, (object)['component' => 'core_course', 'area' => 'course']); 123 $this->fail('Expected exception'); 124 } catch (coding_exception $e) { 125 $this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters ' . 126 'to initialise category_controller - unknown itemid', $e->getMessage()); 127 $this->assertEquals(coding_exception::class, get_class($e)); 128 } 129 130 $handler = \core_course\customfield\course_handler::create(); 131 // Missing required elements. 132 try { 133 category_controller::create(0, (object)['component' => 'x', 'area' => 'course', 'itemid' => 0], $handler); 134 $this->fail('Expected exception'); 135 } catch (coding_exception $e) { 136 $this->assertEquals('Coding error detected, it must be fixed by a programmer: Component of the handler ' . 137 'does not match the one from the record', $e->getMessage()); 138 $this->assertEquals(coding_exception::class, get_class($e)); 139 } 140 141 try { 142 category_controller::create(0, (object)['component' => 'core_course', 'area' => 'x', 'itemid' => 0], $handler); 143 $this->fail('Expected exception'); 144 } catch (coding_exception $e) { 145 $this->assertEquals('Coding error detected, it must be fixed by a programmer: Area of the handler ' . 146 'does not match the one from the record', $e->getMessage()); 147 $this->assertEquals(coding_exception::class, get_class($e)); 148 } 149 150 try { 151 category_controller::create(0, (object)['component' => 'core_course', 'area' => 'course', 'itemid' => 1], $handler); 152 $this->fail('Expected exception'); 153 } catch (coding_exception $e) { 154 $this->assertEquals('Coding error detected, it must be fixed by a programmer: Itemid of the ' . 155 'handler does not match the one from the record', $e->getMessage()); 156 $this->assertEquals(coding_exception::class, get_class($e)); 157 } 158 159 try { 160 $user = $this->getDataGenerator()->create_user(); 161 category_controller::create(0, (object)['component' => 'core_course', 'area' => 'course', 'itemid' => 0, 162 'contextid' => context_user::instance($user->id)->id], $handler); 163 $this->fail('Expected exception'); 164 } catch (coding_exception $e) { 165 $this->assertEquals('Coding error detected, it must be fixed by a programmer: Context of the ' . 166 'handler does not match the one from the record', $e->getMessage()); 167 $this->assertEquals(coding_exception::class, get_class($e)); 168 } 169 } 170 171 /** 172 * Tests for behaviour of: 173 * \core_customfield\category_controller::save() 174 * \core_customfield\category_controller::get() 175 */ 176 public function test_create_category() { 177 $this->resetAfterTest(); 178 179 // Create the category. 180 $lpg = $this->get_generator(); 181 $categorydata = new stdClass(); 182 $categorydata->name = 'Category1'; 183 $categorydata->component = 'core_course'; 184 $categorydata->area = 'course'; 185 $categorydata->itemid = 0; 186 $categorydata->contextid = context_system::instance()->id; 187 $category = category_controller::create(0, $categorydata); 188 $category->save(); 189 $this->assertNotEmpty($category->get('id')); 190 191 // Confirm record exists. 192 $this->assertTrue(\core_customfield\category::record_exists($category->get('id'))); 193 194 // Confirm that base data was inserted correctly. 195 $category = category_controller::create($category->get('id')); 196 $this->assertSame($category->get('name'), $categorydata->name); 197 $this->assertSame($category->get('component'), $categorydata->component); 198 $this->assertSame($category->get('area'), $categorydata->area); 199 $this->assertSame((int)$category->get('itemid'), $categorydata->itemid); 200 } 201 202 /** 203 * Tests for \core_customfield\category_controller::set() behaviour. 204 */ 205 public function test_rename_category() { 206 $this->resetAfterTest(); 207 208 // Create the category. 209 $params = ['component' => 'core_course', 'area' => 'course', 'itemid' => 0, 'name' => 'Cat1', 210 'contextid' => context_system::instance()->id]; 211 $c1 = category_controller::create(0, (object)$params); 212 $c1->save(); 213 $this->assertNotEmpty($c1->get('id')); 214 215 // Checking new name are correct updated. 216 $category = category_controller::create($c1->get('id')); 217 $category->set('name', 'Cat2'); 218 $this->assertSame('Cat2', $category->get('name')); 219 220 // Checking new name are correct updated after save. 221 $category->save(); 222 223 $category = category_controller::create($c1->get('id')); 224 $this->assertSame('Cat2', $category->get('name')); 225 } 226 227 /** 228 * Tests for \core_customfield\category_controller::delete() behaviour. 229 */ 230 public function test_delete_category() { 231 $this->resetAfterTest(); 232 233 // Create the category. 234 $lpg = $this->get_generator(); 235 $category0 = $lpg->create_category(); 236 $id0 = $category0->get('id'); 237 238 $category1 = $lpg->create_category(); 239 $id1 = $category1->get('id'); 240 241 $category2 = $lpg->create_category(); 242 $id2 = $category2->get('id'); 243 244 // Confirm that exist in the database. 245 $this->assertTrue(\core_customfield\category::record_exists($id0)); 246 247 // Delete and confirm that is deleted. 248 $category0->delete(); 249 $this->assertFalse(\core_customfield\category::record_exists($id0)); 250 251 // Confirm correct order after delete. 252 // Check order after re-fetch. 253 $category1 = category_controller::create($id1); 254 $category2 = category_controller::create($id2); 255 256 $this->assertSame((int) $category1->get('sortorder'), 1); 257 $this->assertSame((int) $category2->get('sortorder'), 2); 258 } 259 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body