Skip to content

Commit

Permalink
Merge branch 'wip-mdl-55522' of https://github.com/rajeshtaneja/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Oct 11, 2016
2 parents 287a906 + 389e5a0 commit d5bc946
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 20 deletions.
25 changes: 25 additions & 0 deletions admin/tool/behat/tests/behat/datetime_strings.feature
Original file line number Diff line number Diff line change
@@ -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
90 changes: 70 additions & 20 deletions lib/tests/behat/behat_transformations.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,11 @@
*/
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.
Expand All @@ -82,21 +67,42 @@ public function arg_replace_nasty_strings($argument) {
* 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
*/
Expand All @@ -110,6 +116,13 @@ public function tablenode_transformations(TableNode $tablenode) {
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]);
}
}
}
}

Expand Down Expand Up @@ -138,4 +151,41 @@ function ($matches) {
);
}

/**
* 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;
}
}
}

0 comments on commit d5bc946

Please sign in to comment.