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_my\event;
18
 
19
use context_system;
20
use context_user;
21
 
22
/**
23
 * Unit tests for the dashboard events.
24
 *
25
 * @package    core
26
 * @category   test
27
 * @copyright  2016 Stephen Bourget
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
1441 ariadna 30
final class events_test extends \advanced_testcase {
1 efrain 31
 
32
    /** @var user cobject */
33
    protected $user;
34
 
35
    /**
36
     * Setup often used objects for the following tests.
37
     */
38
    protected function setUp(): void {
39
        global $USER;
1441 ariadna 40
        parent::setUp();
1 efrain 41
 
42
        $this->resetAfterTest();
43
 
44
        // The user we are going to test this on.
45
        $this->setAdminUser();
46
        $this->user = $USER;
47
    }
48
 
49
    /**
50
     * Test the dashboard viewed event.
51
     *
52
     * There is no external API for viewing the dashboard, so the unit test will simply
53
     * create and trigger the event and ensure data is returned as expected.
54
     */
11 efrain 55
    public function test_dashboard_viewed(): void {
1 efrain 56
 
57
        $user = $this->user;
58
        // Trigger an event: dashboard viewed.
59
        $eventparams = array(
60
            'context' => $context = \context_user::instance($user->id)
61
        );
62
 
63
        $event = \core\event\dashboard_viewed::create($eventparams);
64
        // Trigger and capture the event.
65
        $sink = $this->redirectEvents();
66
        $event->trigger();
67
        $events = $sink->get_events();
68
        $event = reset($events);
69
 
70
        // Check that the event data is valid.
71
        $this->assertInstanceOf('\core\event\dashboard_viewed', $event);
72
        $this->assertEquals($user->id, $event->userid);
73
        $this->assertDebuggingNotCalled();
74
    }
75
 
76
    /**
77
     * Test the dashboard reset event.
78
     *
79
     * We will reset the user dashboard to
80
     * trigger the event and ensure data is returned as expected.
81
     *
82
     * @covers ::my_reset_page
83
     */
11 efrain 84
    public function test_dashboard_reset(): void {
1 efrain 85
        global $CFG, $DB;
86
        require_once($CFG->dirroot . '/my/lib.php');
87
 
88
        $user = $this->user;
89
        $usercontext = context_user::instance($this->user->id);
90
 
91
        // Create at least one dashboard.
92
        my_copy_page($this->user->id);
93
        $this->assertNotEmpty($DB->get_records('my_pages', ['userid' => $this->user->id, 'private' => MY_PAGE_PRIVATE,
94
            'name' => MY_PAGE_DEFAULT]));
95
        $this->assertNotEmpty($DB->get_records('block_instances', ['parentcontextid' => $usercontext->id]));
96
 
97
        // Reset the dashboard.
98
        $sink = $this->redirectEvents();
99
        my_reset_page($user->id);
100
 
101
        // Assert that the page and all th blocks were deleted.
102
        $this->assertEmpty($DB->get_records('my_pages', ['userid' => $this->user->id, 'private' => MY_PAGE_PRIVATE,
103
            'name' => MY_PAGE_DEFAULT]));
104
        $this->assertEmpty($DB->get_records('block_instances', ['parentcontextid' => $usercontext->id]));
105
 
106
        // Trigger and capture the event.
107
        $events = $sink->get_events();
108
        $event = reset($events);
109
        $sink->close();
110
 
111
        // Check that the event data is valid.
112
        $this->assertInstanceOf('\core\event\dashboard_reset', $event);
113
        $this->assertEquals($user->id, $event->userid);
114
        $this->assertEquals(MY_PAGE_PRIVATE, $event->other['private']);
115
        $this->assertEquals('my-index', $event->other['pagetype']);
116
        $this->assertDebuggingNotCalled();
117
 
118
        // Reset the dashboard with private parameter is set to MY_PAGE_PUBLIC and pagetype set to 'user-profile'.
119
        $systempage = $DB->get_record('my_pages', ['userid' => null, 'name' => MY_PAGE_DEFAULT, 'private' => MY_PAGE_PUBLIC]);
120
        $this->getDataGenerator()->create_block('online_users', [
121
            'parentcontextid' => context_system::instance()->id,
122
            'pagetypepattern' => 'user-profile',
123
            'subpagepattern' => $systempage->id,
124
        ]);
125
 
126
        my_copy_page($this->user->id, MY_PAGE_PUBLIC, 'user-profile');
127
        $this->assertNotEmpty($DB->get_records('my_pages', ['userid' => $this->user->id, 'private' => MY_PAGE_PUBLIC,
128
            'name' => MY_PAGE_DEFAULT]));
129
        $this->assertNotEmpty($DB->get_records('block_instances', ['parentcontextid' => $usercontext->id]));
130
 
131
        $sink = $this->redirectEvents();
132
        my_reset_page($user->id, MY_PAGE_PUBLIC, 'user-profile');
133
        $this->assertEmpty($DB->get_records('my_pages', ['userid' => $this->user->id, 'private' => MY_PAGE_PUBLIC,
134
            'name' => MY_PAGE_DEFAULT]));
135
        $this->assertEmpty($DB->get_records('block_instances', ['parentcontextid' => $usercontext->id]));
136
 
137
        // Trigger and capture the event.
138
        $events = $sink->get_events();
139
        $event = reset($events);
140
        $sink->close();
141
 
142
        $this->assertEquals(MY_PAGE_PUBLIC, $event->other['private']);
143
        $this->assertEquals('user-profile', $event->other['pagetype']);
144
    }
145
 
146
    /**
147
     * Test the dashboards reset event.
148
     *
149
     * We will reset all user dashboards to
150
     * trigger the event and ensure data is returned as expected.
151
     *
152
     * @covers ::my_reset_page_for_all_users
153
     */
11 efrain 154
    public function test_dashboards_reset(): void {
1 efrain 155
        global $CFG, $USER, $DB;
156
        require_once($CFG->dirroot . '/my/lib.php');
157
 
158
        $usercontext = context_user::instance($this->user->id);
159
 
160
        // Create at least one dashboard.
161
        my_copy_page($this->user->id);
162
        $this->assertNotEmpty($DB->get_records('my_pages', ['userid' => $this->user->id, 'private' => MY_PAGE_PRIVATE,
163
            'name' => MY_PAGE_DEFAULT]));
164
        $this->assertNotEmpty($DB->get_records('block_instances', ['parentcontextid' => $usercontext->id]));
165
 
166
        // Reset all dashbaords.
167
        $sink = $this->redirectEvents();
168
        my_reset_page_for_all_users();
169
 
170
        // Assert that the page and all th blocks were deleted.
171
        $this->assertEmpty($DB->get_records('my_pages', ['userid' => $this->user->id, 'private' => MY_PAGE_PRIVATE,
172
            'name' => MY_PAGE_DEFAULT]));
173
        $this->assertEmpty($DB->get_records('block_instances', ['parentcontextid' => $usercontext->id]));
174
 
175
        // Trigger and capture the event.
176
        $events = $sink->get_events();
177
        $event = reset($events);
178
        $sink->close();
179
 
180
        // Check that the event data is valid.
181
        $this->assertInstanceOf('\core\event\dashboards_reset', $event);
182
        $this->assertEquals($USER->id, $event->userid);
183
        $this->assertEquals(MY_PAGE_PRIVATE, $event->other['private']);
184
        $this->assertEquals('my-index', $event->other['pagetype']);
185
        $this->assertDebuggingNotCalled();
186
 
187
        // Reset the dashboards with private parameter is set to MY_PAGE_PUBLIC and pagetype set to 'user-profile'.
188
        $systempage = $DB->get_record('my_pages', ['userid' => null, 'name' => MY_PAGE_DEFAULT, 'private' => MY_PAGE_PUBLIC]);
189
        $this->getDataGenerator()->create_block('online_users', [
190
            'parentcontextid' => context_system::instance()->id,
191
            'pagetypepattern' => 'user-profile',
192
            'subpagepattern' => $systempage->id,
193
        ]);
194
 
195
        my_copy_page($this->user->id, MY_PAGE_PUBLIC, 'user-profile');
196
        $this->assertNotEmpty($DB->get_records('my_pages', ['userid' => $this->user->id, 'private' => MY_PAGE_PUBLIC,
197
            'name' => MY_PAGE_DEFAULT]));
198
        $this->assertNotEmpty($DB->get_records('block_instances', ['parentcontextid' => $usercontext->id]));
199
 
200
        $sink = $this->redirectEvents();
201
        my_reset_page_for_all_users(MY_PAGE_PUBLIC, 'user-profile');
202
        $this->assertEmpty($DB->get_records('my_pages', ['userid' => $this->user->id, 'private' => MY_PAGE_PUBLIC,
203
            'name' => MY_PAGE_DEFAULT]));
204
        $this->assertEmpty($DB->get_records('block_instances', ['parentcontextid' => $usercontext->id]));
205
 
206
        // Trigger and capture the event.
207
        $events = $sink->get_events();
208
        $event = reset($events);
209
        $sink->close();
210
 
211
        $this->assertEquals(MY_PAGE_PUBLIC, $event->other['private']);
212
        $this->assertEquals('user-profile', $event->other['pagetype']);
213
    }
214
}