Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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'],
  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          ];
  53      }
  54  
  55      /**
  56       * Get the database id using an activity idnumber.
  57       *
  58       * @param string $idnumber
  59       * @return int The database id
  60       */
  61      protected function get_database_id(string $idnumber): int {
  62          $cm = $this->get_cm_by_activity_name('data', $idnumber);
  63  
  64          return $cm->instance;
  65      }
  66  
  67      /**
  68       * Add an entry.
  69       *
  70       * @param array $data Entry data.
  71       */
  72      public function process_entry(array $data): void {
  73          global $DB;
  74  
  75          $database = $DB->get_record('data', ['id' => $data['databaseid']], '*', MUST_EXIST);
  76  
  77          unset($data['databaseid']);
  78  
  79          $data = array_reduce(array_keys($data), function ($fields, $fieldname) use ($data, $database) {
  80              global $DB;
  81  
  82              $field = $DB->get_record('data_fields', ['name' => $fieldname, 'dataid' => $database->id], 'id', MUST_EXIST);
  83  
  84              $fields[$field->id] = $data[$fieldname];
  85  
  86              return $fields;
  87          }, []);
  88  
  89          $this->get_data_generator()->create_entry($database, $data);
  90      }
  91  
  92      /**
  93       * Add a field.
  94       *
  95       * @param array $data Field data.
  96       */
  97      public function process_field(array $data): void {
  98          global $DB;
  99  
 100          $database = $DB->get_record('data', ['id' => $data['databaseid']], '*', MUST_EXIST);
 101  
 102          unset($data['databaseid']);
 103  
 104          $this->get_data_generator()->create_field((object) $data, $database);
 105      }
 106  
 107      /**
 108       * Add a template.
 109       *
 110       * @param array $data Template data.
 111       */
 112      public function process_template(array $data): void {
 113          global $DB;
 114  
 115          $database = $DB->get_record('data', ['id' => $data['databaseid']], '*', MUST_EXIST);
 116  
 117          if (empty($data['content'])) {
 118              data_generate_default_template($database, $data['name']);
 119          } else {
 120              $newdata = new stdClass();
 121              $newdata->id = $database->id;
 122              $newdata->{$data['name']} = $data['content'];
 123              $DB->update_record('data', $newdata);
 124          }
 125      }
 126  
 127      /**
 128       * Get the module data generator.
 129       *
 130       * @return mod_data_generator Database data generator.
 131       */
 132      protected function get_data_generator(): mod_data_generator {
 133          return $this->componentdatagenerator;
 134      }
 135  
 136  }