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 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [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  /**
  18   * Helper to get behat contexts from other contexts.
  19   *
  20   * @package    core
  21   * @category   test
  22   * @copyright  2014 David MonllaĆ³
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
  27  
  28  use Behat\Testwork\Environment\Environment;
  29  use Behat\Mink\Exception\DriverException;
  30  
  31  /**
  32   * Helper to get behat contexts.
  33   *
  34   * @package    core
  35   * @category   test
  36   * @copyright  2014 David MonllaĆ³
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class behat_context_helper {
  40  
  41      /**
  42       * Behat environment.
  43       *
  44       * @var Environment
  45       */
  46      protected static $environment = null;
  47  
  48      /**
  49       * @var Escaper::escapeLiteral
  50       */
  51      protected static $escaper;
  52  
  53      /**
  54       * @var array keep track of nonexisting contexts, to avoid exception tracking.
  55       */
  56      protected static $nonexistingcontexts = array();
  57  
  58      /**
  59       * Sets behat environment.
  60       *
  61       * @param Environment $environment
  62       * @return void
  63       */
  64      public static function set_environment(Environment $environment) {
  65          self::$environment = $environment;
  66      }
  67  
  68      /**
  69       * Gets the required context.
  70       *
  71       * Getting a context you get access to all the steps
  72       * that uses direct API calls; steps returning step chains
  73       * can not be executed like this.
  74       *
  75       * @throws Behat\Behat\Context\Exception\ContextNotFoundException
  76       * @param string $classname Context identifier (the class name).
  77       * @return behat_base
  78       */
  79      public static function get($classname) {
  80          $definedclassname = self::get_theme_override($classname);
  81          if ($definedclassname) {
  82              return self::$environment->getContext($definedclassname);
  83          }
  84  
  85          // Just fall back on getContext to ensure that we throw the correct exception.
  86          return self::$environment->getContext($classname);
  87      }
  88  
  89      /**
  90       * Get the context for the specified component or subsystem.
  91       *
  92       * @param string $component The component or subsystem to find the context for
  93       * @return behat_base|null
  94       */
  95      public static function get_component_context(string $component): ?behat_base {
  96          $component = str_replace('core_', '', $component);
  97  
  98          if ($classname = self::get_theme_override("behat_{$component}")) {
  99              return self::get($classname);
 100          }
 101  
 102          return null;
 103      }
 104  
 105      /**
 106       * Find all Behat contexts which match the specified context class name prefix.
 107       *
 108       * Moodle uses a consistent class naming scheme for all Behat contexts, whereby the context name is in the format:
 109       *
 110       *     behat_{component}
 111       *
 112       * This method will return all contexts which match the specified prefix.
 113       *
 114       * For example, to find all editors, you would pass in 'behat_editor', and this might return:
 115       * - behat_editor_atto
 116       * - behat_editor_textarea
 117       *
 118       * @param string $prefix The prefix to search for
 119       * @return \Behat\Behat\Context\Context[]
 120       */
 121      public static function get_prefixed_contexts(string $prefix): array {
 122          if (!is_a(self::$environment, \Behat\Behat\Context\Environment\InitializedContextEnvironment::class)) {
 123              throw new DriverException(
 124                  'Cannot get prefixed contexts - the environment is not an InitializedContextEnvironment'
 125              );
 126          }
 127  
 128          return array_filter(self::$environment->getContexts(), function($context) use ($prefix): bool {
 129              return (strpos(get_class($context), $prefix) === 0);
 130          });
 131      }
 132  
 133      /**
 134       * Check for any theme override of the specified class name.
 135       *
 136       * @param string $classname
 137       * @return string|null
 138       */
 139      protected static function get_theme_override(string $classname): ?string {
 140          $suitename = self::$environment->getSuite()->getName();
 141          // If default suite, then get the default theme name.
 142          if ($suitename == 'default') {
 143              $suitename = theme_config::DEFAULT_THEME;
 144          }
 145  
 146          $overrideclassname = "behat_theme_{$suitename}_{$classname}";
 147          if (self::$environment->hasContextClass($overrideclassname)) {
 148              return $overrideclassname;
 149          }
 150  
 151          if (self::$environment->hasContextClass($classname)) {
 152              return $classname;
 153          }
 154  
 155          return null;
 156      }
 157  
 158      /**
 159       * Return whether there is a context of the specified classname.
 160       *
 161       * @param string $classname
 162       * @return bool
 163       */
 164      public static function has_context(string $classname): bool {
 165          return self::$environment->hasContextClass($classname);
 166      }
 167  
 168      /**
 169       * Translates string to XPath literal.
 170       *
 171       * @param string $label label to escape
 172       * @return string escaped string.
 173       */
 174      public static function escape($label) {
 175          if (empty(self::$escaper)) {
 176              self::$escaper = new \Behat\Mink\Selector\Xpath\Escaper();
 177          }
 178          return self::$escaper->escapeLiteral($label);
 179      }
 180  }