Skip to content

Commit

Permalink
MDL-81074 core: Add assertTimeStringMatches PHPUnit Assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Mar 21, 2024
1 parent 4aea02a commit cb90694
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/phpunit/classes/base_testcase.php
Expand Up @@ -121,6 +121,31 @@ public static function assertValidKeys(array $hash, array $validKeys) {
return $hash;
}

/**
* Assert that two Date/Time strings are equal.
*
* The strings generated by \DateTime, \strtotime, \date, \time, etc. are generated outside of our control.
* From time-to-time string changes are made.
* One such example is from ICU 72.1 which changed the time format to include a narrow-non-breaking-space (U+202F)
* between the time and AM/PM.
*
* We should not update our tests to match these changes, as it is not our code that is
* generating the strings and they may change again.
* In addition, the changes are not equal amongst all systems as they depend on the version of ICU installed.
*
* @param string $expected
* @param string $actual
* @param string $message
*/
public function assertEqualsIgnoringWhitespace($expected, $actual, string $message = ''): void {
// ICU 72.1 introduced the use of a narrow-non-breaking-space (U+202F) between the time and the AM/PM.
// Normalise all whitespace when performing the comparison.
$expected = preg_replace('/\s+/u', ' ', $expected);
$actual = preg_replace('/\s+/u', ' ', $actual);

$this->assertEquals($expected, $actual, $message);
}

/**
* Parse out the options from the tag using DOM object tree.
*
Expand Down

0 comments on commit cb90694

Please sign in to comment.