See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 310] [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 customfield_select 19 * 20 * @package customfield_select 21 * @copyright 2019 Marina Glancy 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 defined('MOODLE_INTERNAL') || die(); 26 27 use customfield_select\field_controller; 28 use customfield_select\data_controller; 29 30 /** 31 * Functional test for customfield_select 32 * 33 * @package customfield_select 34 * @copyright 2019 Marina Glancy 35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 36 */ 37 class customfield_select_plugin_testcase extends advanced_testcase { 38 39 /** @var stdClass[] */ 40 private $courses = []; 41 /** @var \core_customfield\category_controller */ 42 private $cfcat; 43 /** @var \core_customfield\field_controller[] */ 44 private $cfields; 45 /** @var \core_customfield\data_controller[] */ 46 private $cfdata; 47 48 /** 49 * Tests set up. 50 */ 51 public function setUp() { 52 $this->resetAfterTest(); 53 54 $this->cfcat = $this->get_generator()->create_category(); 55 56 $this->cfields[1] = $this->get_generator()->create_field( 57 ['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield1', 'type' => 'select', 58 'configdata' => ['options' => "a\nb\nc"]]); 59 $this->cfields[2] = $this->get_generator()->create_field( 60 ['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield2', 'type' => 'select', 61 'configdata' => ['required' => 1, 'options' => "a\nb\nc"]]); 62 $this->cfields[3] = $this->get_generator()->create_field( 63 ['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield3', 'type' => 'select', 64 'configdata' => ['defaultvalue' => 'b', 'options' => "a\nb\nc"]]); 65 66 $this->courses[1] = $this->getDataGenerator()->create_course(); 67 $this->courses[2] = $this->getDataGenerator()->create_course(); 68 $this->courses[3] = $this->getDataGenerator()->create_course(); 69 70 $this->cfdata[1] = $this->get_generator()->add_instance_data($this->cfields[1], $this->courses[1]->id, 1); 71 $this->cfdata[2] = $this->get_generator()->add_instance_data($this->cfields[1], $this->courses[2]->id, 1); 72 73 $this->setUser($this->getDataGenerator()->create_user()); 74 } 75 76 /** 77 * Get generator 78 * @return core_customfield_generator 79 */ 80 protected function get_generator() : core_customfield_generator { 81 return $this->getDataGenerator()->get_plugin_generator('core_customfield'); 82 } 83 84 /** 85 * Test for initialising field and data controllers 86 */ 87 public function test_initialise() { 88 $f = \core_customfield\field_controller::create($this->cfields[1]->get('id')); 89 $this->assertTrue($f instanceof field_controller); 90 91 $f = \core_customfield\field_controller::create(0, (object)['type' => 'select'], $this->cfcat); 92 $this->assertTrue($f instanceof field_controller); 93 94 $d = \core_customfield\data_controller::create($this->cfdata[1]->get('id')); 95 $this->assertTrue($d instanceof data_controller); 96 97 $d = \core_customfield\data_controller::create(0, null, $this->cfields[1]); 98 $this->assertTrue($d instanceof data_controller); 99 } 100 101 /** 102 * Test for configuration form functions 103 * 104 * Create a configuration form and submit it with the same values as in the field 105 */ 106 public function test_config_form() { 107 $submitdata = (array)$this->cfields[1]->to_record(); 108 $submitdata['configdata'] = $this->cfields[1]->get('configdata'); 109 110 \core_customfield\field_config_form::mock_submit($submitdata, []); 111 $handler = $this->cfcat->get_handler(); 112 $form = $handler->get_field_config_form($this->cfields[1]); 113 $this->assertTrue($form->is_validated()); 114 $data = $form->get_data(); 115 $handler->save_field_configuration($this->cfields[1], $data); 116 } 117 118 /** 119 * Test for instance form functions 120 */ 121 public function test_instance_form() { 122 global $CFG; 123 require_once($CFG->dirroot . '/customfield/tests/fixtures/test_instance_form.php'); 124 $this->setAdminUser(); 125 $handler = $this->cfcat->get_handler(); 126 127 // First try to submit without required field. 128 $submitdata = (array)$this->courses[1]; 129 core_customfield_test_instance_form::mock_submit($submitdata, []); 130 $form = new core_customfield_test_instance_form('POST', 131 ['handler' => $handler, 'instance' => $this->courses[1]]); 132 $this->assertFalse($form->is_validated()); 133 134 // Now with required field. 135 $submitdata['customfield_myfield2'] = 1; 136 core_customfield_test_instance_form::mock_submit($submitdata, []); 137 $form = new core_customfield_test_instance_form('POST', 138 ['handler' => $handler, 'instance' => $this->courses[1]]); 139 $this->assertTrue($form->is_validated()); 140 141 $data = $form->get_data(); 142 $this->assertNotEmpty($data->customfield_myfield1); 143 $this->assertNotEmpty($data->customfield_myfield2); 144 $handler->instance_form_save($data); 145 } 146 147 /** 148 * Test for data_controller::get_value and export_value 149 */ 150 public function test_get_export_value() { 151 $this->assertEquals(1, $this->cfdata[1]->get_value()); 152 $this->assertEquals('a', $this->cfdata[1]->export_value()); 153 154 // Field without data but with a default value. 155 $d = core_customfield\data_controller::create(0, null, $this->cfields[3]); 156 $this->assertEquals(2, $d->get_value()); 157 $this->assertEquals('b', $d->export_value()); 158 } 159 160 /** 161 * Data provider for {@see test_parse_value} 162 * 163 * @return array 164 */ 165 public function parse_value_provider() : array { 166 return [ 167 ['Red', 1], 168 ['Blue', 2], 169 ['Green', 3], 170 ['Mauve', 0], 171 ]; 172 } 173 174 /** 175 * Test field parse_value method 176 * 177 * @param string $value 178 * @param int $expected 179 * @return void 180 * 181 * @dataProvider parse_value_provider 182 */ 183 public function test_parse_value(string $value, int $expected) { 184 $field = $this->get_generator()->create_field([ 185 'categoryid' => $this->cfcat->get('id'), 186 'type' => 'select', 187 'shortname' => 'myselect', 188 'configdata' => [ 189 'options' => "Red\nBlue\nGreen", 190 ], 191 ]); 192 193 $this->assertSame($expected, $field->parse_value($value)); 194 } 195 196 /** 197 * Deleting fields and data 198 */ 199 public function test_delete() { 200 $this->cfcat->get_handler()->delete_all(); 201 } 202 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body