2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Helper to get behat contexts from other contexts.
22 * @copyright 2014 David MonllaĆ³
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
28 use Behat\Testwork\Environment\Environment
;
31 * Helper to get behat contexts.
35 * @copyright 2014 David MonllaĆ³
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class behat_context_helper
{
45 protected static $environment = null;
48 * @var Escaper::escapeLiteral
50 protected static $escaper;
53 * @var array keep track of nonexisting contexts, to avoid exception tracking.
55 protected static $nonexistingcontexts = array();
58 * Sets the browser session.
60 * @param Environment $environment
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.
65 public static function set_session(Environment
$environment) {
66 debugging('set_session is deprecated. Please use set_environment instead.', DEBUG_DEVELOPER
);
68 self
::set_environment($environment);
72 * Sets behat environment.
74 * @param Environment $environment
77 public static function set_environment(Environment
$environment) {
78 self
::$environment = $environment;
82 * Gets the required context.
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.
88 * @throws Behat\Behat\Context\Exception\ContextNotFoundException
89 * @param string $classname Context identifier (the class name).
92 public static function get($classname) {
93 if (self
::$environment->hasContextClass($classname)) {
94 return self
::$environment->getContext($classname);
97 $suitename = self
::$environment->getSuite()->getName();
98 // If default suite, then get the default theme name.
99 if ($suitename == 'default') {
100 $suitename = theme_config
::DEFAULT_THEME
;
102 $overridencontextname = 'behat_theme_'.$suitename.'_'.$classname;
104 // If contexts has not been checked before and doesn't exist then just use core one.
105 if (!isset(self
::$nonexistingcontexts[$overridencontextname])) {
107 $subcontext = self
::$environment->getContext($overridencontextname);
110 } catch (Behat\Behat\Context\Exception\ContextNotFoundException
$e) {
111 // If context not found then it's not overridden.
112 self
::$nonexistingcontexts[$overridencontextname] = 1;
116 // Get the actual context.
117 return self
::$environment->getContext($classname);
121 * Return whether there is a context of the specified classname.
123 * @param string $classname
126 public static function has_context(string $classname): bool {
127 return self
::$environment->hasContextClass($classname);
131 * Translates string to XPath literal.
133 * @param string $label label to escape
134 * @return string escaped string.
136 public static function escape($label) {
137 if (empty(self
::$escaper)) {
138 self
::$escaper = new \Behat\Mink\Selector\Xpath\
Escaper();
140 return self
::$escaper->escapeLiteral($label);