MDL-70658 phpunit: never apply for time() in data providers

Data providers are executed at the beginning of the execution
of the phpunit run, not when the test is run.

So, setting anything on them being dependent of current time
has high chances or fail when the test is executed (maybe some
hours later in slow databases / systems).

Data provider only can contain the relative values and then,
when the test is effectively run, they are applied to *current* time.
This commit is contained in:
Eloy Lafuente (stronk7) 2021-11-25 18:13:51 +01:00
parent b34da7b184
commit 103a4b5c92

View File

@ -198,6 +198,10 @@ class meeting_test extends \advanced_testcase {
* @covers ::can_join
*/
public function test_can_join_with_dates(int $type, ?string $groupname, int $groupmode, array $canjoin, array $dates) {
// Apply the data provider relative values to now.
array_walk($dates, function(&$val) {
$val = time() + $val;
});
$this->resetAfterTest();
[$meeting, $useringroup, $usernotingroup, $groupid, $activity] =
$this->prepare_meeting($type, $groupname, $groupmode, true, $dates);
@ -283,14 +287,14 @@ class meeting_test extends \advanced_testcase {
'groupname' => null,
'groupmode' => NOGROUPS,
'canjoin' => ['useringroup' => false, 'usernotingroup' => false],
'dates' => ['openingtime' => time() - 7200, 'closingtime' => time() - 3600]
'dates' => ['openingtime' => -7200, 'closingtime' => -3600]
],
'Instance Type ALL - No Group - Open in future' => [
'type' => instance::TYPE_ALL,
'groupname' => null,
'groupmode' => NOGROUPS,
'canjoin' => ['useringroup' => false, 'usernotingroup' => false],
'dates' => ['openingtime' => time() + 3600, 'closingtime' => time() + 7200]
'dates' => ['openingtime' => 3600, 'closingtime' => 7200]
],
];
}