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 test for filtering.
|
|
|
19 |
*
|
|
|
20 |
* @package block_dash
|
|
|
21 |
* @copyright 2022 bdecent gmbh <https://bdecent.de>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace block_dash;
|
|
|
26 |
|
|
|
27 |
use core_message\tests\helper as testhelper;
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Unit test for widgets.
|
|
|
31 |
*
|
|
|
32 |
* @group block_dash
|
|
|
33 |
* @group bdecent
|
|
|
34 |
* @group widgets_test
|
|
|
35 |
* @runInSeparateProcess
|
|
|
36 |
* @runTestsInSeparateProcesses
|
|
|
37 |
*/
|
|
|
38 |
class widgets_test extends \advanced_testcase {
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Demo of test user.
|
|
|
42 |
*
|
|
|
43 |
* @var array
|
|
|
44 |
*/
|
|
|
45 |
protected $user;
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* Demo Course 1
|
|
|
49 |
*
|
|
|
50 |
* @var array
|
|
|
51 |
*/
|
|
|
52 |
protected $course1;
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* Demo Course 2
|
|
|
56 |
*
|
|
|
57 |
* @var array
|
|
|
58 |
*/
|
|
|
59 |
protected $course2;
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* Demo Course 3
|
|
|
63 |
*
|
|
|
64 |
* @var array
|
|
|
65 |
*/
|
|
|
66 |
protected $course3;
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* List of test users.
|
|
|
70 |
*
|
|
|
71 |
* @var array
|
|
|
72 |
*/
|
|
|
73 |
protected $users;
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* This method is called before each test.
|
|
|
77 |
*/
|
|
|
78 |
protected function setUp(): void {
|
|
|
79 |
$this->resetAfterTest();
|
|
|
80 |
$this->setAdminUser();
|
|
|
81 |
global $USER;
|
|
|
82 |
$this->user = $USER;
|
|
|
83 |
$this->course1 = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
|
|
|
84 |
$this->course2 = $this->getDataGenerator()->create_course();
|
|
|
85 |
$this->course3 = $this->getDataGenerator()->create_course();
|
|
|
86 |
foreach (range(1, 5) as $user) {
|
|
|
87 |
$this->users[$user] = self::getDataGenerator()->create_user();
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
/**
|
|
|
92 |
* Constructs a Page object for the User Dashboard.
|
|
|
93 |
*
|
|
|
94 |
* @param \stdClass $user User to create Dashboard for.
|
|
|
95 |
* @return \moodle_page
|
|
|
96 |
*/
|
|
|
97 |
protected function construct_user_page(\stdClass $user) {
|
|
|
98 |
$page = new \moodle_page();
|
|
|
99 |
$page->set_context(\context_user::instance($user->id));
|
|
|
100 |
$page->set_pagelayout('mydashboard');
|
|
|
101 |
$page->set_pagetype('my-index');
|
|
|
102 |
$page->blocks->load_blocks();
|
|
|
103 |
return $page;
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* Creates an HTML block on a user.
|
|
|
108 |
*
|
|
|
109 |
* @param string $title
|
|
|
110 |
* @param string $widget
|
|
|
111 |
* @return \block_instance
|
|
|
112 |
*/
|
|
|
113 |
protected function create_user_block($title, $widget) {
|
|
|
114 |
global $USER;
|
|
|
115 |
|
|
|
116 |
$configdata = (object) [
|
|
|
117 |
'title' => $title,
|
|
|
118 |
'data_source_idnumber' => $widget,
|
|
|
119 |
];
|
|
|
120 |
|
|
|
121 |
$this->create_block($this->construct_user_page($USER));
|
|
|
122 |
$block = $this->get_last_block_on_page($this->construct_user_page($USER));
|
|
|
123 |
$block = block_instance('dash', $block->instance);
|
|
|
124 |
$block->instance_config_save((object) $configdata);
|
|
|
125 |
|
|
|
126 |
return $block;
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
/**
|
|
|
130 |
* Get the last block on the page.
|
|
|
131 |
*
|
|
|
132 |
* @param \page $page Page
|
|
|
133 |
* @return \block_html Block instance object
|
|
|
134 |
*/
|
|
|
135 |
protected function get_last_block_on_page($page) {
|
|
|
136 |
$blocks = $page->blocks->get_blocks_for_region($page->blocks->get_default_region());
|
|
|
137 |
$block = end($blocks);
|
|
|
138 |
|
|
|
139 |
return $block;
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
/**
|
|
|
143 |
* Creates an HTML block on a page.
|
|
|
144 |
*
|
|
|
145 |
* @param \page $page Page
|
|
|
146 |
* @return void
|
|
|
147 |
*/
|
|
|
148 |
protected function create_block($page) {
|
|
|
149 |
$page->blocks->add_block_at_end_of_default_region('dash');
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
/**
|
|
|
153 |
* Test for block_dash\local\widget\contacts\contacts_widget() to confirm the Contacts and converstions are loaded.
|
|
|
154 |
*
|
|
|
155 |
* @covers ::contacts_widget
|
|
|
156 |
* @return void
|
|
|
157 |
*
|
|
|
158 |
* @runInSeparateProcess
|
|
|
159 |
* @runTestsInSeparateProcesses
|
|
|
160 |
*/
|
|
|
161 |
public function test_mylearning() {
|
|
|
162 |
$user = self::getDataGenerator()->create_and_enrol($this->course1, 'student');
|
|
|
163 |
$teacher = self::getDataGenerator()->create_and_enrol($this->course1, 'editingteacher');
|
|
|
164 |
self::getDataGenerator()->enrol_user($user->id, $this->course2->id);
|
|
|
165 |
self::getDataGenerator()->enrol_user($user->id, $this->course3->id);
|
|
|
166 |
$this->setUser($user);
|
|
|
167 |
|
|
|
168 |
$assign = $this->getDataGenerator()->create_module('assign', ['course' => $this->course1->id],
|
|
|
169 |
['completion' => 1]);
|
|
|
170 |
$data = $this->getDataGenerator()->create_module('data', ['course' => $this->course1->id],
|
|
|
171 |
['completion' => 1]);
|
|
|
172 |
$this->getDataGenerator()->create_module('page', ['course' => $this->course1->id],
|
|
|
173 |
['completion' => 1]);
|
|
|
174 |
$this->getDataGenerator()->create_module('page', ['course' => $this->course1->id],
|
|
|
175 |
['completion' => 1]);
|
|
|
176 |
|
|
|
177 |
// Mark two of them as completed for a user.
|
|
|
178 |
$cmassign = get_coursemodule_from_id('assign', $assign->cmid);
|
|
|
179 |
$cmdata = get_coursemodule_from_id('data', $data->cmid);
|
|
|
180 |
$completion = new \completion_info($this->course1);
|
|
|
181 |
$completion->update_state($cmassign, COMPLETION_COMPLETE, $user->id);
|
|
|
182 |
$completion->update_state($cmdata, COMPLETION_COMPLETE, $user->id);
|
|
|
183 |
|
|
|
184 |
$block = $this->create_user_block('My contacts', 'block_dash\local\widget\mylearning\mylearning_widget');
|
|
|
185 |
$context1 = \context_course::instance($this->course1->id);
|
|
|
186 |
|
|
|
187 |
$widget = new \block_dash\local\widget\mylearning\mylearning_widget($context1);
|
|
|
188 |
$widget->set_block_instance($block);
|
|
|
189 |
$data = $widget->build_widget();
|
|
|
190 |
|
|
|
191 |
$endcourse = end($data['courses']);
|
|
|
192 |
$firstmodule = $endcourse->coursecontent[0]['modules'][0];
|
|
|
193 |
$section = $endcourse->coursecontent[0];
|
|
|
194 |
|
|
|
195 |
$this->assertEquals(3, count($data['courses']));
|
|
|
196 |
$this->assertNotFalse(stripos(end($data['courses'])->contacts, fullname($teacher)) );
|
|
|
197 |
$this->assertEquals(4, count($endcourse->coursecontent[0]['modules']));
|
|
|
198 |
$this->assertEquals(1, $firstmodule['completiondata']['state']);
|
|
|
199 |
$this->assertEquals(2, $section['activitycompleted']);
|
|
|
200 |
$this->assertEquals(4, $section['activitycount']);
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
/**
|
|
|
204 |
* Test for block_dash\local\widget\contacts\contacts_widget() to confirm the Contacts and converstions are loaded.
|
|
|
205 |
*
|
|
|
206 |
* @covers ::contacts_widget
|
|
|
207 |
* @return void
|
|
|
208 |
*/
|
|
|
209 |
public function test_mycontacts() {
|
|
|
210 |
global $DB;
|
|
|
211 |
|
|
|
212 |
$block = $this->create_user_block('My contacts', 'block_dash\local\widget\contacts\contacts_widget');
|
|
|
213 |
|
|
|
214 |
\core_message\api::add_contact($this->users[1]->id, $this->users[2]->id);
|
|
|
215 |
\core_message\api::add_contact($this->users[1]->id, $this->users[3]->id);
|
|
|
216 |
\core_message\api::add_contact($this->users[1]->id, $this->users[4]->id);
|
|
|
217 |
|
|
|
218 |
// Create some individual conversations.
|
|
|
219 |
$ic1 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
|
|
|
220 |
[$this->users[1]->id, $this->users[2]->id]);
|
|
|
221 |
$ic2 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
|
|
|
222 |
[$this->users[1]->id, $this->users[3]->id]);
|
|
|
223 |
|
|
|
224 |
// Send some messages to individual conversations.
|
|
|
225 |
$im1 = testhelper::send_fake_message_to_conversation($this->users[1], $ic1->id, 'Message 1');
|
|
|
226 |
$im2 = testhelper::send_fake_message_to_conversation($this->users[2], $ic1->id, 'Message 2');
|
|
|
227 |
$im3 = testhelper::send_fake_message_to_conversation($this->users[2], $ic1->id, 'Message 3');
|
|
|
228 |
$im4 = testhelper::send_fake_message_to_conversation($this->users[3], $ic2->id, 'Message 4');
|
|
|
229 |
|
|
|
230 |
$this->setUser($this->users[1]);
|
|
|
231 |
|
|
|
232 |
$context1 = \context_course::instance($this->course1->id);
|
|
|
233 |
$widget = new \block_dash\local\widget\contacts\contacts_widget($context1);
|
|
|
234 |
|
|
|
235 |
$widget->set_block_instance($block);
|
|
|
236 |
$data = $widget->build_widget();
|
|
|
237 |
|
|
|
238 |
$contacts = $data['contacts'];
|
|
|
239 |
$this->assertEquals(3, count($data['contacts']));
|
|
|
240 |
$this->assertArrayHasKey(0, $contacts);
|
|
|
241 |
$this->assertEquals(2, $contacts[0]->unreadcount);
|
|
|
242 |
$this->assertArrayHasKey(1, $contacts);
|
|
|
243 |
$this->assertEquals(1, $contacts[1]->unreadcount);
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
/**
|
|
|
247 |
* Test for block_dash\local\widget\groups\groups_widget() to confirm the groups and group memebers are loaded.
|
|
|
248 |
*
|
|
|
249 |
* @covers ::groups_widget
|
|
|
250 |
* @return void
|
|
|
251 |
*/
|
|
|
252 |
public function test_mygroups() {
|
|
|
253 |
global $CFG;
|
|
|
254 |
|
|
|
255 |
require_once($CFG->dirroot.'/group/lib.php');
|
|
|
256 |
|
|
|
257 |
role_assign(1, $this->users[1]->id, \context_system::instance()->id);
|
|
|
258 |
|
|
|
259 |
$user = self::getDataGenerator()->enrol_user($this->users[1]->id, $this->course1->id, 'manager');
|
|
|
260 |
$user = self::getDataGenerator()->enrol_user($this->users[2]->id, $this->course2->id, 'student');
|
|
|
261 |
$user = self::getDataGenerator()->enrol_user($this->users[3]->id, $this->course1->id, 'student');
|
|
|
262 |
$user = self::getDataGenerator()->enrol_user($this->users[2]->id, $this->course1->id, 'student');
|
|
|
263 |
|
|
|
264 |
$group1 = self::getDataGenerator()->create_group(['courseid' => $this->course1->id]);
|
|
|
265 |
$group2 = self::getDataGenerator()->create_group(['courseid' => $this->course1->id]);
|
|
|
266 |
|
|
|
267 |
$group3 = self::getDataGenerator()->create_group(['courseid' => $this->course2->id]);
|
|
|
268 |
$group4 = self::getDataGenerator()->create_group(['courseid' => $this->course3->id]);
|
|
|
269 |
|
|
|
270 |
groups_add_member($group1, $this->users[1]);
|
|
|
271 |
groups_add_member($group2, $this->users[1]);
|
|
|
272 |
groups_add_member($group3, $this->users[2]);
|
|
|
273 |
groups_add_member($group1, $this->users[2]);
|
|
|
274 |
groups_add_member($group1, $this->users[3]);
|
|
|
275 |
|
|
|
276 |
$this->setUser($this->users[1]);
|
|
|
277 |
|
|
|
278 |
$block = $this->create_user_block('My Groups', 'block_dash\local\widget\groups\groups_widget');
|
|
|
279 |
|
|
|
280 |
$context1 = \context_course::instance($this->course1->id);
|
|
|
281 |
$widget = new \block_dash\local\widget\groups\groups_widget($context1);
|
|
|
282 |
|
|
|
283 |
$widget->set_block_instance($block);
|
|
|
284 |
$data = $widget->build_widget();
|
|
|
285 |
$groups = $data['groups'];
|
|
|
286 |
|
|
|
287 |
$this->assertEquals(2, count($data['groups']));
|
|
|
288 |
$this->assertArrayHasKey(0, $groups);
|
|
|
289 |
$this->assertEquals(2, count($groups[0]->members));
|
|
|
290 |
$this->assertEquals(1, $data['adduser']);
|
|
|
291 |
$this->assertEquals(1, $data['creategroup']);
|
|
|
292 |
|
|
|
293 |
$this->setUser($this->users[2]);
|
|
|
294 |
|
|
|
295 |
$context1 = \context_course::instance($this->course1->id);
|
|
|
296 |
$widget = new \block_dash\local\widget\groups\groups_widget($context1);
|
|
|
297 |
$widget->set_block_instance($block);
|
|
|
298 |
$data = $widget->build_widget();
|
|
|
299 |
|
|
|
300 |
$this->assertEquals(0, $data['creategroup']);
|
|
|
301 |
$this->assertEquals(0, $data['adduser']);
|
|
|
302 |
|
|
|
303 |
}
|
|
|
304 |
}
|