MDL-59248 mod_workshop: New API check_examples_assessed_before_assessment
[moodle.git] / mod / workshop / tests / external_test.php
CommitLineData
9f1ab2db
JL
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 * Workshop module external functions tests
19 *
20 * @package mod_workshop
21 * @category external
22 * @copyright 2017 Juan Leyva <juan@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @since Moodle 3.4
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30
31require_once($CFG->dirroot . '/webservice/tests/helpers.php');
32require_once($CFG->dirroot . '/mod/workshop/lib.php');
33
34use mod_workshop\external\workshop_summary_exporter;
3f08cfc5 35use mod_workshop\external\submission_exporter;
9f1ab2db
JL
36
37/**
38 * Workshop module external functions tests
39 *
40 * @package mod_workshop
41 * @category external
42 * @copyright 2017 Juan Leyva <juan@moodle.com>
43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 * @since Moodle 3.4
45 */
46class mod_workshop_external_testcase extends externallib_advanced_testcase {
47
48 /** @var stdClass course object */
49 private $course;
50 /** @var stdClass workshop object */
51 private $workshop;
52 /** @var stdClass context object */
53 private $context;
54 /** @var stdClass cm object */
55 private $cm;
56 /** @var stdClass student object */
57 private $student;
58 /** @var stdClass teacher object */
59 private $teacher;
60 /** @var stdClass student role object */
61 private $studentrole;
62 /** @var stdClass teacher role object */
63 private $teacherrole;
64
65 /**
66 * Set up for every test
67 */
68 public function setUp() {
69 global $DB;
70 $this->resetAfterTest();
71 $this->setAdminUser();
72
73 // Setup test data.
3f08cfc5
JL
74 $course = new stdClass();
75 $course->groupmode = SEPARATEGROUPS;
76 $course->groupmodeforce = true;
77 $this->course = $this->getDataGenerator()->create_course($course);
9f1ab2db
JL
78 $this->workshop = $this->getDataGenerator()->create_module('workshop', array('course' => $this->course->id));
79 $this->context = context_module::instance($this->workshop->cmid);
80 $this->cm = get_coursemodule_from_instance('workshop', $this->workshop->id);
81
82 // Create users.
83 $this->student = self::getDataGenerator()->create_user();
3f08cfc5
JL
84 $this->anotherstudentg1 = self::getDataGenerator()->create_user();
85 $this->anotherstudentg2 = self::getDataGenerator()->create_user();
9f1ab2db
JL
86 $this->teacher = self::getDataGenerator()->create_user();
87
88 // Users enrolments.
89 $this->studentrole = $DB->get_record('role', array('shortname' => 'student'));
90 $this->teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
91 $this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $this->studentrole->id, 'manual');
3f08cfc5
JL
92 $this->getDataGenerator()->enrol_user($this->anotherstudentg1->id, $this->course->id, $this->studentrole->id, 'manual');
93 $this->getDataGenerator()->enrol_user($this->anotherstudentg2->id, $this->course->id, $this->studentrole->id, 'manual');
9f1ab2db 94 $this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, $this->teacherrole->id, 'manual');
3f08cfc5
JL
95
96 $this->group1 = $this->getDataGenerator()->create_group(array('courseid' => $this->course->id));
97 $this->group2 = $this->getDataGenerator()->create_group(array('courseid' => $this->course->id));
98 groups_add_member($this->group1, $this->student);
99 groups_add_member($this->group1, $this->anotherstudentg1);
100 groups_add_member($this->group2, $this->anotherstudentg2);
9f1ab2db
JL
101 }
102
103 /**
104 * Test test_mod_workshop_get_workshops_by_courses
105 */
106 public function test_mod_workshop_get_workshops_by_courses() {
107 global $DB;
108
109 // Create additional course.
110 $course2 = self::getDataGenerator()->create_course();
111
112 // Second workshop.
113 $record = new stdClass();
114 $record->course = $course2->id;
115 $workshop2 = self::getDataGenerator()->create_module('workshop', $record);
116
117 // Execute real Moodle enrolment as we'll call unenrol() method on the instance later.
118 $enrol = enrol_get_plugin('manual');
119 $enrolinstances = enrol_get_instances($course2->id, true);
120 foreach ($enrolinstances as $courseenrolinstance) {
121 if ($courseenrolinstance->enrol == "manual") {
122 $instance2 = $courseenrolinstance;
123 break;
124 }
125 }
126 $enrol->enrol_user($instance2, $this->student->id, $this->studentrole->id);
127
128 self::setUser($this->student);
129
130 $returndescription = mod_workshop_external::get_workshops_by_courses_returns();
131
132 // Create what we expect to be returned when querying the two courses.
133 $properties = workshop_summary_exporter::read_properties_definition();
134 $expectedfields = array_keys($properties);
135
136 // Add expected coursemodule and data.
137 $workshop1 = $this->workshop;
138 $workshop1->coursemodule = $workshop1->cmid;
139 $workshop1->introformat = 1;
140 $workshop1->introfiles = [];
141 $workshop1->instructauthorsfiles = [];
142 $workshop1->instructauthorsformat = 1;
143 $workshop1->instructreviewersfiles = [];
144 $workshop1->instructreviewersformat = 1;
145 $workshop1->conclusionfiles = [];
146 $workshop1->conclusionformat = 1;
147
148 $workshop2->coursemodule = $workshop2->cmid;
149 $workshop2->introformat = 1;
150 $workshop2->introfiles = [];
151 $workshop2->instructauthorsfiles = [];
152 $workshop2->instructauthorsformat = 1;
153 $workshop2->instructreviewersfiles = [];
154 $workshop2->instructreviewersformat = 1;
155 $workshop2->conclusionfiles = [];
156 $workshop2->conclusionformat = 1;
157
158 foreach ($expectedfields as $field) {
159 if (!empty($properties[$field]) && $properties[$field]['type'] == PARAM_BOOL) {
160 $workshop1->{$field} = (bool) $workshop1->{$field};
161 $workshop2->{$field} = (bool) $workshop2->{$field};
162 }
163 $expected1[$field] = $workshop1->{$field};
164 $expected2[$field] = $workshop2->{$field};
165 }
166
167 $expectedworkshops = array($expected2, $expected1);
168
169 // Call the external function passing course ids.
170 $result = mod_workshop_external::get_workshops_by_courses(array($course2->id, $this->course->id));
171 $result = external_api::clean_returnvalue($returndescription, $result);
172
173 $this->assertEquals($expectedworkshops, $result['workshops']);
174 $this->assertCount(0, $result['warnings']);
175
176 // Call the external function without passing course id.
177 $result = mod_workshop_external::get_workshops_by_courses();
178 $result = external_api::clean_returnvalue($returndescription, $result);
179 $this->assertEquals($expectedworkshops, $result['workshops']);
180 $this->assertCount(0, $result['warnings']);
181
182 // Unenrol user from second course and alter expected workshops.
183 $enrol->unenrol_user($instance2, $this->student->id);
184 array_shift($expectedworkshops);
185
186 // Call the external function without passing course id.
187 $result = mod_workshop_external::get_workshops_by_courses();
188 $result = external_api::clean_returnvalue($returndescription, $result);
189 $this->assertEquals($expectedworkshops, $result['workshops']);
190
191 // Call for the second course we unenrolled the user from, expected warning.
192 $result = mod_workshop_external::get_workshops_by_courses(array($course2->id));
193 $this->assertCount(1, $result['warnings']);
194 $this->assertEquals('1', $result['warnings'][0]['warningcode']);
195 $this->assertEquals($course2->id, $result['warnings'][0]['itemid']);
196 }
977fdfa3
JL
197
198 /**
199 * Test mod_workshop_get_workshop_access_information for students.
200 */
201 public function test_mod_workshop_get_workshop_access_information_student() {
202
203 self::setUser($this->student);
204 $result = mod_workshop_external::get_workshop_access_information($this->workshop->id);
205 $result = external_api::clean_returnvalue(mod_workshop_external::get_workshop_access_information_returns(), $result);
206 // Check default values for capabilities.
207 $enabledcaps = array('canpeerassess', 'cansubmit', 'canview', 'canviewauthornames', 'canviewauthorpublished',
208 'canviewpublishedsubmissions', 'canexportsubmissions');
209
210 foreach ($result as $capname => $capvalue) {
211 if (strpos($capname, 'can') !== 0) {
212 continue;
213 }
214 if (in_array($capname, $enabledcaps)) {
215 $this->assertTrue($capvalue);
216 } else {
217 $this->assertFalse($capvalue);
218 }
219 }
220 // Now, unassign some capabilities.
221 unassign_capability('mod/workshop:peerassess', $this->studentrole->id);
222 unassign_capability('mod/workshop:submit', $this->studentrole->id);
223 unset($enabledcaps[0]);
224 unset($enabledcaps[1]);
225 accesslib_clear_all_caches_for_unit_testing();
226
227 $result = mod_workshop_external::get_workshop_access_information($this->workshop->id);
228 $result = external_api::clean_returnvalue(mod_workshop_external::get_workshop_access_information_returns(), $result);
229 foreach ($result as $capname => $capvalue) {
230 if (strpos($capname, 'can') !== 0) {
231 continue;
232 }
233 if (in_array($capname, $enabledcaps)) {
234 $this->assertTrue($capvalue);
235 } else {
236 $this->assertFalse($capvalue);
237 }
238 }
239
240 // Now, specific functionalities.
241 $this->assertFalse($result['creatingsubmissionallowed']);
242 $this->assertFalse($result['modifyingsubmissionallowed']);
243 $this->assertFalse($result['assessingallowed']);
244 $this->assertFalse($result['assessingexamplesallowed']);
86928d2a
JL
245 $this->assertTrue($result['examplesassessedbeforesubmission']);
246 $this->assertTrue($result['examplesassessedbeforeassessment']);
977fdfa3
JL
247
248 // Switch phase.
249 $workshop = new workshop($this->workshop, $this->cm, $this->course);
250 $workshop->switch_phase(workshop::PHASE_SUBMISSION);
251 $result = mod_workshop_external::get_workshop_access_information($this->workshop->id);
252 $result = external_api::clean_returnvalue(mod_workshop_external::get_workshop_access_information_returns(), $result);
253
254 $this->assertTrue($result['creatingsubmissionallowed']);
255 $this->assertTrue($result['modifyingsubmissionallowed']);
256 $this->assertFalse($result['assessingallowed']);
257 $this->assertFalse($result['assessingexamplesallowed']);
86928d2a
JL
258 $this->assertTrue($result['examplesassessedbeforesubmission']);
259 $this->assertTrue($result['examplesassessedbeforeassessment']);
977fdfa3
JL
260
261 // Switch to next (to assessment).
262 $workshop = new workshop($this->workshop, $this->cm, $this->course);
263 $workshop->switch_phase(workshop::PHASE_ASSESSMENT);
264 $result = mod_workshop_external::get_workshop_access_information($this->workshop->id);
265 $result = external_api::clean_returnvalue(mod_workshop_external::get_workshop_access_information_returns(), $result);
266
267 $this->assertFalse($result['creatingsubmissionallowed']);
268 $this->assertFalse($result['modifyingsubmissionallowed']);
269 $this->assertTrue($result['assessingallowed']);
270 $this->assertFalse($result['assessingexamplesallowed']);
86928d2a
JL
271 $this->assertTrue($result['examplesassessedbeforesubmission']);
272 $this->assertTrue($result['examplesassessedbeforeassessment']);
977fdfa3
JL
273 }
274
275 /**
276 * Test mod_workshop_get_workshop_access_information for teachers.
277 */
278 public function test_mod_workshop_get_workshop_access_information_teacher() {
279
280 self::setUser($this->teacher);
281 $result = mod_workshop_external::get_workshop_access_information($this->workshop->id);
282 $result = external_api::clean_returnvalue(mod_workshop_external::get_workshop_access_information_returns(), $result);
283 // Check default values.
284 $disabledcaps = array('canpeerassess', 'cansubmit');
285
286 foreach ($result as $capname => $capvalue) {
287 if (strpos($capname, 'can') !== 0) {
288 continue;
289 }
290 if (in_array($capname, $disabledcaps)) {
291 $this->assertFalse($capvalue);
292 } else {
293 $this->assertTrue($capvalue);
294 }
295 }
296
297 // Now, specific functionalities.
298 $this->assertFalse($result['creatingsubmissionallowed']);
299 $this->assertFalse($result['modifyingsubmissionallowed']);
300 $this->assertFalse($result['assessingallowed']);
301 $this->assertFalse($result['assessingexamplesallowed']);
302 }
cd495029
JL
303
304 /**
305 * Test mod_workshop_get_user_plan for students.
306 */
307 public function test_mod_workshop_get_user_plan_student() {
308
309 self::setUser($this->student);
310 $result = mod_workshop_external::get_user_plan($this->workshop->id);
311 $result = external_api::clean_returnvalue(mod_workshop_external::get_user_plan_returns(), $result);
312
313 $this->assertCount(0, $result['userplan']['examples']); // No examples given.
314 $this->assertCount(5, $result['userplan']['phases']); // Always 5 phases.
315 $this->assertEquals(workshop::PHASE_SETUP, $result['userplan']['phases'][0]['code']); // First phase always setup.
316 $this->assertTrue($result['userplan']['phases'][0]['active']); // First phase "Setup" active in new workshops.
317
318 // Switch phase.
319 $workshop = new workshop($this->workshop, $this->cm, $this->course);
320 $workshop->switch_phase(workshop::PHASE_SUBMISSION);
321
322 $result = mod_workshop_external::get_user_plan($this->workshop->id);
323 $result = external_api::clean_returnvalue(mod_workshop_external::get_user_plan_returns(), $result);
324
325 $this->assertEquals(workshop::PHASE_SUBMISSION, $result['userplan']['phases'][1]['code']);
326 $this->assertTrue($result['userplan']['phases'][1]['active']); // We are now in submission phase.
327 }
328
329 /**
330 * Test mod_workshop_get_user_plan for teachers.
331 */
332 public function test_mod_workshop_get_user_plan_teacher() {
333 global $DB;
334
335 self::setUser($this->teacher);
336 $result = mod_workshop_external::get_user_plan($this->workshop->id);
337 $result = external_api::clean_returnvalue(mod_workshop_external::get_user_plan_returns(), $result);
338
339 $this->assertCount(0, $result['userplan']['examples']); // No examples given.
340 $this->assertCount(5, $result['userplan']['phases']); // Always 5 phases.
341 $this->assertEquals(workshop::PHASE_SETUP, $result['userplan']['phases'][0]['code']); // First phase always setup.
342 $this->assertTrue($result['userplan']['phases'][0]['active']); // First phase "Setup" active in new workshops.
343 $this->assertCount(4, $result['userplan']['phases'][0]['tasks']); // For new empty workshops, always 4 tasks.
344
345 foreach ($result['userplan']['phases'][0]['tasks'] as $task) {
346 if ($task['code'] == 'intro' || $task['code'] == 'instructauthors') {
347 $this->assertEquals(1, $task['completed']);
348 } else {
349 $this->assertEmpty($task['completed']);
350 }
351 }
352
353 // Do some of the tasks asked - switch phase.
354 $workshop = new workshop($this->workshop, $this->cm, $this->course);
355 $workshop->switch_phase(workshop::PHASE_SUBMISSION);
356
357 $result = mod_workshop_external::get_user_plan($this->workshop->id);
358 $result = external_api::clean_returnvalue(mod_workshop_external::get_user_plan_returns(), $result);
359 foreach ($result['userplan']['phases'][0]['tasks'] as $task) {
360 if ($task['code'] == 'intro' || $task['code'] == 'instructauthors' || $task['code'] == 'switchtonextphase') {
361 $this->assertEquals(1, $task['completed']);
362 } else {
363 $this->assertEmpty($task['completed']);
364 }
365 }
366
367 $result = mod_workshop_external::get_user_plan($this->workshop->id);
368 $result = external_api::clean_returnvalue(mod_workshop_external::get_user_plan_returns(), $result);
369
370 $this->assertEquals(workshop::PHASE_SUBMISSION, $result['userplan']['phases'][1]['code']);
371 $this->assertTrue($result['userplan']['phases'][1]['active']); // We are now in submission phase.
372 }
291645f7
JL
373
374 /**
375 * Test test_view_workshop invalid id.
376 */
377 public function test_view_workshop_invalid_id() {
378 $this->expectException('moodle_exception');
379 mod_workshop_external::view_workshop(0);
380 }
381
382 /**
383 * Test test_view_workshop user not enrolled.
384 */
385 public function test_view_workshop_user_not_enrolled() {
386 // Test not-enrolled user.
387 $usernotenrolled = self::getDataGenerator()->create_user();
388 $this->setUser($usernotenrolled);
389 $this->expectException('moodle_exception');
390 mod_workshop_external::view_workshop($this->workshop->id);
391 }
392
393 /**
394 * Test test_view_workshop user student.
395 */
396 public function test_view_workshop_user_student() {
397 // Test user with full capabilities.
398 $this->setUser($this->student);
399
400 // Trigger and capture the event.
401 $sink = $this->redirectEvents();
402
403 $result = mod_workshop_external::view_workshop($this->workshop->id);
404 $result = external_api::clean_returnvalue(mod_workshop_external::view_workshop_returns(), $result);
405 $this->assertTrue($result['status']);
406
407 $events = $sink->get_events();
408 $this->assertCount(1, $events);
409 $event = array_shift($events);
410
411 // Checking that the event contains the expected values.
412 $this->assertInstanceOf('\mod_workshop\event\course_module_viewed', $event);
413 $this->assertEquals($this->context, $event->get_context());
414 $moodleworkshop = new \moodle_url('/mod/workshop/view.php', array('id' => $this->cm->id));
415 $this->assertEquals($moodleworkshop, $event->get_url());
416 $this->assertEventContextNotUsed($event);
417 $this->assertNotEmpty($event->get_name());
418 }
419
420 /**
421 * Test test_view_workshop user missing capabilities.
422 */
423 public function test_view_workshop_user_missing_capabilities() {
424 // Test user with no capabilities.
425 // We need a explicit prohibit since this capability is only defined in authenticated user and guest roles.
426 assign_capability('mod/workshop:view', CAP_PROHIBIT, $this->studentrole->id, $this->context->id);
427 // Empty all the caches that may be affected by this change.
428 accesslib_clear_all_caches_for_unit_testing();
429 course_modinfo::clear_instance_cache();
430
431 $this->setUser($this->student);
432 $this->expectException('moodle_exception');
433 mod_workshop_external::view_workshop($this->workshop->id);
434 }
c2cf2450
JL
435
436 /**
437 * Test test_add_submission.
438 */
439 public function test_add_submission() {
440 $fs = get_file_storage();
441
442 // Test user with full capabilities.
443 $this->setUser($this->student);
444
445 $title = 'Submission title';
446 $content = 'Submission contents';
447
448 // Create a file in a draft area for inline attachments.
449 $draftidinlineattach = file_get_unused_draft_itemid();
450 $usercontext = context_user::instance($this->student->id);
451 $filenameimg = 'shouldbeanimage.txt';
452 $filerecordinline = array(
453 'contextid' => $usercontext->id,
454 'component' => 'user',
455 'filearea' => 'draft',
456 'itemid' => $draftidinlineattach,
457 'filepath' => '/',
458 'filename' => $filenameimg,
459 );
460 $fs->create_file_from_string($filerecordinline, 'image contents (not really)');
461
462 // Create a file in a draft area for regular attachments.
463 $draftidattach = file_get_unused_draft_itemid();
464 $filerecordattach = $filerecordinline;
465 $attachfilename = 'attachment.txt';
466 $filerecordattach['filename'] = $attachfilename;
467 $filerecordattach['itemid'] = $draftidattach;
468 $fs->create_file_from_string($filerecordattach, 'simple text attachment');
469
470 // Switch to submission phase.
471 $workshop = new workshop($this->workshop, $this->cm, $this->course);
472 $workshop->switch_phase(workshop::PHASE_SUBMISSION);
473
474 $result = mod_workshop_external::add_submission($this->workshop->id, $title, $content, FORMAT_MOODLE, $draftidinlineattach,
475 $draftidattach);
476 $result = external_api::clean_returnvalue(mod_workshop_external::add_submission_returns(), $result);
477 $this->assertEmpty($result['warnings']);
478
479 // Check submission created.
480 $submission = $workshop->get_submission_by_author($this->student->id);
4834e127 481 $this->assertTrue($result['status']);
c2cf2450
JL
482 $this->assertEquals($result['submissionid'], $submission->id);
483 $this->assertEquals($title, $submission->title);
484 $this->assertEquals($content, $submission->content);
485
486 // Check files.
487 $contentfiles = $fs->get_area_files($this->context->id, 'mod_workshop', 'submission_content', $submission->id);
488 $this->assertCount(2, $contentfiles);
489 foreach ($contentfiles as $file) {
490 if ($file->is_directory()) {
491 continue;
492 } else {
493 $this->assertEquals($filenameimg, $file->get_filename());
494 }
495 }
496 $contentfiles = $fs->get_area_files($this->context->id, 'mod_workshop', 'submission_attachment', $submission->id);
497 $this->assertCount(2, $contentfiles);
498 foreach ($contentfiles as $file) {
499 if ($file->is_directory()) {
500 continue;
501 } else {
502 $this->assertEquals($attachfilename, $file->get_filename());
503 }
504 }
505 }
506
507 /**
508 * Test test_add_submission invalid phase.
509 */
510 public function test_add_submission_invalid_phase() {
511 $this->setUser($this->student);
512
513 $this->expectException('moodle_exception');
514 mod_workshop_external::add_submission($this->workshop->id, 'Test');
515 }
516
517 /**
518 * Test test_add_submission empty title.
519 */
520 public function test_add_submission_empty_title() {
521 $this->setUser($this->student);
522
523 // Switch to submission phase.
524 $workshop = new workshop($this->workshop, $this->cm, $this->course);
525 $workshop->switch_phase(workshop::PHASE_SUBMISSION);
526
527 $this->expectException('moodle_exception');
528 mod_workshop_external::add_submission($this->workshop->id, '');
529 }
530
531 /**
532 * Test test_add_submission already added.
533 */
534 public function test_add_submission_already_added() {
535 $this->setUser($this->student);
536
537 // Switch to submission phase.
538 $workshop = new workshop($this->workshop, $this->cm, $this->course);
539 $workshop->switch_phase(workshop::PHASE_SUBMISSION);
540
541 // Create the submission.
542 $result = mod_workshop_external::add_submission($this->workshop->id, 'My submission');
543 $result = external_api::clean_returnvalue(mod_workshop_external::add_submission_returns(), $result);
544
545 // Try to create it again.
546 $result = mod_workshop_external::add_submission($this->workshop->id, 'My submission');
547 $result = external_api::clean_returnvalue(mod_workshop_external::add_submission_returns(), $result);
4834e127
JL
548 $this->assertFalse($result['status']);
549 $this->assertArrayNotHasKey('submissionid', $result);
c2cf2450
JL
550 $this->assertCount(2, $result['warnings']);
551 $this->assertEquals('fielderror', $result['warnings'][0]['warningcode']);
552 $this->assertEquals('content_editor', $result['warnings'][0]['item']);
553 $this->assertEquals('fielderror', $result['warnings'][1]['warningcode']);
554 $this->assertEquals('attachment_filemanager', $result['warnings'][1]['item']);
555 }
c1698a37
JL
556
557 /**
558 * Helper method to create a submission for testing for the given user.
559 *
560 * @param int $user the submission will be created by this student.
561 * @return int the submission id
562 */
563 protected function create_test_submission($user) {
564 // Test user with full capabilities.
565 $this->setUser($user);
566
567 $title = 'Submission title';
568 $content = 'Submission contents';
569
570 // Create a file in a draft area for inline attachments.
571 $fs = get_file_storage();
572 $draftidinlineattach = file_get_unused_draft_itemid();
573 $usercontext = context_user::instance($this->student->id);
574 $filenameimg = 'shouldbeanimage.txt';
575 $filerecordinline = array(
576 'contextid' => $usercontext->id,
577 'component' => 'user',
578 'filearea' => 'draft',
579 'itemid' => $draftidinlineattach,
580 'filepath' => '/',
581 'filename' => $filenameimg,
582 );
583 $fs->create_file_from_string($filerecordinline, 'image contents (not really)');
584
585 // Create a file in a draft area for regular attachments.
586 $draftidattach = file_get_unused_draft_itemid();
587 $filerecordattach = $filerecordinline;
588 $attachfilename = 'attachment.txt';
589 $filerecordattach['filename'] = $attachfilename;
590 $filerecordattach['itemid'] = $draftidattach;
591 $fs->create_file_from_string($filerecordattach, 'simple text attachment');
592
593 // Switch to submission phase.
594 $workshop = new workshop($this->workshop, $this->cm, $this->course);
595 $workshop->switch_phase(workshop::PHASE_SUBMISSION);
596
597 $result = mod_workshop_external::add_submission($this->workshop->id, $title, $content, FORMAT_MOODLE, $draftidinlineattach,
598 $draftidattach);
599 return $result['submissionid'];
600 }
601
602 /**
603 * Test test_update_submission.
604 */
605 public function test_update_submission() {
606
607 // Create the submission that will be updated.
608 $submissionid = $this->create_test_submission($this->student);
609
610 // Test user with full capabilities.
611 $this->setUser($this->student);
612
613 $title = 'Submission new title';
614 $content = 'Submission new contents';
615
616 // Create a different file in a draft area for inline attachments.
617 $fs = get_file_storage();
618 $draftidinlineattach = file_get_unused_draft_itemid();
619 $usercontext = context_user::instance($this->student->id);
620 $filenameimg = 'shouldbeanimage_new.txt';
621 $filerecordinline = array(
622 'contextid' => $usercontext->id,
623 'component' => 'user',
624 'filearea' => 'draft',
625 'itemid' => $draftidinlineattach,
626 'filepath' => '/',
627 'filename' => $filenameimg,
628 );
629 $fs->create_file_from_string($filerecordinline, 'image contents (not really)');
630
631 // Create a different file in a draft area for regular attachments.
632 $draftidattach = file_get_unused_draft_itemid();
633 $filerecordattach = $filerecordinline;
634 $attachfilename = 'attachment_new.txt';
635 $filerecordattach['filename'] = $attachfilename;
636 $filerecordattach['itemid'] = $draftidattach;
637 $fs->create_file_from_string($filerecordattach, 'simple text attachment');
638
639 $result = mod_workshop_external::update_submission($submissionid, $title, $content, FORMAT_MOODLE, $draftidinlineattach,
640 $draftidattach);
641 $result = external_api::clean_returnvalue(mod_workshop_external::update_submission_returns(), $result);
642 $this->assertEmpty($result['warnings']);
643
644 // Check submission updated.
645 $workshop = new workshop($this->workshop, $this->cm, $this->course);
646 $submission = $workshop->get_submission_by_id($submissionid);
647 $this->assertTrue($result['status']);
648 $this->assertEquals($title, $submission->title);
649 $this->assertEquals($content, $submission->content);
650
651 // Check files.
652 $contentfiles = $fs->get_area_files($this->context->id, 'mod_workshop', 'submission_content', $submission->id);
653 $this->assertCount(2, $contentfiles);
654 foreach ($contentfiles as $file) {
655 if ($file->is_directory()) {
656 continue;
657 } else {
658 $this->assertEquals($filenameimg, $file->get_filename());
659 }
660 }
661 $contentfiles = $fs->get_area_files($this->context->id, 'mod_workshop', 'submission_attachment', $submission->id);
662 $this->assertCount(2, $contentfiles);
663 foreach ($contentfiles as $file) {
664 if ($file->is_directory()) {
665 continue;
666 } else {
667 $this->assertEquals($attachfilename, $file->get_filename());
668 }
669 }
670 }
671
672 /**
673 * Test test_update_submission belonging to other user.
674 */
675 public function test_update_submission_of_other_user() {
676 // Create the submission that will be updated.
677 $submissionid = $this->create_test_submission($this->student);
678
679 $this->setUser($this->teacher);
680
681 $this->expectException('moodle_exception');
682 mod_workshop_external::update_submission($submissionid, 'Test');
683 }
684
685 /**
686 * Test test_update_submission invalid phase.
687 */
688 public function test_update_submission_invalid_phase() {
689 // Create the submission that will be updated.
690 $submissionid = $this->create_test_submission($this->student);
691
692 $this->setUser($this->student);
693
694 // Switch to assessment phase.
695 $workshop = new workshop($this->workshop, $this->cm, $this->course);
696 $workshop->switch_phase(workshop::PHASE_ASSESSMENT);
697
698 $this->expectException('moodle_exception');
699 mod_workshop_external::update_submission($submissionid, 'Test');
700 }
701
702 /**
703 * Test test_update_submission empty title.
704 */
705 public function test_update_submission_empty_title() {
706 // Create the submission that will be updated.
707 $submissionid = $this->create_test_submission($this->student);
708
709 $this->setUser($this->student);
710
711 $this->expectException('moodle_exception');
712 mod_workshop_external::update_submission($submissionid, '');
713 }
bde5631d
JL
714
715 /**
716 * Test test_delete_submission.
717 */
718 public function test_delete_submission() {
719
720 // Create the submission that will be deleted.
721 $submissionid = $this->create_test_submission($this->student);
722
723 $this->setUser($this->student);
724
725 // Trigger and capture the event.
726 $sink = $this->redirectEvents();
727
728 $result = mod_workshop_external::delete_submission($submissionid);
729 $result = external_api::clean_returnvalue(mod_workshop_external::delete_submission_returns(), $result);
730 $this->assertEmpty($result['warnings']);
731 $this->assertTrue($result['status']);
732 $workshop = new workshop($this->workshop, $this->cm, $this->course);
733 $submission = $workshop->get_submission_by_author($this->student->id);
734 $this->assertFalse($submission);
735
736 $events = $sink->get_events();
737 $this->assertCount(1, $events);
738 $event = array_shift($events);
739
740 // Checking event.
741 $this->assertInstanceOf('\mod_workshop\event\submission_deleted', $event);
742 $this->assertEquals($this->context, $event->get_context());
743 }
744
745 /**
746 * Test test_delete_submission_with_assessments.
747 */
748 public function test_delete_submission_with_assessments() {
749
750 // Create the submission that will be deleted.
751 $submissionid = $this->create_test_submission($this->student);
752
753 $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
754 $workshopgenerator->create_assessment($submissionid, $this->teacher->id, array(
755 'weight' => 3,
756 'grade' => 95.00000,
757 ));
758
759 $this->setUser($this->student);
760 $this->expectException('moodle_exception');
761 mod_workshop_external::delete_submission($submissionid);
762 }
763
764 /**
765 * Test test_delete_submission_invalid_phase.
766 */
767 public function test_delete_submission_invalid_phase() {
768
769 // Create the submission that will be deleted.
770 $submissionid = $this->create_test_submission($this->student);
771
772 // Switch to assessment phase.
773 $workshop = new workshop($this->workshop, $this->cm, $this->course);
774 $workshop->switch_phase(workshop::PHASE_ASSESSMENT);
775
776 $this->setUser($this->student);
777 $this->expectException('moodle_exception');
778 mod_workshop_external::delete_submission($submissionid);
779 }
780
781 /**
782 * Test test_delete_submission_as_teacher.
783 */
784 public function test_delete_submission_as_teacher() {
785
786 // Create the submission that will be deleted.
787 $submissionid = $this->create_test_submission($this->student);
788
789 $this->setUser($this->teacher);
790 $result = mod_workshop_external::delete_submission($submissionid);
791 $result = external_api::clean_returnvalue(mod_workshop_external::delete_submission_returns(), $result);
792 $this->assertEmpty($result['warnings']);
793 $this->assertTrue($result['status']);
794 }
795
796 /**
797 * Test test_delete_submission_other_user.
798 */
799 public function test_delete_submission_other_user() {
800
801 $anotheruser = self::getDataGenerator()->create_user();
802 $this->getDataGenerator()->enrol_user($anotheruser->id, $this->course->id, $this->studentrole->id, 'manual');
803 // Create the submission that will be deleted.
804 $submissionid = $this->create_test_submission($this->student);
805
806 $this->setUser($anotheruser);
807 $this->expectException('moodle_exception');
808 mod_workshop_external::delete_submission($submissionid);
809 }
3f08cfc5
JL
810
811 /**
812 * Test test_get_submissions_student.
813 */
814 public function test_get_submissions_student() {
815
816 // Create a couple of submissions with files.
817 $firstsubmissionid = $this->create_test_submission($this->student); // Create submission with files.
818 $secondsubmissionid = $this->create_test_submission($this->anotherstudentg1->id);
819
820 $this->setUser($this->student);
821 $result = mod_workshop_external::get_submissions($this->workshop->id);
822 $result = external_api::clean_returnvalue(mod_workshop_external::get_submissions_returns(), $result);
823 // We should get just our submission.
824 $this->assertCount(1, $result['submissions']);
825 $this->assertEquals(1, $result['totalcount']);
826 $this->assertEquals($firstsubmissionid, $result['submissions'][0]['id']);
827 $this->assertCount(1, $result['submissions'][0]['contentfiles']); // Check we retrieve submission text files.
828 $this->assertCount(1, $result['submissions'][0]['attachmentfiles']); // Check we retrieve attachment files.
829 // We shoul not see the grade or feedback information.
830 $properties = submission_exporter::properties_definition();
831 foreach ($properties as $attribute => $settings) {
832 if (!empty($settings['optional'])) {
833 if (isset($result['submissions'][0][$attribute])) {
834 echo "error $attribute";
835 }
836 $this->assertFalse(isset($result['submissions'][0][$attribute]));
837 }
838 }
839 }
840
841 /**
842 * Test test_get_submissions_published_student.
843 */
844 public function test_get_submissions_published_student() {
845 global $DB;
846
847 $DB->set_field('workshop', 'phase', workshop::PHASE_CLOSED, array('id' => $this->workshop->id));
848 // Create a couple of submissions with files.
849 $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
850 $submission = array('published' => 1);
851 $submissionid = $workshopgenerator->create_submission($this->workshop->id, $this->anotherstudentg1->id, $submission);
852
853 $this->setUser($this->student);
854 $result = mod_workshop_external::get_submissions($this->workshop->id);
855 $result = external_api::clean_returnvalue(mod_workshop_external::get_submissions_returns(), $result);
856 // We should get just our submission.
857 $this->assertCount(1, $result['submissions']);
858 $this->assertEquals(1, $result['totalcount']);
859 $this->assertEquals($submissionid, $result['submissions'][0]['id']);
860
861 // Check with group restrictions.
862 $this->setUser($this->anotherstudentg2);
863 $result = mod_workshop_external::get_submissions($this->workshop->id);
864 $result = external_api::clean_returnvalue(mod_workshop_external::get_submissions_returns(), $result);
865 $this->assertCount(0, $result['submissions']); // I can't see other users in separated groups.
866 $this->assertEquals(0, $result['totalcount']);
867 }
868
869 /**
870 * Test test_get_submissions_from_student_with_feedback_from_teacher.
871 */
872 public function test_get_submissions_from_student_with_feedback_from_teacher() {
873 global $DB;
874
875 // Create a couple of submissions with files.
876 $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
877 $submissionid = $workshopgenerator->create_submission($this->workshop->id, $this->student->id);
878 // Create teacher feedback for submission.
879 $record = new stdclass();
880 $record->id = $submissionid;
881 $record->gradeover = 9;
882 $record->gradeoverby = $this->teacher->id;
883 $record->feedbackauthor = 'Hey';
884 $record->feedbackauthorformat = FORMAT_MOODLE;
885 $record->published = 1;
886 $DB->update_record('workshop_submissions', $record);
887
888 // Remove teacher caps.
889 assign_capability('mod/workshop:viewallsubmissions', CAP_PROHIBIT, $this->teacher->id, $this->context->id);
890 // Empty all the caches that may be affected by this change.
891 accesslib_clear_all_caches_for_unit_testing();
892 course_modinfo::clear_instance_cache();
893
894 $this->setUser($this->teacher);
895 $result = mod_workshop_external::get_submissions($this->workshop->id, $this->student->id);
896 $result = external_api::clean_returnvalue(mod_workshop_external::get_submissions_returns(), $result);
897 // We should get just our submission.
898 $this->assertEquals(1, $result['totalcount']);
899 $this->assertEquals($submissionid, $result['submissions'][0]['id']);
900 }
901
902 /**
903 * Test test_get_submissions_from_students_as_teacher.
904 */
905 public function test_get_submissions_from_students_as_teacher() {
906 global $DB;
907
908 // Create a couple of submissions with files.
909 $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
910 $submissionid1 = $workshopgenerator->create_submission($this->workshop->id, $this->student->id);
911 $submissionid2 = $workshopgenerator->create_submission($this->workshop->id, $this->anotherstudentg1->id);
912 $submissionid3 = $workshopgenerator->create_submission($this->workshop->id, $this->anotherstudentg2->id);
913
914 $this->setUser($this->teacher);
915 $result = mod_workshop_external::get_submissions($this->workshop->id); // Get all.
916 $result = external_api::clean_returnvalue(mod_workshop_external::get_submissions_returns(), $result);
917 $this->assertEquals(3, $result['totalcount']);
918 $this->assertCount(3, $result['submissions']);
919
920 $result = mod_workshop_external::get_submissions($this->workshop->id, 0, 0, 0, 2); // Check pagination.
921 $result = external_api::clean_returnvalue(mod_workshop_external::get_submissions_returns(), $result);
922 $this->assertEquals(3, $result['totalcount']);
923 $this->assertCount(2, $result['submissions']);
924
925 $result = mod_workshop_external::get_submissions($this->workshop->id, 0, $this->group2->id); // Get group 2.
926 $result = external_api::clean_returnvalue(mod_workshop_external::get_submissions_returns(), $result);
927 $this->assertEquals(1, $result['totalcount']);
928 $this->assertCount(1, $result['submissions']);
929 $this->assertEquals($submissionid3, $result['submissions'][0]['id']);
930
931 $result = mod_workshop_external::get_submissions($this->workshop->id, $this->anotherstudentg1->id); // Get one.
932 $result = external_api::clean_returnvalue(mod_workshop_external::get_submissions_returns(), $result);
933 $this->assertEquals(1, $result['totalcount']);
934 $this->assertEquals($submissionid2, $result['submissions'][0]['id']);
935 }
6e2f4866
JL
936
937 /**
938 * Test test_get_submission_student.
939 */
940 public function test_get_submission_student() {
941
942 // Create a couple of submissions with files.
943 $firstsubmissionid = $this->create_test_submission($this->student); // Create submission with files.
944
945 $this->setUser($this->student);
946 $result = mod_workshop_external::get_submission($firstsubmissionid);
947 $result = external_api::clean_returnvalue(mod_workshop_external::get_submission_returns(), $result);
948 $this->assertEquals($firstsubmissionid, $result['submission']['id']);
949 $this->assertCount(1, $result['submission']['contentfiles']); // Check we retrieve submission text files.
950 $this->assertCount(1, $result['submission']['attachmentfiles']); // Check we retrieve attachment files.
951 }
952
953 /**
954 * Test test_get_submission_i_reviewed.
955 */
956 public function test_get_submission_i_reviewed() {
957
958 // Create a couple of submissions with files.
959 $firstsubmissionid = $this->create_test_submission($this->student); // Create submission with files.
960 $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
961 $workshopgenerator->create_assessment($firstsubmissionid, $this->anotherstudentg1->id, array(
962 'weight' => 3,
963 'grade' => 95,
964 ));
965 // Now try to get the submission I just reviewed.
966 $this->setUser($this->anotherstudentg1);
967 $result = mod_workshop_external::get_submission($firstsubmissionid);
968 $result = external_api::clean_returnvalue(mod_workshop_external::get_submission_returns(), $result);
969 $this->assertEquals($firstsubmissionid, $result['submission']['id']);
970 $this->assertCount(1, $result['submission']['contentfiles']); // Check we retrieve submission text files.
971 $this->assertCount(1, $result['submission']['attachmentfiles']); // Check we retrieve attachment files.
972 }
973
974 /**
975 * Test test_get_submission_other_student.
976 */
977 public function test_get_submission_other_student() {
978
979 // Create a couple of submissions with files.
980 $firstsubmissionid = $this->create_test_submission($this->student); // Create submission with files.
981 // Expect failure.
982 $this->setUser($this->anotherstudentg1);
983 $this->expectException('moodle_exception');
984 $result = mod_workshop_external::get_submission($firstsubmissionid);
985 }
986
987 /**
988 * Test test_get_submission_published_student.
989 */
990 public function test_get_submission_published_student() {
991 global $DB;
992
993 $DB->set_field('workshop', 'phase', workshop::PHASE_CLOSED, array('id' => $this->workshop->id));
994 // Create a couple of submissions with files.
995 $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
996 $submission = array('published' => 1);
997 $submissionid = $workshopgenerator->create_submission($this->workshop->id, $this->anotherstudentg1->id, $submission);
998
999 $this->setUser($this->student);
1000 $result = mod_workshop_external::get_submission($submissionid);
1001 $result = external_api::clean_returnvalue(mod_workshop_external::get_submission_returns(), $result);
1002 $this->assertEquals($submissionid, $result['submission']['id']);
1003 // Check that the student don't see the other student grade/feedback data even if is published.
1004 // We shoul not see the grade or feedback information.
1005 $properties = submission_exporter::properties_definition();
1006 foreach ($properties as $attribute => $settings) {
1007 if (!empty($settings['optional'])) {
1008 if (isset($result['submission'][$attribute])) {
1009 echo "error $attribute";
1010 }
1011 $this->assertFalse(isset($result['submission'][$attribute]));
1012 }
1013 }
1014
1015 // Check with group restrictions.
1016 $this->setUser($this->anotherstudentg2);
1017 $this->expectException('moodle_exception');
1018 mod_workshop_external::get_submission($submissionid);
1019 }
1020
1021 /**
1022 * Test test_get_submission_from_student_with_feedback_from_teacher.
1023 */
1024 public function test_get_submission_from_student_with_feedback_from_teacher() {
1025 global $DB;
1026
1027 // Create a couple of submissions with files.
1028 $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
1029 $submissionid = $workshopgenerator->create_submission($this->workshop->id, $this->student->id);
1030 // Create teacher feedback for submission.
1031 $record = new stdclass();
1032 $record->id = $submissionid;
1033 $record->gradeover = 9;
1034 $record->gradeoverby = $this->teacher->id;
1035 $record->feedbackauthor = 'Hey';
1036 $record->feedbackauthorformat = FORMAT_MOODLE;
1037 $record->published = 1;
1038 $DB->update_record('workshop_submissions', $record);
1039
1040 // Remove teacher caps.
1041 assign_capability('mod/workshop:viewallsubmissions', CAP_PROHIBIT, $this->teacher->id, $this->context->id);
1042 // Empty all the caches that may be affected by this change.
1043 accesslib_clear_all_caches_for_unit_testing();
1044 course_modinfo::clear_instance_cache();
1045
1046 $this->setUser($this->teacher);
1047 $result = mod_workshop_external::get_submission($submissionid);
1048 $result = external_api::clean_returnvalue(mod_workshop_external::get_submission_returns(), $result);
1049 $this->assertEquals($submissionid, $result['submission']['id']);
1050 }
1051
1052 /**
1053 * Test test_get_submission_from_students_as_teacher.
1054 */
1055 public function test_get_submission_from_students_as_teacher() {
1056 // Create a couple of submissions with files.
1057 $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
1058 $submissionid1 = $workshopgenerator->create_submission($this->workshop->id, $this->student->id);
1059 $submissionid2 = $workshopgenerator->create_submission($this->workshop->id, $this->anotherstudentg1->id);
1060 $submissionid3 = $workshopgenerator->create_submission($this->workshop->id, $this->anotherstudentg2->id);
1061
1062 $this->setUser($this->teacher);
1063 $result = mod_workshop_external::get_submission($submissionid1); // Get all.
1064 $result = external_api::clean_returnvalue(mod_workshop_external::get_submission_returns(), $result);
1065 $this->assertEquals($submissionid1, $result['submission']['id']);
1066
1067 $result = mod_workshop_external::get_submission($submissionid3); // Get group 2.
1068 $result = external_api::clean_returnvalue(mod_workshop_external::get_submission_returns(), $result);
1069 $this->assertEquals($submissionid3, $result['submission']['id']);
1070 }
30b54b82
JL
1071
1072
1073 /**
1074 * Test get_submission_assessments_student.
1075 */
1076 public function test_get_submission_assessments_student() {
1077 global $DB;
1078
1079 // Create the submission that will be deleted.
1080 $submissionid = $this->create_test_submission($this->student);
1081
1082 $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
1083 $workshopgenerator->create_assessment($submissionid, $this->anotherstudentg1->id, array(
1084 'weight' => 3,
1085 'grade' => 95,
1086 ));
1087 $workshopgenerator->create_assessment($submissionid, $this->student->id, array(
1088 'weight' => 2,
1089 'grade' => 90,
1090 ));
1091
1092 $DB->set_field('workshop', 'phase', workshop::PHASE_CLOSED, array('id' => $this->workshop->id));
1093 $this->setUser($this->student);
1094 $result = mod_workshop_external::get_submission_assessments($submissionid);
1095 $result = external_api::clean_returnvalue(mod_workshop_external::get_submission_assessments_returns(), $result);
1096 $this->assertCount(2, $result['assessments']); // I received my two assessments.
1097 foreach ($result['assessments'] as $assessment) {
1098 if ($assessment['grade'] == 90) {
1099 // My own assessment, I can see me.
1100 $this->assertEquals($this->student->id, $assessment['reviewerid']);
1101 } else {
1102 // Student's can't see who did the review.
1103 $this->assertEquals(0, $assessment['reviewerid']);
1104 }
1105 }
1106 }
1107
1108 /**
1109 * Test get_submission_assessments_invalid_phase.
1110 */
1111 public function test_get_submission_assessments_invalid_phase() {
1112 global $DB;
1113
1114 // Create the submission that will be deleted.
1115 $submissionid = $this->create_test_submission($this->student);
1116
1117 $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
1118 $workshopgenerator->create_assessment($submissionid, $this->anotherstudentg1->id, array(
1119 'weight' => 3,
1120 'grade' => 95,
1121 ));
1122
1123 $this->expectException('moodle_exception');
1124 mod_workshop_external::get_submission_assessments($submissionid);
1125 }
1126
1127 /**
1128 * Test get_submission_assessments_teacher.
1129 */
1130 public function test_get_submission_assessments_teacher() {
1131
1132 // Create the submission that will be deleted.
1133 $submissionid = $this->create_test_submission($this->student);
1134
1135 $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
1136 $assessmentid = $workshopgenerator->create_assessment($submissionid, $this->anotherstudentg1->id, array(
1137 'weight' => 1,
1138 'grade' => 50,
1139 ));
1140
1141 $this->setUser($this->teacher);
1142 $result = mod_workshop_external::get_submission_assessments($submissionid);
1143 $result = external_api::clean_returnvalue(mod_workshop_external::get_submission_assessments_returns(), $result);
1144 $this->assertCount(1, $result['assessments']);
1145 $this->assertEquals(50, $result['assessments'][0]['grade']);
1146 $this->assertEquals($assessmentid, $result['assessments'][0]['id']);
1147 }
b1e896cf
JL
1148
1149 /**
1150 * Test get_assessment_author.
1151 */
1152 public function test_get_assessment_author() {
1153 global $DB;
1154
1155 // Create the submission.
1156 $submissionid = $this->create_test_submission($this->anotherstudentg1);
1157
1158 $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
1159 $assessmentid = $workshopgenerator->create_assessment($submissionid, $this->student->id, array(
1160 'weight' => 2,
1161 'grade' => 90,
1162 ));
1163
1164 // Switch to closed phase.
1165 $DB->set_field('workshop', 'phase', workshop::PHASE_CLOSED, array('id' => $this->workshop->id));
1166 $this->setUser($this->anotherstudentg1);
1167 $result = mod_workshop_external::get_assessment($assessmentid);
1168 $result = external_api::clean_returnvalue(mod_workshop_external::get_assessment_returns(), $result);
1169 $this->assertEquals($assessmentid, $result['assessment']['id']);
1170 $this->assertEquals(90, $result['assessment']['grade']);
1171 // I can't see the reviewer review.
1172 $this->assertFalse(isset($result['assessment']['feedbackreviewer']));
1173 }
1174
1175 /**
1176 * Test get_assessment_reviewer.
1177 */
1178 public function test_get_assessment_reviewer() {
1179 global $DB;
1180
1181 // Create the submission.
1182 $submissionid = $this->create_test_submission($this->anotherstudentg1);
1183
1184 $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
1185 $assessmentid = $workshopgenerator->create_assessment($submissionid, $this->student->id, array(
1186 'weight' => 2,
1187 'grade' => 90,
1188 ));
1189
1190 // Switch to closed phase.
1191 $DB->set_field('workshop', 'phase', workshop::PHASE_CLOSED, array('id' => $this->workshop->id));
1192 $this->setUser($this->student);
1193 $result = mod_workshop_external::get_assessment($assessmentid);
1194 $result = external_api::clean_returnvalue(mod_workshop_external::get_assessment_returns(), $result);
1195 $this->assertEquals($assessmentid, $result['assessment']['id']);
1196 $this->assertEquals(90, $result['assessment']['grade']);
1197 // I can see the reviewer review.
1198 $this->assertTrue(isset($result['assessment']['feedbackreviewer']));
1199 }
1200
1201 /**
1202 * Test get_assessment_teacher.
1203 */
1204 public function test_get_assessment_teacher() {
1205 global $DB;
1206
1207 // Create the submission.
1208 $submissionid = $this->create_test_submission($this->anotherstudentg1);
1209
1210 $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
1211 $assessmentid = $workshopgenerator->create_assessment($submissionid, $this->student->id, array(
1212 'weight' => 2,
1213 'grade' => 90,
1214 ));
1215
1216 // Switch to closed phase.
1217 $DB->set_field('workshop', 'phase', workshop::PHASE_CLOSED, array('id' => $this->workshop->id));
1218 $this->setUser($this->teacher);
1219 $result = mod_workshop_external::get_assessment($assessmentid);
1220 $result = external_api::clean_returnvalue(mod_workshop_external::get_assessment_returns(), $result);
1221 $this->assertEquals($assessmentid, $result['assessment']['id']);
1222 $this->assertEquals(90, $result['assessment']['grade']);
1223 }
1224
1225 /**
1226 * Test get_assessment_student_invalid_phase.
1227 */
1228 public function test_get_assessment_student_invalid_phase() {
1229 global $DB;
1230
1231 // Create the submission.
1232 $submissionid = $this->create_test_submission($this->anotherstudentg1);
1233
1234 $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
1235 $assessmentid = $workshopgenerator->create_assessment($submissionid, $this->student->id, array(
1236 'weight' => 2,
1237 'grade' => 90,
1238 ));
1239
1240 // Switch to closed phase.
1241 $this->setUser($this->anotherstudentg1);
1242
1243 $this->setExpectedException('moodle_exception');
1244 mod_workshop_external::get_assessment($assessmentid);
1245 }
1246
1247 /**
1248 * Test get_assessment_student_invalid_user.
1249 */
1250 public function test_get_assessment_student_invalid_user() {
1251 global $DB;
1252
1253 // Create the submission.
1254 $submissionid = $this->create_test_submission($this->anotherstudentg1);
1255
1256 $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
1257 $assessmentid = $workshopgenerator->create_assessment($submissionid, $this->student->id, array(
1258 'weight' => 2,
1259 'grade' => 90,
1260 ));
1261
1262 // Switch to closed phase.
1263 $DB->set_field('workshop', 'phase', workshop::PHASE_CLOSED, array('id' => $this->workshop->id));
1264 $this->setUser($this->anotherstudentg2);
1265
1266 $this->setExpectedException('moodle_exception');
1267 mod_workshop_external::get_assessment($assessmentid);
1268 }
9f1ab2db 1269}