diff --git a/admin/tool/behat/tests/behat/datetime_strings.feature b/admin/tool/behat/tests/behat/datetime_strings.feature new file mode 100644 index 00000000000..0b4e7e55744 --- /dev/null +++ b/admin/tool/behat/tests/behat/datetime_strings.feature @@ -0,0 +1,25 @@ +@tool @tool_behat +Feature: Transform date time string arguments + In order to write tests with relative date and time + As a user + I need to apply some transformations to the steps arguments + + Scenario: Set date in table and check date with specific format + Given I am on site homepage + And the following "users" exist: + | username | firstname | lastname | + | teacher1 | Teacher | 1 | + And the following "courses" exist: + | fullname | shortname | category | + | Course 1 | C1 | 0 | + And the following "activities" exist: + | activity | course | idnumber | name | intro | duedate | + | assign | C1 | assign1 | Test assignment name | Test assignment description | ##yesterday## | + And the following "course enrolments" exist: + | user | course | role | + | teacher1 | C1 | editingteacher | + And I log in as "teacher1" + And I follow "Course 1" + And I follow "Test assignment name" + And I should see "##yesterday##l, j F Y##" + And I log out diff --git a/lib/tests/behat/behat_transformations.php b/lib/tests/behat/behat_transformations.php index a8f745411cb..08174e1884d 100644 --- a/lib/tests/behat/behat_transformations.php +++ b/lib/tests/behat/behat_transformations.php @@ -44,26 +44,11 @@ use Behat\Gherkin\Node\TableNode; */ class behat_transformations extends behat_base { - /** - * Removes escaped argument delimiters. - * - * We use double quotes as arguments delimiters and - * to add the " as part of an argument we escape it - * with a backslash, this method removes this backslash. - * - * @Transform /^((.*)"(.*))$/ - * @param string $string - * @return string The string with the arguments fixed. - */ - public function arg_replace_slashes($string) { - if (!is_scalar($string)) { - return $string; - } - return str_replace('\"', '"', $string); - } - /** * Replaces $NASTYSTRING vars for a nasty string. + * NOTE: This has to be done before espace transformation, as + * last transformation is performed first and it replaces + * \" with ". * * @Transform /^((.*)\$NASTYSTRING(\d)(.*))$/ * @param string $argument The whole argument value. @@ -82,21 +67,42 @@ class behat_transformations extends behat_base { * Transformations applicable to TableNode arguments should also * be applied, adding them in a different method for Behat API restrictions. * - * @Transform table:Surname,My Surname $NASTYSTRING2 + * @deprecated since Moodle 3.2 MDL-56335 - please do not use this function any more. * @param TableNode $tablenode * @return TableNode The transformed table */ public function prefixed_tablenode_transformations(TableNode $tablenode) { + debugging('prefixed_tablenode_transformations() is deprecated. Please use tablenode_transformations() instead.', + DEBUG_DEVELOPER); + return $this->tablenode_transformations($tablenode); } + /** + * Removes escaped argument delimiters. + * + * We use double quotes as arguments delimiters and + * to add the " as part of an argument we escape it + * with a backslash, this method removes this backslash. + * + * @Transform /^((.*)"(.*))$/ + * @param string $string + * @return string The string with the arguments fixed. + */ + public function arg_replace_slashes($string) { + if (!is_scalar($string)) { + return $string; + } + return str_replace('\"', '"', $string); + } + /** * Transformations for TableNode arguments. * * Transformations applicable to TableNode arguments should also * be applied, adding them in a different method for Behat API restrictions. * - * @Transform table:Surname,$NASTYSTRING1 + * @Transform table:* * @param TableNode $tablenode * @return TableNode The transformed table */ @@ -110,6 +116,13 @@ class behat_transformations extends behat_base { if (preg_match('/\$NASTYSTRING(\d)/', $rows[$rowkey][$colkey])) { $rows[$rowkey][$colkey] = $this->replace_nasty_strings($rows[$rowkey][$colkey]); } + + // Transform time. + if (preg_match('/^##(.*)##$/', $rows[$rowkey][$colkey], $match)) { + if (isset($match[1])) { + $rows[$rowkey][$colkey] = $this->get_transformed_timestamp($match[1]); + } + } } } @@ -138,4 +151,41 @@ class behat_transformations extends behat_base { ); } + /** + * Convert string time to timestamp. + * Use ::time::STRING_TIME_TO_CONVERT::DATE_FORMAT:: + * + * @Transform /^##(.*)##$/ + * @param string $time + * @return int timestamp. + */ + public function arg_time_to_string($time) { + return $this->get_transformed_timestamp($time); + } + + /** + * Return timestamp for the time passed. + * + * @param string $time time to convert + * @return string + */ + protected function get_transformed_timestamp($time) { + $timepassed = explode('##', $time); + + // If not a valid time string, then just return what was passed. + if ((($timestamp = strtotime($timepassed[0])) === false)) { + return $time; + } + + $count = count($timepassed); + if ($count === 2) { + // If timestamp with spcified format, then retrun date. + return date($timepassed[1], $timestamp); + } else if ($count === 1) { + return $timestamp; + } else { + // If not a valid time string, then just return what was passed. + return $time; + } + } }