|
| 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 | +use Behat\Gherkin\Node\TableNode; |
| 18 | +require_once(__DIR__ . '/../../../../lib/behat/behat_base.php'); |
| 19 | + |
| 20 | +/** |
| 21 | + * Defines the behat steps for the block_lifecycle plugin. |
| 22 | + * |
| 23 | + * @package block_lifecycle |
| 24 | + * @copyright 2024 onwards University College London {@link https://www.ucl.ac.uk/} |
| 25 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 26 | + * @author Alex Yeung <k.yeung@ucl.ac.uk> |
| 27 | + */ |
| 28 | +class behat_lifecycle extends behat_base { |
| 29 | + /** |
| 30 | + * Create custom field. |
| 31 | + * |
| 32 | + * @param TableNode $table |
| 33 | + * @throws \dml_exception |
| 34 | + * |
| 35 | + * @Given /^the following custom field exists for lifecycle block:$/ |
| 36 | + */ |
| 37 | + public function the_following_custom_field_exists_for_lifecycle_block(TableNode $table): void { |
| 38 | + global $DB; |
| 39 | + |
| 40 | + $data = $table->getRowsHash(); |
| 41 | + |
| 42 | + // Create a new custom field category if it doesn't exist. |
| 43 | + $category = $DB->get_record( |
| 44 | + 'customfield_category', |
| 45 | + ['name' => $data['category'], |
| 46 | + 'component' => 'core_course', |
| 47 | + 'area' => 'course']); |
| 48 | + |
| 49 | + if (!$category) { |
| 50 | + $category = (object)[ |
| 51 | + 'name' => $data['category'], |
| 52 | + 'component' => 'core_course', |
| 53 | + 'area' => 'course', |
| 54 | + 'sortorder' => 1, |
| 55 | + 'timecreated' => time(), |
| 56 | + 'timemodified' => time(), |
| 57 | + ]; |
| 58 | + $category->id = $DB->insert_record( |
| 59 | + 'customfield_category', |
| 60 | + $category |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + // Check if the field already exists. |
| 65 | + $fieldexists = $DB->record_exists('customfield_field', ['shortname' => $data['shortname'], 'categoryid' => $category->id]); |
| 66 | + |
| 67 | + // Create the custom field if not exists. |
| 68 | + if (!$fieldexists) { |
| 69 | + $field = (object)[ |
| 70 | + 'shortname' => $data['shortname'], |
| 71 | + 'name' => $data['name'], |
| 72 | + 'type' => $data['type'], |
| 73 | + 'categoryid' => $category->id, |
| 74 | + 'sortorder' => 0, |
| 75 | + 'configdata' => json_encode([ |
| 76 | + "required" => 0, |
| 77 | + "uniquevalues" => 0, |
| 78 | + "maxlength" => 4, |
| 79 | + "defaultvalue" => "", |
| 80 | + "ispassword" => 0, |
| 81 | + "displaysize" => 4, |
| 82 | + "locked" => 1, |
| 83 | + "visibility" => 0, |
| 84 | + ]), |
| 85 | + 'timecreated' => time(), |
| 86 | + 'timemodified' => time(), |
| 87 | + ]; |
| 88 | + $DB->insert_record('customfield_field', $field); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments