Skip to content

Commit

Permalink
MDL-55072 behat: Fixed behat_selectors to be removed from contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajesh Taneja authored and David Monllao committed Aug 24, 2016
1 parent 4b5e0c5 commit 3037458
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
6 changes: 3 additions & 3 deletions admin/tool/behat/tests/manager_util_test.php
Expand Up @@ -107,7 +107,7 @@ public function setUp() {
$this->behatconfigutil->expects($this->any())
->method('get_behat_contexts_for_theme')
->with($this->equalTo('testtheme'))
->will($this->returnValue(array_keys($this->themecontexts)));
->will($this->returnValue(array(array_keys($this->themecontexts), $this->themecontexts)));

}

Expand Down Expand Up @@ -233,7 +233,7 @@ public function test_get_config_file_contents_with_theme_features() {
$behatconfigutil->expects($this->once())
->method('get_behat_contexts_for_theme')
->with($this->equalTo('testtheme'))
->will($this->returnValue(array_keys($this->themecontexts)));
->will($this->returnValue(array(array_keys($this->themecontexts), $this->themecontexts)));

$behatconfigutil->expects($this->once())
->method('get_overridden_theme_contexts')
Expand Down Expand Up @@ -311,7 +311,7 @@ public function test_get_config_file_contents_with_theme_and_parallel_run() {
$behatconfigutil->expects($this->atLeastOnce())
->method('get_behat_contexts_for_theme')
->with($this->equalTo('testtheme'))
->will($this->returnValue(array_keys($this->themecontexts)));
->will($this->returnValue(array(array_keys($this->themecontexts), $this->themecontexts)));

$CFG->behat_wwwroot = 'http://example.com/behat';

Expand Down
44 changes: 37 additions & 7 deletions lib/behat/classes/behat_config_util.php
Expand Up @@ -463,6 +463,15 @@ protected function build_config($parallelruns = 0, $currentrun = 0) {
$allcontexts = $this->contexts;
}

// Remove selectors from step definitions.
$themes = $this->get_list_of_themes();
foreach ($themes as $theme) {
$selectorclass = self::get_behat_theme_selector_override_classname($theme);
if (isset($allcontexts[$selectorclass])) {
unset($allcontexts[$selectorclass]);
}
}

// Comments use black color, so failure path is not visible. Using color other then black/white is safer.
// https://github.com/Behat/Behat/pull/628.
$config = array(
Expand Down Expand Up @@ -922,6 +931,7 @@ protected function get_behat_suites($parallelruns = 0, $currentrun = 0) {

$blacklistedfeatures = array();
$themefeatures = array();
$themesuitecontexts = array();
$themecontexts = array();

$themes = $this->get_list_of_themes();
Expand All @@ -931,14 +941,31 @@ protected function get_behat_suites($parallelruns = 0, $currentrun = 0) {
// Get theme features.
list($blacklistedfeatures[$theme], $themefeatures[$theme]) = $this->get_behat_features_for_theme($theme);

$themecontexts[$theme] = $this->get_behat_contexts_for_theme($theme);
list($themecontexts[$theme], $themesuitecontexts[$theme]) = $this->get_behat_contexts_for_theme($theme);
}

// Remove list of theme features for default suite, as default suite should not run theme specific features.
foreach ($themefeatures as $removethemefeatures) {
$features = $this->remove_blacklisted_features_from_list($features, $removethemefeatures);
}

// Remove list of theme features for default suite, as default suite should not run theme specific features.
foreach ($themecontexts as $theme => $themeblacklistcontexts) {
if ($themeblacklistcontexts) {
foreach ($themeblacklistcontexts as $c) {
// Remove theme specific contexts from default contexts.
unset($contexts[$c]);

// Remove theme specific contexts from other themes.
foreach ($themes as $currenttheme) {
if (($currenttheme != $theme) && isset($themesuitecontexts[$currenttheme][$c])) {
unset($themesuitecontexts[$currenttheme][$c]);
}
}
}
}
}

// Return sub-set of features if parallel run.
$featuresforrun = $this->get_features_for_the_run($features, $parallelruns, $currentrun);

Expand Down Expand Up @@ -976,7 +1003,7 @@ protected function get_behat_suites($parallelruns = 0, $currentrun = 0) {
$suites = array_merge($suites, array(
$theme => array(
'paths' => array_values($themesuitefeatures),
'contexts' => array_values($themecontexts[$theme]),
'contexts' => array_keys($themesuitecontexts[$theme]),
)
));
}
Expand Down Expand Up @@ -1170,7 +1197,7 @@ protected function get_overridden_theme_contexts() {
* Return list of behat contexts for theme and update $this->stepdefinitions list.
*
* @param string $theme theme name.
* @return array list($features, $contexts)
* @return array list($themecontexts, $themesuitecontexts)
*/
protected function get_behat_contexts_for_theme($theme) {

Expand All @@ -1186,12 +1213,13 @@ protected function get_behat_contexts_for_theme($theme) {
$contexts = $this->get_components_contexts();

// Create list of contexts used by theme suite.
$overriddencontexts = $this->get_tests_for_theme($theme, 'contexts');
$themecontexts = $this->get_tests_for_theme($theme, 'contexts');
$blacklistedcontexts = $this->get_blacklisted_tests_for_theme($theme, 'contexts');

// Theme suite will use all core contexts, except the one overridden by theme.
$themesuitecontexts = $contexts;
foreach ($overriddencontexts as $context => $path) {

foreach ($themecontexts as $context => $path) {

// If a context in theme starts with behat_theme_{themename}_behat_* then it's overriding core context.
if (preg_match('/^behat_theme_'.$theme.'_(\w+)$/', $context, $match)) {
Expand All @@ -1206,6 +1234,8 @@ protected function get_behat_contexts_for_theme($theme) {

// Don't include behat_selectors.
if ($context === self::get_behat_theme_selector_override_classname($theme)) {
unset($this->contexts[$context]);
unset($themesuitecontexts[$context]);
continue;
}

Expand All @@ -1221,8 +1251,8 @@ protected function get_behat_contexts_for_theme($theme) {
}

// We are only interested in the class name of context.
$this->themecontexts[$theme] = array_keys($themesuitecontexts);
$this->themecontexts[$theme] = $themesuitecontexts;

return $this->themecontexts[$theme];
return array(array_keys($themecontexts), $themesuitecontexts);
}
}
6 changes: 5 additions & 1 deletion lib/tests/behat/behat_hooks.php
Expand Up @@ -148,6 +148,7 @@ public static function before_suite(BeforeSuiteScope $scope) {

// 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');
Expand Down Expand Up @@ -308,7 +309,10 @@ public function before_scenario(BeforeScenarioScope $scope) {
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);
$behatselectorclass = 'behat_selectors';
if ($suitename !== 'default') {
$behatselectorclass = behat_config_util::get_behat_theme_selector_override_classname($suitename, true);
}
if (class_exists($behatselectorclass)) {
$behatselectorclass = new $behatselectorclass();
$behatselectorclass::register_moodle_selectors($session);
Expand Down

0 comments on commit 3037458

Please sign in to comment.