MDL-56006 behat: Get proper feature and context for default theme

No matter which theme is default theme, overridden
context and blacklisted features should be respected
This commit is contained in:
Rajesh Taneja 2016-10-24 13:18:35 +08:00 committed by Damyon Wiese
parent e56b58e3c9
commit 4422ffc55e
4 changed files with 135 additions and 81 deletions

View File

@ -0,0 +1,38 @@
<?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/>.
/**
* Theme test context 1
*
* @package tool_behat
* @copyright 2016 Rajesh Taneja <rajesh@moodle.com>
* @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.
require_once(__DIR__ . '/../../../../../lib/behat/behat_base.php');
/**
* Default Theme test context 1
*
* @package tool_behat
* @copyright 2016 Rajesh Taneja <rajesh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_theme_defaulttheme_test_context_1 extends behat_base {
}

View File

@ -61,7 +61,8 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
private $contextspath = array(
'default' => array(
'behat_test_context_1',
'behat_test_context_2'
'behat_test_context_2',
'behat_theme_defaulttheme_test_context_1'
),
'withfeatures' => array(
'behat_test_context_2',
@ -76,7 +77,7 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
);
/** @var array List of core features. */
private $corefatures = array('test_1_core_fixtures_tests_behat_tool' => __DIR__.'/fixtures/core/test_1.feature',
private $corefeatures = array('test_1_core_fixtures_tests_behat_tool' => __DIR__.'/fixtures/core/test_1.feature',
'test_2_core_fixtures_tests_behat_tool' => __DIR__.'/fixtures/core/test_2.feature');
/** @var array List of core contexts. */
@ -104,14 +105,15 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
// Create a map of arguments to return values.
$map = array(
array('withfeatures', __DIR__.'/fixtures/theme/withfeatures'),
array('nofeatures', __DIR__.'/fixtures/theme/nofeatures')
array('nofeatures', __DIR__.'/fixtures/theme/nofeatures'),
array('defaulttheme', __DIR__.'/fixtures/theme/defaulttheme'),
);
// List of themes is const for test.
if ($notheme) {
$themelist = array();
$themelist = array('defaulttheme');
} else {
$themelist = array('withfeatures', 'nofeatures');
$themelist = array('withfeatures', 'nofeatures', 'defaulttheme');
}
$behatconfigutil->expects($this->any())
@ -123,6 +125,10 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
->method('get_theme_test_directory')
->will($this->returnValueMap($map));
$behatconfigutil->expects($this->any())
->method('get_default_theme')
->will($this->returnValue('defaulttheme'));
return $behatconfigutil;
}
@ -132,12 +138,12 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
public function test_get_config_file_contents_with_single_run() {
$mockbuilder = $this->getMockBuilder('behat_config_util');
$mockbuilder->setMethods(array('get_theme_test_directory', 'get_list_of_themes'));
$mockbuilder->setMethods(array('get_theme_test_directory', 'get_list_of_themes', 'get_default_theme'));
$behatconfigutil = $mockbuilder->getMock();
$behatconfigutil = $this->get_behat_config_util($behatconfigutil);
$config = $behatconfigutil->get_config_file_contents($this->corefatures, $this->corecontexts);
$config = $behatconfigutil->get_config_file_contents($this->corefeatures, $this->corecontexts);
// Two suites should be present.
$suites = $config['default']['suites'];
@ -161,8 +167,8 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
}
}
// There are 6 step definitions.
$this->assertCount(6, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
// There are 7 step definitions.
$this->assertCount(7, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
}
/**
@ -171,12 +177,12 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
public function test_get_config_file_contents_with_single_run_no_theme() {
$mockbuilder = $this->getMockBuilder('behat_config_util');
$mockbuilder->setMethods(array('get_theme_test_directory', 'get_list_of_themes'));
$mockbuilder->setMethods(array('get_theme_test_directory', 'get_list_of_themes', 'get_default_theme'));
$behatconfigutil = $mockbuilder->getMock();
$behatconfigutil = $this->get_behat_config_util($behatconfigutil, true);
$config = $behatconfigutil->get_config_file_contents($this->corefatures, $this->corecontexts);
$config = $behatconfigutil->get_config_file_contents($this->corefeatures, $this->corecontexts);
// Two suites should be present.
$suites = $config['default']['suites'];
@ -192,7 +198,8 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
$contextspath = array(
'default' => array(
'behat_test_context_1',
'behat_test_context_2'
'behat_test_context_2',
'behat_theme_defaulttheme_test_context_1',
)
);
@ -214,8 +221,8 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
}
}
// There are 6 step definitions.
$this->assertCount(2, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
// There are 3 step definitions.
$this->assertCount(3, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
}
/**
@ -224,14 +231,14 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
public function test_get_config_file_contents_with_parallel_run() {
$mockbuilder = $this->getMockBuilder('behat_config_util');
$mockbuilder->setMethods(array('get_theme_test_directory', 'get_list_of_themes'));
$mockbuilder->setMethods(array('get_theme_test_directory', 'get_list_of_themes', 'get_default_theme'));
$behatconfigutil = $mockbuilder->getMock();
$behatconfigutil = $this->get_behat_config_util($behatconfigutil);
// Test first run out of 3.
$config = $behatconfigutil->get_config_file_contents($this->corefatures, $this->corecontexts, '', 3, 1);
$config = $behatconfigutil->get_config_file_contents($this->corefeatures, $this->corecontexts, '', 3, 1);
// Three suites should be present.
$suites = $config['default']['suites'];
$this->assertCount(3, $suites);
@ -249,6 +256,7 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
$this->assertContains($feature, $suites[$themename]['paths'][$key]);
}
}
// Check contexts.
foreach ($this->contextspath as $themename => $paths) {
$this->assertCount(count($paths), $suites[$themename]['contexts']);
@ -257,8 +265,8 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
$this->assertTrue(in_array($context, $suites[$themename]['contexts']));
}
}
// There are 6 step definitions.
$this->assertCount(6, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
// There are 7 step definitions.
$this->assertCount(7, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
// Test second run out of 3.
$config = $behatconfigutil->get_config_file_contents('', '', '', 3, 2);
@ -287,8 +295,8 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
$this->assertTrue(in_array($context, $suites[$themename]['contexts']));
}
}
// There are 6 step definitions.
$this->assertCount(6, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
// There are 7 step definitions.
$this->assertCount(7, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
// Test third run out of 3.
$config = $behatconfigutil->get_config_file_contents('', '', '', 3, 3);
@ -316,8 +324,8 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
$this->assertTrue(in_array($context, $suites[$themename]['contexts']));
}
}
// There are 6 step definitions.
$this->assertCount(6, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
// There are 7 step definitions.
$this->assertCount(7, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
}
/**
@ -326,14 +334,14 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
public function test_get_config_file_contents_with_parallel_run_optimize_tags() {
$mockbuilder = $this->getMockBuilder('behat_config_util');
$mockbuilder->setMethods(array('get_theme_test_directory', 'get_list_of_themes'));
$mockbuilder->setMethods(array('get_theme_test_directory', 'get_list_of_themes', 'get_default_theme'));
$behatconfigutil = $mockbuilder->getMock();
$behatconfigutil = $this->get_behat_config_util($behatconfigutil);
// Test first run out of 3.
$config = $behatconfigutil->get_config_file_contents($this->corefatures, $this->corecontexts, '@commontag', 3, 1);
$config = $behatconfigutil->get_config_file_contents($this->corefeatures, $this->corecontexts, '@commontag', 3, 1);
// Three suites should be present.
$suites = $config['default']['suites'];
@ -360,8 +368,8 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
$this->assertTrue(in_array($context, $suites[$themename]['contexts']));
}
}
// There are 6 step definitions.
$this->assertCount(6, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
// There are 7step definitions.
$this->assertCount(7, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
// Test second run out of 3.
$config = $behatconfigutil->get_config_file_contents('', '', '@commontag', 3, 2);
@ -391,8 +399,8 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
$this->assertTrue(in_array($context, $suites[$themename]['contexts']));
}
}
// There are 6 step definitions.
$this->assertCount(6, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
// There are 7 step definitions.
$this->assertCount(7, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
// Test third run out of 3.
$config = $behatconfigutil->get_config_file_contents('', '', '', 3, 3);
@ -420,8 +428,8 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
$this->assertTrue(in_array($context, $suites[$themename]['contexts']));
}
}
// There are 6 step definitions.
$this->assertCount(6, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
// There are 7 step definitions.
$this->assertCount(7, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
}
/**
@ -470,7 +478,8 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
public function test_get_config_file_contents_with_blacklisted_tags() {
$mockbuilder = $this->getMockBuilder('behat_config_util');
$mockbuilder->setMethods(array('get_theme_test_directory', 'get_list_of_themes', 'get_blacklisted_tests_for_theme'));
$mockbuilder->setMethods(array('get_theme_test_directory', 'get_list_of_themes', 'get_blacklisted_tests_for_theme',
'get_default_theme'));
$behatconfigutil = $mockbuilder->getMock();
@ -480,10 +489,13 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
$map = array(
array('withfeatures', 'tags', array('@test1')),
array('nofeatures', 'tags', array('@test2')),
array('defaulttheme', 'tags', array()),
array('withfeatures', 'features', array()),
array('nofeatures', 'features', array()),
array('defaulttheme', 'features', array()),
array('withfeatures', 'contexts', array()),
array('nofeatures', 'contexts', array())
array('nofeatures', 'contexts', array()),
array('defaulttheme', 'contexts', array())
);
$behatconfigutil->expects($this->any())
@ -491,7 +503,7 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
->will($this->returnValueMap($map));
$behatconfigutil->set_theme_suite_to_include_core_features(true);
$config = $behatconfigutil->get_config_file_contents($this->corefatures, $this->corecontexts);
$config = $behatconfigutil->get_config_file_contents($this->corefeatures, $this->corecontexts);
// Three suites should be present.
$suites = $config['default']['suites'];
@ -520,8 +532,8 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
$this->assertTrue(in_array($context, $suites[$themename]['contexts']));
}
}
// There are 6 step definitions.
$this->assertCount(6, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
// There are 7 step definitions.
$this->assertCount(7, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
}
/**
@ -530,7 +542,8 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
public function test_get_config_file_contents_with_blacklisted_features_contexts() {
$mockbuilder = $this->getMockBuilder('behat_config_util');
$mockbuilder->setMethods(array('get_theme_test_directory', 'get_list_of_themes', 'get_blacklisted_tests_for_theme'));
$mockbuilder->setMethods(array('get_theme_test_directory', 'get_list_of_themes', 'get_blacklisted_tests_for_theme',
'get_default_theme'));
$behatconfigutil = $mockbuilder->getMock();
@ -540,10 +553,13 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
$map = array(
array('withfeatures', 'tags', array()),
array('nofeatures', 'tags', array()),
array('defaulttheme', 'tags', array()),
array('withfeatures', 'features', array('admin/tool/behat/tests/fixtures/core/test_1.feature')),
array('nofeatures', 'features', array('admin/tool/behat/tests/fixtures/core/test_2.feature')),
array('defaulttheme', 'features', array()),
array('withfeatures', 'contexts', array('admin/tool/behat/tests/fixtures/core/behat_test_context_2.php')),
array('nofeatures', 'contexts', array('admin/tool/behat/tests/fixtures/core/behat_test_context_1.php'))
array('nofeatures', 'contexts', array('admin/tool/behat/tests/fixtures/core/behat_test_context_1.php')),
array('defaulttheme', 'contexts', array()),
);
$behatconfigutil->expects($this->any())
@ -551,7 +567,7 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
->will($this->returnValueMap($map));
$behatconfigutil->set_theme_suite_to_include_core_features(true);
$config = $behatconfigutil->get_config_file_contents($this->corefatures, $this->corecontexts);
$config = $behatconfigutil->get_config_file_contents($this->corefeatures, $this->corecontexts);
// Three suites should be present.
$suites = $config['default']['suites'];
@ -566,7 +582,8 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
$contextspath = array(
'default' => array(
'behat_test_context_1',
'behat_test_context_2'
'behat_test_context_2',
'behat_theme_defaulttheme_test_context_1',
),
'withfeatures' => array(
'behat_theme_withfeatures_test_context_2',
@ -594,8 +611,8 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
$this->assertTrue(in_array($context, $suites[$themename]['contexts']));
}
}
// There are 6 step definitions.
$this->assertCount(6, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
// There are 7 step definitions.
$this->assertCount(7, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
}
/**
@ -604,7 +621,7 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
public function test_core_features_to_include_in_specified_theme() {
$mockbuilder = $this->getMockBuilder('behat_config_util');
$mockbuilder->setMethods(array('get_theme_test_directory', 'get_list_of_themes'));
$mockbuilder->setMethods(array('get_theme_test_directory', 'get_list_of_themes', 'get_default_theme'));
$behatconfigutil = $mockbuilder->getMock();
@ -612,7 +629,7 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
// Check features when, no theme is specified.
$behatconfigutil->set_theme_suite_to_include_core_features('');
$config = $behatconfigutil->get_config_file_contents($this->corefatures, $this->corecontexts);
$config = $behatconfigutil->get_config_file_contents($this->corefeatures, $this->corecontexts);
$suites = $config['default']['suites'];
foreach ($this->featurepaths as $themename => $paths) {
$this->assertCount(count($paths), $suites[$themename]['paths']);
@ -628,7 +645,7 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
$featurepaths['nofeatures'] = array_merge ($featurepaths['default'], $featurepaths['nofeatures']);
$behatconfigutil->set_theme_suite_to_include_core_features('ALL');
$config = $behatconfigutil->get_config_file_contents($this->corefatures, $this->corecontexts);
$config = $behatconfigutil->get_config_file_contents($this->corefeatures, $this->corecontexts);
$suites = $config['default']['suites'];
foreach ($featurepaths as $themename => $paths) {
$this->assertCount(count($paths), $suites[$themename]['paths']);
@ -644,7 +661,7 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
$featurepaths['nofeatures'] = array_merge ($featurepaths['default'], $featurepaths['nofeatures']);
$behatconfigutil->set_theme_suite_to_include_core_features('withfeatures, nofeatures');
$config = $behatconfigutil->get_config_file_contents($this->corefatures, $this->corecontexts);
$config = $behatconfigutil->get_config_file_contents($this->corefeatures, $this->corecontexts);
$suites = $config['default']['suites'];
foreach ($featurepaths as $themename => $paths) {
$this->assertCount(count($paths), $suites[$themename]['paths']);
@ -659,7 +676,7 @@ class tool_behat_manager_util_testcase extends advanced_testcase {
$featurepaths['nofeatures'] = array_merge ($featurepaths['default'], $featurepaths['nofeatures']);
$behatconfigutil->set_theme_suite_to_include_core_features('nofeatures');
$config = $behatconfigutil->get_config_file_contents($this->corefatures, $this->corecontexts);
$config = $behatconfigutil->get_config_file_contents($this->corefeatures, $this->corecontexts);
$suites = $config['default']['suites'];
foreach ($featurepaths as $themename => $paths) {
$this->assertCount(count($paths), $suites[$themename]['paths']);

View File

@ -979,7 +979,6 @@ class behat_config_util {
*/
protected function get_behat_suites($parallelruns = 0, $currentrun = 0) {
$features = $this->get_components_features();
$contexts = $this->get_components_contexts();
// Get number of parallel runs and current run.
if (!empty($parallelruns) && !empty($currentrun)) {
@ -1003,19 +1002,16 @@ class behat_config_util {
}
// Remove list of theme features for default suite, as default suite should not run theme specific features.
foreach ($themefeatures as $removethemefeatures) {
foreach ($themefeatures as $themename => $removethemefeatures) {
if (!empty($removethemefeatures['features'])) {
$features = $this->remove_blacklisted_features_from_list($features, $removethemefeatures['features']);
}
}
// Remove list of theme features for default suite, as default suite should not run theme specific features.
// Remove list of theme contexts form other suite contexts, as suite don't require other theme specific contexts.
foreach ($themecontexts as $themename => $themecontext) {
if (!empty($themecontext['contexts'])) {
foreach ($themecontext['contexts'] as $contextkey => $contextpath) {
// Remove theme specific contexts from default contexts.
unset($contexts[$contextkey]);
// Remove theme specific contexts from other themes.
foreach ($themes as $currenttheme) {
if (($currenttheme != $themename) && isset($themecontexts[$currenttheme]['suitecontexts'][$contextkey])) {
@ -1026,22 +1022,13 @@ class behat_config_util {
}
}
$featuresforrun = $this->get_features_for_the_run($features, $parallelruns, $currentrun);
// Default suite.
$suites = array(
'default' => array(
'paths' => array_values($featuresforrun),
'contexts' => array_keys($contexts),
)
);
// Set suite for each theme.
$suites = array();
foreach ($themes as $theme) {
// Get list of features which will be included in theme.
// If theme suite with all features is set, then we want all core features to be part of theme suite.
// If theme suite with all features or default theme, then we want all core features to be part of theme suite.
if ((is_string($this->themesuitewithallfeatures) && ($this->themesuitewithallfeatures === self::ALL_THEMES_TO_RUN)) ||
in_array($theme, $this->themesuitewithallfeatures)) {
in_array($theme, $this->themesuitewithallfeatures) || ($this->get_default_theme() === $theme)) {
// If there is no theme specific feature. Then it's just core features.
if (empty($themefeatures[$theme]['features'])) {
$themesuitefeatures = $features;
@ -1059,10 +1046,17 @@ class behat_config_util {
// Return sub-set of features if parallel run.
$themesuitefeatures = $this->get_features_for_the_run($themesuitefeatures, $parallelruns, $currentrun);
// Default theme is part of default suite.
if ($this->get_default_theme() === $theme) {
$suitename = 'default';
} else {
$suitename = $theme;
}
// Add suite no matter what. If there is no feature in suite then it will just exist successfully with no
// scenarios. But if we don't set this then the user has to know which run doesn't have suite and which run do.
$suites = array_merge($suites, array(
$theme => array(
$suitename => array(
'paths' => array_values($themesuitefeatures),
'contexts' => array_keys($themecontexts[$theme]['suitecontexts']),
)
@ -1072,6 +1066,15 @@ class behat_config_util {
return $suites;
}
/**
* Return name of default theme.
*
* @return string
*/
protected function get_default_theme() {
return theme_config::DEFAULT_THEME;
}
/**
* Return list of themes which can be set in moodle.
*
@ -1101,10 +1104,6 @@ class behat_config_util {
// designer mode is switched off we will respect that decision.
continue;
}
if ($themename == theme_config::DEFAULT_THEME) {
// Don't include default theme, as default suite will be running with this theme.
continue;
}
$selectablethemes[] = $themename;
}
@ -1251,6 +1250,7 @@ class behat_config_util {
list($featurekey, $featurepath) = $this->get_clean_feature_key_and_path($themefeature);
$features[$featurekey] = $featurepath;
}
foreach ($themeblacklistfeatures as $themeblacklistfeature) {
list($blacklistfeaturekey, $blacklistfeaturepath) = $this->get_clean_feature_key_and_path($themeblacklistfeature);
$blacklistfeatures[$blacklistfeaturekey] = $blacklistfeaturepath;

View File

@ -317,19 +317,18 @@ class behat_hooks extends behat_base {
$namedpartialclass = 'behat_partial_named_selector';
$namedexactclass = 'behat_exact_named_selector';
if ($suitename !== 'default') {
// If override selector exist, then set it as default behat selectors class.
$overrideclass = behat_config_util::get_behat_theme_selector_override_classname($suitename, 'named_partial', true);
if (class_exists($overrideclass)) {
$namedpartialclass = $overrideclass;
}
// If override selector exist, then set it as default behat selectors class.
$overrideclass = behat_config_util::get_behat_theme_selector_override_classname($suitename, 'named_exact', true);
if (class_exists($overrideclass)) {
$namedexactclass = $overrideclass;
}
// If override selector exist, then set it as default behat selectors class.
$overrideclass = behat_config_util::get_behat_theme_selector_override_classname($suitename, 'named_partial', true);
if (class_exists($overrideclass)) {
$namedpartialclass = $overrideclass;
}
// If override selector exist, then set it as default behat selectors class.
$overrideclass = behat_config_util::get_behat_theme_selector_override_classname($suitename, 'named_exact', true);
if (class_exists($overrideclass)) {
$namedexactclass = $overrideclass;
}
$this->getSession()->getSelectorsHandler()->registerSelector('named_partial', new $namedpartialclass());
$this->getSession()->getSelectorsHandler()->registerSelector('named_exact', new $namedexactclass());
}