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.
   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_glossary.
  19   *
  20   * @package   mod_glossary
  21   * @category  test
  22   * @copyright 2021 Noel De Martin
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  class behat_mod_glossary_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              'categories' => [
  35                  'singular' => 'category',
  36                  'datagenerator' => 'category',
  37                  'required' => ['glossary', 'name'],
  38                  'switchids' => ['glossary' => 'glossaryid'],
  39              ],
  40              'entries' => [
  41                  'singular' => 'entry',
  42                  'datagenerator' => 'entry',
  43                  'required' => ['glossary', 'concept', 'definition'],
  44                  'switchids' => ['glossary' => 'glossaryid', 'user' => 'userid'],
  45              ],
  46          ];
  47      }
  48  
  49      /**
  50       * Get the glossary id using an activity idnumber.
  51       *
  52       * @param string $idnumber
  53       * @return int The glossary id
  54       */
  55      protected function get_glossary_id(string $idnumber): int {
  56          $cm = $this->get_cm_by_activity_name('glossary', $idnumber);
  57  
  58          return $cm->instance;
  59      }
  60  
  61      /**
  62       * Add a category.
  63       *
  64       * @param array $data Category data.
  65       */
  66      public function process_category(array $data) {
  67          global $DB;
  68  
  69          $glossary = $DB->get_record('glossary', ['id' => $data['glossaryid']], '*', MUST_EXIST);
  70  
  71          unset($data['glossaryid']);
  72  
  73          $this->get_data_generator()->create_category($glossary, $data);
  74      }
  75  
  76      /**
  77       * Preprocess entry data.
  78       *
  79       * @param array $data Raw data.
  80       * @return array Processed data.
  81       */
  82      protected function preprocess_entry(array $data): array {
  83          if (isset($data['categories'])) {
  84              $categorynames = array_map('trim', explode(',', $data['categories']));
  85              $categoryids = array_map(function ($categoryname) {
  86                  global $DB;
  87  
  88                  if (!$id = $DB->get_field('glossary_categories', 'id', ['name' => $categoryname])) {
  89                      throw new Exception('The specified category with name "' . $categoryname . '" could not be found.');
  90                  }
  91  
  92                  return $id;
  93              }, $categorynames);
  94  
  95              $data['categoryids'] = $categoryids;
  96              unset($data['categories']);
  97          }
  98  
  99          return $data;
 100      }
 101  
 102      /**
 103       * Get the module data generator.
 104       *
 105       * @return mod_glossary_generator Glossary data generator.
 106       */
 107      protected function get_data_generator() {
 108          return $this->componentdatagenerator;
 109      }
 110  }