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 |
* This file contains unit test related to xAPI library.
|
|
|
19 |
*
|
|
|
20 |
* @package core_xapi
|
|
|
21 |
* @copyright 2020 Ferran Recio
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace core_xapi\local\statement;
|
|
|
26 |
|
|
|
27 |
use advanced_testcase;
|
|
|
28 |
use core_xapi\xapi_exception;
|
|
|
29 |
use core_xapi\iri;
|
|
|
30 |
|
|
|
31 |
defined('MOODLE_INTERNAL') || die();
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Contains test cases for testing statement group class.
|
|
|
35 |
*
|
|
|
36 |
* @package core_xapi
|
|
|
37 |
* @since Moodle 3.9
|
|
|
38 |
* @copyright 2020 Ferran Recio
|
|
|
39 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
40 |
*/
|
|
|
41 |
class item_group_test extends advanced_testcase {
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* Test item creation.
|
|
|
45 |
*/
|
|
|
46 |
public function test_create(): void {
|
|
|
47 |
global $CFG;
|
|
|
48 |
|
|
|
49 |
$this->resetAfterTest();
|
|
|
50 |
|
|
|
51 |
// Create one course with a group.
|
|
|
52 |
$course = $this->getDataGenerator()->create_course();
|
|
|
53 |
$user = $this->getDataGenerator()->create_user();
|
|
|
54 |
$this->getDataGenerator()->enrol_user($user->id, $course->id);
|
|
|
55 |
$user2 = $this->getDataGenerator()->create_user();
|
|
|
56 |
$this->getDataGenerator()->enrol_user($user2->id, $course->id);
|
|
|
57 |
|
|
|
58 |
$group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
|
|
|
59 |
$this->getDataGenerator()->create_group_member(array('groupid' => $group->id, 'userid' => $user->id));
|
|
|
60 |
$this->getDataGenerator()->create_group_member(array('groupid' => $group->id, 'userid' => $user2->id));
|
|
|
61 |
|
|
|
62 |
$data = (object) [
|
|
|
63 |
'objectType' => 'Group',
|
|
|
64 |
'account' => (object) [
|
|
|
65 |
'homePage' => $CFG->wwwroot,
|
|
|
66 |
'name' => $group->id,
|
|
|
67 |
],
|
|
|
68 |
];
|
|
|
69 |
$item = item_group::create_from_data($data);
|
|
|
70 |
|
|
|
71 |
$this->assertEquals(json_encode($item), json_encode($data));
|
|
|
72 |
$itemgroup = $item->get_group();
|
|
|
73 |
$this->assertEquals($itemgroup->id, $group->id);
|
|
|
74 |
$itemusers = $item->get_all_users();
|
|
|
75 |
$this->assertCount(2, $itemusers);
|
|
|
76 |
|
|
|
77 |
// Get user in group item must throw an exception.
|
|
|
78 |
$this->expectException(xapi_exception::class);
|
|
|
79 |
$itemusers = $item->get_user();
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* Test item creation from Record.
|
|
|
84 |
*/
|
|
|
85 |
public function test_create_from_group(): void {
|
|
|
86 |
global $CFG;
|
|
|
87 |
|
|
|
88 |
$this->resetAfterTest();
|
|
|
89 |
$course = $this->getDataGenerator()->create_course();
|
|
|
90 |
$group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
|
|
|
91 |
|
|
|
92 |
$item = item_group::create_from_group($group);
|
|
|
93 |
|
|
|
94 |
$itemgroup = $item->get_group();
|
|
|
95 |
$this->assertEquals($itemgroup->id, $group->id);
|
|
|
96 |
|
|
|
97 |
// Check generated data.
|
|
|
98 |
$data = $item->get_data();
|
|
|
99 |
$this->assertEquals('Group', $data->objectType);
|
|
|
100 |
$this->assertEquals($CFG->wwwroot, $data->account->homePage);
|
|
|
101 |
$this->assertEquals($group->id, $data->account->name);
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* Test for invalid structures.
|
|
|
106 |
*
|
|
|
107 |
* @dataProvider invalid_data_provider
|
|
|
108 |
* @param string $objecttype object type attribute
|
|
|
109 |
* @param bool $validhome if valid homepage is user
|
|
|
110 |
* @param bool $validid if valid group id is used
|
|
|
111 |
*/
|
|
|
112 |
public function test_invalid_data(string $objecttype, bool $validhome, bool $validid): void {
|
|
|
113 |
global $CFG;
|
|
|
114 |
|
|
|
115 |
// Create one course with a group if necessary.
|
|
|
116 |
$id = 'Wrong ID';
|
|
|
117 |
if ($validid) {
|
|
|
118 |
$this->resetAfterTest();
|
|
|
119 |
$course = $this->getDataGenerator()->create_course();
|
|
|
120 |
$group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
|
|
|
121 |
$id = $group->id;
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
$homepage = 'Invalid homepage!';
|
|
|
125 |
if ($validhome) {
|
|
|
126 |
$homepage = $CFG->wwwroot;
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
$data = (object) [
|
|
|
130 |
'objectType' => $objecttype,
|
|
|
131 |
'account' => (object) [
|
|
|
132 |
'homePage' => $homepage,
|
|
|
133 |
'name' => $id,
|
|
|
134 |
],
|
|
|
135 |
];
|
|
|
136 |
|
|
|
137 |
$this->expectException(xapi_exception::class);
|
|
|
138 |
$item = item_group::create_from_data($data);
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
/**
|
|
|
142 |
* Data provider for the test_invalid_data tests.
|
|
|
143 |
*
|
|
|
144 |
* @return array
|
|
|
145 |
*/
|
|
|
146 |
public function invalid_data_provider(): array {
|
|
|
147 |
return [
|
|
|
148 |
'Wrong objecttype' => [
|
|
|
149 |
'Invalid', true, true
|
|
|
150 |
],
|
|
|
151 |
'Wrong homepage' => [
|
|
|
152 |
'Group', false, true
|
|
|
153 |
],
|
|
|
154 |
'Wrong id' => [
|
|
|
155 |
'Group', true, false
|
|
|
156 |
],
|
|
|
157 |
];
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
/**
|
|
|
161 |
* Test for missing object type.
|
|
|
162 |
*/
|
|
|
163 |
public function test_missing_object_type(): void {
|
|
|
164 |
$data = (object) ['id' => -1];
|
|
|
165 |
$this->expectException(xapi_exception::class);
|
|
|
166 |
$item = item_group::create_from_data($data);
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
/**
|
|
|
170 |
* Test for invalid anonymous group.
|
|
|
171 |
*/
|
|
|
172 |
public function test_invalid_anonymous_group(): void {
|
|
|
173 |
$data = (object) [
|
|
|
174 |
'objectType' => 'Group'
|
|
|
175 |
];
|
|
|
176 |
$this->expectException(xapi_exception::class);
|
|
|
177 |
$item = item_group::create_from_data($data);
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
/**
|
|
|
181 |
* Test for invalid anonymous group.
|
|
|
182 |
*/
|
|
|
183 |
public function test_inexistent_group(): void {
|
|
|
184 |
global $CFG;
|
|
|
185 |
$data = (object) [
|
|
|
186 |
'objectType' => 'Group',
|
|
|
187 |
'account' => (object) [
|
|
|
188 |
'homePage' => $CFG->wwwroot,
|
|
|
189 |
'name' => 0,
|
|
|
190 |
],
|
|
|
191 |
];
|
|
|
192 |
$this->expectException(xapi_exception::class);
|
|
|
193 |
$item = item_group::create_from_data($data);
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
/**
|
|
|
197 |
* Test for invalid group record.
|
|
|
198 |
*/
|
|
|
199 |
public function test_inexistent_group_id(): void {
|
|
|
200 |
$group = (object) ['name' => 'My Group'];
|
|
|
201 |
$this->expectException(xapi_exception::class);
|
|
|
202 |
$item = item_group::create_from_group($group);
|
|
|
203 |
}
|
|
|
204 |
}
|