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
 * Unit tests for events found in /grade/letter and /grade/scale.
19
 *
20
 * @package   core_grades
21
 * @category  test
22
 * @copyright 2017 Stephen Bourget
23
 * @license   http://www.gnu.org/copyleft/gpl.html GNU Public License
24
 */
25
 
26
namespace core_grades\event;
27
 
28
defined('MOODLE_INTERNAL') || die();
29
 
30
global $CFG;
31
require_once($CFG->dirroot . '/grade/lib.php');
32
 
33
/**
34
 * Unit tests for grade events.
35
 *
36
 * @package   core_grades
37
 * @category  test
38
 * @copyright 2017 Stephen Bourget
39
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40
 */
1441 ariadna 41
final class events_test extends \advanced_testcase {
1 efrain 42
 
43
    /** @var stdClass the course used for testing */
44
    private $course;
45
 
46
    /**
47
     * Test set up.
48
     *
49
     * This is executed before running any test in this file.
50
     */
51
    public function setUp(): void {
1441 ariadna 52
        parent::setUp();
1 efrain 53
        $this->resetAfterTest();
54
 
55
        $this->setAdminUser();
56
        $this->course = $this->getDataGenerator()->create_course();
57
    }
58
 
59
    /**
60
     * Test the grade letter created event.
61
     *
62
     * There is no external API for triggering this event, so the unit test will simply
63
     * create and trigger the event and ensure the data is returned as expected.
64
     */
11 efrain 65
    public function test_grade_letter_created(): void {
1 efrain 66
        // Create a grade letter created event.
67
        $event = \core\event\grade_letter_created::create(array(
68
            'objectid' => 10,
69
            'context' => \context_course::instance($this->course->id)
70
        ));
71
 
72
        // Trigger and capture the event.
73
        $sink = $this->redirectEvents();
74
        $event->trigger();
75
        $events = $sink->get_events();
76
        $event = reset($events);
77
 
78
        // Check that the event data is valid.
79
        $this->assertInstanceOf('\core\event\grade_letter_created', $event);
80
        $this->assertEquals(\context_course::instance($this->course->id), $event->get_context());
81
    }
82
 
83
    /**
84
     * Test the grade letter deleted event.
85
     *
86
     * There is no external API for triggering this event, so the unit test will simply
87
     * create and trigger the event and ensure the data is returned as expected.
88
     */
11 efrain 89
    public function test_grade_letter_deleted(): void {
1 efrain 90
        // Create a grade letter deleted event.
91
        $event = \core\event\grade_letter_deleted::create(array(
92
            'objectid' => 10,
93
            'context' => \context_course::instance($this->course->id)
94
        ));
95
 
96
        // Trigger and capture the event.
97
        $sink = $this->redirectEvents();
98
        $event->trigger();
99
        $events = $sink->get_events();
100
        $event = reset($events);
101
 
102
        // Check that the event data is valid.
103
        $this->assertInstanceOf('\core\event\grade_letter_deleted', $event);
104
        $this->assertEquals(\context_course::instance($this->course->id), $event->get_context());
105
    }
106
 
107
    /**
108
     * Test the grade letter updated event.
109
     *
110
     * There is no external API for triggering this event, so the unit test will simply
111
     * create and trigger the event and ensure the data is returned as expected.
112
     */
11 efrain 113
    public function test_grade_letter_updated(): void {
1 efrain 114
        // Create a grade letter updated event.
115
        $event = \core\event\grade_letter_updated::create(array(
116
            'objectid' => 10,
117
            'context' => \context_course::instance($this->course->id)
118
        ));
119
 
120
        // Trigger and capture the event.
121
        $sink = $this->redirectEvents();
122
        $event->trigger();
123
        $events = $sink->get_events();
124
        $event = reset($events);
125
 
126
        // Check that the event data is valid.
127
        $this->assertInstanceOf('\core\event\grade_letter_updated', $event);
128
        $this->assertEquals(\context_course::instance($this->course->id), $event->get_context());
129
    }
130
 
131
    /**
132
     * Test the scale created event.
133
     */
11 efrain 134
    public function test_scale_created(): void {
1 efrain 135
        $gradescale = new \grade_scale();
136
        $gradescale->name        = 'unittestscale3';
137
        $gradescale->courseid    = $this->course->id;
138
        $gradescale->userid      = 317;
139
        $gradescale->scale       = 'Distinction, Very Good, Good, Pass, Fail';
140
        $gradescale->description = 'This scale is used to mark standard assignments.';
141
 
142
        $url = new \moodle_url('/grade/edit/scale/index.php', array('id' => $this->course->id));
143
 
144
        // Trigger and capture the event.
145
        $sink = $this->redirectEvents();
146
        $id = $gradescale->insert();
147
        $events = $sink->get_events();
148
        $event = reset($events);
149
 
150
        // Check that the event data is valid.
151
        $this->assertInstanceOf('\core\event\scale_created', $event);
152
        $this->assertEquals($id, $event->objectid);
153
        $this->assertEquals($url, $event->get_url());
154
        $this->assertEquals(\context_course::instance($this->course->id), $event->get_context());
155
    }
156
 
157
    /**
158
     * Test the scale deleted event.
159
     */
11 efrain 160
    public function test_scale_deleted(): void {
1 efrain 161
        $gradescale = new \grade_scale();
162
        $gradescale->name        = 'unittestscale3';
163
        $gradescale->courseid    = $this->course->id;
164
        $gradescale->userid      = 317;
165
        $gradescale->scale       = 'Distinction, Very Good, Good, Pass, Fail';
166
        $gradescale->description = 'This scale is used to mark standard assignments.';
167
        $gradescale->insert();
168
 
169
        // Trigger and capture the event.
170
        $sink = $this->redirectEvents();
171
        $gradescale->delete();
172
        $events = $sink->get_events();
173
        $event = reset($events);
174
 
175
        // Check that the event data is valid.
176
        $this->assertInstanceOf('\core\event\scale_deleted', $event);
177
        $this->assertEquals(\context_course::instance($this->course->id), $event->get_context());
178
    }
179
 
180
    /**
181
     * Test the scale updated event.
182
     */
11 efrain 183
    public function test_scale_updated(): void {
1 efrain 184
        $gradescale = new \grade_scale();
185
        $gradescale->name        = 'unittestscale3';
186
        $gradescale->courseid    = $this->course->id;
187
        $gradescale->userid      = 317;
188
        $gradescale->scale       = 'Distinction, Very Good, Good, Pass, Fail';
189
        $gradescale->description = 'This scale is used to mark standard assignments.';
190
        $id = $gradescale->insert();
191
 
192
        $gradescale->name = 'Updated info for this unittest grade_scale';
193
        $url = new \moodle_url('/grade/edit/scale/index.php', array('id' => $this->course->id));
194
 
195
        // Trigger and capture the event.
196
        $sink = $this->redirectEvents();
197
        $gradescale->update();
198
        $events = $sink->get_events();
199
        $event = reset($events);
200
 
201
        // Check that the event data is valid.
202
        $this->assertInstanceOf('\core\event\scale_updated', $event);
203
        $this->assertEquals($id, $event->objectid);
204
        $this->assertEquals($url, $event->get_url());
205
        $this->assertEquals(\context_course::instance($this->course->id), $event->get_context());
206
    }
207
}