Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]
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_textarea 19 * 20 * @package customfield_textarea 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_textarea\field_controller; 28 use customfield_textarea\data_controller; 29 30 /** 31 * Functional test for customfield_textarea 32 * 33 * @package customfield_textarea 34 * @copyright 2019 Marina Glancy 35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 36 */ 37 class customfield_textarea_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(): void { 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' => 'textarea']); 58 $this->cfields[2] = $this->get_generator()->create_field( 59 ['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield2', 'type' => 'textarea', 60 'configdata' => ['required' => 1]]); 61 $this->cfields[3] = $this->get_generator()->create_field( 62 ['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield3', 'type' => 'textarea', 63 'configdata' => ['defaultvalue' => 'Value3', 'defaultvalueformat' => FORMAT_MOODLE]]); 64 65 $this->courses[1] = $this->getDataGenerator()->create_course(); 66 $this->courses[2] = $this->getDataGenerator()->create_course(); 67 $this->courses[3] = $this->getDataGenerator()->create_course(); 68 69 $this->cfdata[1] = $this->get_generator()->add_instance_data($this->cfields[1], $this->courses[1]->id, 70 ['text' => 'Value1', 'format' => FORMAT_MOODLE]); 71 $this->cfdata[2] = $this->get_generator()->add_instance_data($this->cfields[1], $this->courses[2]->id, 72 ['text' => 'Value2', 'format' => FORMAT_MOODLE]); 73 74 $this->setUser($this->getDataGenerator()->create_user()); 75 } 76 77 /** 78 * Get generator 79 * @return core_customfield_generator 80 */ 81 protected function get_generator() : core_customfield_generator { 82 return $this->getDataGenerator()->get_plugin_generator('core_customfield'); 83 } 84 85 /** 86 * Test for initialising field and data controllers 87 */ 88 public function test_initialise() { 89 $f = \core_customfield\field_controller::create($this->cfields[1]->get('id')); 90 $this->assertTrue($f instanceof field_controller); 91 92 $f = \core_customfield\field_controller::create(0, (object)['type' => 'textarea'], $this->cfcat); 93 $this->assertTrue($f instanceof field_controller); 94 95 $d = \core_customfield\data_controller::create($this->cfdata[1]->get('id')); 96 $this->assertTrue($d instanceof data_controller); 97 98 $d = \core_customfield\data_controller::create(0, null, $this->cfields[1]); 99 $this->assertTrue($d instanceof data_controller); 100 } 101 102 /** 103 * Test for configuration form functions 104 * 105 * Create a configuration form and submit it with the same values as in the field 106 */ 107 public function test_config_form() { 108 $submitdata = (array)$this->cfields[3]->to_record(); 109 $submitdata['configdata'] = $this->cfields[3]->get('configdata'); 110 111 \core_customfield\field_config_form::mock_submit($submitdata, []); 112 $handler = $this->cfcat->get_handler(); 113 $form = $handler->get_field_config_form($this->cfields[3]); 114 $this->assertTrue($form->is_validated()); 115 $data = $form->get_data(); 116 $handler->save_field_configuration($this->cfields[3], $data); 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_editor'] = ['text' => 'Some text', 'format' => FORMAT_HTML]; 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_editor); 144 $this->assertNotEmpty($data->customfield_myfield2_editor); 145 $handler->instance_form_save($data); 146 } 147 148 /** 149 * Test that instance form save empties the field content for blank values 150 */ 151 public function test_instance_form_save_clear(): void { 152 global $CFG; 153 154 require_once("{$CFG->dirroot}/customfield/tests/fixtures/test_instance_form.php"); 155 156 $this->setAdminUser(); 157 158 $handler = $this->cfcat->get_handler(); 159 160 // Set our custom field to a known value. 161 $submitdata = (array) $this->courses[1] + [ 162 'customfield_myfield1_editor' => ['text' => 'I can see it in your eyes', 'format' => FORMAT_HTML], 163 'customfield_myfield2_editor' => ['text' => 'I can see it in your smile', 'format' => FORMAT_HTML], 164 ]; 165 166 core_customfield_test_instance_form::mock_submit($submitdata, []); 167 $form = new core_customfield_test_instance_form('post', ['handler' => $handler, 'instance' => $this->courses[1]]); 168 $handler->instance_form_save($form->get_data()); 169 170 $this->assertEquals($submitdata['customfield_myfield1_editor']['text'], 171 core_customfield\data_controller::create($this->cfdata[1]->get('id'))->export_value()); 172 173 // Now empty our non-required field. 174 $submitdata['customfield_myfield1_editor']['text'] = ''; 175 176 core_customfield_test_instance_form::mock_submit($submitdata, []); 177 $form = new core_customfield_test_instance_form('post', ['handler' => $handler, 'instance' => $this->courses[1]]); 178 $handler->instance_form_save($form->get_data()); 179 180 $this->assertEmpty(core_customfield\data_controller::create($this->cfdata[1]->get('id'))->export_value()); 181 } 182 183 /** 184 * Test for data_controller::get_value and export_value 185 */ 186 public function test_get_export_value() { 187 $this->assertEquals('Value1', $this->cfdata[1]->get_value()); 188 $this->assertEquals('<div class="text_to_html">Value1</div>', $this->cfdata[1]->export_value()); 189 190 // Field without data but with a default value. 191 $d = core_customfield\data_controller::create(0, null, $this->cfields[3]); 192 $this->assertEquals('Value3', $d->get_value()); 193 $this->assertEquals('<div class="text_to_html">Value3</div>', $d->export_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