From cdf17eb7d5e7f68ea0b78584554d46c767b6cbd6 Mon Sep 17 00:00:00 2001 From: Michael Hawkins Date: Fri, 25 Aug 2023 03:40:17 +0800 Subject: [PATCH] MDL-78619 behat: Add wwwroot transformation This will replace #wwwroot# with the value of the wwwroot config. This is useful where a FQDN within the test instance is required. --- lib/tests/behat/behat_transformations.php | 29 +++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/lib/tests/behat/behat_transformations.php b/lib/tests/behat/behat_transformations.php index cd5c4a41221..bbd80ad44d3 100644 --- a/lib/tests/behat/behat_transformations.php +++ b/lib/tests/behat/behat_transformations.php @@ -107,6 +107,7 @@ class behat_transformations extends behat_base { * @return TableNode The transformed table */ public function tablenode_transformations(TableNode $tablenode) { + global $CFG; // Walk through all values including the optional headers. $rows = $tablenode->getRows(); foreach ($rows as $rowkey => $row) { @@ -123,6 +124,11 @@ class behat_transformations extends behat_base { $rows[$rowkey][$colkey] = $this->get_transformed_timestamp($match[1]); } } + + // Transform wwwroot. + if (preg_match('/#wwwroot#/', $rows[$rowkey][$colkey])) { + $rows[$rowkey][$colkey] = $this->replace_wwwroot($rows[$rowkey][$colkey]); + } } } @@ -133,6 +139,18 @@ 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); + } + /** * Replaces $NASTYSTRING vars for a nasty string. * @@ -176,4 +194,15 @@ 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); + } }