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_text; 18 19 use core_customfield_generator; 20 use core_customfield_test_instance_form; 21 22 /** 23 * Functional test for customfield_text 24 * 25 * @package customfield_text 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' => 'text', 50 'configdata' => ['maxlength' => 30, 'displaysize' => 50]]); 51 $this->cfields[2] = $this->get_generator()->create_field( 52 ['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield2', 'type' => 'text', 53 'configdata' => ['required' => 1, 'maxlength' => 30, 'displaysize' => 50]]); 54 $this->cfields[3] = $this->get_generator()->create_field( 55 ['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield3', 'type' => 'text', 56 'configdata' => ['defaultvalue' => 'Defvalue', 'maxlength' => 30, 'displaysize' => 50]]); 57 $this->cfields[4] = $this->get_generator()->create_field( 58 ['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield4', 'type' => 'text', 59 'configdata' => ['link' => 'https://twitter.com/$$', 'maxlength' => 30, 'displaysize' => 50]]); 60 61 $this->courses[1] = $this->getDataGenerator()->create_course(); 62 $this->courses[2] = $this->getDataGenerator()->create_course(); 63 $this->courses[3] = $this->getDataGenerator()->create_course(); 64 65 $this->cfdata[1] = $this->get_generator()->add_instance_data($this->cfields[1], $this->courses[1]->id, 66 'Value1'); 67 $this->cfdata[2] = $this->get_generator()->add_instance_data($this->cfields[1], $this->courses[2]->id, 68 'Value2'); 69 70 $this->setUser($this->getDataGenerator()->create_user()); 71 } 72 73 /** 74 * Get generator 75 * @return core_customfield_generator 76 */ 77 protected function get_generator() : core_customfield_generator { 78 return $this->getDataGenerator()->get_plugin_generator('core_customfield'); 79 } 80 81 /** 82 * Test for initialising field and data controllers 83 */ 84 public function test_initialise() { 85 $f = \core_customfield\field_controller::create($this->cfields[1]->get('id')); 86 $this->assertTrue($f instanceof field_controller); 87 88 $f = \core_customfield\field_controller::create(0, (object)['type' => 'text'], $this->cfcat); 89 $this->assertTrue($f instanceof field_controller); 90 91 $d = \core_customfield\data_controller::create($this->cfdata[1]->get('id')); 92 $this->assertTrue($d instanceof data_controller); 93 94 $d = \core_customfield\data_controller::create(0, null, $this->cfields[1]); 95 $this->assertTrue($d instanceof data_controller); 96 } 97 98 /** 99 * Test for configuration form functions 100 * 101 * Create a configuration form and submit it with the same values as in the field 102 */ 103 public function test_config_form() { 104 $this->setAdminUser(); 105 $submitdata = (array)$this->cfields[1]->to_record(); 106 $submitdata['configdata'] = $this->cfields[1]->get('configdata'); 107 108 $submitdata = \core_customfield\field_config_form::mock_ajax_submit($submitdata); 109 $form = new \core_customfield\field_config_form(null, null, 'post', '', null, true, 110 $submitdata, true); 111 $form->set_data_for_dynamic_submission(); 112 $this->assertTrue($form->is_validated()); 113 $form->process_dynamic_submission(); 114 } 115 116 /** 117 * Test for instance form functions 118 */ 119 public function test_instance_form() { 120 global $CFG; 121 require_once($CFG->dirroot . '/customfield/tests/fixtures/test_instance_form.php'); 122 $this->setAdminUser(); 123 $handler = $this->cfcat->get_handler(); 124 125 // First try to submit without required field. 126 $submitdata = (array)$this->courses[1]; 127 core_customfield_test_instance_form::mock_submit($submitdata, []); 128 $form = new core_customfield_test_instance_form('POST', 129 ['handler' => $handler, 'instance' => $this->courses[1]]); 130 $this->assertFalse($form->is_validated()); 131 132 // Now with required field. 133 $submitdata['customfield_myfield2'] = 'Some text'; 134 core_customfield_test_instance_form::mock_submit($submitdata, []); 135 $form = new core_customfield_test_instance_form('POST', 136 ['handler' => $handler, 'instance' => $this->courses[1]]); 137 $this->assertTrue($form->is_validated()); 138 139 $data = $form->get_data(); 140 $this->assertNotEmpty($data->customfield_myfield1); 141 $this->assertNotEmpty($data->customfield_myfield2); 142 $handler->instance_form_save($data); 143 } 144 145 /** 146 * Test for data_controller::get_value and export_value 147 */ 148 public function test_get_export_value() { 149 $this->assertEquals('Value1', $this->cfdata[1]->get_value()); 150 $this->assertEquals('Value1', $this->cfdata[1]->export_value()); 151 152 // Field without data but with a default value. 153 $d = \core_customfield\data_controller::create(0, null, $this->cfields[3]); 154 $this->assertEquals('Defvalue', $d->get_value()); 155 $this->assertEquals('Defvalue', $d->export_value()); 156 157 // Field with a link. 158 $d = $this->get_generator()->add_instance_data($this->cfields[4], $this->courses[1]->id, 'mynickname'); 159 $this->assertEquals('mynickname', $d->get_value()); 160 $this->assertEquals('<a href="https://twitter.com/mynickname">mynickname</a>', $d->export_value()); 161 } 162 163 /** 164 * Deleting fields and data 165 */ 166 public function test_delete() { 167 $this->cfcat->get_handler()->delete_all(); 168 } 169 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body