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 20, 2024
1 parent 39b8e19 commit 643797b
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/phpunit/classes/base_testcase.php
Expand Up @@ -121,6 +121,30 @@ 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 = '') {
// 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 643797b

Please sign in to comment.