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 |
* Basic downloadcenter PHP Unit tests.
|
|
|
19 |
*
|
|
|
20 |
* @author Simeon Naydenov (moniNaydenov@gmail.com)
|
|
|
21 |
* @package local_downloadcenter
|
|
|
22 |
* @category phpunit
|
|
|
23 |
* @copyright 2020 Academic Moodle Cooperation {@link http://www.academic-moodle-cooperation.org}
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
25 |
*/
|
|
|
26 |
defined('MOODLE_INTERNAL') || die();
|
|
|
27 |
|
|
|
28 |
/*
|
|
|
29 |
* TODO tests:
|
|
|
30 |
* - add resources, files and check if the students
|
|
|
31 |
*
|
|
|
32 |
*/
|
|
|
33 |
class local_downloadcenter_files_visible_testcase extends advanced_testcase {
|
|
|
34 |
|
|
|
35 |
public function test_empty() {
|
|
|
36 |
global $DB, $CFG;
|
|
|
37 |
require_once(__DIR__ . '/../locallib.php');
|
|
|
38 |
|
|
|
39 |
$this->resetAfterTest(true);
|
|
|
40 |
|
|
|
41 |
$teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
|
|
|
42 |
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
|
|
43 |
|
|
|
44 |
$student1 = $this->getDataGenerator()->create_user();
|
|
|
45 |
$teacher1 = $this->getDataGenerator()->create_user();
|
|
|
46 |
|
|
|
47 |
$this->setAdminUser();
|
|
|
48 |
|
|
|
49 |
$course1 = $this->getDataGenerator()->create_course();
|
|
|
50 |
|
|
|
51 |
$this->getDataGenerator()->enrol_user($student1->id, $course1->id, $studentrole->id);
|
|
|
52 |
$this->getDataGenerator()->enrol_user($teacher1->id, $course1->id, $teacherrole->id);
|
|
|
53 |
|
|
|
54 |
$this->setUser($student1);
|
|
|
55 |
|
|
|
56 |
$downloadcenter = new local_downloadcenter_factory($course1, null);
|
|
|
57 |
$userresources = $downloadcenter->get_resources_for_user();
|
|
|
58 |
|
|
|
59 |
foreach ($userresources as $resources) {
|
|
|
60 |
$this->assertEmpty($resources->res);
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
$this->setUser($teacher1);
|
|
|
64 |
|
|
|
65 |
$downloadcenter = new local_downloadcenter_factory($course1, null);
|
|
|
66 |
$userresources = $downloadcenter->get_resources_for_user();
|
|
|
67 |
|
|
|
68 |
foreach ($userresources as $resources) {
|
|
|
69 |
$this->assertEmpty($resources->res);
|
|
|
70 |
}
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
public function test_student_visibility() {
|
|
|
74 |
global $DB, $CFG;
|
|
|
75 |
require_once(__DIR__ . '/../locallib.php');
|
|
|
76 |
|
|
|
77 |
$this->resetAfterTest(true);
|
|
|
78 |
|
|
|
79 |
$teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
|
|
|
80 |
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
|
|
81 |
|
|
|
82 |
$student1 = $this->getDataGenerator()->create_user();
|
|
|
83 |
$teacher1 = $this->getDataGenerator()->create_user();
|
|
|
84 |
|
|
|
85 |
$this->setAdminUser();
|
|
|
86 |
|
|
|
87 |
$course1 = $this->getDataGenerator()->create_course();
|
|
|
88 |
|
|
|
89 |
$this->setUser($student1);
|
|
|
90 |
|
|
|
91 |
$this->getDataGenerator()->enrol_user($student1->id, $course1->id, $studentrole->id);
|
|
|
92 |
$this->getDataGenerator()->enrol_user($teacher1->id, $course1->id, $teacherrole->id);
|
|
|
93 |
|
|
|
94 |
$resources = $this->helper_add_resources_to_course($course1, $teacher1);
|
|
|
95 |
|
|
|
96 |
// Test for student - must not see not visible resources.
|
|
|
97 |
$downloadcenter = new local_downloadcenter_factory($course1, $student1);
|
|
|
98 |
$userresources = $downloadcenter->get_resources_for_user();
|
|
|
99 |
|
|
|
100 |
$this->assertCount($resources->visiblefilecount, $userresources[$resources->filesection]->res);
|
|
|
101 |
$this->assertCount($resources->visiblefoldercount, $userresources[$resources->foldersection]->res);
|
|
|
102 |
$this->assertCount($resources->visiblepagecount, $userresources[$resources->pagesection]->res);
|
|
|
103 |
$this->assertCount($resources->visiblebookcount, $userresources[$resources->booksection]->res);
|
|
|
104 |
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
public function test_teacher_visibility() {
|
|
|
108 |
global $DB, $CFG;
|
|
|
109 |
require_once(__DIR__ . '/../locallib.php');
|
|
|
110 |
|
|
|
111 |
$this->resetAfterTest(true);
|
|
|
112 |
|
|
|
113 |
$teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
|
|
|
114 |
|
|
|
115 |
$this->setAdminUser();
|
|
|
116 |
|
|
|
117 |
$teacher1 = $this->getDataGenerator()->create_user();
|
|
|
118 |
$course1 = $this->getDataGenerator()->create_course();
|
|
|
119 |
|
|
|
120 |
$this->getDataGenerator()->enrol_user($teacher1->id, $course1->id, $teacherrole->id);
|
|
|
121 |
|
|
|
122 |
$resources = $this->helper_add_resources_to_course($course1, $teacher1);
|
|
|
123 |
|
|
|
124 |
// Test for teacher - must see all resources.
|
|
|
125 |
|
|
|
126 |
$this->setUser($teacher1);
|
|
|
127 |
|
|
|
128 |
$downloadcenter = new local_downloadcenter_factory($course1, $teacher1);
|
|
|
129 |
$userresources = $downloadcenter->get_resources_for_user();
|
|
|
130 |
|
|
|
131 |
$this->assertCount($resources->filecount, $userresources[$resources->filesection]->res);
|
|
|
132 |
$this->assertCount($resources->foldercount, $userresources[$resources->foldersection]->res);
|
|
|
133 |
$this->assertCount($resources->pagecount, $userresources[$resources->pagesection]->res);
|
|
|
134 |
$this->assertCount($resources->bookcount, $userresources[$resources->booksection]->res);
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
private function helper_add_file_to_context($filename, $filecontent , $context) {
|
|
|
138 |
// Pick a random context id for specified user.
|
|
|
139 |
$fileid = file_get_unused_draft_itemid();
|
|
|
140 |
|
|
|
141 |
// Add actual file there.
|
|
|
142 |
$filerecord = array('component' => 'user', 'filearea' => 'draft',
|
|
|
143 |
'contextid' => $context->id, 'itemid' => $fileid,
|
|
|
144 |
'filename' => $filename, 'filepath' => '/', );
|
|
|
145 |
$fs = get_file_storage();
|
|
|
146 |
$fs->create_file_from_string($filerecord, $filecontent);
|
|
|
147 |
|
|
|
148 |
return $fileid;
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
private function helper_add_resources_to_course($course, $teacher) {
|
|
|
152 |
|
|
|
153 |
$filesection = 0;
|
|
|
154 |
$foldersection = 1;
|
|
|
155 |
$pagesection = 2;
|
|
|
156 |
$booksection = 3;
|
|
|
157 |
$publicationsection = 5;
|
|
|
158 |
|
|
|
159 |
$generator = $this->getDataGenerator()->get_plugin_generator('mod_resource');
|
|
|
160 |
|
|
|
161 |
$record = new stdClass;
|
|
|
162 |
$record->course = $course->id;
|
|
|
163 |
$usercontext = context_user::instance($teacher->id);
|
|
|
164 |
|
|
|
165 |
// Add 10 files with random visibility.
|
|
|
166 |
$filecount = 10;
|
|
|
167 |
$visiblefilecount = 0;
|
|
|
168 |
$record->section = $filesection;
|
|
|
169 |
for ($i = 0; $i < $filecount; $i++) {
|
|
|
170 |
$record->visible = rand(0, 1000) > 500;
|
|
|
171 |
$visiblefilecount += intval($record->visible);
|
|
|
172 |
$record->files = $this->helper_add_file_to_context('resource' . ($i + 1) . '.jpg', 'some random content', $usercontext);
|
|
|
173 |
$generator->create_instance($record);
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
// Add 10 folders with random visibility.
|
|
|
177 |
$generator = $this->getDataGenerator()->get_plugin_generator('mod_folder');
|
|
|
178 |
|
|
|
179 |
$record->section = $foldersection;
|
|
|
180 |
$foldercount = 10;
|
|
|
181 |
$visiblefoldercount = 0;
|
|
|
182 |
for ($i = 0; $i < $foldercount; $i++) {
|
|
|
183 |
|
|
|
184 |
$record->visible = rand(0, 1000) > 500;
|
|
|
185 |
$visiblefoldercount += intval($record->visible);
|
|
|
186 |
$record->files = $this->helper_add_file_to_context('resource' . ($i + 1) . '.jpg', 'some random content', $usercontext);
|
|
|
187 |
$generator->create_instance($record);
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
// Add 10 pages with random visibility.
|
|
|
191 |
$generator = $this->getDataGenerator()->get_plugin_generator('mod_page');
|
|
|
192 |
|
|
|
193 |
unset($record->files);
|
|
|
194 |
$record->section = $pagesection;
|
|
|
195 |
$pagecount = 10;
|
|
|
196 |
$visiblepagecount = 0;
|
|
|
197 |
for ($i = 0; $i < $pagecount; $i++) {
|
|
|
198 |
$record->visible = rand(0, 1000) > 500;
|
|
|
199 |
$visiblepagecount += intval($record->visible);
|
|
|
200 |
$record->content = 'Some random content';
|
|
|
201 |
$record->contentformat = FORMAT_HTML;
|
|
|
202 |
|
|
|
203 |
$generator->create_instance($record);
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
// Add 10 books with random visibility.
|
|
|
207 |
$generator = $this->getDataGenerator()->get_plugin_generator('mod_book');
|
|
|
208 |
|
|
|
209 |
unset($record->content);
|
|
|
210 |
unset($record->contentformat);
|
|
|
211 |
|
|
|
212 |
$record->section = $booksection;
|
|
|
213 |
$bookcount = 10;
|
|
|
214 |
$visiblebookcount = 0;
|
|
|
215 |
for ($i = 0; $i < $bookcount; $i++) {
|
|
|
216 |
$record->visible = rand(0, 1000) > 500;
|
|
|
217 |
$visiblebookcount += intval($record->visible);
|
|
|
218 |
$record->content = 'Some random content';
|
|
|
219 |
$record->contentformat = FORMAT_HTML;
|
|
|
220 |
|
|
|
221 |
$book = $generator->create_instance($record);
|
|
|
222 |
// Add 5 chapters to each book.
|
|
|
223 |
for ($j = 0; $j < 5; $j++) {
|
|
|
224 |
$generator->create_chapter(['bookid' => $book->id]);
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
$result = new stdClass;
|
|
|
230 |
$result->filecount = $filecount;
|
|
|
231 |
$result->visiblefilecount = $visiblefilecount;
|
|
|
232 |
$result->filesection = $filesection;
|
|
|
233 |
|
|
|
234 |
$result->foldercount = $foldercount;
|
|
|
235 |
$result->visiblefoldercount = $visiblefoldercount;
|
|
|
236 |
$result->foldersection = $foldersection;
|
|
|
237 |
|
|
|
238 |
$result->pagecount = $pagecount;
|
|
|
239 |
$result->visiblepagecount = $visiblepagecount;
|
|
|
240 |
$result->pagesection = $pagesection;
|
|
|
241 |
|
|
|
242 |
$result->bookcount = $bookcount;
|
|
|
243 |
$result->visiblebookcount = $visiblebookcount;
|
|
|
244 |
$result->booksection = $booksection;
|
|
|
245 |
|
|
|
246 |
return $result;
|
|
|
247 |
}
|
|
|
248 |
|
|
|
249 |
|
|
|
250 |
}
|