MDL-41157 calendar: add unit tests for calendar_get_course_cached
[moodle.git] / calendar / tests / lib_test.php
CommitLineData
3607961d
DP
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 * Calendar lib unit tests
19 *
20 * @package core_calendar
21 * @copyright 2013 Dan Poltawski <dan@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26global $CFG;
27require_once($CFG->dirroot . '/calendar/lib.php');
28
29/**
30 * Unit tests for calendar lib
31 *
32 * @package core_calendar
33 * @copyright 2013 Dan Poltawski <dan@moodle.com>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class core_calendar_lib_testcase extends advanced_testcase {
37
38 public function test_calendar_get_course_cached() {
39 $this->resetAfterTest(true);
40
41 // Setup some test courses.
42 $course1 = $this->getDataGenerator()->create_course();
43 $course2 = $this->getDataGenerator()->create_course();
44 $course3 = $this->getDataGenerator()->create_course();
45
46 // Load courses into cache.
47 $coursecache = null;
48 calendar_get_course_cached($coursecache, $course1->id);
49 calendar_get_course_cached($coursecache, $course2->id);
50 calendar_get_course_cached($coursecache, $course3->id);
51
52 // Verify the cache.
53 $this->assertArrayHasKey($course1->id, $coursecache);
54 $cachedcourse1 = $coursecache[$course1->id];
55 $this->assertEquals($course1->id, $cachedcourse1->id);
56 $this->assertEquals($course1->shortname, $cachedcourse1->shortname);
57 $this->assertEquals($course1->fullname, $cachedcourse1->fullname);
58
59 $this->assertArrayHasKey($course2->id, $coursecache);
60 $cachedcourse2 = $coursecache[$course2->id];
61 $this->assertEquals($course2->id, $cachedcourse2->id);
62 $this->assertEquals($course2->shortname, $cachedcourse2->shortname);
63 $this->assertEquals($course2->fullname, $cachedcourse2->fullname);
64
65 $this->assertArrayHasKey($course3->id, $coursecache);
66 $cachedcourse3 = $coursecache[$course3->id];
67 $this->assertEquals($course3->id, $cachedcourse3->id);
68 $this->assertEquals($course3->shortname, $cachedcourse3->shortname);
69 $this->assertEquals($course3->fullname, $cachedcourse3->fullname);
70 }
71}