Differences Between: [Versions 310 and 311] [Versions 39 and 311]
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 namespace customfield_checkbox; 18 19 use core_customfield_generator; 20 use core_customfield_test_instance_form; 21 22 /** 23 * Functional test for customfield_checkbox 24 * 25 * @package customfield_checkbox 26 * @copyright 2019 Marina Glancy 27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 28 */ 29 class plugin_test extends \advanced_testcase { 30 31 /** @var \stdClass[] */ 32 private $courses = []; 33 /** @var \core_customfield\category_controller */ 34 private $cfcat; 35 /** @var \core_customfield\field_controller[] */ 36 private $cfields; 37 /** @var \core_customfield\data_controller[] */ 38 private $cfdata; 39 40 /** 41 * Tests set up. 42 */ 43 public function setUp(): void { 44 $this->resetAfterTest(); 45 46 $this->cfcat = $this->get_generator()->create_category(); 47 48 $this->cfields[1] = $this->get_generator()->create_field( 49 ['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield1', 'type' => 'checkbox']); 50 $this->cfields[2] = $this->get_generator()->create_field( 51 ['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield2', 'type' => 'checkbox', 52 'configdata' => ['required' => 1]]); 53 $this->cfields[3] = $this->get_generator()->create_field( 54 ['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield3', 'type' => 'checkbox', 55 'configdata' => ['checkbydefault' => 1]]); 56 57 $this->courses[1] = $this->getDataGenerator()->create_course(); 58 $this->courses[2] = $this->getDataGenerator()->create_course(); 59 $this->courses[3] = $this->getDataGenerator()->create_course(); 60 61 $this->cfdata[1] = $this->get_generator()->add_instance_data($this->cfields[1], $this->courses[1]->id, 1); 62 $this->cfdata[2] = $this->get_generator()->add_instance_data($this->cfields[1], $this->courses[2]->id, 1); 63 64 $this->setUser($this->getDataGenerator()->create_user()); 65 } 66 67 /** 68 * Get generator 69 * @return core_customfield_generator 70 */ 71 protected function get_generator() : core_customfield_generator { 72 return $this->getDataGenerator()->get_plugin_generator('core_customfield'); 73 } 74 75 /** 76 * Test for initialising field and data controllers 77 */ 78 public function test_initialise() { 79 $f = \core_customfield\field_controller::create($this->cfields[1]->get('id')); 80 $this->assertTrue($f instanceof field_controller); 81 82 $f = \core_customfield\field_controller::create(0, (object)['type' => 'checkbox'], $this->cfcat); 83 $this->assertTrue($f instanceof field_controller); 84 85 $d = \core_customfield\data_controller::create($this->cfdata[1]->get('id')); 86 $this->assertTrue($d instanceof data_controller); 87 88 $d = \core_customfield\data_controller::create(0, null, $this->cfields[1]); 89 $this->assertTrue($d instanceof data_controller); 90 } 91 92 /** 93 * Test for configuration form functions 94 * 95 * Create a configuration form and submit it with the same values as in the field 96 */ 97 public function test_config_form() { 98 $this->setAdminUser(); 99 $submitdata = (array)$this->cfields[1]->to_record(); 100 $submitdata['configdata'] = $this->cfields[1]->get('configdata'); 101 102 $submitdata = \core_customfield\field_config_form::mock_ajax_submit($submitdata); 103 $form = new \core_customfield\field_config_form(null, null, 'post', '', null, true, 104 $submitdata, true); 105 $form->set_data_for_dynamic_submission(); 106 $this->assertTrue($form->is_validated()); 107 $form->process_dynamic_submission(); 108 109 // Try submitting with 'unique values' checked. 110 $submitdata['configdata']['uniquevalues'] = 1; 111 112 $submitdata = \core_customfield\field_config_form::mock_ajax_submit($submitdata); 113 $form = new \core_customfield\field_config_form(null, null, 'post', '', null, true, 114 $submitdata, true); 115 $form->set_data_for_dynamic_submission(); 116 $this->assertFalse($form->is_validated()); 117 } 118 119 /** 120 * Test for instance form functions 121 */ 122 public function test_instance_form() { 123 global $CFG; 124 require_once($CFG->dirroot . '/customfield/tests/fixtures/test_instance_form.php'); 125 $this->setAdminUser(); 126 $handler = $this->cfcat->get_handler(); 127 128 // First try to submit without required field. 129 $submitdata = (array)$this->courses[1]; 130 core_customfield_test_instance_form::mock_submit($submitdata, []); 131 $form = new core_customfield_test_instance_form('POST', 132 ['handler' => $handler, 'instance' => $this->courses[1]]); 133 $this->assertFalse($form->is_validated()); 134 135 // Now with required field. 136 $submitdata['customfield_myfield2'] = 1; 137 core_customfield_test_instance_form::mock_submit($submitdata, []); 138 $form = new core_customfield_test_instance_form('POST', 139 ['handler' => $handler, 'instance' => $this->courses[1]]); 140 $this->assertTrue($form->is_validated()); 141 142 $data = $form->get_data(); 143 $this->assertNotEmpty($data->customfield_myfield1); 144 $this->assertNotEmpty($data->customfield_myfield2); 145 $handler->instance_form_save($data); 146 } 147 148 /** 149 * Test for data_controller::get_value and export_value 150 */ 151 public function test_get_export_value() { 152 $this->assertEquals(1, $this->cfdata[1]->get_value()); 153 $this->assertEquals('Yes', $this->cfdata[1]->export_value()); 154 155 // Field without data. 156 $d = \core_customfield\data_controller::create(0, null, $this->cfields[2]); 157 $this->assertEquals(0, $d->get_value()); 158 $this->assertEquals('No', $d->export_value()); 159 160 // Field without data that is checked by default. 161 $d = \core_customfield\data_controller::create(0, null, $this->cfields[3]); 162 $this->assertEquals(1, $d->get_value()); 163 $this->assertEquals('Yes', $d->export_value()); 164 } 165 166 /** 167 * Deleting fields and data 168 */ 169 public function test_delete() { 170 $this->cfcat->get_handler()->delete_all(); 171 } 172 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body