Skip to content

Commit

Permalink
MDL-77837 phpunit: Ensure that the cron user setter is used
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
andrewnicols committed Apr 6, 2023
1 parent 72a5114 commit 22ac74f
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/phpunit/classes/advanced_testcase.php
Expand Up @@ -702,7 +702,7 @@ protected function runAdhocTasks($matchclass = '', $matchuserid = null) {
}

cron_prepare_core_renderer();
$this->setUser($user);
cron_setup_user($user);

$task->execute();
\core\task\manager::adhoc_task_complete($task);
Expand Down
54 changes: 54 additions & 0 deletions lib/phpunit/tests/advanced_test.php
Expand Up @@ -33,8 +33,13 @@
* @category phpunit
* @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 core_phpunit_advanced_testcase extends advanced_testcase {
public static function setUpBeforeClass(): void {
global $CFG;
require_once(__DIR__ . '/fixtures/adhoc_test_task.php');
}

public function test_debugging() {
global $CFG;
Expand Down Expand Up @@ -680,4 +685,53 @@ public function test_it_resets_useragent_after_test() {
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}/");
}
}
35 changes: 35 additions & 0 deletions lib/phpunit/tests/fixtures/adhoc_test_task.php
@@ -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}");
}
}

0 comments on commit 22ac74f

Please sign in to comment.