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  declare(strict_types=1);
  18  
  19  /**
  20   * Badges test generator for Behat
  21   *
  22   * @package     core_badges
  23   * @copyright   2023 Paul Holden <paulh@moodle.com>
  24   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  class behat_core_badges_generator extends behat_generator_base {
  27  
  28      /**
  29       * Get a list of the entities that can be created for this component
  30       *
  31       * @return array[]
  32       */
  33      protected function get_creatable_entities(): array {
  34          return [
  35              'Badges' => [
  36                  'singular' => 'Badge',
  37                  'datagenerator' => 'badge',
  38                  'required' => [
  39                      'name',
  40                  ],
  41                  'switchids' => [
  42                      'course' => 'courseid',
  43                  ],
  44              ],
  45              'Criterias' => [
  46                  'singular' => 'Criteria',
  47                  'datagenerator' => 'criteria',
  48                  'required' => [
  49                      'badge',
  50                      'role',
  51                  ],
  52                  'switchids' => [
  53                      'badge' => 'badgeid',
  54                      'role' => 'roleid',
  55                  ],
  56              ],
  57              'Issued badges' => [
  58                  'singular' => 'Issued badge',
  59                  'datagenerator' => 'issued_badge',
  60                  'required' => [
  61                      'badge',
  62                      'user',
  63                  ],
  64                  'switchids' => [
  65                      'badge' => 'badgeid',
  66                      'user' => 'userid',
  67                  ],
  68              ],
  69          ];
  70      }
  71  
  72      /**
  73       * Look up badge ID from given name
  74       *
  75       * @param string $name
  76       * @return int
  77       */
  78      protected function get_badge_id(string $name): int {
  79          global $DB;
  80  
  81          return (int) $DB->get_field('badge', 'id', ['name' => $name], MUST_EXIST);
  82      }
  83  
  84      /**
  85       * Pre-process badge entity
  86       *
  87       * @param array $badge
  88       * @return array
  89       */
  90      protected function preprocess_badge(array $badge): array {
  91          global $CFG;
  92  
  93          require_once("{$CFG->libdir}/badgeslib.php");
  94  
  95          // Allow text status' that correspond to badge constants.
  96          if (array_key_exists('status', $badge) && !is_numeric($badge['status'])) {
  97              $badge['status'] = constant('BADGE_STATUS_' . strtoupper($badge['status']));
  98          }
  99  
 100          return $badge;
 101      }
 102  }