Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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_calendar;
18
 
19
use core_calendar\external\events_related_objects_cache;
20
use core_calendar\local\event\container;
21
 
22
defined('MOODLE_INTERNAL') || die();
23
 
24
require_once(__DIR__ . '/helpers.php');
25
 
26
/**
27
 * Tests for the events_related_objects_cache.
28
 *
29
 * @package    core_calendar
30
 * @copyright  2017 Ryan Wyllie <ryan@moodle.com>
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
1441 ariadna 33
final class events_related_objects_cache_test extends \advanced_testcase {
1 efrain 34
 
35
    /**
36
     * Tests set up
37
     */
38
    protected function setUp(): void {
1441 ariadna 39
        parent::setUp();
1 efrain 40
        $this->resetAfterTest();
41
    }
42
 
43
    /**
44
     * An event with no module should return null when trying to retrieve
45
     * the module instance.
46
     */
11 efrain 47
    public function test_get_module_instance_no_module(): void {
1 efrain 48
        $this->setAdminUser();
49
        $mapper = container::get_event_mapper();
50
        $legacyevent = create_event([
51
            'modulename' => '',
52
            'instance' => 0
53
        ]);
54
        $event = $mapper->from_legacy_event_to_event($legacyevent);
55
        $cache = new events_related_objects_cache([$event]);
56
 
57
        $this->assertNull($cache->get_module_instance($event));
58
    }
59
 
60
    /**
61
     * The get_module_instance should return the correct module instances
62
     * for the given set of events in the cache.
63
     */
11 efrain 64
    public function test_get_module_instance_with_modules(): void {
1 efrain 65
        $this->setAdminUser();
66
        $mapper = container::get_event_mapper();
67
        $generator = $this->getDataGenerator();
68
        $course = $generator->create_course();
69
        $plugingenerator = $generator->get_plugin_generator('mod_assign');
70
        $instance1 = $plugingenerator->create_instance(['course' => $course->id]);
71
        $instance2 = $plugingenerator->create_instance(['course' => $course->id]);
72
        unset($instance1->cmid);
73
        unset($instance2->cmid);
74
 
75
        $params = [
76
            'type' => CALENDAR_EVENT_TYPE_ACTION,
77
            'courseid' => $course->id,
78
            'modulename' => 'assign',
79
            'userid' => 0,
80
            'eventtype' => 'due',
81
            'repeats' => 0,
82
            'timestart' => 1,
83
        ];
84
 
85
        $legacyevent1 = create_event(array_merge($params, ['name' => 'Event 1', 'instance' => $instance1->id]));
86
        $legacyevent2 = create_event(array_merge($params, ['name' => 'Event 2', 'instance' => $instance1->id]));
87
        $legacyevent3 = create_event(array_merge($params, ['name' => 'Event 3', 'instance' => $instance2->id]));
88
        $event1 = $mapper->from_legacy_event_to_event($legacyevent1);
89
        $event2 = $mapper->from_legacy_event_to_event($legacyevent2);
90
        $event3 = $mapper->from_legacy_event_to_event($legacyevent3);
91
        $cache = new events_related_objects_cache([$event1, $event2, $event3]);
92
 
93
        $eventinstance1 = $cache->get_module_instance($event1);
94
        $eventinstance2 = $cache->get_module_instance($event2);
95
        $eventinstance3 = $cache->get_module_instance($event3);
96
 
97
        $this->assertEquals($instance1, $eventinstance1);
98
        $this->assertEquals($instance1, $eventinstance2);
99
        $this->assertEquals($instance2, $eventinstance3);
100
    }
101
 
102
    /**
103
     * Trying to load the course module of an event that isn't in
104
     * the cache should return null.
105
     */
11 efrain 106
    public function test_module_instance_unknown_event(): void {
1 efrain 107
        $this->setAdminUser();
108
        $mapper = container::get_event_mapper();
109
        $generator = $this->getDataGenerator();
110
        $course = $generator->create_course();
111
        $plugingenerator = $generator->get_plugin_generator('mod_assign');
112
        $instance1 = $plugingenerator->create_instance(['course' => $course->id]);
113
        $instance2 = $plugingenerator->create_instance(['course' => $course->id]);
114
        unset($instance1->cmid);
115
        unset($instance2->cmid);
116
 
117
        $params = [
118
            'type' => CALENDAR_EVENT_TYPE_ACTION,
119
            'courseid' => $course->id,
120
            'modulename' => 'assign',
121
            'userid' => 0,
122
            'eventtype' => 'due',
123
            'repeats' => 0,
124
            'timestart' => 1,
125
        ];
126
 
127
        $legacyevent1 = create_event(array_merge($params, ['name' => 'Event 1', 'instance' => $instance1->id]));
128
        $legacyevent2 = create_event(array_merge($params, ['name' => 'Event 2', 'instance' => $instance2->id]));
129
        $event1 = $mapper->from_legacy_event_to_event($legacyevent1);
130
        $event2 = $mapper->from_legacy_event_to_event($legacyevent2);
131
        $cache = new events_related_objects_cache([$event1]);
132
 
133
        $this->assertNull($cache->get_module_instance($event2));
134
    }
135
}