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:
@@ -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}/");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user