From 68b4f0ea52ce82e8aebf194a55f85924f8128f09 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Wed, 30 Oct 2024 13:04:23 +0000 Subject: [PATCH] MDL-80689 behat: add a transformation to get dirroot Also, back-port #wwwroot# from MDL-78619 which was not backported at the time. --- lib/tests/behat/behat_transformations.php | 46 +++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/lib/tests/behat/behat_transformations.php b/lib/tests/behat/behat_transformations.php index cd5c4a41221..3385530baed 100644 --- a/lib/tests/behat/behat_transformations.php +++ b/lib/tests/behat/behat_transformations.php @@ -133,6 +133,30 @@ class behat_transformations extends behat_base { return $tablenode; } + /** + * Convert #wwwroot# to the wwwroot config value, so it is + * possible to reference fully qualified URLs within the site. + * + * @Transform /^((.*)#wwwroot#(.*))$/ + * @param string $string + * @return string + */ + public function arg_insert_wwwroot(string $string): string { + return $this->replace_wwwroot($string); + } + + /** + * Convert #dirroot# to the dirroot config value, so it is + * possible to reference files (e.g. fixtures) with an absolute path. + * + * @Transform /^((.*)#dirroot#(.*))$/ + * @param string $string + * @return string + */ + public function arg_insert_dirroot(string $string): string { + return $this->replace_dirroot($string); + } + /** * Replaces $NASTYSTRING vars for a nasty string. * @@ -176,4 +200,26 @@ class behat_transformations extends behat_base { return $time; } } + + /** + * Replace #wwwroot# with the actual wwwroot config value. + * + * @param string $string String to attempt the replacement in. + * @return string + */ + protected function replace_wwwroot(string $string): string { + global $CFG; + return str_replace('#wwwroot#', $CFG->wwwroot, $string); + } + + /** + * Replace #dirroot# with the actual dirroot config value. + * + * @param string $string String to attempt the replacement in. + * @return string + */ + protected function replace_dirroot(string $string): string { + global $CFG; + return str_replace('#dirroot#', $CFG->dirroot, $string); + } }