first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
+278
View File
@@ -0,0 +1,278 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Behat command utils
*
* @package core
* @category test
* @copyright 2012 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/../lib.php');
/**
* Behat command related utils
*
* @package core
* @category test
* @copyright 2013 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_command {
/**
* Docs url
*/
const DOCS_URL = 'https://moodledev.io/general/development/tools/behat';
/**
* Ensures the behat dir exists in moodledata
*
* @return string Full path
*/
public static function get_parent_behat_dir() {
global $CFG;
// If not set then return empty string.
if (!isset($CFG->behat_dataroot_parent)) {
return "";
}
return $CFG->behat_dataroot_parent;
}
/**
* Ensures the behat dir exists in moodledata
* @param int $runprocess run process for which behat dir is returned.
* @return string Full path
*/
public static function get_behat_dir($runprocess = 0) {
global $CFG;
// If not set then return empty string.
if (!isset($CFG->behat_dataroot)) {
return "";
}
// If $CFG->behat_parallel_run starts with index 0 and $runprocess for parallel run starts with 1.
if (!empty($runprocess) && isset($CFG->behat_parallel_run[$runprocess - 1]['behat_dataroot'])) {
$behatdir = $CFG->behat_parallel_run[$runprocess - 1]['behat_dataroot'] . '/behat';;
} else {
$behatdir = $CFG->behat_dataroot . '/behat';
}
if (!is_dir($behatdir)) {
if (!mkdir($behatdir, $CFG->directorypermissions, true)) {
behat_error(BEHAT_EXITCODE_PERMISSIONS, 'Directory ' . $behatdir . ' can not be created');
}
}
if (!is_writable($behatdir)) {
behat_error(BEHAT_EXITCODE_PERMISSIONS, 'Directory ' . $behatdir . ' is not writable');
}
return $behatdir;
}
/**
* Returns the executable path
*
* Allows returning a customized command for cygwin when the
* command is just displayed, when using exec(), system() and
* friends we stay with DIRECTORY_SEPARATOR as they use the
* normal cmd.exe (in Windows).
*
* @param bool $custombyterm If the provided command should depend on the terminal where it runs
* @param bool $parallelrun If parallel run is installed.
* @param bool $absolutepath return command with absolute path.
* @return string
*/
final public static function get_behat_command($custombyterm = false, $parallerun = false, $absolutepath = false) {
$separator = DIRECTORY_SEPARATOR;
$exec = 'behat';
// Cygwin uses linux-style directory separators.
if ($custombyterm && testing_is_cygwin()) {
$separator = '/';
// MinGW can not execute .bat scripts.
if (!testing_is_mingw()) {
$exec = 'behat.bat';
}
}
// If relative path then prefix relative path.
if ($absolutepath) {
$pathprefix = testing_cli_argument_path('/');
if (!empty($pathprefix)) {
$pathprefix .= $separator;
}
} else {
$pathprefix = '';
}
if (!$parallerun) {
$command = $pathprefix . 'vendor' . $separator . 'bin' . $separator . $exec;
} else {
$command = 'php ' . $pathprefix . 'admin' . $separator . 'tool' . $separator . 'behat' . $separator . 'cli'
. $separator . 'run.php';
}
return $command;
}
/**
* Runs behat command with provided options
*
* Execution continues when the process finishes
*
* @param string $options Defaults to '' so tests would be executed
* @return array CLI command outputs [0] => string, [1] => integer
*/
final public static function run($options = '') {
global $CFG;
$currentcwd = getcwd();
chdir($CFG->dirroot);
exec(self::get_behat_command() . ' ' . $options, $output, $code);
chdir($currentcwd);
return array($output, $code);
}
/**
* Checks if behat is set up and working
*
* Notifies failures both from CLI and web interface.
*
* It checks behat dependencies have been installed and runs
* the behat help command to ensure it works as expected
*
* @return int Error code or 0 if all ok
*/
public static function behat_setup_problem() {
global $CFG;
// Moodle setting.
if (!self::are_behat_dependencies_installed()) {
// Returning composer error code to avoid conflicts with behat and moodle error codes.
self::output_msg(get_string('errorcomposer', 'tool_behat'));
return TESTING_EXITCODE_COMPOSER;
}
// Behat test command.
$dirrootconfigpath = $CFG->dirroot . DIRECTORY_SEPARATOR . 'behat.yml';
if (file_exists($dirrootconfigpath)) {
self::output_msg(get_string('warndirrootconfigfound', 'tool_behat', $dirrootconfigpath));
}
list($output, $code) = self::run(" --help");
if ($code != 0) {
// Returning composer error code to avoid conflicts with behat and moodle error codes.
self::output_msg(get_string('errorbehatcommand', 'tool_behat', self::get_behat_command()));
return TESTING_EXITCODE_COMPOSER;
}
// No empty values.
if (empty($CFG->behat_dataroot) || empty($CFG->behat_prefix) || empty($CFG->behat_wwwroot)) {
self::output_msg(get_string('errorsetconfig', 'tool_behat'));
return BEHAT_EXITCODE_CONFIG;
}
// Not repeated values.
// We only need to check this when the behat site is not running as
// at this point, when it is running, all $CFG->behat_* vars have
// already been copied to $CFG->dataroot, $CFG->prefix and $CFG->wwwroot.
$phpunitprefix = empty($CFG->phpunit_prefix) ? '' : $CFG->phpunit_prefix;
$behatdbname = empty($CFG->behat_dbname) ? $CFG->dbname : $CFG->behat_dbname;
$phpunitdbname = empty($CFG->phpunit_dbname) ? $CFG->dbname : $CFG->phpunit_dbname;
$behatdbhost = empty($CFG->behat_dbhost) ? $CFG->dbhost : $CFG->behat_dbhost;
$phpunitdbhost = empty($CFG->phpunit_dbhost) ? $CFG->dbhost : $CFG->phpunit_dbhost;
$samedataroot = $CFG->behat_dataroot == $CFG->dataroot;
$samedataroot = $samedataroot || (!empty($CFG->phpunit_dataroot) && $CFG->phpunit_dataroot == $CFG->behat_dataroot);
$samewwwroot = $CFG->behat_wwwroot == $CFG->wwwroot;
$sameprefix = ($CFG->behat_prefix == $CFG->prefix && $behatdbname == $CFG->dbname && $behatdbhost == $CFG->dbhost);
$sameprefix = $sameprefix || ($CFG->behat_prefix == $phpunitprefix && $behatdbname == $phpunitdbname &&
$behatdbhost == $phpunitdbhost);
if (!defined('BEHAT_SITE_RUNNING') && ($samedataroot || $samewwwroot || $sameprefix)) {
self::output_msg(get_string('erroruniqueconfig', 'tool_behat'));
return BEHAT_EXITCODE_CONFIG;
}
// Checking behat dataroot existence otherwise echo about admin/tool/behat/cli/init.php.
if (!empty($CFG->behat_dataroot)) {
$CFG->behat_dataroot = realpath($CFG->behat_dataroot);
}
if (empty($CFG->behat_dataroot) || !is_dir($CFG->behat_dataroot) || !is_writable($CFG->behat_dataroot)) {
self::output_msg(get_string('errordataroot', 'tool_behat'));
return BEHAT_EXITCODE_CONFIG;
}
return 0;
}
/**
* Has the site installed composer.
* @return bool
*/
public static function are_behat_dependencies_installed() {
if (!is_dir(__DIR__ . '/../../../vendor/behat')) {
return false;
}
return true;
}
/**
* Outputs a message.
*
* Used in CLI + web UI methods. Stops the
* execution in web.
*
* @param string $msg
* @return void
*/
protected static function output_msg($msg) {
global $CFG, $PAGE;
// If we are using the web interface we want pretty messages.
if (!CLI_SCRIPT) {
$renderer = $PAGE->get_renderer('tool_behat');
echo $renderer->render_error($msg);
// Stopping execution.
exit(1);
} else {
// We continue execution after this.
$clibehaterrorstr = "Ensure you set \$CFG->behat_* vars in config.php " .
"and you ran admin/tool/behat/cli/init.php.\n" .
"More info in " . self::DOCS_URL;
echo 'Error: ' . $msg . "\n\n" . $clibehaterrorstr;
}
}
}
+292
View File
@@ -0,0 +1,292 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Utils to set Behat config
*
* @package core
* @category test
* @copyright 2012 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/behat_config_util.php');
/**
* Behat configuration manager
*
* Creates/updates Behat config files getting tests
* and steps from Moodle codebase
*
* @package core
* @category test
* @copyright 2012 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_config_manager {
/**
* @var bool Keep track of the automatic profile conversion. So we can notify user.
*/
public static $autoprofileconversion = false;
/**
* @var behat_config_util keep object of behat_config_util for use.
*/
public static $behatconfigutil = null;
/**
* Returns behat_config_util.
*
* @return behat_config_util
*/
private static function get_behat_config_util() {
if (!self::$behatconfigutil) {
self::$behatconfigutil = new behat_config_util();
}
return self::$behatconfigutil;
}
/**
* Updates a config file
*
* The tests runner and the steps definitions list uses different
* config files to avoid problems with concurrent executions.
*
* The steps definitions list can be filtered by component so it's
* behat.yml is different from the $CFG->dirroot one.
*
* @param string $component Restricts the obtained steps definitions to the specified component
* @param string $testsrunner If the config file will be used to run tests
* @param string $tags features files including tags.
* @param bool $themesuitewithallfeatures if only theme specific features need to be included in the suite.
* @param int $parallelruns number of parallel runs.
* @param int $run current run for which config needs to be updated.
* @return void
*/
public static function update_config_file($component = '', $testsrunner = true, $tags = '',
$themesuitewithallfeatures = false, $parallelruns = 0, $run = 0) {
global $CFG;
// Behat must have a separate behat.yml to have access to the whole set of features and steps definitions.
if ($testsrunner === true) {
$configfilepath = behat_command::get_behat_dir($run) . '/behat.yml';
} else {
// Alternative for steps definitions filtering, one for each user.
$configfilepath = self::get_steps_list_config_filepath();
}
$behatconfigutil = self::get_behat_config_util();
$behatconfigutil->set_theme_suite_to_include_core_features($themesuitewithallfeatures);
$behatconfigutil->set_tag_for_feature_filter($tags);
// Gets all the components with features, if running the tests otherwise not required.
$features = array();
if ($testsrunner) {
$features = $behatconfigutil->get_components_features();
}
// Gets all the components with steps definitions.
$stepsdefinitions = $behatconfigutil->get_components_contexts($component);
if (!$testsrunner) {
// Exclude deprecated steps definitions from the available steps list.
foreach (array_keys($stepsdefinitions) as $key) {
if (preg_match('/_deprecated$/', $key)) {
unset($stepsdefinitions[$key]);
}
}
}
// Get current run.
if (empty($run) && ($run !== false) && !empty($CFG->behatrunprocess)) {
$run = $CFG->behatrunprocess;
}
// Get number of parallel runs if not passed.
if (empty($parallelruns) && ($parallelruns !== false)) {
$parallelruns = self::get_behat_run_config_value('parallel');
}
// Behat config file specifing the main context class,
// the required Behat extensions and Moodle test wwwroot.
$contents = $behatconfigutil->get_config_file_contents($features, $stepsdefinitions, $tags, $parallelruns, $run);
// Stores the file.
if (!file_put_contents($configfilepath, $contents)) {
behat_error(BEHAT_EXITCODE_PERMISSIONS, 'File ' . $configfilepath . ' can not be created');
}
}
/**
* Returns the behat config file path used by the steps definition list
*
* @return string
*/
public static function get_steps_list_config_filepath() {
global $USER;
// We don't cygwin-it as it is called using exec() which uses cmd.exe.
$userdir = behat_command::get_behat_dir() . '/users/' . $USER->id;
make_writable_directory($userdir);
return $userdir . '/behat.yml';
}
/**
* Returns the behat config file path used by the behat cli command.
*
* @param int $runprocess Runprocess.
* @return string
*/
public static function get_behat_cli_config_filepath($runprocess = 0) {
global $CFG;
if ($runprocess) {
if (isset($CFG->behat_parallel_run[$runprocess - 1 ]['behat_dataroot'])) {
$command = $CFG->behat_parallel_run[$runprocess - 1]['behat_dataroot'];
} else {
$command = $CFG->behat_dataroot . $runprocess;
}
} else {
$command = $CFG->behat_dataroot;
}
$command .= DIRECTORY_SEPARATOR . 'behat' . DIRECTORY_SEPARATOR . 'behat.yml';
// Cygwin uses linux-style directory separators.
if (testing_is_cygwin()) {
$command = str_replace('\\', '/', $command);
}
return $command;
}
/**
* Returns the path to the parallel run file which specifies if parallel test environment is enabled
* and how many parallel runs to execute.
*
* @return string
*/
final public static function get_behat_run_config_file_path() {
return behat_command::get_parent_behat_dir() . '/run_environment.json';
}
/**
* Get config for parallel run.
*
* @param string $key Key to store
* @return string|int|array value which is stored.
*/
final public static function get_behat_run_config_value($key) {
$parallelrunconfigfile = self::get_behat_run_config_file_path();
if (file_exists($parallelrunconfigfile)) {
if ($parallelrunconfigs = @json_decode(file_get_contents($parallelrunconfigfile), true)) {
if (isset($parallelrunconfigs[$key])) {
return $parallelrunconfigs[$key];
}
}
}
return false;
}
/**
* Save/update config for parallel run.
*
* @param string $key Key to store
* @param string|int|array $value to store.
*/
final public static function set_behat_run_config_value($key, $value) {
$parallelrunconfigs = array();
$parallelrunconfigfile = self::get_behat_run_config_file_path();
// Get any existing config first.
if (file_exists($parallelrunconfigfile)) {
$parallelrunconfigs = @json_decode(file_get_contents($parallelrunconfigfile), true);
}
$parallelrunconfigs[$key] = $value;
@file_put_contents($parallelrunconfigfile, json_encode($parallelrunconfigs, JSON_PRETTY_PRINT));
}
/**
* Drops parallel site links.
*
* @return bool true on success else false.
*/
final public static function drop_parallel_site_links() {
global $CFG;
// Get parallel test runs.
$parallelrun = self::get_behat_run_config_value('parallel');
if (empty($parallelrun)) {
return false;
}
// If parallel run then remove links and original file.
clearstatcache();
for ($i = 1; $i <= $parallelrun; $i++) {
// Don't delete links for specified sites, as they should be accessible.
if (!empty($CFG->behat_parallel_run['behat_wwwroot'][$i - 1]['behat_wwwroot'])) {
continue;
}
$link = $CFG->dirroot . '/' . BEHAT_PARALLEL_SITE_NAME . $i;
if (file_exists($link) && is_link($link)) {
@unlink($link);
}
}
return true;
}
/**
* Create parallel site links.
*
* @param int $fromrun first run
* @param int $torun last run.
* @return bool true for sucess, else false.
*/
final public static function create_parallel_site_links($fromrun, $torun) {
global $CFG;
// Create site symlink if necessary.
clearstatcache();
for ($i = $fromrun; $i <= $torun; $i++) {
// Don't create links for specified sites, as they should be accessible.
if (!empty($CFG->behat_parallel_run['behat_wwwroot'][$i - 1]['behat_wwwroot'])) {
continue;
}
$link = $CFG->dirroot.'/'.BEHAT_PARALLEL_SITE_NAME.$i;
clearstatcache();
if (file_exists($link)) {
if (!is_link($link) || !is_dir($link)) {
echo "File exists at link location ($link) but is not a link or directory!" . PHP_EOL;
return false;
}
} else if (!symlink($CFG->dirroot, $link)) {
// Try create link in case it's not already present.
echo "Unable to create behat site symlink ($link)" . PHP_EOL;
return false;
}
}
return true;
}
}
File diff suppressed because it is too large Load Diff
+180
View File
@@ -0,0 +1,180 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Helper to get behat contexts from other contexts.
*
* @package core
* @category test
* @copyright 2014 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
use Behat\Testwork\Environment\Environment;
use Behat\Mink\Exception\DriverException;
/**
* Helper to get behat contexts.
*
* @package core
* @category test
* @copyright 2014 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_context_helper {
/**
* Behat environment.
*
* @var Environment
*/
protected static $environment = null;
/**
* @var Escaper::escapeLiteral
*/
protected static $escaper;
/**
* @var array keep track of nonexisting contexts, to avoid exception tracking.
*/
protected static $nonexistingcontexts = array();
/**
* Sets behat environment.
*
* @param Environment $environment
* @return void
*/
public static function set_environment(Environment $environment) {
self::$environment = $environment;
}
/**
* Gets the required context.
*
* Getting a context you get access to all the steps
* that uses direct API calls; steps returning step chains
* can not be executed like this.
*
* @throws Behat\Behat\Context\Exception\ContextNotFoundException
* @param string $classname Context identifier (the class name).
* @return behat_base
*/
public static function get($classname) {
$definedclassname = self::get_theme_override($classname);
if ($definedclassname) {
return self::$environment->getContext($definedclassname);
}
// Just fall back on getContext to ensure that we throw the correct exception.
return self::$environment->getContext($classname);
}
/**
* Get the context for the specified component or subsystem.
*
* @param string $component The component or subsystem to find the context for
* @return behat_base|null
*/
public static function get_component_context(string $component): ?behat_base {
$component = str_replace('core_', '', $component);
if ($classname = self::get_theme_override("behat_{$component}")) {
return self::get($classname);
}
return null;
}
/**
* Find all Behat contexts which match the specified context class name prefix.
*
* Moodle uses a consistent class naming scheme for all Behat contexts, whereby the context name is in the format:
*
* behat_{component}
*
* This method will return all contexts which match the specified prefix.
*
* For example, to find all editors, you would pass in 'behat_editor', and this might return:
* - behat_editor_atto
* - behat_editor_textarea
*
* @param string $prefix The prefix to search for
* @return \Behat\Behat\Context\Context[]
*/
public static function get_prefixed_contexts(string $prefix): array {
if (!is_a(self::$environment, \Behat\Behat\Context\Environment\InitializedContextEnvironment::class)) {
throw new DriverException(
'Cannot get prefixed contexts - the environment is not an InitializedContextEnvironment'
);
}
return array_filter(self::$environment->getContexts(), function($context) use ($prefix): bool {
return (strpos(get_class($context), $prefix) === 0);
});
}
/**
* Check for any theme override of the specified class name.
*
* @param string $classname
* @return string|null
*/
protected static function get_theme_override(string $classname): ?string {
$suitename = self::$environment->getSuite()->getName();
// If default suite, then get the default theme name.
if ($suitename == 'default') {
$suitename = theme_config::DEFAULT_THEME;
}
$overrideclassname = "behat_theme_{$suitename}_{$classname}";
if (self::$environment->hasContextClass($overrideclassname)) {
return $overrideclassname;
}
if (self::$environment->hasContextClass($classname)) {
return $classname;
}
return null;
}
/**
* Return whether there is a context of the specified classname.
*
* @param string $classname
* @return bool
*/
public static function has_context(string $classname): bool {
return self::$environment->hasContextClass($classname);
}
/**
* Translates string to XPath literal.
*
* @param string $label label to escape
* @return string escaped string.
*/
public static function escape($label) {
if (empty(self::$escaper)) {
self::$escaper = new \Behat\Mink\Selector\Xpath\Escaper();
}
return self::$escaper->escapeLiteral($label);
}
}
File diff suppressed because it is too large Load Diff
+626
View File
@@ -0,0 +1,626 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Base class for data generators component support for acceptance testing.
*
* @package core
* @category test
* @copyright 2012 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/../../behat/behat_base.php');
use Behat\Gherkin\Node\TableNode as TableNode;
use Behat\Behat\Tester\Exception\PendingException as PendingException;
/**
* Class to quickly create Behat test data using component data generators.
*
* There is a subclass of class for each component that wants to be able to
* generate entities using the Behat step
* Given the following "entity types" exist:
* | test | data |
*
* For core entities, the entity type is like "courses" or "users" and
* generating those is handled by behat_core_generator. For other components
* the entity type is like "mod_quiz > User override" and that is handled by
* behat_mod_quiz_generator defined in mod/quiz/tests/generator/behat_mod_quiz_generator.php.
*
* The types of entities that can be generated are described by the array returned
* by the {@link get_generateable_entities()} method. The list in
* {@link behat_core_generator} is a good (if complex) example.
*
* How things work is best explained with a few examples. All this is implemented
* in the {@link generate_items()} method below, if you want to see every detail of
* how it works.
*
* Simple example from behat_core_generator:
* 'users' => [
* 'datagenerator' => 'user',
* 'required' => ['username'],
* ],
* The steps performed are:
*
* 1. 'datagenerator' => 'user' means that the word used in the method names below is 'user'.
*
* 2. Because 'required' is present, check the supplied data exists 'username' column is present
* in the supplied data table and if not display an error.
*
* 3. Then for each row in the table as an array $elementdata (array keys are column names)
* and process it as follows
*
* 4. (Not used in this example.)
*
* 5. If the method 'preprocess_user' exists, then call it to update $elementdata.
* (It does, in this case it sets the password to the username, if password was not given.)
*
* We then do one of 4 things:
*
* 6a. If there is a method 'process_user' we call it. (It doesn't for user,
* but there are other examples like process_enrol_user() in behat_core_generator.)
*
* 6b. (Not used in this example.)
*
* 6c. Else, if testing_data_generator::create_user exists, we call it with $elementdata. (it does.)
*
* 6d. If none of these three things work. an error is thrown.
*
* To understand the missing steps above, consider the example from behat_mod_quiz_generator:
* 'group override' => [
* 'datagenerator' => 'override',
* 'required' => ['quiz', 'group'],
* 'switchids' => ['quiz' => 'quiz', 'group' => 'groupid'],
* ],
* Processing is as above, except that:
*
* 1. Note 'datagenerator' is 'override' (not group_override). 'user override' maps to the
* same datagenerator. This works fine.
*
* 4. Because 'switchids' is present, human-readable data in the table gets converted to ids.
* They array key 'group' refers to a column which may be present in the table (it will be
* here because it is required, but it does not have to be in general). If that column
* is present and contains a value, then the method matching name like get_group_id() is
* called with the value from that column in the data table. You must implement this
* method. You can see several examples of this sort of method below.
*
* If that method returns a group id, then $elementdata['group'] is unset and
* $elementdata['groupid'] is set to the result of the get_group_id() call. 'groupid' here
* because of the definition is 'switchids' => [..., 'group' => 'groupid'].
* If get_group_id() cannot find the group, it should throw a helpful exception.
*
* Similarly, 'quiz' (the quiz name) is looked up with a call to get_quiz_id(). Here, the
* new array key set matches the old one removed. This is fine.
*
* 6b. We are in a plugin, so before checking whether testing_data_generator::create_override
* exists we first check whether mod_quiz_generator::create_override() exists. It does,
* and this is what gets called.
*
* This second example shows why the get_..._id methods for core entities are in this base
* class, not in behat_core_generator. Plugins may need to look up the ids of
* core entities.
*
* behat_core_generator is defined in lib/behat/classes/behat_core_generator.php
* and for components, behat_..._generator is defined in tests/generator/behat_..._generator.php
* inside the plugin. For example behat_mod_quiz_generator is defined in
* mod/quiz/tests/generator/behat_mod_quiz_generator.php.
*
* @package core
* @category test
* @copyright 2012 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class behat_generator_base {
/**
* @var string the name of the component we belong to.
*
* This should probably only be used to make error messages clearer.
*/
protected $component;
/**
* @var testing_data_generator the core data generator
*/
protected $datagenerator;
/**
* @var testing_data_generator the data generator for this component.
*/
protected $componentdatagenerator;
/**
* Constructor.
*
* @param string $component component name, to make error messages more readable.
*/
public function __construct(string $component) {
$this->component = $component;
}
/**
* Get a list of the entities that can be created for this component.
*
* This function must be overridden in subclasses. See class comment
* above for a description of the data structure.
* See {@link behat_core_generator} for an example.
*
* @return array entity name => information about how to generate.
*/
abstract protected function get_creatable_entities(): array;
/**
* Get the list of available generators for this class.
*
* @return array
*/
final public function get_available_generators(): array {
return $this->get_creatable_entities();
}
/**
* Do the work to generate an entity.
*
* This is called by {@link behat_data_generators::the_following_entities_exist()}.
*
* @param string $generatortype The name of the entity to create.
* @param TableNode $data from the step.
* @param bool $singular Whether there is only one record and it is pivotted
*/
public function generate_items(string $generatortype, TableNode $data, bool $singular = false) {
// Now that we need them require the data generators.
require_once(__DIR__ . '/../../testing/generator/lib.php');
$elements = $this->get_creatable_entities();
foreach ($elements as $key => $configuration) {
if (array_key_exists('singular', $configuration)) {
$singularverb = $configuration['singular'];
unset($configuration['singular']);
unset($elements[$key]['singular']);
$elements[$singularverb] = $configuration;
}
}
if (!isset($elements[$generatortype])) {
throw new PendingException($this->name_for_errors($generatortype) .
' is not a known type of entity that can be generated.');
}
$entityinfo = $elements[$generatortype];
$this->datagenerator = testing_util::get_data_generator();
if ($this->component === 'core') {
$this->componentdatagenerator = $this->datagenerator;
} else {
$this->componentdatagenerator = $this->datagenerator->get_plugin_generator($this->component);
}
$generatortype = $entityinfo['datagenerator'];
if ($singular) {
// There is only one record to generate, and the table has been pivotted.
// The rows each represent a single field.
$rows = [$data->getRowsHash()];
} else {
// There are multiple records to generate.
// The rows represent an item to create.
$rows = $data->getHash();
}
foreach ($rows as $elementdata) {
// Check if all the required fields are there.
foreach ($entityinfo['required'] as $requiredfield) {
if (!isset($elementdata[$requiredfield])) {
throw new Exception($this->name_for_errors($generatortype) .
' requires the field ' . $requiredfield . ' to be specified');
}
}
// Switch from human-friendly references to ids.
if (!empty($entityinfo['switchids'])) {
foreach ($entityinfo['switchids'] as $element => $field) {
$methodname = 'get_' . $element . '_id';
// Not all the switch fields are required, default vars will be assigned by data generators.
if (isset($elementdata[$element])) {
if (!method_exists($this, $methodname)) {
throw new coding_exception('The generator for ' .
$this->name_for_errors($generatortype) .
' entities specifies \'switchids\' => [..., \'' . $element .
'\' => \'' . $field . '\', ...] but the required method ' .
$methodname . '() has not been defined in ' .
get_class($this) . '.');
}
// Temp $id var to avoid problems when $element == $field.
$id = $this->{$methodname}($elementdata[$element]);
unset($elementdata[$element]);
$elementdata[$field] = $id;
}
}
}
// Preprocess the entities that requires a special treatment.
if (method_exists($this, 'preprocess_' . $generatortype)) {
$elementdata = $this->{'preprocess_' . $generatortype}($elementdata);
}
// Creates element.
if (method_exists($this, 'process_' . $generatortype)) {
// Use a method on this class to do the work.
$this->{'process_' . $generatortype}($elementdata);
} else if (method_exists($this->componentdatagenerator, 'create_' . $generatortype)) {
// Using the component't own data generator if it exists.
$this->componentdatagenerator->{'create_' . $generatortype}($elementdata);
} else if (method_exists($this->datagenerator, 'create_' . $generatortype)) {
// Use a method on the core data geneator, if there is one.
$this->datagenerator->{'create_' . $generatortype}($elementdata);
} else {
// Give up.
throw new PendingException($this->name_for_errors($generatortype) .
' data generator is not implemented');
}
}
// Notify that the all the elements have been generated.
if (method_exists($this->componentdatagenerator, 'finish_generate_' . $generatortype)) {
// Using the component's own data generator if it exists.
$this->componentdatagenerator->{'finish_generate_' . $generatortype}();
} else if (method_exists($this->datagenerator, 'finish_generate_' . $generatortype)) {
// Use a method on the core data geneator, if there is one.
$this->datagenerator->{'finish_generate_' . $generatortype}();
}
}
/**
* Helper for formatting error messages.
*
* @param string $entitytype entity type without prefix, e.g. 'frog'.
* @return string either 'frog' for core entities, or 'mod_mymod > frog' for components.
*/
protected function name_for_errors(string $entitytype): string {
if ($this->component === 'core') {
return '"' . $entitytype . '"';
} else {
return '"' . $this->component . ' > ' . $entitytype . '"';
}
}
/**
* Gets the grade category id from the grade category fullname
*
* @param string $fullname the grade category name.
* @return int corresponding id.
*/
protected function get_gradecategory_id($fullname) {
global $DB;
if (!$id = $DB->get_field('grade_categories', 'id', array('fullname' => $fullname))) {
throw new Exception('The specified grade category with fullname "' . $fullname . '" does not exist');
}
return $id;
}
/**
* Gets the user id from it's username.
* @throws Exception
* @param string $username
* @return int
*/
protected function get_user_id($username) {
global $DB;
if (!$id = $DB->get_field('user', 'id', array('username' => $username))) {
throw new Exception('The specified user with username "' . $username . '" does not exist');
}
return $id;
}
/**
* Gets the user id from it's username.
* @throws Exception
* @param string $username
* @return int
*/
protected function get_userfrom_id(string $username) {
global $DB;
if (!$id = $DB->get_field('user', 'id', ['username' => $username])) {
throw new Exception('The specified user with username "' . $username . '" does not exist');
}
return $id;
}
/**
* Gets the user id from it's username.
* @throws Exception
* @param string $username
* @return int
*/
protected function get_userto_id(string $username) {
global $DB;
if (!$id = $DB->get_field('user', 'id', ['username' => $username])) {
throw new Exception('The specified user with username "' . $username . '" does not exist');
}
return $id;
}
/**
* Gets the role id from it's shortname.
* @throws Exception
* @param string $roleshortname
* @return int
*/
protected function get_role_id($roleshortname) {
global $DB;
if (!$id = $DB->get_field('role', 'id', array('shortname' => $roleshortname))) {
throw new Exception('The specified role with shortname "' . $roleshortname . '" does not exist');
}
return $id;
}
/**
* Gets the category id from it's idnumber.
* @throws Exception
* @param string $idnumber
* @return int
*/
protected function get_category_id($idnumber) {
global $DB;
// If no category was specified use the data generator one.
if ($idnumber == false) {
return null;
}
if (!$id = $DB->get_field('course_categories', 'id', array('idnumber' => $idnumber))) {
throw new Exception('The specified category with idnumber "' . $idnumber . '" does not exist');
}
return $id;
}
/**
* Gets the course id from it's shortname.
* @throws Exception
* @param string $shortname
* @return int
*/
protected function get_course_id($shortname) {
global $DB;
if (!$id = $DB->get_field('course', 'id', array('shortname' => $shortname))) {
throw new Exception('The specified course with shortname "' . $shortname . '" does not exist');
}
return $id;
}
/**
* Gets the course cmid for the specified activity based on the activity's idnumber.
*
* Note: this does not check the module type, only the idnumber.
*
* @throws Exception
* @param string $idnumber
* @return int
*/
protected function get_activity_id(string $idnumber) {
global $DB;
if (!$id = $DB->get_field('course_modules', 'id', ['idnumber' => $idnumber])) {
throw new Exception('The specified activity with idnumber "' . $idnumber . '" could not be found.');
}
return $id;
}
/**
* Gets the group id from it's idnumber.
* @throws Exception
* @param string $idnumber
* @return int
*/
protected function get_group_id($idnumber) {
global $DB;
if (!$id = $DB->get_field('groups', 'id', array('idnumber' => $idnumber))) {
throw new Exception('The specified group with idnumber "' . $idnumber . '" does not exist');
}
return $id;
}
/**
* Gets the grouping id from it's idnumber.
* @throws Exception
* @param string $idnumber
* @return int
*/
protected function get_grouping_id($idnumber) {
global $DB;
// Do not fetch grouping ID for empty grouping idnumber.
if (empty($idnumber)) {
return null;
}
if (!$id = $DB->get_field('groupings', 'id', array('idnumber' => $idnumber))) {
throw new Exception('The specified grouping with idnumber "' . $idnumber . '" does not exist');
}
return $id;
}
/**
* Gets the cohort id from it's idnumber.
* @throws Exception
* @param string $idnumber
* @return int
*/
protected function get_cohort_id($idnumber) {
global $DB;
if (!$id = $DB->get_field('cohort', 'id', array('idnumber' => $idnumber))) {
throw new Exception('The specified cohort with idnumber "' . $idnumber . '" does not exist');
}
return $id;
}
/**
* Gets the outcome item id from its shortname.
* @throws Exception
* @param string $shortname
* @return int
*/
protected function get_outcome_id($shortname) {
global $DB;
if (!$id = $DB->get_field('grade_outcomes', 'id', array('shortname' => $shortname))) {
throw new Exception('The specified outcome with shortname "' . $shortname . '" does not exist');
}
return $id;
}
/**
* Get the id of a named scale.
* @param string $name the name of the scale.
* @return int the scale id.
*/
protected function get_scale_id($name) {
global $DB;
if (!$id = $DB->get_field('scale', 'id', array('name' => $name))) {
throw new Exception('The specified scale with name "' . $name . '" does not exist');
}
return $id;
}
/**
* Get the id of a named question category (must be globally unique).
* Note that 'Top' is a special value, used when setting the parent of another
* category, meaning top-level.
*
* @param string $name the question category name.
* @return int the question category id.
*/
protected function get_questioncategory_id($name) {
global $DB;
if ($name == 'Top') {
return 0;
}
if (!$id = $DB->get_field('question_categories', 'id', array('name' => $name))) {
throw new Exception('The specified question category with name "' . $name . '" does not exist');
}
return $id;
}
/**
* Gets the internal context id from the context reference.
*
* The context reference changes depending on the context
* level, it can be the system, a user, a category, a course or
* a module.
*
* @throws Exception
* @param string $levelname The context level string introduced by the test writer
* @param string $contextref The context reference introduced by the test writer
* @return context
*/
protected function get_context($levelname, $contextref) {
return behat_base::get_context($levelname, $contextref);
}
/**
* Gets the contact id from it's username.
* @throws Exception
* @param string $username
* @return int
*/
protected function get_contact_id($username) {
global $DB;
if (!$id = $DB->get_field('user', 'id', array('username' => $username))) {
throw new Exception('The specified user with username "' . $username . '" does not exist');
}
return $id;
}
/**
* Gets the external backpack id from it's backpackweburl.
* @param string $backpackweburl
* @return mixed
* @throws dml_exception
*/
protected function get_externalbackpack_id($backpackweburl) {
global $DB;
if (!$id = $DB->get_field('badge_external_backpack', 'id', ['backpackweburl' => $backpackweburl])) {
throw new Exception('The specified external backpack with backpackweburl "' . $username . '" does not exist');
}
return $id;
}
/**
* Get a coursemodule from an activity name or idnumber.
*
* @param string $activity
* @param string $identifier
* @return cm_info
*/
protected function get_cm_by_activity_name(string $activity, string $identifier): cm_info {
global $DB;
$coursetable = new \core\dml\table('course', 'c', 'c');
$courseselect = $coursetable->get_field_select();
$coursefrom = $coursetable->get_from_sql();
$cmtable = new \core\dml\table('course_modules', 'cm', 'cm');
$cmfrom = $cmtable->get_from_sql();
$acttable = new \core\dml\table($activity, 'a', 'a');
$actselect = $acttable->get_field_select();
$actfrom = $acttable->get_from_sql();
$sql = <<<EOF
SELECT cm.id as cmid, {$courseselect}, {$actselect}
FROM {$cmfrom}
INNER JOIN {$coursefrom} ON c.id = cm.course
INNER JOIN {modules} m ON m.id = cm.module AND m.name = :modname
INNER JOIN {$actfrom} ON cm.instance = a.id
WHERE cm.idnumber = :idnumber OR a.name = :name
EOF;
$result = $DB->get_record_sql($sql, [
'modname' => $activity,
'idnumber' => $identifier,
'name' => $identifier,
], MUST_EXIST);
$course = $coursetable->extract_from_result($result);
$instancedata = $acttable->extract_from_result($result);
return get_fast_modinfo($course)->get_cm($result->cmid);
}
}
+121
View File
@@ -0,0 +1,121 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Moodle-specific selectors.
*
* @package core
* @category test
* @copyright 2013 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/named_selector.php');
require_once(__DIR__ . '/exact_named_selector.php');
require_once(__DIR__ . '/partial_named_selector.php');
use Behat\Mink\Exception\ExpectationException as ExpectationException;
use Behat\Mink\Element\Element;
/**
* Moodle selectors manager.
*
* @package core
* @category test
* @copyright 2013 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_selectors {
/**
* Returns the behat selector and locator for a given moodle selector and locator
*
* @param string $selectortype The moodle selector type, which includes moodle selectors
* @param string $element The locator we look for in that kind of selector
* @param Session $session The Mink opened session
* @return array Contains the selector and the locator expected by Mink.
*/
public static function get_behat_selector($selectortype, $element, Behat\Mink\Session $session) {
// Note: This function is not deprecated, but not the recommended way of doing things.
[
'selector' => $selector,
'locator' => $locator,
] = $session->normalise_selector($selectortype, $element, $session->getPage());
// CSS and XPath selectors locator is one single argument.
return [$selector, $locator];
}
/**
* Allowed selectors getter.
*
* @return array
*/
public static function get_allowed_selectors() {
return array_merge(
behat_partial_named_selector::get_allowed_selectors(),
behat_exact_named_selector::get_allowed_selectors()
);
}
/**
* Allowed text selectors getter.
*
* @return array
*/
public static function get_allowed_text_selectors() {
return array_merge(
behat_partial_named_selector::get_allowed_text_selectors(),
behat_exact_named_selector::get_allowed_text_selectors()
);
}
/**
* Normalise the selector and locator for a named partial.
*
* @param string $selector The selector name
* @param string $locator The value to normalise
* @return array
*/
public static function normalise_named_selector(string $selector, string $locator): array {
return [
$selector,
behat_context_helper::escape($locator),
];
}
/**
* Transform the selector for a field.
*
* @param string $label The label to find
* @param Element $container The container to look within
* @return array The selector, locator, and container to search within
*/
public static function transform_find_for_field(behat_base $context, string $label, Element $container): array {
$hasfieldset = strpos($label, '>');
if (false !== $hasfieldset) {
[$containerlabel, $label] = explode(">", $label, 2);
$container = $context->find_fieldset(trim($containerlabel), $container);
$label = trim($label);
}
return [
'selector' => 'named_partial',
'locator' => self::normalise_named_selector('field', $label),
'container' => $container,
];
}
}
@@ -0,0 +1,56 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* The Interface for a behat root context.
*
* @package core
* @category test
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* The Interface for a behat root context.
*
* This interface should be implemented by the behat_base context, and behat form fields, and it should be paired with
* the behat_session_trait.
*
* It should not be necessary to implement this interface, and the behat_session_trait trait in normal circumstances.
*
* @package core
* @category test
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
interface behat_session_interface {
/**
* The JS code to check that the page is ready.
*
* The document must be complete and either M.util.pending_js must be empty, or it must not be defined at all.
*/
const PAGE_READY_JS = "document.readyState === 'complete' && " .
"(typeof M !== 'object' || typeof M.util !== 'object' || " .
"typeof M.util.pending_js === 'undefined' || M.util.pending_js.length === 0)";
/**
* Returns the Mink session.
*
* @param string|null $name name of the session OR active session will be used
* @return \Behat\Mink\Session
*/
public function getSession($name = null);
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,99 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* A class for recording the definition of Mink replacements.
*
* @package core
* @category test
* @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* A class for recording the definition of Mink replacements for use in Mink selectors.
*
* These are comprised of a source string, and a replacement.
*
* During use the source string is converted from the string to be in the format:
*
* %[component]/[string]%
*
* For example:
*
* %mod_forum/title%
*
* Mink replacements are used in xpath translation to translate regularly used items such as title.
* Here is an example from the upstream Mink project:
*
* '%tagTextMatch%' => 'contains(normalize-space(string(.)), %locator%)'
*
* And can be used in an xpath:
*
* .//label[%tagTextMatch%]
*
* This would be expanded to:
*
* .//label[contains(normalize-space(string(.)), %locator%)]
*
* Replacements can also be used in other replacements, as long as that replacement is defined later.
*
* '%linkMatch%' => '(%idMatch% or %tagTextMatch% or %titleMatch% or %relMatch%)'
*
* @package core
* @category test
* @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_component_named_replacement {
/** @var string */
protected $from;
/** @var string */
protected $to;
/**
* Create the replacement.
*
* @param string $from this is the old selector that should no longer be used.
* For example 'group_message'.
* @param string $to this is the new equivalent that should be used instead.
* For example 'core_message > Message'.
*/
public function __construct(string $from, string $to) {
$this->from = $from;
$this->to = $to;
}
/**
* Get the 'from' part of the replacement, formatted for the component.
*
* @param string $component
* @return string
*/
public function get_from(string $component): string {
return "%{$component}/{$this->from}%";
}
/**
* Get the 'to' part of the replacement.
*
* @return string Target xpath
*/
public function get_to(): string {
return $this->to;
}
}
@@ -0,0 +1,124 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Class representing a named selector that can be used in Behat tests.
*
* @package core
* @category test
* @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Class representing a named selector that can be used in Behat tests.
*
* Named selectors are what make Behat steps like
* Then I should see "Useful text" in the "General" "fieldset"
* Here, "fieldset" is the named selector, and "General" is the locator.
*
* Selectors can either be exact, in which case the locator needs to
* match exactly, or can be partial, for example the way
* When I click "Save" "button"
* will trigger a "Save changes" button.
*
* Instances of this class get returned by the get_exact_named_selectors()
* and get_partial_named_selectors() methods in classes like behat_mod_mymod.
* The code that makes the magic work is in the trait behat_named_selector
* used by both behat_exact_named_selector and behat_partial_named_selector.
*
* @package core
* @category test
* @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_component_named_selector {
/** @var string */
protected $alias;
/** @var array List of xpaths */
protected $xpaths;
/** @var string */
protected $istextselector;
/**
* Create the selector definition.
*
* As an example, if you define
* new behat_component_named_selector('Message',
* [".//*[@data-conversation-id]//img[contains(@alt, %locator%)]/.."])
* in get_partial_named_selectors in behat_message in
* message/tests/behat/behat_message.php, then steps like
* When "Group 1" "core_message > Message" should exist
* will work.
*
* Text selectors are things that contain other things (e.g. some particular text), e.g.
* Then I can see "Some text" in the "Whatever" "text_selector"
* whereas non-text selectors are atomic things, like
* When I click the "Whatever" "widget".
*
* @param string $alias The 'friendly' name of the thing. This will be prefixed with the component name.
* For example, if the mod_mymod plugin, says 'Thingy', then "mod_mymod > Thingy" becomes a selector.
* @param array $xpaths A list of xpaths one or more XPaths that the selector gets transformed into.
* @param bool $istextselector Whether this selector can also be used as a text selector.
*/
public function __construct(string $alias, array $xpaths, bool $istextselector = true) {
$this->alias = $alias;
$this->xpaths = $xpaths;
$this->istextselector = $istextselector;
}
/**
* Whether this is a text selector.
*
* @return bool
*/
public function is_text_selector(): bool {
return $this->istextselector;
}
/**
* Get the name of the selector.
* This is a back-end feature and contains a namespaced md5 of the human-readable name.
*
* @param string $component
* @return string
*/
public function get_name(string $component): string {
return implode('_', [$component, md5($this->alias)]);
}
/**
* Get the alias of the selector.
* This is the human-readable name that you would typically interact with.
*
* @param string $component
* @return string
*/
public function get_alias(string $component): string {
return implode(" > ", [$component, $this->alias]);;
}
/**
* Get the list of combined xpaths.
*
* @return string The list of xpaths combined with the xpath | (OR) operator
*/
public function get_combined_xpath(): string {
return implode(' | ', $this->xpaths);
}
}
@@ -0,0 +1,89 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Moodle-specific named exact selectors.
*
* @package core
* @category test
* @copyright 2016 Andrew Nicols
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Moodle selectors manager.
*
* @package core
* @copyright 2016 Andrew Nicols
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_exact_named_selector extends \Behat\Mink\Selector\ExactNamedSelector {
// Use the named selector trait.
use behat_named_selector;
/**
* Creates selector instance.
*/
public function __construct() {
$this->registerReplacement('%iconMatch%', "(contains(concat(' ', @class, ' '), ' icon ') or self::img)");
$this->registerReplacement('%imgAltMatch%', './/*[%iconMatch% and (%altMatch% or %titleMatch%)]');
parent::__construct();
}
/**
* @var Allowed types when using text selectors arguments.
*/
protected static $allowedtextselectors = [];
/**
* @var Allowed types when using selector arguments.
*/
protected static $allowedselectors = array(
'button_exact' => 'button',
'checkbox_exact' => 'checkbox',
'field_exact' => 'field',
'fieldset_exact' => 'fieldset',
'link_exact' => 'link',
'link_or_button_exact' => 'link_or_button',
'option_exact' => 'option',
'radio_exact' => 'radio',
'select_exact' => 'select',
'table_exact' => 'table',
'text_exact' => 'text',
);
/** @var List of deprecated selectors */
protected static $deprecatedselectors = [];
/**
* Allowed selectors getter.
*
* @return array
*/
public static function get_allowed_selectors() {
return static::$allowedselectors;
}
/**
* Allowed text selectors getter.
*
* @return array
*/
public static function get_allowed_text_selectors() {
return static::$allowedtextselectors;
}
}
+111
View File
@@ -0,0 +1,111 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Moodle-specific common functions for named selectors.
*
* @package core
* @category test
* @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Common functions for named selectors.
*
* This has to be a trait, because we need this in both the classes
* behat_exact_named_selector and behat_partial_named_selector, and
* those classes have to be subclasses of \Behat\Mink\Selector\ExactNamedSelector
* and \Behat\Mink\Selector\PartialNamedSelector. This trait is a way achieve
* that without duplciated code.
*
* @package core
* @category test
* @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
trait behat_named_selector {
/**
* Registers new XPath selector with specified name.
*
* @param string $component
* @param behat_component_named_selector $selector
*/
public function register_component_selector(string $component, behat_component_named_selector $selector) {
$alias = $selector->get_alias($component);
$name = $selector->get_name($component);
static::$allowedselectors[$alias] = $name;
if ($selector->is_text_selector()) {
static::$allowedtextselectors[$alias] = $name;
}
// We must use Reflection here. The replacements property is private and cannot be accessed otherwise.
// This is due to an API limitation in Mink.
$rc = new \ReflectionClass(\Behat\Mink\Selector\NamedSelector::class);
$r = $rc->getProperty('replacements');
$replacements = $r->getValue($this);
$selectorxpath = strtr($selector->get_combined_xpath(), $replacements);
parent::registerNamedXpath($name, $selectorxpath);
}
/**
* Registers new XPath selector with specified name.
*
* @param string $component
* @param behat_component_named_replacement $replacement
*/
public function register_replacement(string $component, behat_component_named_replacement $replacement) {
// We must use Reflection here. The replacements property is private and cannot be accessed otherwise.
// This is due to an API limitation in Mink.
$rc = new \ReflectionClass(\Behat\Mink\Selector\NamedSelector::class);
$r = $rc->getProperty('replacements');
$existing = $r->getValue($this);
$from = $replacement->get_from($component);
if (isset($existing[$from])) {
throw new \coding_exception("A named replacement already exists in the partial named selector for '{$from}'. " .
"Replacement names must be unique, and should be namespaced to the component");
}
$translatedto = strtr($replacement->get_to(), $existing);
$this->registerReplacement($from, $translatedto);
}
/**
* Check whether the specified selector has been deprecated and marked for replacement.
*
* @param string $selector
* @return bool
*/
public static function is_deprecated_selector(string $selector): bool {
return array_key_exists($selector, static::$deprecatedselectors);
}
/**
* Fetch the replacement name of a deprecated selector.
*
* @param string $selector
* @return bool
*/
public static function get_deprecated_replacement(string $selector): ?string {
return static::$deprecatedselectors[$selector];
}
}
@@ -0,0 +1,386 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Moodle-specific selectors.
*
* @package core
* @category test
* @copyright 2013 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Moodle selectors manager.
*
* @package core
* @category test
* @copyright 2013 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_partial_named_selector extends \Behat\Mink\Selector\PartialNamedSelector {
// Use the named selector trait.
use behat_named_selector;
/**
* Creates selector instance.
*/
public function __construct() {
foreach (self::$customselectors as $alias => $selectors) {
$this->registerNamedXpath($alias, implode(' | ', $selectors));
}
foreach (static::$moodleselectors as $name => $xpath) {
$this->registerNamedXpath($name, $xpath);
}
foreach (self::$customreplacements as $from => $tos) {
$this->registerReplacement($from, implode(' or ', $tos));
}
$this->registerReplacement('%iconMatch%', "(contains(concat(' ', @class, ' '), ' icon ') or self::img)");
$this->registerReplacement('%imgAltMatch%', './/*[%iconMatch% and (%altMatch% or %titleMatch%)]');
parent::__construct();
}
/**
* @var array Allowed types when using text selectors arguments.
*/
protected static $allowedtextselectors = array(
'activity' => 'activity',
'block' => 'block',
'css_element' => 'css_element',
'dialogue' => 'dialogue',
'dropdown_item' => 'dropdown_item',
'fieldset' => 'fieldset',
'icon' => 'icon',
'list_item' => 'list_item',
'question' => 'question',
'region' => 'region',
'section' => 'section',
'table' => 'table',
'table_row' => 'table_row',
'xpath_element' => 'xpath_element',
'form_row' => 'form_row',
'group_message_header' => 'group_message_header',
'group_message' => 'group_message',
'autocomplete' => 'autocomplete',
'iframe' => 'iframe',
'option_role' => 'option_role',
);
/**
* @var array Allowed types when using selector arguments.
*/
protected static $allowedselectors = array(
'activity' => 'activity',
'actionmenu' => 'actionmenu',
'badge' => 'badge',
'block' => 'block',
'button' => 'button',
'checkbox' => 'checkbox',
'combobox' => 'combobox',
'css_element' => 'css_element',
'dialogue' => 'dialogue',
'dropdown' => 'dropdown',
'dropdown_item' => 'dropdown_item',
'field' => 'field',
'fieldset' => 'fieldset',
'file' => 'file',
'filemanager' => 'filemanager',
'group_message' => 'group_message',
'group_message_conversation' => 'group_message_conversation',
'group_message_header' => 'group_message_header',
'group_message_member' => 'group_message_member',
'group_message_tab' => 'group_message_tab',
'group_message_list_area' => 'group_message_list_area',
'group_message_message_content' => 'group_message_message_content',
'heading' => 'heading',
'icon_container' => 'icon_container',
'icon' => 'icon',
'link' => 'link',
'link_or_button' => 'link_or_button',
'list_item' => 'list_item',
'menuitem' => 'menuitem',
'optgroup' => 'optgroup',
'option' => 'option',
'option_role' => 'option_role',
'question' => 'question',
'radio' => 'radio',
'region' => 'region',
'section' => 'section',
'select' => 'select',
'table' => 'table',
'table_row' => 'table_row',
'text' => 'text',
'xpath_element' => 'xpath_element',
'form_row' => 'form_row',
'autocomplete_selection' => 'autocomplete_selection',
'autocomplete_suggestions' => 'autocomplete_suggestions',
'autocomplete' => 'autocomplete',
'iframe' => 'iframe',
);
/**
* Behat by default comes with XPath, CSS and named selectors,
* named selectors are a mapping between names (like button) and
* xpaths that represents that names and includes a placeholder that
* will be replaced by the locator. These are Moodle's own xpaths.
*
* @var array XPaths for moodle elements.
*/
protected static $moodleselectors = array(
'activity' => <<<XPATH
.//li[contains(concat(' ', normalize-space(@class), ' '), ' activity ')][descendant::*[contains(normalize-space(.), %locator%)]]
XPATH
, 'actionmenu' => <<<XPATH
.//*[
contains(concat(' ', normalize-space(@class), ' '), ' action-menu ')
and
descendant::*[
contains(concat(' ', normalize-space(@class), ' '), ' dropdown-toggle ')
and
(contains(normalize-space(.), %locator%) or descendant::*[%titleMatch%])
]
]
XPATH
, 'badge' => <<<XPATH
.//*[self::span or self::button][(contains(@class, 'badge')) and text()[contains(., %locator%)]]
XPATH
, 'block' => <<<XPATH
.//*[@data-block][contains(concat(' ', normalize-space(@class), ' '), concat(' ', %locator%, ' ')) or
descendant::*[self::h2|self::h3|self::h4|self::h5][normalize-space(.) = %locator%] or
@aria-label = %locator%]
XPATH
, 'combobox' => <<<XPATH
.//*[@role='combobox'][%titleMatch% or %ariaLabelMatch% or text()[contains(., %locator%)]]
XPATH
, 'dialogue' => <<<XPATH
.//div[contains(concat(' ', normalize-space(@class), ' '), ' moodle-dialogue ') and
not(contains(concat(' ', normalize-space(@class), ' '), ' moodle-dialogue-hidden ')) and
normalize-space(descendant::div[
contains(concat(' ', normalize-space(@class), ' '), ' moodle-dialogue-hd ')
]) = %locator%] |
.//div[contains(concat(' ', normalize-space(@class), ' '), ' yui-dialog ') and
normalize-space(descendant::div[@class='hd']) = %locator%]
|
.//div[@data-region='modal' and descendant::*[@data-region='title'] = %locator%]
|
.//div[
contains(concat(' ', normalize-space(@class), ' '), ' modal-content ')
and
normalize-space(descendant::*[self::h4 or self::h5][contains(concat(' ', normalize-space(@class), ' '), ' modal-title ')]) = %locator%
]
|
.//div[
contains(concat(' ', normalize-space(@class), ' '), ' modal ')
and
normalize-space(descendant::*[contains(concat(' ', normalize-space(@class), ' '), ' modal-header ')]) = %locator%
]
XPATH
, 'dropdown' => <<<XPATH
.//*[
contains(concat(' ', normalize-space(@class), ' '), ' dropdown-menu ')
and
@aria-labelledby =
(//*[
contains(concat(' ', normalize-space(@class), ' '), ' dropdown-toggle ')
and
(contains(normalize-space(.), %locator%) or descendant::*[%titleMatch%])
]/@id)
]
XPATH
, 'dropdown_item' => <<<XPATH
.//*[
@role = 'listitem'
and
(contains(normalize-space(.), %locator%) or descendant::*[%titleMatch%])
]
XPATH
, 'group_message' => <<<XPATH
.//*[@data-conversation-id]//img[contains(@alt, %locator%)]/..
XPATH
, 'group_message_conversation' => <<<XPATH
.//*[@data-region='message-drawer' and contains(., %locator%)]//div[@data-region='content-message-container']
XPATH
, 'group_message_header' => <<<XPATH
.//*[@data-region='message-drawer']//div[@data-region='header-content' and contains(., %locator%)]
XPATH
, 'group_message_member' => <<<XPATH
.//*[@data-region='message-drawer']//div[@data-region='group-info-content-container']
//div[@class='list-group' and not(contains(@class, 'hidden'))]//*[text()[contains(., %locator%)]] |
.//*[@data-region='message-drawer']//div[@data-region='group-info-content-container']
//div[@data-region='empty-message-container' and not(contains(@class, 'hidden')) and contains(., %locator%)]
XPATH
, 'group_message_tab' => <<<XPATH
.//*[@data-region='message-drawer']//button[@data-toggle='collapse' and contains(string(), %locator%)]
XPATH
, 'group_message_list_area' => <<<XPATH
.//*[@data-region='message-drawer']//*[contains(@data-region, concat('view-overview-', %locator%))]
XPATH
, 'group_message_message_content' => <<<XPATH
.//*[@data-region='message-drawer']//*[@data-region='message' and @data-message-id and contains(., %locator%)]
XPATH
, 'heading' => <<<XPATH
.//*[self::h1 or self::h2 or self::h3 or self::h4 or self::h5 or self::h6][contains(normalize-space(.), %locator%)]
XPATH
, 'icon_container' => <<<XPATH
.//span[contains(@data-region, concat(%locator%,'-icon-container'))]
XPATH
, 'icon' => <<<XPATH
.//*[contains(concat(' ', normalize-space(@class), ' '), ' icon ') and ( contains(normalize-space(@title), %locator%))]
XPATH
, 'list_item' => <<<XPATH
.//li[contains(normalize-space(.), %locator%) and not(.//li[contains(normalize-space(.), %locator%)])]
XPATH
, 'menuitem' => <<<XPATH
.//*[@role='menuitem'][%titleMatch% or %ariaLabelMatch% or text()[contains(., %locator%)]]
XPATH
, 'option_role' => <<<XPATH
.//*[@role='option'][%titleMatch% or %ariaLabelMatch% or text()[contains(., %locator%)]] |
.//*[@role='option']/following-sibling::label[contains(., %locator%)]/preceding-sibling::input
XPATH
, 'question' => <<<XPATH
.//div[contains(concat(' ', normalize-space(@class), ' '), ' que ')]
[contains(div[@class='content']/div[contains(concat(' ', normalize-space(@class), ' '), ' formulation ')], %locator%)]
XPATH
, 'region' => <<<XPATH
.//*[self::div | self::section | self::aside | self::header | self::footer][./@id = %locator%]
XPATH
, 'section' => <<<XPATH
.//li[contains(concat(' ', normalize-space(@class), ' '), ' section ')][./descendant::*[self::h3]
[normalize-space(.) = %locator%][contains(concat(' ', normalize-space(@class), ' '), ' sectionname ') or
contains(concat(' ', normalize-space(@class), ' '), ' section-title ')]] |
.//div[contains(concat(' ', normalize-space(@class), ' '), ' sitetopic ')]
[./descendant::*[self::h2][normalize-space(.) = %locator%] or %locator% = 'frontpage']
XPATH
, 'table' => <<<XPATH
.//table[(./@id = %locator% or contains(.//caption, %locator%) or contains(.//th, %locator%) or contains(concat(' ', normalize-space(@class), ' '), %locator% ))]
XPATH
, 'table_row' => <<<XPATH
.//tr[contains(normalize-space(.), %locator%) and not(.//tr[contains(normalize-space(.), %locator%)])]
XPATH
, 'text' => <<<XPATH
.//*[contains(., %locator%) and not(.//*[contains(., %locator%)])]
XPATH
, 'form_row' => <<<XPATH
.//*[contains(concat(' ', @class, ' '), ' col-form-label ')]
[normalize-space(.)= %locator%]
/ancestor::*[contains(concat(' ', @class, ' '), ' fitem ')]
XPATH
, 'autocomplete_selection' => <<<XPATH
.//div[contains(concat(' ', normalize-space(@class), ' '), concat(' ', 'form-autocomplete-selection', ' '))]/span[@role='option'][contains(normalize-space(.), %locator%)]
XPATH
, 'autocomplete_suggestions' => <<<XPATH
.//ul[contains(concat(' ', normalize-space(@class), ' '), concat(' ', 'form-autocomplete-suggestions', ' '))]/li[@role='option'][contains(normalize-space(.), %locator%)]
XPATH
, 'autocomplete' => <<<XPATH
.//descendant::input[@id = //label[contains(normalize-space(string(.)), %locator%)]/@for]/ancestor::*[@data-fieldtype = 'autocomplete']
XPATH
, 'iframe' => <<<XPATH
.//iframe[(%idOrNameMatch% or (contains(concat(' ', normalize-space(@class), ' '), %locator% )))]
XPATH
);
protected static $customselectors = [
'field' => [
'upstream' => <<<XPATH
.//*
[%fieldFilterWithPlaceholder%][%notFieldTypeFilter%][%fieldMatchWithPlaceholder%]
|
.//label[%tagTextMatch%]//.//*[%fieldFilterWithPlaceholder%][%notFieldTypeFilter%]
|
.//*
[%fieldFilterWithoutPlaceholder%][%notFieldTypeFilter%][%fieldMatchWithoutPlaceholder%]
|
.//label[%tagTextMatch%]//.//*[%fieldFilterWithoutPlaceholder%][%notFieldTypeFilter%]
XPATH
,
'filemanager' => <<<XPATH
.//*[@data-fieldtype = 'filemanager' or @data-fieldtype = 'filepicker']
/descendant::input[@id = substring-before(//p[contains(normalize-space(string(.)), %locator%)]/@id, '_label')]
XPATH
,
'passwordunmask' => <<<XPATH
.//*[@data-passwordunmask='wrapper']
/descendant::input[@id = %locator% or @id = //label[contains(normalize-space(string(.)), %locator%)]/@for]
XPATH
,
'inplaceeditable' => <<<XPATH
.//descendant::span[@data-inplaceeditable][descendant::a[%titleMatch%]]
XPATH
,
'date_time' => <<<XPATH
.//fieldset[(%idMatch% or ./legend[%exactTagTextMatch%]) and (@data-fieldtype='date' or @data-fieldtype='date_time')]
XPATH
,
'select_menu' => <<<XPATH
//*[@role='combobox'][@aria-labelledby = //label[contains(normalize-space(string(.)), %locator%)]/@id]
XPATH
,
],
];
/**
* Mink comes with a number of named replacements.
* Sometimes we want to add our own.
*
* @var array XPaths for moodle elements.
*/
protected static $customreplacements = [
'%buttonMatch%' => [
'upstream' => '%idOrNameMatch% or %valueMatch% or %titleMatch%',
'aria' => '%ariaLabelMatch%',
],
'%ariaLabelMatch%' => [
'moodle' => 'contains(./@aria-label, %locator%)',
],
'%exactTagTextMatch%' => [
// This is based upon the upstream tagTextMatch but performs an exact match rather than a loose match using
// contains().
// If possible we should only use exact matches for any new form fields that we add.
'moodle' => 'normalize-space(text())=%locator%',
],
];
/** @var List of deprecated selectors */
protected static $deprecatedselectors = [
'group_message' => 'core_message > Message',
'group_message_member' => 'core_message > Message member',
'group_message_tab' => 'core_message > Message tab',
'group_message_list_area' => 'core_message > Message list area',
'group_message_message_content' => 'core_message > Message content',
];
/**
* Allowed selectors getter.
*
* @return array
*/
public static function get_allowed_selectors() {
return static::$allowedselectors;
}
/**
* Allowed text selectors getter.
*
* @return array
*/
public static function get_allowed_text_selectors() {
return static::$allowedtextselectors;
}
}
+37
View File
@@ -0,0 +1,37 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Editor interface for setting editor values.
*
* @package behat
* @category test
* @copyright 2022 Andrew Lyons <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_behat;
interface settable_editor {
/**
* Set the editor value.
*
* @param string $editorid The id of the editor within the page
* @param string $value The intended content of the editor
*/
public function set_editor_value(string $editorid, string $value): void;
}
+532
View File
@@ -0,0 +1,532 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Utils for behat-related stuff
*
* @package core
* @category test
* @copyright 2012 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/../lib.php');
require_once(__DIR__ . '/../../testing/classes/util.php');
require_once(__DIR__ . '/behat_command.php');
require_once(__DIR__ . '/behat_config_manager.php');
require_once(__DIR__ . '/../../filelib.php');
require_once(__DIR__ . '/../../clilib.php');
require_once(__DIR__ . '/../../csslib.php');
use Behat\Mink\Session;
use Behat\Mink\Exception\ExpectationException;
/**
* Init/reset utilities for Behat database and dataroot
*
* @package core
* @category test
* @copyright 2013 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_util extends testing_util {
/**
* The behat test site fullname and shortname.
*/
const BEHATSITENAME = "Acceptance test site";
/**
* @var array Files to skip when resetting dataroot folder
*/
protected static $datarootskiponreset = array('.', '..', 'behat', 'behattestdir.txt');
/**
* @var array Files to skip when dropping dataroot folder
*/
protected static $datarootskipondrop = array('.', '..', 'lock');
/**
* Installs a site using $CFG->dataroot and $CFG->prefix
* @throws coding_exception
* @return void
*/
public static function install_site() {
global $DB, $CFG;
require_once($CFG->dirroot.'/user/lib.php');
if (!defined('BEHAT_UTIL')) {
throw new coding_exception('This method can be only used by Behat CLI tool');
}
$tables = $DB->get_tables(false);
if (!empty($tables)) {
behat_error(BEHAT_EXITCODE_INSTALLED);
}
// New dataroot.
self::reset_dataroot();
$options = array();
$options['adminuser'] = 'admin';
$options['adminpass'] = 'admin';
$options['fullname'] = self::BEHATSITENAME;
$options['shortname'] = self::BEHATSITENAME;
install_cli_database($options, false);
// We need to keep the installed dataroot filedir files.
// So each time we reset the dataroot before running a test, the default files are still installed.
self::save_original_data_files();
$frontpagesummary = new admin_setting_special_frontpagedesc();
$frontpagesummary->write_setting(self::BEHATSITENAME);
// Update admin user info.
$user = $DB->get_record('user', array('username' => 'admin'));
$user->email = 'moodle@example.com';
$user->firstname = 'Admin';
$user->lastname = 'User';
$user->city = 'Perth';
$user->country = 'AU';
user_update_user($user, false);
// Disable email message processor.
$DB->set_field('message_processors', 'enabled', '0', array('name' => 'email'));
// Sets maximum debug level.
set_config('debug', DEBUG_DEVELOPER);
set_config('debugdisplay', 1);
// Disable some settings that are not wanted on test sites.
set_config('noemailever', 1);
// Enable web cron.
set_config('cronclionly', 0);
// Set editor autosave to high value, so as to avoid unwanted ajax.
set_config('autosavefrequency', '604800', 'editor_atto');
// Set noreplyaddress to an example domain, as it should be valid email address and test site can be a localhost.
set_config('noreplyaddress', 'noreply@example.com');
// Set the support email address.
set_config('supportemail', 'email@example.com');
// Remove any default blocked hosts and port restrictions, to avoid blocking tests (eg those using local files).
set_config('curlsecurityblockedhosts', '');
set_config('curlsecurityallowedport', '');
// Execute all the adhoc tasks.
while ($task = \core\task\manager::get_next_adhoc_task(time())) {
$task->execute();
\core\task\manager::adhoc_task_complete($task);
}
// Keeps the current version of database and dataroot.
self::store_versions_hash();
// Stores the database contents for fast reset.
self::store_database_state();
}
/**
* Build theme CSS.
*/
public static function build_themes($mtraceprogress = false) {
global $CFG;
require_once("{$CFG->libdir}/outputlib.php");
$themenames = array_keys(\core_component::get_plugin_list('theme'));
// Load the theme configs.
$themeconfigs = array_map(function($themename) {
return \theme_config::load($themename);
}, $themenames);
// Build the list of themes and cache them in local cache.
$themes = theme_build_css_for_themes($themeconfigs, ['ltr'], true, $mtraceprogress);
$framework = self::get_framework();
$storageroot = self::get_dataroot() . "/{$framework}/themedata";
foreach ($themes as $themename => $themedata) {
$dirname = "{$storageroot}/{$themename}";
check_dir_exists($dirname);
foreach ($themedata as $direction => $css) {
file_put_contents("{$dirname}/{$direction}.css", $css);
}
}
}
/**
* Drops dataroot and remove test database tables
* @throws coding_exception
* @return void
*/
public static function drop_site() {
if (!defined('BEHAT_UTIL')) {
throw new coding_exception('This method can be only used by Behat CLI tool');
}
self::reset_dataroot();
self::drop_database(true);
self::drop_dataroot();
}
/**
* Delete files and directories under dataroot.
*/
public static function drop_dataroot() {
global $CFG;
// As behat directory is now created under default $CFG->behat_dataroot_parent, so remove the whole dir.
if ($CFG->behat_dataroot !== $CFG->behat_dataroot_parent) {
remove_dir($CFG->behat_dataroot, false);
} else {
// It should never come here.
throw new moodle_exception("Behat dataroot should not be same as parent behat data root.");
}
}
/**
* Checks if $CFG->behat_wwwroot is available and using same versions for cli and web.
*
* @return void
*/
public static function check_server_status() {
global $CFG;
$url = $CFG->behat_wwwroot . '/admin/tool/behat/tests/behat/fixtures/environment.php';
// Get web versions used by behat site.
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$statuscode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($statuscode !== 200 || empty($result) || (!$result = json_decode($result, true))) {
behat_error (BEHAT_EXITCODE_REQUIREMENT, $CFG->behat_wwwroot . ' is not available, ensure you specified ' .
'correct url and that the server is set up and started.' . PHP_EOL . ' More info in ' .
behat_command::DOCS_URL . PHP_EOL . parent::get_site_info());
}
// Check if cli version is same as web version.
$clienv = self::get_environment();
if ($result != $clienv) {
$output = 'Differences detected between cli and webserver...'.PHP_EOL;
foreach ($result as $key => $version) {
if ($clienv[$key] != $version) {
$output .= ' ' . $key . ': ' . PHP_EOL;
$output .= ' - web server: ' . $version . PHP_EOL;
$output .= ' - cli: ' . $clienv[$key] . PHP_EOL;
}
}
echo $output;
ob_flush();
}
}
/**
* Checks whether the test database and dataroot is ready
* Stops execution if something went wrong
* @throws coding_exception
* @return void
*/
protected static function test_environment_problem() {
global $CFG, $DB;
if (!defined('BEHAT_UTIL')) {
throw new coding_exception('This method can be only used by Behat CLI tool');
}
if (!self::is_test_site()) {
behat_error(1, 'This is not a behat test site!');
}
$tables = $DB->get_tables(false);
if (empty($tables)) {
behat_error(BEHAT_EXITCODE_INSTALL, '');
}
if (!self::is_test_data_updated()) {
behat_error(BEHAT_EXITCODE_REINSTALL, 'The test environment was initialised for a different version');
}
}
/**
* Enables test mode
*
* It uses CFG->behat_dataroot
*
* Starts the test mode checking the composer installation and
* the test environment and updating the available
* features and steps definitions.
*
* Stores a file in dataroot/behat to allow Moodle to switch
* to the test environment when using cli-server.
* @param bool $themesuitewithallfeatures List themes to include core features.
* @param string $tags comma separated tag, which will be given preference while distributing features in parallel run.
* @param int $parallelruns number of parallel runs.
* @param int $run current run.
* @throws coding_exception
* @return void
*/
public static function start_test_mode($themesuitewithallfeatures = false, $tags = '', $parallelruns = 0, $run = 0) {
if (!defined('BEHAT_UTIL')) {
throw new coding_exception('This method can be only used by Behat CLI tool');
}
// Checks the behat set up and the PHP version.
if ($errorcode = behat_command::behat_setup_problem()) {
exit($errorcode);
}
// Check that test environment is correctly set up.
self::test_environment_problem();
// Updates all the Moodle features and steps definitions.
behat_config_manager::update_config_file('', true, $tags, $themesuitewithallfeatures, $parallelruns, $run);
if (self::is_test_mode_enabled()) {
return;
}
$contents = '$CFG->behat_wwwroot, $CFG->behat_prefix and $CFG->behat_dataroot' .
' are currently used as $CFG->wwwroot, $CFG->prefix and $CFG->dataroot';
$filepath = self::get_test_file_path();
if (!file_put_contents($filepath, $contents)) {
behat_error(BEHAT_EXITCODE_PERMISSIONS, 'File ' . $filepath . ' can not be created');
}
}
/**
* Returns the status of the behat test environment
*
* @return int Error code
*/
public static function get_behat_status() {
if (!defined('BEHAT_UTIL')) {
throw new coding_exception('This method can be only used by Behat CLI tool');
}
// Checks the behat set up and the PHP version, returning an error code if something went wrong.
if ($errorcode = behat_command::behat_setup_problem()) {
return $errorcode;
}
// Check that test environment is correctly set up, stops execution.
self::test_environment_problem();
}
/**
* Disables test mode
* @throws coding_exception
* @return void
*/
public static function stop_test_mode() {
if (!defined('BEHAT_UTIL')) {
throw new coding_exception('This method can be only used by Behat CLI tool');
}
$testenvfile = self::get_test_file_path();
behat_config_manager::set_behat_run_config_value('behatsiteenabled', 0);
if (!self::is_test_mode_enabled()) {
echo "Test environment was already disabled\n";
} else {
if (!unlink($testenvfile)) {
behat_error(BEHAT_EXITCODE_PERMISSIONS, 'Can not delete test environment file');
}
}
}
/**
* Checks whether test environment is enabled or disabled
*
* To check is the current script is running in the test
* environment
*
* @return bool
*/
public static function is_test_mode_enabled() {
$testenvfile = self::get_test_file_path();
if (file_exists($testenvfile)) {
return true;
}
return false;
}
/**
* Returns the path to the file which specifies if test environment is enabled
* @return string
*/
final public static function get_test_file_path() {
return behat_command::get_parent_behat_dir() . '/test_environment_enabled.txt';
}
/**
* Removes config settings that were added to the main $CFG config within the Behat CLI
* run.
*
* Database storage is already handled by reset_database and existing config values will
* be reset automatically by initialise_cfg(), so we only need to remove added ones.
*/
public static function remove_added_config() {
global $CFG;
if (!empty($CFG->behat_cli_added_config)) {
foreach ($CFG->behat_cli_added_config as $key => $value) {
unset($CFG->{$key});
}
unset($CFG->behat_cli_added_config);
}
}
/**
* Reset contents of all database tables to initial values, reset caches, etc.
*/
public static function reset_all_data() {
// Reset database.
self::reset_database();
// Purge dataroot directory.
self::reset_dataroot();
// Reset all static caches.
accesslib_clear_all_caches(true);
accesslib_reset_role_cache();
// Reset the nasty strings list used during the last test.
nasty_strings::reset_used_strings();
filter_manager::reset_caches();
\core_reportbuilder\manager::reset_caches();
// Reset course and module caches.
core_courseformat\base::reset_course_cache(0);
get_fast_modinfo(0, 0, true);
// Reset the DI container.
\core\di::reset_container();
// Inform data generator.
self::get_data_generator()->reset();
// Reset the task manager.
\core\task\manager::reset_state();
// Initialise $CFG with default values. This is needed for behat cli process, so we don't have modified
// $CFG values from the old run. @see set_config.
self::remove_added_config();
initialise_cfg();
}
/**
* Restore theme CSS stored during behat setup.
*/
public static function restore_saved_themes(): void {
global $CFG;
$themerev = theme_get_revision();
$framework = self::get_framework();
$storageroot = self::get_dataroot() . "/{$framework}/themedata";
$themenames = array_keys(\core_component::get_plugin_list('theme'));
$directions = ['ltr', 'rtl'];
$themeconfigs = array_map(function($themename) {
return \theme_config::load($themename);
}, $themenames);
foreach ($themeconfigs as $themeconfig) {
$themename = $themeconfig->name;
$themesubrev = theme_get_sub_revision_for_theme($themename);
$dirname = "{$storageroot}/{$themename}";
foreach ($directions as $direction) {
$cssfile = "{$dirname}/{$direction}.css";
if (file_exists($cssfile)) {
$themeconfig->set_css_content_cache(file_get_contents($cssfile));
}
}
}
}
/**
* Pause execution immediately.
*
* @param Session $session
* @param string $message The message to show when pausing.
* This will be passed through cli_ansi_format so appropriate ANSI formatting and features are available.
*/
public static function pause(Session $session, string $message): void {
$posixexists = function_exists('posix_isatty');
// Make sure this step is only used with interactive terminal (if detected).
if ($posixexists && !@posix_isatty(STDOUT)) {
throw new ExpectationException('Break point should only be used with interactive terminal.', $session);
}
// Save the cursor position, ring the bell, and add a new line.
fwrite(STDOUT, cli_ansi_format("<cursor:save><bell><newline>"));
// Output the formatted message and reset colour back to normal.
$formattedmessage = cli_ansi_format("{$message}<colour:normal>");
fwrite(STDOUT, $formattedmessage);
// Wait for input.
fread(STDIN, 1024);
// Move the cursor back up to the previous position, then restore the original position stored earlier, and move
// it back down again.
fwrite(STDOUT, cli_ansi_format("<cursor:up><cursor:up><cursor:restore><cursor:down><cursor:down>"));
// Add any extra lines back if the provided message was spread over multiple lines.
$linecount = count(explode("\n", $formattedmessage));
fwrite(STDOUT, str_repeat(cli_ansi_format("<cursor:down>"), $linecount - 1));
}
/**
* Gets a text-based site version description.
*
* @return string The site info
*/
public static function get_site_info() {
$siteinfo = parent::get_site_info();
$accessibility = empty(behat_config_manager::get_behat_run_config_value('axe')) ? 'No' : 'Yes';
$scssdeprecations = empty(behat_config_manager::get_behat_run_config_value('scss-deprecations')) ? 'No' : 'Yes';
$siteinfo .= <<<EOF
Run optional tests:
- Accessibility: {$accessibility}
- SCSS deprecations: {$scssdeprecations}
EOF;
return $siteinfo;
}
}