Differences Between: [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]
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 * Behat data generator for mod_data. 19 * 20 * @package mod_data 21 * @category test 22 * @copyright 2022 Noel De Martin 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 class behat_mod_data_generator extends behat_generator_base { 26 27 /** 28 * Get a list of the entities that Behat can create using the generator step. 29 * 30 * @return array 31 */ 32 protected function get_creatable_entities(): array { 33 return [ 34 'entries' => [ 35 'singular' => 'entry', 36 'datagenerator' => 'entry', 37 'required' => ['database'], 38 'switchids' => ['database' => 'databaseid', 'user' => 'userid', 'group' => 'groupid'], 39 ], 40 'fields' => [ 41 'singular' => 'field', 42 'datagenerator' => 'field', 43 'required' => ['database', 'type', 'name'], 44 'switchids' => ['database' => 'databaseid'], 45 ], 46 'templates' => [ 47 'singular' => 'template', 48 'datagenerator' => 'template', 49 'required' => ['database', 'name'], 50 'switchids' => ['database' => 'databaseid'], 51 ], 52 'presets' => [ 53 'singular' => 'preset', 54 'datagenerator' => 'preset', 55 'required' => ['database', 'name'], 56 'switchids' => ['database' => 'databaseid', 'user' => 'userid'], 57 ], 58 ]; 59 } 60 61 /** 62 * Get the database id using an activity idnumber. 63 * 64 * @param string $idnumber 65 * @return int The database id 66 */ 67 protected function get_database_id(string $idnumber): int { 68 $cm = $this->get_cm_by_activity_name('data', $idnumber); 69 70 return $cm->instance; 71 } 72 73 /** 74 * Add an entry. 75 * 76 * @param array $data Entry data. 77 */ 78 public function process_entry(array $data): void { 79 global $DB; 80 81 $database = $DB->get_record('data', ['id' => $data['databaseid']], '*', MUST_EXIST); 82 83 unset($data['databaseid']); 84 $userid = 0; 85 if (array_key_exists('userid', $data)) { 86 $userid = $data['userid']; 87 unset($data['userid']); 88 } 89 if (array_key_exists('groupid', $data)) { 90 $groupid = $data['groupid']; 91 unset($data['groupid']); 92 } else { 93 $groupid = 0; 94 } 95 96 $data = array_reduce(array_keys($data), function ($fields, $fieldname) use ($data, $database) { 97 global $DB; 98 99 $field = $DB->get_record('data_fields', ['name' => $fieldname, 'dataid' => $database->id], 'id', MUST_EXIST); 100 101 $fields[$field->id] = $data[$fieldname]; 102 103 return $fields; 104 }, []); 105 106 $this->get_data_generator()->create_entry($database, $data, $groupid, [], null, $userid); 107 } 108 109 /** 110 * Add a field. 111 * 112 * @param array $data Field data. 113 */ 114 public function process_field(array $data): void { 115 global $DB; 116 117 $database = $DB->get_record('data', ['id' => $data['databaseid']], '*', MUST_EXIST); 118 119 unset($data['databaseid']); 120 121 $this->get_data_generator()->create_field((object) $data, $database); 122 } 123 124 /** 125 * Add a template. 126 * 127 * @param array $data Template data. 128 */ 129 public function process_template(array $data): void { 130 global $DB; 131 132 $database = $DB->get_record('data', ['id' => $data['databaseid']], '*', MUST_EXIST); 133 134 if (empty($data['content'])) { 135 data_generate_default_template($database, $data['name']); 136 } else { 137 $newdata = new stdClass(); 138 $newdata->id = $database->id; 139 $newdata->{$data['name']} = $data['content']; 140 $DB->update_record('data', $newdata); 141 } 142 } 143 144 /** 145 * Saves a preset. 146 * 147 * @param array $data Preset data. 148 */ 149 protected function process_preset(array $data): void { 150 global $DB; 151 152 $instance = $DB->get_record('data', ['id' => $data['databaseid']], '*', MUST_EXIST); 153 154 $this->get_data_generator()->create_preset($instance, (object) $data); 155 } 156 157 /** 158 * Get the module data generator. 159 * 160 * @return mod_data_generator Database data generator. 161 */ 162 protected function get_data_generator(): mod_data_generator { 163 return $this->componentdatagenerator; 164 } 165 166 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body