Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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  
  30  /**
  31   * Helper to get behat contexts.
  32   *
  33   * @package    core
  34   * @category   test
  35   * @copyright  2014 David MonllaĆ³
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class behat_context_helper {
  39  
  40      /**
  41       * Behat environment.
  42       *
  43       * @var Environment
  44       */
  45      protected static $environment = null;
  46  
  47      /**
  48       * @var Escaper::escapeLiteral
  49       */
  50      protected static $escaper;
  51  
  52      /**
  53       * @var array keep track of nonexisting contexts, to avoid exception tracking.
  54       */
  55      protected static $nonexistingcontexts = array();
  56  
  57      /**
  58       * Sets the browser session.
  59       *
  60       * @param Environment $environment
  61       * @return void
  62       * @deprecated since 3.2 MDL-55072 - please use behat_context_helper::set_environment()
  63       * @todo MDL-55365 This will be deleted in Moodle 3.6.
  64       */
  65      public static function set_session(Environment $environment) {
  66          debugging('set_session is deprecated. Please use set_environment instead.', DEBUG_DEVELOPER);
  67  
  68          self::set_environment($environment);
  69      }
  70  
  71      /**
  72       * Sets behat environment.
  73       *
  74       * @param Environment $environment
  75       * @return void
  76       */
  77      public static function set_environment(Environment $environment) {
  78          self::$environment = $environment;
  79      }
  80  
  81      /**
  82       * Gets the required context.
  83       *
  84       * Getting a context you get access to all the steps
  85       * that uses direct API calls; steps returning step chains
  86       * can not be executed like this.
  87       *
  88       * @throws Behat\Behat\Context\Exception\ContextNotFoundException
  89       * @param string $classname Context identifier (the class name).
  90       * @return behat_base
  91       */
  92      public static function get($classname) {
  93          $definedclassname = self::get_theme_override($classname);
  94          if ($definedclassname) {
  95              return self::$environment->getContext($definedclassname);
  96          }
  97  
  98          // Just fall back on getContext to ensure that we throw the correct exception.
  99          return self::$environment->getContext($classname);
 100      }
 101  
 102      /**
 103       * Get the context for the specified component or subsystem.
 104       *
 105       * @param string $component The component or subsystem to find the context for
 106       * @return behat_base|null
 107       */
 108      public static function get_component_context(string $component): ?behat_base {
 109          $component = str_replace('core_', '', $component);
 110  
 111          if ($classname = self::get_theme_override("behat_{$component}")) {
 112              return self::get($classname);
 113          }
 114  
 115          return null;
 116      }
 117  
 118      /**
 119       * Check for any theme override of the specified class name.
 120       *
 121       * @param string $classname
 122       * @return string|null
 123       */
 124      protected static function get_theme_override(string $classname): ?string {
 125          $suitename = self::$environment->getSuite()->getName();
 126          // If default suite, then get the default theme name.
 127          if ($suitename == 'default') {
 128              $suitename = theme_config::DEFAULT_THEME;
 129          }
 130  
 131          $overrideclassname = "behat_theme_{$suitename}_{$classname}";
 132          if (self::$environment->hasContextClass($overrideclassname)) {
 133              return $overrideclassname;
 134          }
 135  
 136          if (self::$environment->hasContextClass($classname)) {
 137              return $classname;
 138          }
 139  
 140          return null;
 141      }
 142  
 143      /**
 144       * Return whether there is a context of the specified classname.
 145       *
 146       * @param string $classname
 147       * @return bool
 148       */
 149      public static function has_context(string $classname): bool {
 150          return self::$environment->hasContextClass($classname);
 151      }
 152  
 153      /**
 154       * Translates string to XPath literal.
 155       *
 156       * @param string $label label to escape
 157       * @return string escaped string.
 158       */
 159      public static function escape($label) {
 160          if (empty(self::$escaper)) {
 161              self::$escaper = new \Behat\Mink\Selector\Xpath\Escaper();
 162          }
 163          return self::$escaper->escapeLiteral($label);
 164      }
 165  }