From 7c16d5818fec354615dfbd21c138c6676ffde0bf Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Mon, 9 Oct 2023 11:18:37 +0200 Subject: [PATCH] MOBILE-4069 behat: Support time transformation in find step --- .../tests/behat/behat_app_helper.php | 37 ++++++++++++++++++- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/local_moodleappbehat/tests/behat/behat_app_helper.php b/local_moodleappbehat/tests/behat/behat_app_helper.php index 70235a55c..c5538a701 100644 --- a/local_moodleappbehat/tests/behat/behat_app_helper.php +++ b/local_moodleappbehat/tests/behat/behat_app_helper.php @@ -275,13 +275,13 @@ class behat_app_helper extends behat_base { ); $locator = [ - 'text' => str_replace('\\"', '"', $matches[1]), + 'text' => $this->transform_time_to_string(str_replace('\\"', '"', $matches[1])), 'selector' => $matches[2] ?? null, ]; if (!empty($matches[3])) { $locator[$matches[3]] = (object) [ - 'text' => str_replace('\\"', '"', $matches[4]), + 'text' => $this->transform_time_to_string(str_replace('\\"', '"', $matches[4])), 'selector' => $matches[5] ?? null, ]; } @@ -608,4 +608,37 @@ EOF; $this->getSession()->getDriver()->resizeWindow($width + $offset['x'], $height + $offset['y']); } + + /** + * Given a string, search if it contains a time with the ## format and convert it to a timestamp or readable time. + * Only allows 1 occurence, if the text contains more than one time sub-string it won't work as expected. + * This function is similar to the arg_time_to_string transformation, but it allows the time to be a sub-text of the string. + * + * @param string $text + * @return string Transformed text. + */ + protected function transform_time_to_string(string $text): string { + if (!preg_match('/##(.*)##/', $text, $matches)) { + // No time found, return the original text. + return $text; + } + + $timepassed = explode('##', $matches[1]); + + // If not a valid time string, then just return what was passed. + if ((($timestamp = strtotime($timepassed[0])) === false)) { + return $text; + } + + $count = count($timepassed); + if ($count === 2) { + // If timestamp with specified strftime format, then return formatted date string. + return str_replace($matches[0], userdate($timestamp, $timepassed[1]), $text); + } else if ($count === 1) { + return str_replace($matches[0], $timestamp, $text); + } else { + // If not a valid time string, then just return what was passed. + return $text; + } + } }