Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 402] [Versions 39 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  defined('MOODLE_INTERNAL') || die();
  29  
  30  require_once($CFG->libdir . '/externallib.php');
  31  
  32  /**
  33   * External function for getting properties of entity generators.
  34   */
  35  class get_entity_generator extends \external_api {
  36  
  37      /**
  38       * Define parameters for external function.
  39       *
  40       * The parameter is either in the format 'entity' or 'component_name > entity'. There is no appropriate param type for a
  41       * string like this containing angle brackets, so we will do PARAM_RAW. The value will be parsed by
  42       * behat_data_generators::parse_entity_type, which validates the format of the parameter and throws an exception if it is not
  43       * correct.
  44       *
  45       * @return \external_function_parameters
  46       */
  47      public static function execute_parameters(): \external_function_parameters {
  48          return new \external_function_parameters([
  49              'entitytype' => new \external_value(PARAM_RAW, 'Entity type that can be created by a generator.'),
  50          ]);
  51      }
  52  
  53      /**
  54       * Return a list of the required fields for a given entity type.
  55       *
  56       * @param string $entitytype
  57       * @return array
  58       */
  59      public static function execute(string $entitytype): array {
  60          global $CFG;
  61  
  62          // Ensure we can load Behat and Facebook namespaces in behat libraries.
  63          require_once("{$CFG->dirroot}/vendor/autoload.php");
  64          require_once("{$CFG->libdir}/tests/behat/behat_data_generators.php");
  65  
  66          $params = self::validate_parameters(self::execute_parameters(), ['entitytype' => $entitytype]);
  67          $context = \context_system::instance();
  68          self::validate_context($context);
  69          require_capability('moodle/site:config', $context);
  70  
  71          $generators = new \behat_data_generators();
  72          $entity = $generators->get_entity($params['entitytype']);
  73          return ['required' => $entity['required']];
  74      }
  75  
  76      /**
  77       * Define return values.
  78       *
  79       * Return required fields
  80       *
  81       * @return \external_single_structure
  82       */
  83      public static function execute_returns(): \external_single_structure {
  84          return new \external_single_structure([
  85              'required' => new \external_multiple_structure(
  86                  new \external_value(PARAM_TEXT, 'Required field'),
  87                  'Required fields',
  88                  VALUE_OPTIONAL
  89              ),
  90          ]);
  91      }
  92  }