See Release Notes
Long Term Support Release
Differences Between: [Versions 401 and 402] [Versions 401 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 namespace mod_data\external; 18 19 defined('MOODLE_INTERNAL') || die(); 20 21 global $CFG; 22 require_once($CFG->dirroot . '/webservice/tests/helpers.php'); 23 24 use external_api; 25 use mod_data\manager; 26 27 /** 28 * External function tests class for get_mapping_information. 29 * 30 * @package mod_data 31 * @category external 32 * @copyright 2022 Amaia Anabitarte <amaia@moodle.com> 33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 34 * @coversDefaultClass \mod_data\external\get_mapping_information 35 */ 36 class get_mapping_information_test extends \advanced_testcase { 37 38 /** 39 * Data provider for test_get_mapping_information(). 40 * 41 * @return array[] 42 */ 43 public function get_mapping_information_provider(): array { 44 // Image gallery preset is: ['title' => 'text', 'description' => 'textarea', 'image' => 'picture']; 45 46 $titlefield = new \stdClass(); 47 $titlefield->name = 'title'; 48 $titlefield->type = 'text'; 49 50 $descfield = new \stdClass(); 51 $descfield->name = 'description'; 52 $descfield->type = 'textarea'; 53 54 $imagefield = new \stdClass(); 55 $imagefield->name = 'image'; 56 $imagefield->type = 'picture'; 57 58 $difffield = new \stdClass(); 59 $difffield->name = 'title'; 60 $difffield->type = 'textarea'; 61 62 $newfield = new \stdClass(); 63 $newfield->name = 'number'; 64 $newfield->type = 'number'; 65 66 return [ 67 'Empty database / Empty importer' => [ 68 'currentfields' => [], 69 'newfields' => [], 70 'pluginname' => '', 71 'fieldstocreate' => '', 72 'fieldstoremove' => '', 73 ], 74 'Empty database / Importer with fields' => [ 75 'currentfields' => [], 76 'newfields' => [$imagefield, $titlefield, $descfield], 77 'pluginname' => 'imagegallery', 78 'fieldstocreate' => 'image, title, description', 79 'fieldstoremove' => '', 80 ], 81 'Database with fields / Empty importer' => [ 82 'currentfields' => [$imagefield, $titlefield, $descfield], 83 'newfields' => [], 84 'pluginname' => '', 85 'fieldstocreate' => '', 86 'fieldstoremove' => 'image, title, description', 87 ], 88 'Same fields' => [ 89 'currentfields' => [$imagefield, $titlefield, $descfield], 90 'newfields' => [$imagefield, $titlefield, $descfield], 91 'pluginname' => 'imagegallery', 92 'fieldstocreate' => '', 93 'fieldstoremove' => '', 94 ], 95 'Fields to create' => [ 96 'currentfields' => [$titlefield, $descfield], 97 'newfields' => [$imagefield, $titlefield, $descfield], 98 'pluginname' => 'imagegallery', 99 'fieldstocreate' => 'image', 100 'fieldstoremove' => '', 101 ], 102 'Fields to remove' => [ 103 'currentfields' => [$imagefield, $titlefield, $descfield, $difffield], 104 'newfields' => [$imagefield, $titlefield, $descfield], 105 'pluginname' => 'imagegallery', 106 'fieldstocreate' => '', 107 'fieldstoremove' => 'title', 108 ], 109 'Fields to update' => [ 110 'currentfields' => [$imagefield, $difffield, $descfield], 111 'newfields' => [$imagefield, $titlefield, $descfield], 112 'pluginname' => 'imagegallery', 113 'fieldstocreate' => 'title', 114 'fieldstoremove' => 'title', 115 ], 116 'Fields to create, remove and update' => [ 117 'currentfields' => [$titlefield, $descfield, $imagefield, $difffield], 118 'newfields' => [$titlefield, $descfield, $newfield], 119 'pluginname' => '', 120 'fieldstocreate' => 'number', 121 'fieldstoremove' => 'image, title', 122 ], 123 ]; 124 } 125 126 /** 127 * Test for get_mapping_information method. 128 * 129 * @dataProvider get_mapping_information_provider 130 * @covers ::execute 131 * 132 * @param array $currentfields Fields of the current activity. 133 * @param array $newfields Fields to be imported. 134 * @param string $pluginname The plugin preset to be imported. 135 * @param string $fieldstocreate Expected fields on $fieldstocreate. 136 * @param string $fieldstoremove Expected fields on $fieldstoremove. 137 */ 138 public function test_get_mapping_information( 139 array $currentfields, 140 array $newfields, 141 string $pluginname, 142 string $fieldstocreate, 143 string $fieldstoremove 144 ) { 145 global $USER; 146 147 $this->resetAfterTest(); 148 $this->setAdminUser(); 149 $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_data'); 150 151 // Create a course and a database activity. 152 $course = $this->getDataGenerator()->create_course(); 153 $activity = $this->getDataGenerator()->create_module(manager::MODULE, ['course' => $course]); 154 // Add current fields to the activity. 155 foreach ($currentfields as $field) { 156 $plugingenerator->create_field($field, $activity); 157 } 158 $manager = manager::create_from_instance($activity); 159 $module = $manager->get_coursemodule(); 160 161 $presetactivity = $this->getDataGenerator()->create_module(manager::MODULE, ['course' => $course]); 162 // Add current fields to the activity. 163 foreach ($newfields as $field) { 164 $plugingenerator->create_field($field, $presetactivity); 165 } 166 167 $record = (object) [ 168 'name' => 'Testing preset name', 169 'description' => 'Testing preset description', 170 ]; 171 $saved = $plugingenerator->create_preset($presetactivity, $record); 172 173 $result = get_mapping_information::execute($module->id, $USER->id . '/' . $saved->name); 174 $result = external_api::clean_returnvalue(get_mapping_information::execute_returns(), $result); 175 176 $this->assertEquals($result['data']['fieldstocreate'], $fieldstocreate); 177 $this->assertEquals($result['data']['fieldstoremove'], $fieldstoremove); 178 179 // Create presets and importers. 180 if ($pluginname) { 181 $result = get_mapping_information::execute($module->id, '/' . $pluginname);; 182 $result = external_api::clean_returnvalue(get_mapping_information::execute_returns(), $result); 183 $this->assertEquals($result['data']['fieldstoremove'], $fieldstoremove); 184 $this->assertEquals($result['data']['fieldstocreate'], $fieldstocreate); 185 } 186 } 187 188 /** 189 * Test for get_mapping_information method for wrong presets. 190 * 191 * @covers ::execute 192 * 193 */ 194 public function test_get_mapping_information_for_wrong_preset() { 195 global $USER; 196 197 $this->resetAfterTest(); 198 $this->setAdminUser(); 199 $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_data'); 200 201 // Create a course and a database activity. 202 $course = $this->getDataGenerator()->create_course(); 203 $activity = $this->getDataGenerator()->create_module(manager::MODULE, ['course' => $course]); 204 205 $manager = manager::create_from_instance($activity); 206 $module = $manager->get_coursemodule(); 207 208 // We get warnings with empty preset name. 209 $result = get_mapping_information::execute($module->id, ''); 210 $result = external_api::clean_returnvalue(get_mapping_information::execute_returns(), $result); 211 212 $this->assertFalse(array_key_exists('data', $result)); 213 $this->assertTrue(array_key_exists('warnings', $result)); 214 215 // We get warnings with non-existing preset name. 216 $result = get_mapping_information::execute($module->id, $USER->id . '/Non-existing'); 217 $result = external_api::clean_returnvalue(get_mapping_information::execute_returns(), $result); 218 219 $this->assertFalse(array_key_exists('data', $result)); 220 $this->assertTrue(array_key_exists('warnings', $result)); 221 222 $record = (object) [ 223 'name' => 'Testing preset name', 224 'description' => 'Testing preset description', 225 ]; 226 $saved = $plugingenerator->create_preset($activity, $record); 227 228 // We get no warning with the right preset. 229 $result = get_mapping_information::execute($module->id, $USER->id . '/' . $saved->name); 230 $result = external_api::clean_returnvalue(get_mapping_information::execute_returns(), $result); 231 232 $this->assertTrue(array_key_exists('data', $result)); 233 $this->assertFalse(array_key_exists('warnings', $result)); 234 } 235 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body