Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [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  /**
  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'],
  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  
  90          $data = array_reduce(array_keys($data), function ($fields, $fieldname) use ($data, $database) {
  91              global $DB;
  92  
  93              $field = $DB->get_record('data_fields', ['name' => $fieldname, 'dataid' => $database->id], 'id', MUST_EXIST);
  94  
  95              $fields[$field->id] = $data[$fieldname];
  96  
  97              return $fields;
  98          }, []);
  99  
 100          $this->get_data_generator()->create_entry($database, $data, 0, [], null, $userid);
 101      }
 102  
 103      /**
 104       * Add a field.
 105       *
 106       * @param array $data Field data.
 107       */
 108      public function process_field(array $data): void {
 109          global $DB;
 110  
 111          $database = $DB->get_record('data', ['id' => $data['databaseid']], '*', MUST_EXIST);
 112  
 113          unset($data['databaseid']);
 114  
 115          $this->get_data_generator()->create_field((object) $data, $database);
 116      }
 117  
 118      /**
 119       * Add a template.
 120       *
 121       * @param array $data Template data.
 122       */
 123      public function process_template(array $data): void {
 124          global $DB;
 125  
 126          $database = $DB->get_record('data', ['id' => $data['databaseid']], '*', MUST_EXIST);
 127  
 128          if (empty($data['content'])) {
 129              data_generate_default_template($database, $data['name']);
 130          } else {
 131              $newdata = new stdClass();
 132              $newdata->id = $database->id;
 133              $newdata->{$data['name']} = $data['content'];
 134              $DB->update_record('data', $newdata);
 135          }
 136      }
 137  
 138      /**
 139       * Saves a preset.
 140       *
 141       * @param array $data Preset data.
 142       */
 143      protected function process_preset(array $data): void {
 144          global $DB;
 145  
 146          $instance = $DB->get_record('data', ['id' => $data['databaseid']], '*', MUST_EXIST);
 147  
 148          $this->get_data_generator()->create_preset($instance, (object) $data);
 149      }
 150  
 151      /**
 152       * Get the module data generator.
 153       *
 154       * @return mod_data_generator Database data generator.
 155       */
 156      protected function get_data_generator(): mod_data_generator {
 157          return $this->componentdatagenerator;
 158      }
 159  
 160  }