diff --git a/admin/tool/behat/cli/init.php b/admin/tool/behat/cli/init.php index 6c665db7395..b423cebc68b 100644 --- a/admin/tool/behat/cli/init.php +++ b/admin/tool/behat/cli/init.php @@ -49,8 +49,7 @@ list($options, $unrecognized) = cli_get_params( 'torun' => 0, 'optimize-runs' => '', 'add-core-features-to-theme' => false, - 'axe' => '', - 'no-axe' => false, + 'axe' => null, 'disable-composer' => false, 'composer-upgrade' => true, 'composer-self-update' => true, @@ -111,11 +110,9 @@ if (!empty($options['help'])) { exit(0); } -if (!empty($options['axe'])) { +if ($options['axe']) { echo "Axe accessibility tests are enabled by default, to disable them, use the --no-axe option.\n"; -} - -if (!empty($options['no-axe'])) { +} else if ($options['axe'] === false) { echo "Axe accessibility tests have been disabled.\n"; } @@ -127,22 +124,17 @@ if ($options['parallel'] && $options['parallel'] > 1) { $utilfile = 'util.php'; // Sanitize all input options, so they can be passed to util. foreach ($options as $option => $value) { - if ($value) { - $commandoptions .= " --$option=\"$value\""; - } + $commandoptions .= behat_get_command_flags($option, $value); } } else { // Only sanitize options for single run. $cmdoptionsforsinglerun = [ 'add-core-features-to-theme', 'axe', - 'no-axe', ]; foreach ($cmdoptionsforsinglerun as $option) { - if (!empty($options[$option])) { - $commandoptions .= " --$option='$options[$option]'"; - } + $commandoptions .= behat_get_command_flags($option, $options[$option]); } } diff --git a/admin/tool/behat/cli/util.php b/admin/tool/behat/cli/util.php index c13116977e9..61247ab0e8f 100644 --- a/admin/tool/behat/cli/util.php +++ b/admin/tool/behat/cli/util.php @@ -57,8 +57,7 @@ list($options, $unrecognized) = cli_get_params( 'torun' => 0, 'optimize-runs' => '', 'add-core-features-to-theme' => false, - 'axe' => '', - 'no-axe' => false, + 'axe' => true, ), array( 'h' => 'help', @@ -321,12 +320,7 @@ function commands_to_execute($options) { } foreach ($extraoptions as $option => $value) { - if ($options[$option]) { - $extra .= " --$option"; - if ($value) { - $extra .= "=\"$value\""; - } - } + $extra .= behat_get_command_flags($option, $value); } if (empty($options['parallel'])) { diff --git a/admin/tool/behat/cli/util_single_run.php b/admin/tool/behat/cli/util_single_run.php index ca0d5a61ac3..1b82f625b20 100644 --- a/admin/tool/behat/cli/util_single_run.php +++ b/admin/tool/behat/cli/util_single_run.php @@ -52,8 +52,7 @@ list($options, $unrecognized) = cli_get_params( 'updatesteps' => false, 'optimize-runs' => '', 'add-core-features-to-theme' => false, - 'axe' => '', - 'no-axe' => false, + 'axe' => true, ), array( 'h' => 'help', @@ -187,11 +186,8 @@ if ($options['install']) { behat_config_manager::set_behat_run_config_value('behatsiteenabled', 1); } - // Always run Behat with axe tests. - if (!$options['no-axe']) { - behat_config_manager::set_behat_run_config_value('axe', true); - } - + // Configure axe according to option. + behat_config_manager::set_behat_run_config_value('axe', $options['axe']); // Enable test mode. $timestart = microtime(true); diff --git a/lib/behat/lib.php b/lib/behat/lib.php index ccf2ebd159f..bbe95e66099 100644 --- a/lib/behat/lib.php +++ b/lib/behat/lib.php @@ -563,3 +563,24 @@ function cli_execute_parallel($cmds, $cwd = null, $delay = 0) { } return $processes; } + +/** + * Get command flags for an option/value combination + * + * @param string $option + * @param string|bool|null $value + * @return string + */ +function behat_get_command_flags(string $option, $value): string { + $commandoptions = ''; + if (is_bool($value)) { + if ($value) { + return " --{$option}"; + } else { + return " --no-{$option}"; + } + } else if ($value !== null) { + return " --$option=\"$value\""; + } + return ''; +}