MDL-55072 behat: Allow behat_selectors to be overridden

if theme/tests/behat/behat_theme_{THEME_NAME}_behat_selectors.php
file exits, and has class with same file name then we will
call behat_theme_{THEME_NAME}_behat_selectors::register_moodle_selectors()
to register theme specific selectors. If they are different
then core then they will override
This commit is contained in:
Rajesh Taneja 2016-08-03 15:07:52 +08:00
parent 5aa9b5cee3
commit b4bc4286bb
No known key found for this signature in database
GPG Key ID: B363F7FB787F80E4
5 changed files with 74 additions and 24 deletions

View File

@ -356,6 +356,10 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
* @dataProvider clean_features_path_list
*/
public function test_get_clean_feature_key_and_path($featurepath, $key, $cleanfeaturepath) {
global $CFG;
// This is a hack so directory name is correctly detected in tests.
$CFG->dirroot = 'C:';
$behatconfigutil = $this->behatconfigutil;
// Fix expected directory path for OS.
@ -380,10 +384,10 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
['/home/test/behat/mod_assign.feature', 'mod_assign_behat_test_home', '/home/test/behat/mod_assign.feature'],
['mod_assign.feature', 'mod_assign', 'mod_assign.feature'],
['C:\test\this\that\test\behat\mod_assign.feature', 'mod_assign_behat_test_that_this_test', 'C:\test\this\that\test\behat\mod_assign.feature'],
['C:\this\that\test\behat\mod_assign.feature', 'mod_assign_behat_test_that_this_C:', 'C:\this\that\test\behat\mod_assign.feature'],
['C:\that\test\behat\mod_assign.feature', 'mod_assign_behat_test_that_C:', 'C:\that\test\behat\mod_assign.feature'],
['C:\test\behat\mod_assign.feature', 'mod_assign_behat_test_C:', 'C:\test\behat\mod_assign.feature'],
['C:\mod_assign.feature', 'mod_assign_C:', 'C:\mod_assign.feature'],
['C:\this\that\test\behat\mod_assign.feature', 'mod_assign_behat_test_that_this', 'C:\this\that\test\behat\mod_assign.feature'],
['C:\that\test\behat\mod_assign.feature', 'mod_assign_behat_test_that', 'C:\that\test\behat\mod_assign.feature'],
['C:\test\behat\mod_assign.feature', 'mod_assign_behat_test', 'C:\test\behat\mod_assign.feature'],
['C:\mod_assign.feature', 'mod_assign', 'C:\mod_assign.feature'],
);
}
}

View File

@ -173,7 +173,7 @@ class behat_config_util {
if ($components) {
foreach ($components as $componentname => $path) {
$path = $this->clean_path($path) . $this->get_behat_tests_path();
$path = $this->clean_path($path) . self::get_behat_tests_path();
if (empty($featurespaths[$path]) && file_exists($path)) {
list($key, $featurepath) = $this->get_clean_feature_key_and_path($path);
$featurespaths[$key] = $featurepath;
@ -287,10 +287,10 @@ class behat_config_util {
foreach ($components as $componentname => $componentpath) {
$componentpath = self::clean_path($componentpath);
if (!file_exists($componentpath . $this->get_behat_tests_path())) {
if (!file_exists($componentpath . self::get_behat_tests_path())) {
continue;
}
$diriterator = new DirectoryIterator($componentpath . $this->get_behat_tests_path());
$diriterator = new DirectoryIterator($componentpath . self::get_behat_tests_path());
$regite = new RegexIterator($diriterator, '|behat_.*\.php$|');
// All behat_*.php inside self::get_behat_tests_path() are added as steps definitions files.
@ -815,10 +815,33 @@ class behat_config_util {
*
* @return string
*/
public final function get_behat_tests_path() {
public static final function get_behat_tests_path() {
return DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'behat';
}
/**
* Return context name of behat_theme selector to use.
*
* @param string $themename name of the theme.
* @param bool $includeclass if class should be included.
* @return string
*/
public static final function get_behat_theme_selector_override_classname($themename, $includeclass = false) {
global $CFG;
$overridebehatclassname = 'behat_theme_'.$themename.'_behat_selectors';
if ($includeclass) {
$themeoverrideselector = $CFG->dirroot . DIRECTORY_SEPARATOR . 'theme' . DIRECTORY_SEPARATOR . $themename .
self::get_behat_tests_path() . DIRECTORY_SEPARATOR . $overridebehatclassname . '.php';
if (file_exists($themeoverrideselector)) {
require_once($themeoverrideselector);
}
}
return $overridebehatclassname;
}
/**
* List of components which contain behat context or features.
@ -1055,7 +1078,7 @@ class behat_config_util {
global $CFG;
$themetestpath = $CFG->dirroot . DIRECTORY_SEPARATOR . "theme" . DIRECTORY_SEPARATOR . $theme .
$this->get_behat_tests_path();
self::get_behat_tests_path();
if (file_exists($themetestpath . DIRECTORY_SEPARATOR . 'blacklist.json')) {
// Blacklist file exist. Leave it for last to clear the feature and contexts.
@ -1181,6 +1204,11 @@ class behat_config_util {
$this->overriddenthemescontexts[$context] = $path;
}
// Don't include behat_selectors.
if ($context === self::get_behat_theme_selector_override_classname($theme)) {
continue;
}
// Add theme specific contexts with suffix to steps definitions.
$themesuitecontexts[$context] = $path;
}

View File

@ -55,8 +55,22 @@ class behat_context_helper {
*
* @param Environment $environment
* @return void
* @deprecated since 3.2 MDL-55072 - please use behat_context_helper::set_environment()
* @todo MDL-55365 This will be deleted in Moodle 3.6.
*/
public static function set_session(Environment $environment) {
debugging('set_session is deprecated. Please use set_environment instead.', DEBUG_DEVELOPER);
self::set_environment($environment);
}
/**
* Sets behat environment.
*
* @param Environment $environment
* @return void
*/
public static function set_environment(Environment $environment) {
self::$environment = $environment;
}

View File

@ -23,7 +23,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Moodle selectors manager.

View File

@ -101,7 +101,7 @@ class behat_hooks extends behat_base {
*
* @var string current running suite name
*/
protected static $runningsuite = 'default';
protected static $runningsuite = '';
/**
* Hook to capture BeforeSuite event so as to give access to moodle codebase.
@ -148,7 +148,6 @@ class behat_hooks extends behat_base {
// Now that we are MOODLE_INTERNAL.
require_once(__DIR__ . '/../../behat/classes/behat_command.php');
require_once(__DIR__ . '/../../behat/classes/behat_selectors.php');
require_once(__DIR__ . '/../../behat/classes/behat_context_helper.php');
require_once(__DIR__ . '/../../behat/classes/util.php');
require_once(__DIR__ . '/../../testing/classes/test_lock.php');
@ -301,10 +300,18 @@ class behat_hooks extends behat_base {
throw new behat_stop_exception($e->getMessage());
}
// We need the Mink session to do it and we do it only before the first scenario.
if (self::is_first_scenario()) {
behat_selectors::register_moodle_selectors($session);
behat_context_helper::set_session($scope->getEnvironment());
$suitename = $scope->getSuite()->getName();
// Register behat selectors for theme, if suite is changed. We do it for every suite change.
if ($suitename !== self::$runningsuite) {
behat_context_helper::set_environment($scope->getEnvironment());
// We need the Mink session to do it and we do it only before the first scenario.
$behatselectorclass = behat_config_util::get_behat_theme_selector_override_classname($suitename, true);
if (class_exists($behatselectorclass)) {
$behatselectorclass = new $behatselectorclass();
$behatselectorclass::register_moodle_selectors($session);
}
}
// Reset mink session between the scenarios.
@ -328,6 +335,12 @@ class behat_hooks extends behat_base {
}
}
// Set the theme if not default.
if ($suitename !== "default") {
set_config('theme', $suitename);
self::$runningsuite = $suitename;
}
// Start always in the the homepage.
try {
// Let's be conservative as we never know when new upstream issues will affect us.
@ -336,7 +349,6 @@ class behat_hooks extends behat_base {
throw new behat_stop_exception($e->getMessage());
}
// Checking that the root path is a Moodle test site.
if (self::is_first_scenario()) {
$notestsiteexception = new behat_stop_exception('The base URL (' . $CFG->wwwroot . ') is not a behat test site, ' .
@ -346,13 +358,6 @@ class behat_hooks extends behat_base {
self::$initprocessesfinished = true;
}
// Set theme if suite is different then default.
$suitename = $scope->getSuite()->getName();
if ($suitename !== self::$runningsuite) {
set_config('theme', $suitename);
self::$runningsuite = $suitename;
}
// Run all test with medium (1024x768) screen size, to avoid responsive problems.
$this->resize_window('medium');
}