Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [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   * Return data about an entity generator.
  19   *
  20   * @package   tool_behat
  21   * @copyright 2022 onwards Catalyst IT EU {@link https://catalyst-eu.net}
  22   * @author    Mark Johnson <mark.johnson@catalyst-eu.net>
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace tool_behat\external;
  27  
  28  use core_external\external_api;
  29  use core_external\external_function_parameters;
  30  use core_external\external_multiple_structure;
  31  use core_external\external_single_structure;
  32  use core_external\external_value;
  33  
  34  /**
  35   * External function for getting properties of entity generators.
  36   */
  37  class get_entity_generator extends external_api {
  38  
  39      /**
  40       * Define parameters for external function.
  41       *
  42       * The parameter is either in the format 'entity' or 'component_name > entity'. There is no appropriate param type for a
  43       * string like this containing angle brackets, so we will do PARAM_RAW. The value will be parsed by
  44       * behat_data_generators::parse_entity_type, which validates the format of the parameter and throws an exception if it is not
  45       * correct.
  46       *
  47       * @return external_function_parameters
  48       */
  49      public static function execute_parameters(): external_function_parameters {
  50          return new external_function_parameters([
  51              'entitytype' => new external_value(PARAM_RAW, 'Entity type that can be created by a generator.'),
  52          ]);
  53      }
  54  
  55      /**
  56       * Return a list of the required fields for a given entity type.
  57       *
  58       * @param string $entitytype
  59       * @return array
  60       */
  61      public static function execute(string $entitytype): array {
  62          global $CFG;
  63  
  64          // Ensure we can load Behat and Facebook namespaces in behat libraries.
  65          require_once("{$CFG->dirroot}/vendor/autoload.php");
  66          require_once("{$CFG->libdir}/tests/behat/behat_data_generators.php");
  67  
  68          $params = self::validate_parameters(self::execute_parameters(), ['entitytype' => $entitytype]);
  69          $context = \context_system::instance();
  70          self::validate_context($context);
  71          require_capability('moodle/site:config', $context);
  72  
  73          $generators = new \behat_data_generators();
  74          $entity = $generators->get_entity($params['entitytype']);
  75          return ['required' => $entity['required']];
  76      }
  77  
  78      /**
  79       * Define return values.
  80       *
  81       * Return required fields
  82       *
  83       * @return external_single_structure
  84       */
  85      public static function execute_returns(): external_single_structure {
  86          return new external_single_structure([
  87              'required' => new external_multiple_structure(
  88                  new external_value(PARAM_TEXT, 'Required field'),
  89                  'Required fields',
  90                  VALUE_OPTIONAL
  91              ),
  92          ]);
  93      }
  94  }