1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 namespace core; 18 19 /** 20 * Tests for core\cron. 21 * 22 * @package core 23 * @copyright 2023 Andrew Nicols <andrew@nicols.co.uk> 24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 25 * @covers \core\cron 26 */ 27 class cron_test extends \advanced_testcase { 28 /** 29 * Reset relevant caches between tests. 30 */ 31 public function setUp(): void { 32 cron::reset_user_cache(); 33 } 34 35 /** 36 * Test the setup_user function. 37 * 38 * @covers ::setup_user 39 * @covers ::reset_user_cache 40 */ 41 public function test_setup_user(): void { 42 // This function uses the $GLOBALS super global. Disable the VariableNameLowerCase sniff for this function. 43 // phpcs:disable moodle.NamingConventions.ValidVariableName.VariableNameLowerCase 44 global $PAGE, $USER, $SESSION, $SITE, $CFG; 45 $this->resetAfterTest(); 46 47 $admin = get_admin(); 48 $user1 = $this->getDataGenerator()->create_user(); 49 $user2 = $this->getDataGenerator()->create_user(); 50 $course = $this->getDataGenerator()->create_course(); 51 52 cron::setup_user(); 53 $this->assertSame($admin->id, $USER->id); 54 $this->assertSame($PAGE->context, \context_course::instance($SITE->id)); 55 $this->assertSame($CFG->timezone, $USER->timezone); 56 $this->assertSame('', $USER->lang); 57 $this->assertSame('', $USER->theme); 58 $SESSION->test1 = true; 59 $adminsession = $SESSION; 60 $adminuser = $USER; 61 $this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']); 62 $this->assertSame($GLOBALS['SESSION'], $SESSION); 63 $this->assertSame($GLOBALS['USER'], $_SESSION['USER']); 64 $this->assertSame($GLOBALS['USER'], $USER); 65 66 cron::setup_user(null, $course); 67 $this->assertSame($admin->id, $USER->id); 68 $this->assertSame($PAGE->context, \context_course::instance($course->id)); 69 $this->assertSame($adminsession, $SESSION); 70 $this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']); 71 $this->assertSame($GLOBALS['SESSION'], $SESSION); 72 $this->assertSame($GLOBALS['USER'], $_SESSION['USER']); 73 $this->assertSame($GLOBALS['USER'], $USER); 74 75 cron::setup_user($user1); 76 $this->assertSame($user1->id, $USER->id); 77 $this->assertSame($PAGE->context, \context_course::instance($SITE->id)); 78 $this->assertNotSame($adminsession, $SESSION); 79 $this->assertObjectNotHasAttribute('test1', $SESSION); 80 $this->assertEmpty((array)$SESSION); 81 $usersession1 = $SESSION; 82 $SESSION->test2 = true; 83 $this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']); 84 $this->assertSame($GLOBALS['SESSION'], $SESSION); 85 $this->assertSame($GLOBALS['USER'], $_SESSION['USER']); 86 $this->assertSame($GLOBALS['USER'], $USER); 87 88 cron::setup_user($user1); 89 $this->assertSame($user1->id, $USER->id); 90 $this->assertSame($PAGE->context, \context_course::instance($SITE->id)); 91 $this->assertNotSame($adminsession, $SESSION); 92 $this->assertSame($usersession1, $SESSION); 93 $this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']); 94 $this->assertSame($GLOBALS['SESSION'], $SESSION); 95 $this->assertSame($GLOBALS['USER'], $_SESSION['USER']); 96 $this->assertSame($GLOBALS['USER'], $USER); 97 98 cron::setup_user($user2); 99 $this->assertSame($user2->id, $USER->id); 100 $this->assertSame($PAGE->context, \context_course::instance($SITE->id)); 101 $this->assertNotSame($adminsession, $SESSION); 102 $this->assertNotSame($usersession1, $SESSION); 103 $this->assertEmpty((array)$SESSION); 104 $usersession2 = $SESSION; 105 $usersession2->test3 = true; 106 $this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']); 107 $this->assertSame($GLOBALS['SESSION'], $SESSION); 108 $this->assertSame($GLOBALS['USER'], $_SESSION['USER']); 109 $this->assertSame($GLOBALS['USER'], $USER); 110 111 cron::setup_user($user2, $course); 112 $this->assertSame($user2->id, $USER->id); 113 $this->assertSame($PAGE->context, \context_course::instance($course->id)); 114 $this->assertNotSame($adminsession, $SESSION); 115 $this->assertNotSame($usersession1, $SESSION); 116 $this->assertSame($usersession2, $SESSION); 117 $this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']); 118 $this->assertSame($GLOBALS['SESSION'], $SESSION); 119 $this->assertSame($GLOBALS['USER'], $_SESSION['USER']); 120 $this->assertSame($GLOBALS['USER'], $USER); 121 122 cron::setup_user($user1); 123 $this->assertSame($user1->id, $USER->id); 124 $this->assertSame($PAGE->context, \context_course::instance($SITE->id)); 125 $this->assertNotSame($adminsession, $SESSION); 126 $this->assertNotSame($usersession1, $SESSION); 127 $this->assertEmpty((array)$SESSION); 128 $this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']); 129 $this->assertSame($GLOBALS['SESSION'], $SESSION); 130 $this->assertSame($GLOBALS['USER'], $_SESSION['USER']); 131 $this->assertSame($GLOBALS['USER'], $USER); 132 133 cron::setup_user(); 134 $this->assertSame($admin->id, $USER->id); 135 $this->assertSame($PAGE->context, \context_course::instance($SITE->id)); 136 $this->assertSame($adminsession, $SESSION); 137 $this->assertSame($adminuser, $USER); 138 $this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']); 139 $this->assertSame($GLOBALS['SESSION'], $SESSION); 140 $this->assertSame($GLOBALS['USER'], $_SESSION['USER']); 141 $this->assertSame($GLOBALS['USER'], $USER); 142 143 cron::reset_user_cache(); 144 $this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']); 145 $this->assertSame($GLOBALS['SESSION'], $SESSION); 146 $this->assertSame($GLOBALS['USER'], $_SESSION['USER']); 147 $this->assertSame($GLOBALS['USER'], $USER); 148 149 cron::setup_user(); 150 $this->assertNotSame($adminsession, $SESSION); 151 $this->assertNotSame($adminuser, $USER); 152 $this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']); 153 $this->assertSame($GLOBALS['SESSION'], $SESSION); 154 $this->assertSame($GLOBALS['USER'], $_SESSION['USER']); 155 $this->assertSame($GLOBALS['USER'], $USER); 156 157 // phpcs:enable 158 } 159 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body