Skip to content

Commit

Permalink
MDL-78619 behat: Add wwwroot transformation
Browse files Browse the repository at this point in the history
This will replace #wwwroot# with the value of the wwwroot config.
This is useful where a FQDN within the test instance is required.
  • Loading branch information
mickhawkins committed Aug 27, 2023
1 parent 9bfcd77 commit cdf17eb
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/tests/behat/behat_transformations.php
Expand Up @@ -107,6 +107,7 @@ public function arg_time_to_string($time) {
* @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) {
Expand All @@ -123,6 +124,11 @@ public function tablenode_transformations(TableNode $tablenode) {
$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]);
}
}
}

Expand All @@ -133,6 +139,18 @@ public function tablenode_transformations(TableNode $tablenode) {
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.
*
Expand Down Expand Up @@ -176,4 +194,15 @@ protected function get_transformed_timestamp($time) {
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);
}
}

0 comments on commit cdf17eb

Please sign in to comment.