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\event;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Unit tests for the context_locked and context_unlocked events.
|
|
|
21 |
*
|
|
|
22 |
* @package core
|
|
|
23 |
* @category test
|
|
|
24 |
* @copyright 2019 University of Nottingham
|
|
|
25 |
* @author Neill Magill <neill.magill@nottingham.ac.uk>
|
|
|
26 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
27 |
*/
|
|
|
28 |
class context_locked_test extends \advanced_testcase {
|
|
|
29 |
/**
|
|
|
30 |
* Locks an unlocked context and checks that a core\event\context_locked event is created.
|
|
|
31 |
*
|
|
|
32 |
* @param \context $context
|
|
|
33 |
*/
|
|
|
34 |
protected function lock_context(\context $context) {
|
|
|
35 |
self::assertFalse($context->is_locked());
|
|
|
36 |
|
|
|
37 |
$locksink = $this->redirectEvents();
|
|
|
38 |
$context->set_locked(true);
|
|
|
39 |
// This second call should not create an event as the lock status has not changed.
|
|
|
40 |
$context->set_locked(true);
|
|
|
41 |
$lockevents = $locksink->get_events();
|
|
|
42 |
$locksink->close();
|
|
|
43 |
|
|
|
44 |
self::assertCount(1, $lockevents);
|
|
|
45 |
self::assertContainsOnlyInstancesOf(context_locked::class, $lockevents);
|
|
|
46 |
self::assertEquals($context->id, $lockevents[0]->objectid);
|
|
|
47 |
$this->assertSame('context', $lockevents[0]->objecttable);
|
|
|
48 |
$this->assertEquals($context, $lockevents[0]->get_context());
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Tests that events are created when contexts are locked and unlocked.
|
|
|
53 |
*/
|
|
|
54 |
public function test_creation() {
|
|
|
55 |
$this->resetAfterTest();
|
|
|
56 |
|
|
|
57 |
$category = self::getDataGenerator()->create_category();
|
|
|
58 |
$catcontext = \context_coursecat::instance($category->id);
|
|
|
59 |
$course = self::getDataGenerator()->create_course(['category' => $category->id]);
|
|
|
60 |
$coursecontext = \context_course::instance($course->id);
|
|
|
61 |
/** @var \mod_forum_generator $activitygenerator */
|
|
|
62 |
$activitygenerator = self::getDataGenerator()->get_plugin_generator('mod_forum');
|
|
|
63 |
$activity = $activitygenerator->create_instance(['course' => $course->id]);
|
|
|
64 |
$activitycontext = \context_module::instance($activity->cmid);
|
|
|
65 |
|
|
|
66 |
$this->lock_context($catcontext);
|
|
|
67 |
$this->unlock_context($catcontext);
|
|
|
68 |
|
|
|
69 |
$this->lock_context($coursecontext);
|
|
|
70 |
$this->unlock_context($coursecontext);
|
|
|
71 |
|
|
|
72 |
$this->lock_context($activitycontext);
|
|
|
73 |
$this->unlock_context($activitycontext);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Unlocks a locked context and checks that a core\event\context_unlocked event is created.
|
|
|
78 |
*
|
|
|
79 |
* @param \context $context
|
|
|
80 |
*/
|
|
|
81 |
protected function unlock_context(\context $context) {
|
|
|
82 |
self::assertTrue($context->is_locked());
|
|
|
83 |
|
|
|
84 |
$unlocksink = $this->redirectEvents();
|
|
|
85 |
$context->set_locked(false);
|
|
|
86 |
// This second call should not create an event as the lock status has not changed.
|
|
|
87 |
$context->set_locked(false);
|
|
|
88 |
$unlockevents = $unlocksink->get_events();
|
|
|
89 |
$unlocksink->close();
|
|
|
90 |
|
|
|
91 |
self::assertCount(1, $unlockevents);
|
|
|
92 |
self::assertContainsOnlyInstancesOf(context_unlocked::class, $unlockevents);
|
|
|
93 |
self::assertEquals($context->id, $unlockevents[0]->objectid);
|
|
|
94 |
$this->assertSame('context', $unlockevents[0]->objecttable);
|
|
|
95 |
$this->assertEquals($context, $unlockevents[0]->get_context());
|
|
|
96 |
}
|
|
|
97 |
}
|