mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 17:06:53 +02:00
MDL-77186 core: Move cron_setup_user to namespaced class
This commit is contained in:
parent
070c781097
commit
3cd05c7a15
4 changed files with 250 additions and 55 deletions
159
lib/tests/cron_test.php
Normal file
159
lib/tests/cron_test.php
Normal file
|
@ -0,0 +1,159 @@
|
|||
<?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;
|
||||
|
||||
/**
|
||||
* Tests for core\cron.
|
||||
*
|
||||
* @package core
|
||||
* @copyright 2023 Andrew Nicols <andrew@nicols.co.uk>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @covers \core\cron
|
||||
*/
|
||||
class cron_test extends \advanced_testcase {
|
||||
/**
|
||||
* Reset relevant caches between tests.
|
||||
*/
|
||||
public function setUp(): void {
|
||||
cron::reset_user_cache();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the setup_user function.
|
||||
*
|
||||
* @covers ::setup_user
|
||||
* @covers ::reset_user_cache
|
||||
*/
|
||||
public function test_setup_user(): void {
|
||||
// This function uses the $GLOBALS super global. Disable the VariableNameLowerCase sniff for this function.
|
||||
// phpcs:disable moodle.NamingConventions.ValidVariableName.VariableNameLowerCase
|
||||
global $PAGE, $USER, $SESSION, $SITE, $CFG;
|
||||
$this->resetAfterTest();
|
||||
|
||||
$admin = get_admin();
|
||||
$user1 = $this->getDataGenerator()->create_user();
|
||||
$user2 = $this->getDataGenerator()->create_user();
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
|
||||
cron::setup_user();
|
||||
$this->assertSame($admin->id, $USER->id);
|
||||
$this->assertSame($PAGE->context, \context_course::instance($SITE->id));
|
||||
$this->assertSame($CFG->timezone, $USER->timezone);
|
||||
$this->assertSame('', $USER->lang);
|
||||
$this->assertSame('', $USER->theme);
|
||||
$SESSION->test1 = true;
|
||||
$adminsession = $SESSION;
|
||||
$adminuser = $USER;
|
||||
$this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
|
||||
$this->assertSame($GLOBALS['SESSION'], $SESSION);
|
||||
$this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
|
||||
$this->assertSame($GLOBALS['USER'], $USER);
|
||||
|
||||
cron::setup_user(null, $course);
|
||||
$this->assertSame($admin->id, $USER->id);
|
||||
$this->assertSame($PAGE->context, \context_course::instance($course->id));
|
||||
$this->assertSame($adminsession, $SESSION);
|
||||
$this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
|
||||
$this->assertSame($GLOBALS['SESSION'], $SESSION);
|
||||
$this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
|
||||
$this->assertSame($GLOBALS['USER'], $USER);
|
||||
|
||||
cron::setup_user($user1);
|
||||
$this->assertSame($user1->id, $USER->id);
|
||||
$this->assertSame($PAGE->context, \context_course::instance($SITE->id));
|
||||
$this->assertNotSame($adminsession, $SESSION);
|
||||
$this->assertObjectNotHasAttribute('test1', $SESSION);
|
||||
$this->assertEmpty((array)$SESSION);
|
||||
$usersession1 = $SESSION;
|
||||
$SESSION->test2 = true;
|
||||
$this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
|
||||
$this->assertSame($GLOBALS['SESSION'], $SESSION);
|
||||
$this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
|
||||
$this->assertSame($GLOBALS['USER'], $USER);
|
||||
|
||||
cron::setup_user($user1);
|
||||
$this->assertSame($user1->id, $USER->id);
|
||||
$this->assertSame($PAGE->context, \context_course::instance($SITE->id));
|
||||
$this->assertNotSame($adminsession, $SESSION);
|
||||
$this->assertSame($usersession1, $SESSION);
|
||||
$this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
|
||||
$this->assertSame($GLOBALS['SESSION'], $SESSION);
|
||||
$this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
|
||||
$this->assertSame($GLOBALS['USER'], $USER);
|
||||
|
||||
cron::setup_user($user2);
|
||||
$this->assertSame($user2->id, $USER->id);
|
||||
$this->assertSame($PAGE->context, \context_course::instance($SITE->id));
|
||||
$this->assertNotSame($adminsession, $SESSION);
|
||||
$this->assertNotSame($usersession1, $SESSION);
|
||||
$this->assertEmpty((array)$SESSION);
|
||||
$usersession2 = $SESSION;
|
||||
$usersession2->test3 = true;
|
||||
$this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
|
||||
$this->assertSame($GLOBALS['SESSION'], $SESSION);
|
||||
$this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
|
||||
$this->assertSame($GLOBALS['USER'], $USER);
|
||||
|
||||
cron::setup_user($user2, $course);
|
||||
$this->assertSame($user2->id, $USER->id);
|
||||
$this->assertSame($PAGE->context, \context_course::instance($course->id));
|
||||
$this->assertNotSame($adminsession, $SESSION);
|
||||
$this->assertNotSame($usersession1, $SESSION);
|
||||
$this->assertSame($usersession2, $SESSION);
|
||||
$this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
|
||||
$this->assertSame($GLOBALS['SESSION'], $SESSION);
|
||||
$this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
|
||||
$this->assertSame($GLOBALS['USER'], $USER);
|
||||
|
||||
cron::setup_user($user1);
|
||||
$this->assertSame($user1->id, $USER->id);
|
||||
$this->assertSame($PAGE->context, \context_course::instance($SITE->id));
|
||||
$this->assertNotSame($adminsession, $SESSION);
|
||||
$this->assertNotSame($usersession1, $SESSION);
|
||||
$this->assertEmpty((array)$SESSION);
|
||||
$this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
|
||||
$this->assertSame($GLOBALS['SESSION'], $SESSION);
|
||||
$this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
|
||||
$this->assertSame($GLOBALS['USER'], $USER);
|
||||
|
||||
cron::setup_user();
|
||||
$this->assertSame($admin->id, $USER->id);
|
||||
$this->assertSame($PAGE->context, \context_course::instance($SITE->id));
|
||||
$this->assertSame($adminsession, $SESSION);
|
||||
$this->assertSame($adminuser, $USER);
|
||||
$this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
|
||||
$this->assertSame($GLOBALS['SESSION'], $SESSION);
|
||||
$this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
|
||||
$this->assertSame($GLOBALS['USER'], $USER);
|
||||
|
||||
cron::reset_user_cache();
|
||||
$this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
|
||||
$this->assertSame($GLOBALS['SESSION'], $SESSION);
|
||||
$this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
|
||||
$this->assertSame($GLOBALS['USER'], $USER);
|
||||
|
||||
cron::setup_user();
|
||||
$this->assertNotSame($adminsession, $SESSION);
|
||||
$this->assertNotSame($adminuser, $USER);
|
||||
$this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
|
||||
$this->assertSame($GLOBALS['SESSION'], $SESSION);
|
||||
$this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
|
||||
$this->assertSame($GLOBALS['USER'], $USER);
|
||||
|
||||
// phpcs:enable
|
||||
}
|
||||
}
|
|
@ -26,12 +26,20 @@ namespace core;
|
|||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class sessionlib_test extends \advanced_testcase {
|
||||
|
||||
/**
|
||||
* @covers ::cron_setup_user
|
||||
*/
|
||||
public function test_cron_setup_user() {
|
||||
// This function uses the $GLOBALS super global. Disable the VariableNameLowerCase sniff for this function.
|
||||
// phpcs:disable moodle.NamingConventions.ValidVariableName.VariableNameLowerCase
|
||||
|
||||
global $PAGE, $USER, $SESSION, $SITE, $CFG;
|
||||
$this->resetAfterTest();
|
||||
|
||||
// NOTE: this function contains some static caches, let's reset first.
|
||||
cron_setup_user('reset');
|
||||
$this->assertDebuggingCalledCount(1);
|
||||
|
||||
$admin = get_admin();
|
||||
$user1 = $this->getDataGenerator()->create_user();
|
||||
|
@ -39,6 +47,7 @@ class sessionlib_test extends \advanced_testcase {
|
|||
$course = $this->getDataGenerator()->create_course();
|
||||
|
||||
cron_setup_user();
|
||||
$this->assertDebuggingCalledCount(1);
|
||||
$this->assertSame($admin->id, $USER->id);
|
||||
$this->assertSame($PAGE->context, \context_course::instance($SITE->id));
|
||||
$this->assertSame($CFG->timezone, $USER->timezone);
|
||||
|
@ -53,6 +62,7 @@ class sessionlib_test extends \advanced_testcase {
|
|||
$this->assertSame($GLOBALS['USER'], $USER);
|
||||
|
||||
cron_setup_user(null, $course);
|
||||
$this->assertDebuggingCalledCount(1);
|
||||
$this->assertSame($admin->id, $USER->id);
|
||||
$this->assertSame($PAGE->context, \context_course::instance($course->id));
|
||||
$this->assertSame($adminsession, $SESSION);
|
||||
|
@ -62,6 +72,7 @@ class sessionlib_test extends \advanced_testcase {
|
|||
$this->assertSame($GLOBALS['USER'], $USER);
|
||||
|
||||
cron_setup_user($user1);
|
||||
$this->assertDebuggingCalledCount(1);
|
||||
$this->assertSame($user1->id, $USER->id);
|
||||
$this->assertSame($PAGE->context, \context_course::instance($SITE->id));
|
||||
$this->assertNotSame($adminsession, $SESSION);
|
||||
|
@ -75,6 +86,7 @@ class sessionlib_test extends \advanced_testcase {
|
|||
$this->assertSame($GLOBALS['USER'], $USER);
|
||||
|
||||
cron_setup_user($user1);
|
||||
$this->assertDebuggingCalledCount(1);
|
||||
$this->assertSame($user1->id, $USER->id);
|
||||
$this->assertSame($PAGE->context, \context_course::instance($SITE->id));
|
||||
$this->assertNotSame($adminsession, $SESSION);
|
||||
|
@ -85,6 +97,7 @@ class sessionlib_test extends \advanced_testcase {
|
|||
$this->assertSame($GLOBALS['USER'], $USER);
|
||||
|
||||
cron_setup_user($user2);
|
||||
$this->assertDebuggingCalledCount(1);
|
||||
$this->assertSame($user2->id, $USER->id);
|
||||
$this->assertSame($PAGE->context, \context_course::instance($SITE->id));
|
||||
$this->assertNotSame($adminsession, $SESSION);
|
||||
|
@ -98,6 +111,7 @@ class sessionlib_test extends \advanced_testcase {
|
|||
$this->assertSame($GLOBALS['USER'], $USER);
|
||||
|
||||
cron_setup_user($user2, $course);
|
||||
$this->assertDebuggingCalledCount(1);
|
||||
$this->assertSame($user2->id, $USER->id);
|
||||
$this->assertSame($PAGE->context, \context_course::instance($course->id));
|
||||
$this->assertNotSame($adminsession, $SESSION);
|
||||
|
@ -109,6 +123,7 @@ class sessionlib_test extends \advanced_testcase {
|
|||
$this->assertSame($GLOBALS['USER'], $USER);
|
||||
|
||||
cron_setup_user($user1);
|
||||
$this->assertDebuggingCalledCount(1);
|
||||
$this->assertSame($user1->id, $USER->id);
|
||||
$this->assertSame($PAGE->context, \context_course::instance($SITE->id));
|
||||
$this->assertNotSame($adminsession, $SESSION);
|
||||
|
@ -120,6 +135,7 @@ class sessionlib_test extends \advanced_testcase {
|
|||
$this->assertSame($GLOBALS['USER'], $USER);
|
||||
|
||||
cron_setup_user();
|
||||
$this->assertDebuggingCalledCount(1);
|
||||
$this->assertSame($admin->id, $USER->id);
|
||||
$this->assertSame($PAGE->context, \context_course::instance($SITE->id));
|
||||
$this->assertSame($adminsession, $SESSION);
|
||||
|
@ -130,18 +146,22 @@ class sessionlib_test extends \advanced_testcase {
|
|||
$this->assertSame($GLOBALS['USER'], $USER);
|
||||
|
||||
cron_setup_user('reset');
|
||||
$this->assertDebuggingCalledCount(1);
|
||||
$this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
|
||||
$this->assertSame($GLOBALS['SESSION'], $SESSION);
|
||||
$this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
|
||||
$this->assertSame($GLOBALS['USER'], $USER);
|
||||
|
||||
cron_setup_user();
|
||||
$this->assertDebuggingCalledCount(1);
|
||||
$this->assertNotSame($adminsession, $SESSION);
|
||||
$this->assertNotSame($adminuser, $USER);
|
||||
$this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
|
||||
$this->assertSame($GLOBALS['SESSION'], $SESSION);
|
||||
$this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
|
||||
$this->assertSame($GLOBALS['USER'], $USER);
|
||||
|
||||
// phpcs:enable
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue