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 400 and 403] [Versions 401 and 403] [Versions 402 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  defined('MOODLE_INTERNAL') || die();
  18  
  19  /**
  20   * Define behat generator for mod_lti.
  21   *
  22   * @package    mod_lti
  23   * @category   test
  24   * @author     Andrew Madden <andrewmadden@catalyst-au.net>
  25   * @copyright  2020 Catalyst IT
  26   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  class behat_mod_lti_generator extends behat_generator_base {
  29  
  30      /**
  31       * Get list of entities for mod_lti behat tests.
  32       *
  33       * @return array[] List of entity definitions.
  34       */
  35      protected function get_creatable_entities(): array {
  36          return [
  37              'tool proxies' => [
  38                  'singular' => 'tool proxy',
  39                  'datagenerator' => 'tool_proxies',
  40                  'required'  => [],
  41  
  42              ],
  43              'tool types' => [
  44                  'singular' => 'tool type',
  45                  'datagenerator' => 'tool_types',
  46                  'required' => ['baseurl'],
  47                  'switchids' => ['lti_coursecategories' => 'lti_coursecategories']
  48              ],
  49              'course tools' => [
  50                  'singular' => 'course tool',
  51                  'datagenerator' => 'course_tool_types',
  52                  'required' => ['baseurl', 'course'],
  53                  'switchids' => ['course' => 'course']
  54              ],
  55              'tool instances' => [
  56                  'singular' => 'instance',
  57                  'datagenerator' => 'instance',
  58                  'required' => ['course', 'tool'],
  59                  'switchids' => ['course' => 'course', 'tool' => 'typeid']
  60              ],
  61          ];
  62      }
  63  
  64      /**
  65       * Handles the switchid ['tool' => 'typeid'] for finding a tool by name.
  66       *
  67       * @param string $name the name of the tool.
  68       * @return int the id of the tool type identified by the name $name.
  69       */
  70      protected function get_tool_id(string $name): int {
  71          global $DB;
  72  
  73          if (!$id = $DB->get_field('lti_types', 'id', ['name' => $name])) {
  74              throw new coding_exception('The specified tool with name "' . $name . '" does not exist');
  75          }
  76          return (int) $id;
  77      }
  78  
  79      /**
  80       * Handles the switchid ['lti_coursecategories' => 'lti_coursecategories'] for restricting a tool to certain categories.
  81       *
  82       * @param string $idnumbers a comma-separated string containing the course category id numbers, e.g. 'cata, catb, catc'.
  83       * @return string a comma-separated string containing the course category ids.
  84       * @throws coding_exception if one or more of the categories is unable to be matched by its idnumber.
  85       */
  86      protected function get_lti_coursecategories_id(string $idnumbers): string {
  87          global $DB;
  88          $categoryids = array_map('trim', explode(',', $idnumbers));
  89  
  90          [$insql, $inparams] = $DB->get_in_or_equal($categoryids);
  91          $ids = $DB->get_fieldset_sql("SELECT id FROM {course_categories} WHERE idnumber $insql", $inparams);
  92          if (!$ids || count($ids) != count($categoryids)) {
  93              throw new coding_exception("One or more course categories unable to be matched using idnumbers: $idnumbers");
  94          }
  95  
  96          return implode(',', $ids);
  97      }
  98  }