MDL-77837 phpunit: Ensure that the cron user setter is used

When running an adhoc task in a unit test we should use the cron variant
of the set user method to mimic the behaviour of a real cron run.
This commit is contained in:
Andrew Nicols 2023-04-04 09:26:34 +08:00
parent 346cb39cff
commit 44d734147a
3 changed files with 90 additions and 1 deletions

View File

@ -733,7 +733,7 @@ abstract class advanced_testcase extends base_testcase {
}
cron_prepare_core_renderer();
$this->setUser($user);
cron_setup_user($user);
$task->execute();
\core\task\manager::adhoc_task_complete($task);

View File

@ -23,8 +23,13 @@ namespace core;
* @category test
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \advanced_testcase
*/
class advanced_test extends \advanced_testcase {
public static function setUpBeforeClass(): void {
global $CFG;
require_once(__DIR__ . '/fixtures/adhoc_test_task.php');
}
public function test_debugging() {
global $CFG;
@ -697,4 +702,53 @@ class advanced_test extends \advanced_testcase {
self::resetAllData(false);
self::assertFalse(\core_useragent::get_user_agent_string(), 'It should not be set again, data was reset.');
}
/**
* @covers ::runAdhocTasks
*/
public function test_runadhoctasks_no_tasks_queued(): void {
$this->runAdhocTasks();
$this->expectOutputRegex('/^$/');
}
/**
* @covers ::runAdhocTasks
*/
public function test_runadhoctasks_tasks_queued(): void {
$this->resetAfterTest(true);
$admin = get_admin();
\core\task\manager::queue_adhoc_task(new \core_phpunit\adhoc_test_task());
$this->runAdhocTasks();
$this->expectOutputRegex("/Task was run as {$admin->id}/");
}
/**
* @covers ::runAdhocTasks
*/
public function test_runadhoctasks_with_existing_user_change(): void {
$this->resetAfterTest(true);
$admin = get_admin();
$this->setGuestUser();
\core\task\manager::queue_adhoc_task(new \core_phpunit\adhoc_test_task());
$this->runAdhocTasks();
$this->expectOutputRegex("/Task was run as {$admin->id}/");
}
/**
* @covers ::runAdhocTasks
*/
public function test_runadhoctasks_with_existing_user_change_and_specified(): void {
global $USER;
$this->resetAfterTest(true);
$user = $this->getDataGenerator()->create_user();
$this->setGuestUser();
$task = new \core_phpunit\adhoc_test_task();
$task->set_userid($user->id);
\core\task\manager::queue_adhoc_task($task);
$this->runAdhocTasks();
$this->expectOutputRegex("/Task was run as {$user->id}/");
}
}

View File

@ -0,0 +1,35 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_phpunit;
/**
* Fixtures for task tests.
*
* @package core
* @category phpunit
* @copyright 2023 Andrew Lyons <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class adhoc_test_task extends \core\task\adhoc_task {
/**
* Execute.
*/
public function execute() {
global $USER;
mtrace("Task was run as {$USER->id}");
}
}