Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.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   * Steps definitions related to mod_chat.
  19   *
  20   * @package   mod_chat
  21   * @category  test
  22   * @copyright 2021 Dongsheng Cai
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  require_once (__DIR__ . '/../../../../lib/behat/behat_base.php');
  27  
  28  /**
  29   * Steps definitions related to mod_chat.
  30   *
  31   */
  32  class behat_mod_chat extends behat_base {
  33      /**
  34       * Convert page names to URLs for steps like 'When I am on the "[identifier]" "[page type]" page'.
  35       *
  36       * Recognised page names are:
  37       * | pagetype          | name meaning | description                   |
  38       * | View              | Chat name    | The chat info page (view.php) |
  39       *
  40       * @param string $type identifies which type of page this is, e.g. 'View'.
  41       * @param string $name chat instance name
  42       * @return moodle_url the corresponding URL.
  43       * @throws Exception with a meaningful error message if the specified page cannot be found.
  44       */
  45      protected function resolve_page_instance_url(string $type, string $name): moodle_url {
  46          switch (strtolower($type)) {
  47              case 'view':
  48                  $cm = $this->get_cm_by_chat_name($name);
  49                  return new moodle_url('/mod/chat/view.php', ['id' => $cm->id]);
  50              default:
  51                  throw new Exception('Unrecognised chat page type "' . $type . '."');
  52          }
  53      }
  54  
  55      /**
  56       * Get a chat by name.
  57       *
  58       * @param string $name chat name.
  59       * @return stdClass the corresponding DB row.
  60       */
  61      protected function get_chat_by_name(string $name): stdClass {
  62          global $DB;
  63          return $DB->get_record('chat', ['name' => $name], '*', MUST_EXIST);
  64      }
  65  
  66      /**
  67       * Get a chat coursemodule object from the name.
  68       *
  69       * @param string $name chat name.
  70       * @return stdClass cm from get_coursemodule_from_instance.
  71       */
  72      protected function get_cm_by_chat_name(string $name): stdClass {
  73          $chat = $this->get_chat_by_name($name);
  74          return get_coursemodule_from_instance('chat', $chat->id, $chat->course);
  75      }
  76  }