diff --git a/lib/behat/behat_deprecated_base.php b/lib/behat/behat_deprecated_base.php new file mode 100644 index 0000000000000..735c514e90de1 --- /dev/null +++ b/lib/behat/behat_deprecated_base.php @@ -0,0 +1,75 @@ +. + +// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php. + +require_once(__DIR__ . '/behat_base.php'); + +/** + * Base class for steps definitions classes that contain deprecated steps. + * + * To be extended by the deprecated steps definitions of the different Moodle components and add-ons. + * For example, deprecated core steps can be found in lib/tests/behat/behat_deprecated.php , + * deprecated steps for mod_forum would be in mod/forum/tests/behat/behat_mod_forum_deprecated.php etc. + * + * @package core + * @category test + * @copyright 2022 Marina Glancy + * @author David Monllaó + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class behat_deprecated_base extends behat_base { + + /** + * Throws an exception if $CFG->behat_usedeprecated is not allowed. + * + * @throws Exception + * @param string|array $alternatives Alternative/s to the requested step + * @param bool $throwexception If set to true we always throw exception, irrespective of behat_usedeprecated setting. + * @return void + */ + protected function deprecated_message($alternatives, bool $throwexception = false): void { + global $CFG; + + // We do nothing if it is enabled. + if (!empty($CFG->behat_usedeprecated) && !$throwexception) { + return; + } + + if (is_scalar($alternatives)) { + $alternatives = array($alternatives); + } + + // Show an appropriate message based on the throwexception flag. + if ($throwexception) { + $message = 'This step has been removed. Rather than using this step you can:'; + } else { + $message = 'Deprecated step, rather than using this step you can:'; + } + + // Add all alternatives to the message. + foreach ($alternatives as $alternative) { + $message .= PHP_EOL . '- ' . $alternative; + } + + if (!$throwexception) { + $message .= PHP_EOL . '- Set $CFG->behat_usedeprecated in config.php to allow the use of deprecated steps + if you don\'t have any other option'; + } + + throw new Exception($message); + } +} diff --git a/lib/behat/classes/behat_config_manager.php b/lib/behat/classes/behat_config_manager.php index 75c727fd9d8a2..b34daf43d73ce 100644 --- a/lib/behat/classes/behat_config_manager.php +++ b/lib/behat/classes/behat_config_manager.php @@ -105,9 +105,13 @@ public static function update_config_file($component = '', $testsrunner = true, // Gets all the components with steps definitions. $stepsdefinitions = $behatconfigutil->get_components_contexts($component); - // We don't want the deprecated steps definitions here. if (!$testsrunner) { - unset($stepsdefinitions['behat_deprecated']); + // 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. diff --git a/lib/tests/behat/behat_deprecated.php b/lib/tests/behat/behat_deprecated.php index 49009d0e48264..3f47e17cde255 100644 --- a/lib/tests/behat/behat_deprecated.php +++ b/lib/tests/behat/behat_deprecated.php @@ -14,72 +14,26 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Steps definitions that will be deprecated in the next releases. - * - * @package core - * @category test - * @copyright 2013 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. -require_once(__DIR__ . '/../../../lib/behat/behat_base.php'); +require_once(__DIR__ . '/../../../lib/behat/behat_deprecated_base.php'); use Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException, - Behat\Gherkin\Node\TableNode as TableNode, - Behat\Gherkin\Node\PyStringNode as PyStringNode; + Behat\Gherkin\Node\TableNode as TableNode; /** - * Deprecated behat step definitions. + * Steps definitions that are now deprecated and will be removed in the next releases. + * + * This file only contains the steps that previously were in the behat_*.php files in the SAME DIRECTORY. + * When deprecating steps from other components or plugins, create a behat_COMPONENT_deprecated.php + * file in the same directory where the steps were defined. * * @package core * @category test * @copyright 2013 David Monllaó * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class behat_deprecated extends behat_base { - - /** - * Throws an exception if $CFG->behat_usedeprecated is not allowed. - * - * @throws Exception - * @param string|array $alternatives Alternative/s to the requested step - * @param bool $throwexception If set to true we always throw exception, irrespective of behat_usedeprecated setting. - * @return void - */ - protected function deprecated_message($alternatives, $throwexception = false) { - global $CFG; - - // We do nothing if it is enabled. - if (!empty($CFG->behat_usedeprecated) && !$throwexception) { - return; - } - - if (is_scalar($alternatives)) { - $alternatives = array($alternatives); - } - - // Show an appropriate message based on the throwexception flag. - if ($throwexception) { - $message = 'This step has been removed. Rather than using this step you can:'; - } else { - $message = 'Deprecated step, rather than using this step you can:'; - } - - // Add all alternatives to the message. - foreach ($alternatives as $alternative) { - $message .= PHP_EOL . '- ' . $alternative; - } - - if (!$throwexception) { - $message .= PHP_EOL . '- Set $CFG->behat_usedeprecated in config.php to allow the use of deprecated steps - if you don\'t have any other option'; - } - - throw new Exception($message); - } +class behat_deprecated extends behat_deprecated_base { /** * Clicks link with specified id|title|alt|text in the flat navigation drawer.