Skip to content

Commit

Permalink
MDL-73669 behat: allow multiple files with deprecated steps
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed Feb 4, 2022
1 parent c352b70 commit 31751c2
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 56 deletions.
75 changes: 75 additions & 0 deletions lib/behat/behat_deprecated_base.php
@@ -0,0 +1,75 @@
<?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/>.

// 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);
}
}
8 changes: 6 additions & 2 deletions lib/behat/classes/behat_config_manager.php
Expand Up @@ -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.
Expand Down
62 changes: 8 additions & 54 deletions lib/tests/behat/behat_deprecated.php
Expand Up @@ -14,72 +14,26 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* 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.
Expand Down

0 comments on commit 31751c2

Please sign in to comment.