Merge branch 'MDL-77837-401' of https://github.com/andrewnicols/moodle into MOODLE_401_STABLE

This commit is contained in:
Sara Arjona 2023-04-11 12:12:49 +02:00
commit 5563e99f2c
5 changed files with 103 additions and 2 deletions

View File

@ -248,6 +248,10 @@ function cron_run_inner_scheduled_task(\core\task\task_base $task) {
$predbqueries = null;
$predbqueries = $DB->perf_get_queries();
$pretime = microtime(1);
// Ensure that we have a clean session with the correct cron user.
cron_setup_user();
try {
get_mailer('buffer');
cron_prepare_core_renderer();
@ -346,6 +350,10 @@ function cron_run_inner_adhoc_task(\core\task\adhoc_task $task) {
}
cron_setup_user($user);
} else {
// No user specified, ensure that we have a clean session with the correct cron user.
cron_setup_user();
}
try {

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}");
}
}

View File

@ -170,7 +170,11 @@ function get_moodle_cookie() {
/**
* Sets up current user and course environment (lang, etc.) in cron.
* Do not use outside of cron script!
* Note: This function is intended only for use in:
* - the cron runner scripts
* - individual tasks which extend the adhoc_task and scheduled_task classes
* - unit tests related to tasks
* - other parts of the cron/task system
*
* @param stdClass $user full user object, null means default cron user (admin),
* value 'reset' means reset internal static caches.