From 3d17b48855ac5ea0b066190fd147dfa2b23b82c0 Mon Sep 17 00:00:00 2001 From: sam marshall Date: Mon, 10 Mar 2025 15:57:21 +0000 Subject: [PATCH] MDL-84793 Behat: Allow CLI scripts to run on Behat instance --- admin/tool/behat/cli/init.php | 4 ++++ admin/tool/behat/cli/run.php | 4 ++++ admin/tool/behat/cli/util.php | 4 ++++ admin/tool/behat/cli/util_single_run.php | 4 ++++ admin/tool/phpunit/cli/init.php | 4 ++++ admin/tool/phpunit/cli/util.php | 4 ++++ lib/behat/lib.php | 5 +++++ 7 files changed, 29 insertions(+) diff --git a/admin/tool/behat/cli/init.php b/admin/tool/behat/cli/init.php index 87f77742605..b9b45ad9846 100644 --- a/admin/tool/behat/cli/init.php +++ b/admin/tool/behat/cli/init.php @@ -36,6 +36,10 @@ if (function_exists('opcache_reset')) { define('CLI_SCRIPT', true); define('CACHE_DISABLE_ALL', true); +// It makes no sense to use BEHAT_CLI for this script (the Behat launch scripts expect to start +// from the normal environment), so in case user has set tne environment variable, disable it. +putenv('BEHAT_CLI=0'); + // Basic functions. require_once(__DIR__ . '/../../../../lib/clilib.php'); require_once(__DIR__ . '/../../../../lib/behat/lib.php'); diff --git a/admin/tool/behat/cli/run.php b/admin/tool/behat/cli/run.php index cfcaedd96d2..8ffad281da9 100644 --- a/admin/tool/behat/cli/run.php +++ b/admin/tool/behat/cli/run.php @@ -31,6 +31,10 @@ define('ABORT_AFTER_CONFIG', true); define('CACHE_DISABLE_ALL', true); define('NO_OUTPUT_BUFFERING', true); +// It makes no sense to use BEHAT_CLI for this script (the Behat launch scripts expect to start +// from the normal environment), so in case user has set tne environment variable, disable it. +putenv('BEHAT_CLI=0'); + require_once(__DIR__ .'/../../../../config.php'); require_once(__DIR__.'/../../../../lib/clilib.php'); require_once(__DIR__.'/../../../../lib/behat/lib.php'); diff --git a/admin/tool/behat/cli/util.php b/admin/tool/behat/cli/util.php index 7d36ffa611c..168bdadd2a2 100644 --- a/admin/tool/behat/cli/util.php +++ b/admin/tool/behat/cli/util.php @@ -39,6 +39,10 @@ define('NO_OUTPUT_BUFFERING', true); define('IGNORE_COMPONENT_CACHE', true); define('ABORT_AFTER_CONFIG', true); +// It makes no sense to use BEHAT_CLI for this script (the Behat launch scripts expect to start +// from the normal environment), so in case user has set tne environment variable, disable it. +putenv('BEHAT_CLI=0'); + require_once(__DIR__ . '/../../../../lib/clilib.php'); // CLI options. diff --git a/admin/tool/behat/cli/util_single_run.php b/admin/tool/behat/cli/util_single_run.php index 942a6f834f5..14627eb333c 100644 --- a/admin/tool/behat/cli/util_single_run.php +++ b/admin/tool/behat/cli/util_single_run.php @@ -33,6 +33,10 @@ if (isset($_SERVER['REMOTE_ADDR'])) { die(); // No access from web!. } +// It makes no sense to use BEHAT_CLI for this script (the Behat launch scripts expect to start +// from the normal environment), so in case user has set tne environment variable, disable it. +putenv('BEHAT_CLI=0'); + // Basic functions. require_once(__DIR__ . '/../../../../lib/clilib.php'); require_once(__DIR__ . '/../../../../lib/behat/lib.php'); diff --git a/admin/tool/phpunit/cli/init.php b/admin/tool/phpunit/cli/init.php index 9c4714c87db..a352f6c48f6 100644 --- a/admin/tool/phpunit/cli/init.php +++ b/admin/tool/phpunit/cli/init.php @@ -34,6 +34,10 @@ if (function_exists('opcache_reset')) { define('IGNORE_COMPONENT_CACHE', true); +// It makes no sense to use BEHAT_CLI for this script (you cannot initialise PHPunit starting from +// the Behat environment), so in case user has set tne environment variable, disable it. +putenv('BEHAT_CLI=0'); + require_once(__DIR__.'/../../../../lib/clilib.php'); require_once(__DIR__.'/../../../../lib/phpunit/bootstraplib.php'); require_once(__DIR__.'/../../../../lib/testing/lib.php'); diff --git a/admin/tool/phpunit/cli/util.php b/admin/tool/phpunit/cli/util.php index a806aaacfbb..3cc928a9e5a 100644 --- a/admin/tool/phpunit/cli/util.php +++ b/admin/tool/phpunit/cli/util.php @@ -30,6 +30,10 @@ if (isset($_SERVER['REMOTE_ADDR'])) { define('IGNORE_COMPONENT_CACHE', true); +// It makes no sense to use BEHAT_CLI for this script (you cannot initialise PHPunit starting from +// the Behat environment), so in case user has set tne environment variable, disable it. +putenv('BEHAT_CLI=0'); + require_once(__DIR__.'/../../../../lib/clilib.php'); require_once(__DIR__.'/../../../../lib/phpunit/bootstraplib.php'); require_once(__DIR__.'/../../../../lib/testing/lib.php'); diff --git a/lib/behat/lib.php b/lib/behat/lib.php index d525e6fb5d3..f17c6858bd1 100644 --- a/lib/behat/lib.php +++ b/lib/behat/lib.php @@ -347,6 +347,11 @@ function behat_is_test_site() { if (empty($CFG->behat_wwwroot)) { return false; } + if (defined('CLI_SCRIPT') && CLI_SCRIPT && getenv('BEHAT_CLI')) { + // Environment variable makes CLI script run on Behat instance. + echo "BEHAT_CLI: This command line script is running on the acceptance testing site.\n\n"; + return true; + } if (isset($_SERVER['REMOTE_ADDR']) and behat_is_requested_url($CFG->behat_wwwroot)) { // Something is accessing the web server like a real browser. return true;