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
/**
18
 * Events tests.
19
 *
20
 * @package    mod_book
21
 * @category   phpunit
22
 * @copyright  2013 Frédéric Massart
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
namespace mod_book\event;
27
 
28
/**
29
 * Events tests class.
30
 *
31
 * @package    mod_book
32
 * @category   phpunit
33
 * @copyright  2013 Frédéric Massart
34
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
1441 ariadna 36
final class events_test extends \advanced_testcase {
1 efrain 37
 
38
    public function setUp(): void {
1441 ariadna 39
        parent::setUp();
1 efrain 40
        $this->resetAfterTest();
41
    }
42
 
11 efrain 43
    public function test_chapter_created(): void {
1 efrain 44
        // There is no proper API to call to generate chapters for a book, so what we are
45
        // doing here is simply making sure that the events returns the right information.
46
 
47
        $course = $this->getDataGenerator()->create_course();
48
        $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
49
        $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
50
        $context = \context_module::instance($book->cmid);
51
 
52
        $chapter = $bookgenerator->create_chapter(array('bookid' => $book->id));
53
 
54
        $event = \mod_book\event\chapter_created::create_from_chapter($book, $context, $chapter);
55
 
56
        // Triggering and capturing the event.
57
        $sink = $this->redirectEvents();
58
        $event->trigger();
59
        $events = $sink->get_events();
60
        $this->assertCount(1, $events);
61
        $event = reset($events);
62
 
63
        // Checking that the event contains the expected values.
64
        $this->assertInstanceOf('\mod_book\event\chapter_created', $event);
65
        $this->assertEquals(\context_module::instance($book->cmid), $event->get_context());
66
        $this->assertEquals($chapter->id, $event->objectid);
67
    }
68
 
11 efrain 69
    public function test_chapter_updated(): void {
1 efrain 70
        // There is no proper API to call to generate chapters for a book, so what we are
71
        // doing here is simply making sure that the events returns the right information.
72
 
73
        $course = $this->getDataGenerator()->create_course();
74
        $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
75
        $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
76
        $context = \context_module::instance($book->cmid);
77
 
78
        $chapter = $bookgenerator->create_chapter(array('bookid' => $book->id));
79
 
80
        $event = \mod_book\event\chapter_updated::create_from_chapter($book, $context, $chapter);
81
 
82
        // Triggering and capturing the event.
83
        $sink = $this->redirectEvents();
84
        $event->trigger();
85
        $events = $sink->get_events();
86
        $this->assertCount(1, $events);
87
        $event = reset($events);
88
 
89
        // Checking that the event contains the expected values.
90
        $this->assertInstanceOf('\mod_book\event\chapter_updated', $event);
91
        $this->assertEquals(\context_module::instance($book->cmid), $event->get_context());
92
        $this->assertEquals($chapter->id, $event->objectid);
93
    }
94
 
11 efrain 95
    public function test_chapter_deleted(): void {
1 efrain 96
        // There is no proper API to call to delete chapters for a book, so what we are
97
        // doing here is simply making sure that the events returns the right information.
98
 
99
        $course = $this->getDataGenerator()->create_course();
100
        $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
101
        $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
102
        $context = \context_module::instance($book->cmid);
103
 
104
        $chapter = $bookgenerator->create_chapter(array('bookid' => $book->id));
105
 
106
        $event = \mod_book\event\chapter_deleted::create_from_chapter($book, $context, $chapter);
107
        $legacy = array($course->id, 'book', 'update', 'view.php?id='.$book->cmid, $book->id, $book->cmid);
108
 
109
        // Triggering and capturing the event.
110
        $sink = $this->redirectEvents();
111
        $event->trigger();
112
        $events = $sink->get_events();
113
        $this->assertCount(1, $events);
114
        $event = reset($events);
115
 
116
        // Checking that the event contains the expected values.
117
        $this->assertInstanceOf('\mod_book\event\chapter_deleted', $event);
118
        $this->assertEquals(\context_module::instance($book->cmid), $event->get_context());
119
        $this->assertEquals($chapter->id, $event->objectid);
120
        $this->assertEquals($chapter, $event->get_record_snapshot('book_chapters', $chapter->id));
121
    }
122
 
11 efrain 123
    public function test_course_module_instance_list_viewed(): void {
1 efrain 124
        // There is no proper API to call to trigger this event, so what we are
125
        // doing here is simply making sure that the events returns the right information.
126
 
127
        $course = $this->getDataGenerator()->create_course();
128
        $params = array(
129
            'context' => \context_course::instance($course->id)
130
        );
131
        $event = \mod_book\event\course_module_instance_list_viewed::create($params);
132
 
133
        // Triggering and capturing the event.
134
        $sink = $this->redirectEvents();
135
        $event->trigger();
136
        $events = $sink->get_events();
137
        $this->assertCount(1, $events);
138
        $event = reset($events);
139
 
140
        // Checking that the event contains the expected values.
141
        $this->assertInstanceOf('\mod_book\event\course_module_instance_list_viewed', $event);
142
        $this->assertEquals(\context_course::instance($course->id), $event->get_context());
143
    }
144
 
11 efrain 145
    public function test_course_module_viewed(): void {
1 efrain 146
        // There is no proper API to call to trigger this event, so what we are
147
        // doing here is simply making sure that the events returns the right information.
148
 
149
        $course = $this->getDataGenerator()->create_course();
150
        $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
151
 
152
        $params = array(
153
            'context' => \context_module::instance($book->cmid),
154
            'objectid' => $book->id
155
        );
156
        $event = \mod_book\event\course_module_viewed::create($params);
157
 
158
        // Triggering and capturing the event.
159
        $sink = $this->redirectEvents();
160
        $event->trigger();
161
        $events = $sink->get_events();
162
        $this->assertCount(1, $events);
163
        $event = reset($events);
164
 
165
        // Checking that the event contains the expected values.
166
        $this->assertInstanceOf('\mod_book\event\course_module_viewed', $event);
167
        $this->assertEquals(\context_module::instance($book->cmid), $event->get_context());
168
        $this->assertEquals($book->id, $event->objectid);
169
    }
170
 
11 efrain 171
    public function test_chapter_viewed(): void {
1 efrain 172
        // There is no proper API to call to trigger this event, so what we are
173
        // doing here is simply making sure that the events returns the right information.
174
 
175
        $course = $this->getDataGenerator()->create_course();
176
        $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
177
        $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
178
        $context = \context_module::instance($book->cmid);
179
 
180
        $chapter = $bookgenerator->create_chapter(array('bookid' => $book->id));
181
 
182
        $event = \mod_book\event\chapter_viewed::create_from_chapter($book, $context, $chapter);
183
 
184
        // Triggering and capturing the event.
185
        $sink = $this->redirectEvents();
186
        $event->trigger();
187
        $events = $sink->get_events();
188
        $this->assertCount(1, $events);
189
        $event = reset($events);
190
 
191
        // Checking that the event contains the expected values.
192
        $this->assertInstanceOf('\mod_book\event\chapter_viewed', $event);
193
        $this->assertEquals(\context_module::instance($book->cmid), $event->get_context());
194
        $this->assertEquals($chapter->id, $event->objectid);
195
    }
196
 
197
}