Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [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   * 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 the browser session.
  60       *
  61       * @param Environment $environment
  62       * @return void
  63       * @deprecated since 3.2 MDL-55072 - please use behat_context_helper::set_environment()
  64       * @todo MDL-55365 This will be deleted in Moodle 3.6.
  65       */
  66      public static function set_session(Environment $environment) {
  67          debugging('set_session is deprecated. Please use set_environment instead.', DEBUG_DEVELOPER);
  68  
  69          self::set_environment($environment);
  70      }
  71  
  72      /**
  73       * Sets behat environment.
  74       *
  75       * @param Environment $environment
  76       * @return void
  77       */
  78      public static function set_environment(Environment $environment) {
  79          self::$environment = $environment;
  80      }
  81  
  82      /**
  83       * Gets the required context.
  84       *
  85       * Getting a context you get access to all the steps
  86       * that uses direct API calls; steps returning step chains
  87       * can not be executed like this.
  88       *
  89       * @throws Behat\Behat\Context\Exception\ContextNotFoundException
  90       * @param string $classname Context identifier (the class name).
  91       * @return behat_base
  92       */
  93      public static function get($classname) {
  94          $definedclassname = self::get_theme_override($classname);
  95          if ($definedclassname) {
  96              return self::$environment->getContext($definedclassname);
  97          }
  98  
  99          // Just fall back on getContext to ensure that we throw the correct exception.
 100          return self::$environment->getContext($classname);
 101      }
 102  
 103      /**
 104       * Get the context for the specified component or subsystem.
 105       *
 106       * @param string $component The component or subsystem to find the context for
 107       * @return behat_base|null
 108       */
 109      public static function get_component_context(string $component): ?behat_base {
 110          $component = str_replace('core_', '', $component);
 111  
 112          if ($classname = self::get_theme_override("behat_{$component}")) {
 113              return self::get($classname);
 114          }
 115  
 116          return null;
 117      }
 118  
 119      /**
 120       * Find all Behat contexts which match the specified context class name prefix.
 121       *
 122       * Moodle uses a consistent class naming scheme for all Behat contexts, whereby the context name is in the format:
 123       *
 124       *     behat_{component}
 125       *
 126       * This method will return all contexts which match the specified prefix.
 127       *
 128       * For example, to find all editors, you would pass in 'behat_editor', and this might return:
 129       * - behat_editor_atto
 130       * - behat_editor_textarea
 131       *
 132       * @param string $prefix The prefix to search for
 133       * @return \Behat\Behat\Context\Context[]
 134       */
 135      public static function get_prefixed_contexts(string $prefix): array {
 136          if (!is_a(self::$environment, \Behat\Behat\Context\Environment\InitializedContextEnvironment::class)) {
 137              throw new DriverException(
 138                  'Cannot get prefixed contexts - the environment is not an InitializedContextEnvironment'
 139              );
 140          }
 141  
 142          return array_filter(self::$environment->getContexts(), function($context) use ($prefix): bool {
 143              return (strpos(get_class($context), $prefix) === 0);
 144          });
 145      }
 146  
 147      /**
 148       * Check for any theme override of the specified class name.
 149       *
 150       * @param string $classname
 151       * @return string|null
 152       */
 153      protected static function get_theme_override(string $classname): ?string {
 154          $suitename = self::$environment->getSuite()->getName();
 155          // If default suite, then get the default theme name.
 156          if ($suitename == 'default') {
 157              $suitename = theme_config::DEFAULT_THEME;
 158          }
 159  
 160          $overrideclassname = "behat_theme_{$suitename}_{$classname}";
 161          if (self::$environment->hasContextClass($overrideclassname)) {
 162              return $overrideclassname;
 163          }
 164  
 165          if (self::$environment->hasContextClass($classname)) {
 166              return $classname;
 167          }
 168  
 169          return null;
 170      }
 171  
 172      /**
 173       * Return whether there is a context of the specified classname.
 174       *
 175       * @param string $classname
 176       * @return bool
 177       */
 178      public static function has_context(string $classname): bool {
 179          return self::$environment->hasContextClass($classname);
 180      }
 181  
 182      /**
 183       * Translates string to XPath literal.
 184       *
 185       * @param string $label label to escape
 186       * @return string escaped string.
 187       */
 188      public static function escape($label) {
 189          if (empty(self::$escaper)) {
 190              self::$escaper = new \Behat\Mink\Selector\Xpath\Escaper();
 191          }
 192          return self::$escaper->escapeLiteral($label);
 193      }
 194  }