Skip to content

Commit c58a1e9

Browse files
committed
MDL-65084 recent items: Test handling of deleted courses
1 parent d0b1204 commit c58a1e9

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
/**
18+
* Block recentlyaccesseditems helper tests.
19+
*
20+
* @package block_recentlyaccesseditems
21+
* @copyright 2019 University of Nottingham
22+
* @author Neill Magill <[email protected]>
23+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24+
*/
25+
26+
use block_recentlyaccesseditems\helper;
27+
28+
defined('MOODLE_INTERNAL') || die();
29+
30+
/**
31+
* Block Recently accessed helper class tests.
32+
*
33+
* @package block_recentlyaccesseditems
34+
* @copyright 2019 University of Nottingham
35+
* @author Neill Magill <[email protected]>
36+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37+
*/
38+
class block_recentlyaccesseditems_helper_testcase extends advanced_testcase {
39+
/**
40+
* Tests that the get recent items method can handle getting records when courses have been deleted.
41+
*/
42+
public function test_get_recent_items() {
43+
$this->resetAfterTest();
44+
$course = self::getDataGenerator()->create_course();
45+
$coursetodelete = self::getDataGenerator()->create_course();
46+
$user = self::getDataGenerator()->create_and_enrol($course, 'student');
47+
self::getDataGenerator()->enrol_user($user->id, $coursetodelete->id, 'student');
48+
49+
// Add an activity to each course.
50+
$forum = self::getDataGenerator()->create_module('forum', ['course' => $course]);
51+
$glossary = self::getDataGenerator()->create_module('glossary', ['course' => $coursetodelete]);
52+
self::setUser($user);
53+
54+
// Get the user to visit the activities.
55+
$event1params = ['context' => context_module::instance($forum->cmid), 'objectid' => $forum->id];
56+
$event1 = \mod_forum\event\course_module_viewed::create($event1params);
57+
$event1->trigger();
58+
$event2params = ['context' => context_module::instance($glossary->cmid), 'objectid' => $glossary->id];
59+
$event2 = \mod_glossary\event\course_module_viewed::create($event2params);
60+
$event2->trigger();
61+
$recent1 = helper::get_recent_items();
62+
self::assertCount(2, $recent1);
63+
$recentlinited = helper::get_recent_items(1);
64+
self::assertCount(1, $recentlinited);
65+
delete_course($coursetodelete, false);
66+
67+
// There should be no errors if a course has been deleted.
68+
$recent2 = helper::get_recent_items();
69+
self::assertCount(1, $recent2);
70+
}
71+
}

0 commit comments

Comments
 (0)