Proyectos de Subversion Moodle

Rev

Rev 11 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 11 Rev 1441
Línea 11... Línea 11...
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
12
// GNU General Public License for more details.
13
//
13
//
14
// You should have received a copy of the GNU General Public License
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/>.
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
/**
-
 
17
 * Badge events tests.
-
 
18
 *
16
 
19
 * @package    core_badges
-
 
20
 * @copyright  2015 onwards Simey Lameze <simey@moodle.com>
-
 
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
22
 */
-
 
23
defined('MOODLE_INTERNAL') || die();
-
 
24
global $CFG;
-
 
25
require_once($CFG->dirroot . '/badges/tests/badgeslib_test.php');
17
use core_badges\tests\badges_testcase;
Línea 26... Línea 18...
26
 
18
 
27
/**
19
/**
28
 * Badge events tests class.
20
 * Badge events tests class.
29
 *
21
 *
30
 * @package    core_badges
22
 * @package    core_badges
31
 * @copyright  2015 onwards Simey Lameze <simey@moodle.com>
23
 * @copyright  2015 onwards Simey Lameze <simey@moodle.com>
32
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
25
 */
34
class events_test extends badgeslib_test {
-
 
35
 
26
final class events_test extends badges_testcase {
36
    /**
27
    /**
37
     * Test badge awarded event.
28
     * Test badge awarded event.
38
     */
29
     */
39
    public function test_badge_awarded(): void {
-
 
40
 
30
    public function test_badge_awarded(): void {
Línea 41... Línea 31...
41
        $systemcontext = context_system::instance();
31
        $systemcontext = context_system::instance();
Línea 42... Línea 32...
42
 
32
 
Línea 61... Línea 51...
61
     *
51
     *
62
     * There is no external API for creating a badge, so the unit test will simply
52
     * There is no external API for creating a badge, so the unit test will simply
63
     * create and trigger the event and ensure data is returned as expected.
53
     * create and trigger the event and ensure data is returned as expected.
64
     */
54
     */
65
    public function test_badge_created(): void {
55
    public function test_badge_created(): void {
66
 
-
 
67
        $badge = new badge($this->badgeid);
56
        $badge = new badge($this->badgeid);
68
        // Trigger an event: badge created.
57
        // Trigger an event: badge created.
69
        $eventparams = array(
58
        $eventparams = array(
70
            'userid' => $badge->usercreated,
59
            'userid' => $badge->usercreated,
71
            'objectid' => $badge->id,
60
            'objectid' => $badge->id,
Línea 83... Línea 72...
83
        $this->assertInstanceOf('\core\event\badge_created', $event);
72
        $this->assertInstanceOf('\core\event\badge_created', $event);
84
        $this->assertEquals($badge->usercreated, $event->userid);
73
        $this->assertEquals($badge->usercreated, $event->userid);
85
        $this->assertEquals($badge->id, $event->objectid);
74
        $this->assertEquals($badge->id, $event->objectid);
86
        $this->assertDebuggingNotCalled();
75
        $this->assertDebuggingNotCalled();
87
        $sink->close();
76
        $sink->close();
88
 
-
 
89
    }
77
    }
Línea 90... Línea 78...
90
 
78
 
91
    /**
79
    /**
92
     * Test the badge archived event.
80
     * Test the badge archived event.
Línea 105... Línea 93...
105
        // Check that the event data is valid.
93
        // Check that the event data is valid.
106
        $this->assertInstanceOf('\core\event\badge_archived', $event);
94
        $this->assertInstanceOf('\core\event\badge_archived', $event);
107
        $this->assertEquals($badge->id, $event->objectid);
95
        $this->assertEquals($badge->id, $event->objectid);
108
        $this->assertDebuggingNotCalled();
96
        $this->assertDebuggingNotCalled();
109
        $sink->close();
97
        $sink->close();
110
 
-
 
111
    }
98
    }
Línea 112... Línea -...
112
 
-
 
113
 
99
 
114
    /**
100
    /**
115
     * Test the badge updated event.
101
     * Test the badge updated event.
116
     *
102
     *
117
     */
103
     */
Línea 128... Línea 114...
128
        // Check that the event data is valid.
114
        // Check that the event data is valid.
129
        $this->assertInstanceOf('\core\event\badge_updated', $event);
115
        $this->assertInstanceOf('\core\event\badge_updated', $event);
130
        $this->assertEquals($badge->id, $event->objectid);
116
        $this->assertEquals($badge->id, $event->objectid);
131
        $this->assertDebuggingNotCalled();
117
        $this->assertDebuggingNotCalled();
132
        $sink->close();
118
        $sink->close();
133
 
-
 
134
    }
119
    }
-
 
120
 
135
    /**
121
    /**
136
     * Test the badge deleted event.
122
     * Test the badge deleted event.
137
     */
123
     */
138
    public function test_badge_deleted(): void {
124
    public function test_badge_deleted(): void {
139
        $badge = new badge($this->badgeid);
125
        $badge = new badge($this->badgeid);
Línea 148... Línea 134...
148
        // Check that the event data is valid.
134
        // Check that the event data is valid.
149
        $this->assertInstanceOf('\core\event\badge_deleted', $event);
135
        $this->assertInstanceOf('\core\event\badge_deleted', $event);
150
        $this->assertEquals($badge->id, $event->objectid);
136
        $this->assertEquals($badge->id, $event->objectid);
151
        $this->assertDebuggingNotCalled();
137
        $this->assertDebuggingNotCalled();
152
        $sink->close();
138
        $sink->close();
153
 
-
 
154
    }
139
    }
Línea 155... Línea 140...
155
 
140
 
156
    /**
141
    /**
157
     * Test the badge duplicated event.
142
     * Test the badge duplicated event.
Línea 170... Línea 155...
170
        // Check that the event data is valid.
155
        // Check that the event data is valid.
171
        $this->assertInstanceOf('\core\event\badge_duplicated', $event);
156
        $this->assertInstanceOf('\core\event\badge_duplicated', $event);
172
        $this->assertEquals($newid, $event->objectid);
157
        $this->assertEquals($newid, $event->objectid);
173
        $this->assertDebuggingNotCalled();
158
        $this->assertDebuggingNotCalled();
174
        $sink->close();
159
        $sink->close();
175
 
-
 
176
    }
160
    }
Línea 177... Línea 161...
177
 
161
 
178
    /**
162
    /**
179
     * Test the badge disabled event.
163
     * Test the badge disabled event.
Línea 193... Línea 177...
193
        // Check that the event data is valid.
177
        // Check that the event data is valid.
194
        $this->assertInstanceOf('\core\event\badge_disabled', $event);
178
        $this->assertInstanceOf('\core\event\badge_disabled', $event);
195
        $this->assertEquals($badge->id, $event->objectid);
179
        $this->assertEquals($badge->id, $event->objectid);
196
        $this->assertDebuggingNotCalled();
180
        $this->assertDebuggingNotCalled();
197
        $sink->close();
181
        $sink->close();
198
 
-
 
199
    }
182
    }
Línea 200... Línea 183...
200
 
183
 
201
    /**
184
    /**
202
     * Test the badge enabled event.
185
     * Test the badge enabled event.
Línea 216... Línea 199...
216
        // Check that the event data is valid.
199
        // Check that the event data is valid.
217
        $this->assertInstanceOf('\core\event\badge_enabled', $event);
200
        $this->assertInstanceOf('\core\event\badge_enabled', $event);
218
        $this->assertEquals($badge->id, $event->objectid);
201
        $this->assertEquals($badge->id, $event->objectid);
219
        $this->assertDebuggingNotCalled();
202
        $this->assertDebuggingNotCalled();
220
        $sink->close();
203
        $sink->close();
221
 
-
 
222
    }
204
    }
Línea 223... Línea 205...
223
 
205
 
224
    /**
206
    /**
225
     * Test the badge criteria created event.
207
     * Test the badge criteria created event.
226
     *
208
     *
227
     * There is no external API for this, so the unit test will simply
209
     * There is no external API for this, so the unit test will simply
228
     * create and trigger the event and ensure data is returned as expected.
210
     * create and trigger the event and ensure data is returned as expected.
229
     */
211
     */
230
    public function test_badge_criteria_created(): void {
-
 
231
 
212
    public function test_badge_criteria_created(): void {
Línea 232... Línea 213...
232
        $badge = new badge($this->badgeid);
213
        $badge = new badge($this->badgeid);
233
 
214
 
234
        // Trigger and capture the event.
215
        // Trigger and capture the event.
Línea 246... Línea 227...
246
        $this->assertInstanceOf('\core\event\badge_criteria_created', $event);
227
        $this->assertInstanceOf('\core\event\badge_criteria_created', $event);
247
        $this->assertEquals($criteriaprofile->id, $event->objectid);
228
        $this->assertEquals($criteriaprofile->id, $event->objectid);
248
        $this->assertEquals($criteriaprofile->badgeid, $event->other['badgeid']);
229
        $this->assertEquals($criteriaprofile->badgeid, $event->other['badgeid']);
249
        $this->assertDebuggingNotCalled();
230
        $this->assertDebuggingNotCalled();
250
        $sink->close();
231
        $sink->close();
251
 
-
 
252
    }
232
    }
Línea 253... Línea 233...
253
 
233
 
254
    /**
234
    /**
255
     * Test the badge criteria updated event.
235
     * Test the badge criteria updated event.
256
     *
236
     *
257
     * There is no external API for this, so the unit test will simply
237
     * There is no external API for this, so the unit test will simply
258
     * create and trigger the event and ensure data is returned as expected.
238
     * create and trigger the event and ensure data is returned as expected.
259
     */
239
     */
260
    public function test_badge_criteria_updated(): void {
-
 
261
 
240
    public function test_badge_criteria_updated(): void {
262
        $criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $this->badgeid));
241
        $criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $this->badgeid));
263
        $criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
242
        $criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
264
        $criteriaprofile = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $this->badgeid));
243
        $criteriaprofile = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $this->badgeid));
265
        $params = array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address');
244
        $params = array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address');
Línea 279... Línea 258...
279
        $this->assertInstanceOf('\core\event\badge_criteria_updated', $event);
258
        $this->assertInstanceOf('\core\event\badge_criteria_updated', $event);
280
        $this->assertEquals($criteria->id, $event->objectid);
259
        $this->assertEquals($criteria->id, $event->objectid);
281
        $this->assertEquals($this->badgeid, $event->other['badgeid']);
260
        $this->assertEquals($this->badgeid, $event->other['badgeid']);
282
        $this->assertDebuggingNotCalled();
261
        $this->assertDebuggingNotCalled();
283
        $sink->close();
262
        $sink->close();
284
 
-
 
285
    }
263
    }
Línea 286... Línea 264...
286
 
264
 
287
    /**
265
    /**
288
     * Test the badge criteria deleted event.
266
     * Test the badge criteria deleted event.
289
     *
267
     *
290
     * There is no external API for this, so the unit test will simply
268
     * There is no external API for this, so the unit test will simply
291
     * create and trigger the event and ensure data is returned as expected.
269
     * create and trigger the event and ensure data is returned as expected.
292
     */
270
     */
293
    public function test_badge_criteria_deleted(): void {
-
 
294
 
271
    public function test_badge_criteria_deleted(): void {
295
        $criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $this->badgeid));
272
        $criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $this->badgeid));
296
        $criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
273
        $criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
Línea 297... Línea 274...
297
        $badge = new badge($this->badgeid);
274
        $badge = new badge($this->badgeid);
Línea 306... Línea 283...
306
        $this->assertCount(1, $events);
283
        $this->assertCount(1, $events);
307
        $this->assertInstanceOf('\core\event\badge_criteria_deleted', $event);
284
        $this->assertInstanceOf('\core\event\badge_criteria_deleted', $event);
308
        $this->assertEquals($criteriaoverall->badgeid, $event->other['badgeid']);
285
        $this->assertEquals($criteriaoverall->badgeid, $event->other['badgeid']);
309
        $this->assertDebuggingNotCalled();
286
        $this->assertDebuggingNotCalled();
310
        $sink->close();
287
        $sink->close();
311
 
-
 
312
    }
288
    }
Línea 313... Línea 289...
313
 
289
 
314
    /**
290
    /**
315
     * Test the badge viewed event.
291
     * Test the badge viewed event.
316
     *
292
     *
317
     * There is no external API for viewing a badge, so the unit test will simply
293
     * There is no external API for viewing a badge, so the unit test will simply
318
     * create and trigger the event and ensure data is returned as expected.
294
     * create and trigger the event and ensure data is returned as expected.
319
     */
295
     */
320
    public function test_badge_viewed(): void {
-
 
321
 
296
    public function test_badge_viewed(): void {
322
        $badge = new badge($this->badgeid);
297
        $badge = new badge($this->badgeid);
323
        // Trigger an event: badge viewed.
298
        // Trigger an event: badge viewed.
324
        $other = array('badgeid' => $badge->id, 'badgehash' => '12345678');
299
        $other = array('badgeid' => $badge->id, 'badgehash' => '12345678');
325
        $eventparams = array(
300
        $eventparams = array(
Línea 338... Línea 313...
338
        $this->assertInstanceOf('\core\event\badge_viewed', $event);
313
        $this->assertInstanceOf('\core\event\badge_viewed', $event);
339
        $this->assertEquals('12345678', $event->other['badgehash']);
314
        $this->assertEquals('12345678', $event->other['badgehash']);
340
        $this->assertEquals($badge->id, $event->other['badgeid']);
315
        $this->assertEquals($badge->id, $event->other['badgeid']);
341
        $this->assertDebuggingNotCalled();
316
        $this->assertDebuggingNotCalled();
342
        $sink->close();
317
        $sink->close();
343
 
-
 
344
    }
318
    }
Línea 345... Línea 319...
345
 
319
 
346
    /**
320
    /**
347
     * Test the badge listing viewed event.
321
     * Test the badge listing viewed event.
348
     *
322
     *
349
     * There is no external API for viewing a badge, so the unit test will simply
323
     * There is no external API for viewing a badge, so the unit test will simply
350
     * create and trigger the event and ensure data is returned as expected.
324
     * create and trigger the event and ensure data is returned as expected.
351
     */
325
     */
352
    public function test_badge_listing_viewed(): void {
-
 
353
 
326
    public function test_badge_listing_viewed(): void {
354
        // Trigger an event: badge listing viewed.
327
        // Trigger an event: badge listing viewed.
355
        $context = context_system::instance();
328
        $context = context_system::instance();
356
        $eventparams = array(
329
        $eventparams = array(
357
            'context' => $context,
330
            'context' => $context,
Línea 368... Línea 341...
368
        // Check that the event data is valid.
341
        // Check that the event data is valid.
369
        $this->assertInstanceOf('\core\event\badge_listing_viewed', $event);
342
        $this->assertInstanceOf('\core\event\badge_listing_viewed', $event);
370
        $this->assertEquals(BADGE_TYPE_SITE, $event->other['badgetype']);
343
        $this->assertEquals(BADGE_TYPE_SITE, $event->other['badgetype']);
371
        $this->assertDebuggingNotCalled();
344
        $this->assertDebuggingNotCalled();
372
        $sink->close();
345
        $sink->close();
373
 
-
 
374
    }
346
    }
375
}
347
}