Merge branch 'MDL-42625_master-failures-fix' of git://github.com/dmonllao/moodle
[moodle.git] / mod / assign / tests / locallib_test.php
CommitLineData
47f48152
DW
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 tests for (some of) mod/assign/locallib.php.
19 *
20 * @package mod_assign
21 * @category phpunit
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30require_once($CFG->dirroot . '/mod/assign/locallib.php');
31require_once($CFG->dirroot . '/mod/assign/upgradelib.php');
9c986ee0 32require_once($CFG->dirroot . '/mod/assign/tests/base_test.php');
47f48152
DW
33
34/**
35 * Unit tests for (some of) mod/assign/locallib.php.
36 *
37 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
9c986ee0 40class mod_assign_locallib_testcase extends mod_assign_base_testcase {
47f48152
DW
41
42 public function test_return_links() {
c2114099 43 global $PAGE;
47f48152
DW
44 $this->setUser($this->editingteachers[0]);
45 $returnaction = 'RETURNACTION';
c2114099 46 $returnparams = array('param'=>'1');
47f48152 47 $assign = $this->create_instance();
c2114099 48 $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
47f48152
DW
49 $assign->register_return_link($returnaction, $returnparams);
50 $this->assertEquals($returnaction, $assign->get_return_action());
51 $this->assertEquals($returnparams, $assign->get_return_params());
52 }
53
54 public function test_get_feedback_plugins() {
55 $this->setUser($this->editingteachers[0]);
56 $assign = $this->create_instance();
bd3b3bba 57 $installedplugins = array_keys(core_component::get_plugin_list('assignfeedback'));
11527706
DW
58
59 foreach ($assign->get_feedback_plugins() as $plugin) {
60 $this->assertContains($plugin->get_type(), $installedplugins, 'Feedback plugin not in list of installed plugins');
61 }
47f48152
DW
62 }
63
64 public function test_get_submission_plugins() {
65 $this->setUser($this->editingteachers[0]);
66 $assign = $this->create_instance();
bd3b3bba 67 $installedplugins = array_keys(core_component::get_plugin_list('assignsubmission'));
11527706
DW
68
69 foreach ($assign->get_submission_plugins() as $plugin) {
70 $this->assertContains($plugin->get_type(), $installedplugins, 'Submission plugin not in list of installed plugins');
71 }
47f48152
DW
72 }
73
74 public function test_is_blind_marking() {
75 $this->setUser($this->editingteachers[0]);
76 $assign = $this->create_instance(array('blindmarking'=>1));
77 $this->assertEquals(true, $assign->is_blind_marking());
78
79 // Test cannot see student names.
80 $gradingtable = new assign_grading_table($assign, 1, '', 0, true);
81 $output = $assign->get_renderer()->render($gradingtable);
82 $this->assertEquals(true, strpos($output, get_string('hiddenuser', 'assign')));
83
84 // Test students cannot reveal identities.
85 $nopermission = false;
05a6445a 86 $this->students[0]->ignoresesskey = true;
47f48152
DW
87 $this->setUser($this->students[0]);
88 $this->setExpectedException('required_capability_exception');
05a6445a
DW
89 $assign->reveal_identities();
90 $this->students[0]->ignoresesskey = false;
47f48152
DW
91
92 // Test teachers cannot reveal identities.
93 $nopermission = false;
05a6445a 94 $this->teachers[0]->ignoresesskey = true;
47f48152
DW
95 $this->setUser($this->teachers[0]);
96 $this->setExpectedException('required_capability_exception');
05a6445a
DW
97 $assign->reveal_identities();
98 $this->teachers[0]->ignoresesskey = false;
47f48152
DW
99
100 // Test sesskey is required.
47f48152
DW
101 $this->setUser($this->editingteachers[0]);
102 $this->setExpectedException('moodle_exception');
05a6445a 103 $assign->reveal_identities();
47f48152
DW
104
105 // Test editingteacher can reveal identities if sesskey is ignored.
106 $this->editingteachers[0]->ignoresesskey = true;
107 $this->setUser($this->editingteachers[0]);
05a6445a 108 $assign->reveal_identities();
47f48152 109 $this->assertEquals(false, $assign->is_blind_marking());
05a6445a 110 $this->editingteachers[0]->ignoresesskey = false;
47f48152
DW
111
112 // Test student names are visible.
113 $gradingtable = new assign_grading_table($assign, 1, '', 0, true);
114 $output = $assign->get_renderer()->render($gradingtable);
115 $this->assertEquals(false, strpos($output, get_string('hiddenuser', 'assign')));
116
117 // Set this back to default.
118 $this->editingteachers[0]->ignoresesskey = false;
119 }
120
121 public function test_show_intro() {
122 // Test whether we are showing the intro at the correct times.
123 $this->setUser($this->editingteachers[0]);
124 $assign = $this->create_instance(array('alwaysshowdescription'=>1));
125
126 $this->assertEquals(true, $assign->testable_show_intro());
127
128 $tomorrow = time() + (24*60*60);
129
130 $assign = $this->create_instance(array('alwaysshowdescription'=>0,
131 'allowsubmissionsfromdate'=>$tomorrow));
132 $this->assertEquals(false, $assign->testable_show_intro());
133 $yesterday = time() - (24*60*60);
134 $assign = $this->create_instance(array('alwaysshowdescription'=>0,
135 'allowsubmissionsfromdate'=>$yesterday));
136 $this->assertEquals(true, $assign->testable_show_intro());
137 }
138
139 public function test_has_submissions_or_grades() {
140 $this->setUser($this->editingteachers[0]);
141 $assign = $this->create_instance(array('assignsubmission_onlinetext_enabled'=>1));
142
143 $instance = $assign->get_instance();
144
145 // Should start empty.
146 $this->assertEquals(false, $assign->has_submissions_or_grades());
147
148 // Simulate a submission.
149 $this->setUser($this->students[0]);
150 $submission = $assign->get_user_submission($this->students[0]->id, true);
151 $data = new stdClass();
152 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
153 'text'=>'Submission text',
154 'format'=>FORMAT_MOODLE);
155 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
156 $plugin->save($submission, $data);
157
158 // Now test again.
159 $this->assertEquals(true, $assign->has_submissions_or_grades());
160 // Set this back to default.
161 $this->students[0]->ignoresesskey = false;
162 }
163
164 public function test_delete_grades() {
165 $this->setUser($this->editingteachers[0]);
166 $assign = $this->create_instance();
167
168 // Simulate adding a grade.
169 $this->setUser($this->teachers[0]);
170 $data = new stdClass();
171 $data->grade = '50.0';
df211804 172 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
47f48152
DW
173
174 // Now see if the data is in the gradebook.
175 $gradinginfo = grade_get_grades($this->course->id,
176 'mod',
177 'assign',
178 $assign->get_instance()->id);
179
180 $this->assertNotEquals(0, count($gradinginfo->items));
181
182 $assign->testable_delete_grades();
183 $gradinginfo = grade_get_grades($this->course->id,
184 'mod',
185 'assign',
186 $assign->get_instance()->id);
187
188 $this->assertEquals(0, count($gradinginfo->items));
189 }
190
191 public function test_delete_instance() {
192 $this->setUser($this->editingteachers[0]);
193 $assign = $this->create_instance(array('assignsubmission_onlinetext_enabled'=>1));
194
195 // Simulate adding a grade.
196 $this->setUser($this->teachers[0]);
197 $data = new stdClass();
198 $data->grade = '50.0';
df211804 199 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
47f48152
DW
200
201 // Simulate a submission.
202 $this->setUser($this->students[0]);
203 $submission = $assign->get_user_submission($this->students[0]->id, true);
204 $data = new stdClass();
205 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
206 'text'=>'Submission text',
207 'format'=>FORMAT_MOODLE);
208 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
209 $plugin->save($submission, $data);
210
211 // Now try and delete.
212 $this->assertEquals(true, $assign->delete_instance());
213 }
214
215 public function test_reset_userdata() {
216 global $DB;
217
218 $now = time();
219 $this->setUser($this->editingteachers[0]);
220 $assign = $this->create_instance(array('assignsubmission_onlinetext_enabled'=>1,
221 'duedate'=>$now));
222
223 // Simulate adding a grade.
224 $this->setUser($this->teachers[0]);
225 $data = new stdClass();
226 $data->grade = '50.0';
df211804 227 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
47f48152
DW
228
229 // Simulate a submission.
230 $this->setUser($this->students[0]);
231 $submission = $assign->get_user_submission($this->students[0]->id, true);
232 $data = new stdClass();
233 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
234 'text'=>'Submission text',
235 'format'=>FORMAT_MOODLE);
236 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
237 $plugin->save($submission, $data);
238
239 $this->assertEquals(true, $assign->has_submissions_or_grades());
240 // Now try and reset.
241 $data = new stdClass();
242 $data->reset_assign_submissions = 1;
243 $data->reset_gradebook_grades = 1;
244 $data->courseid = $this->course->id;
245 $data->timeshift = 24*60*60;
246 $this->setUser($this->editingteachers[0]);
247 $assign->reset_userdata($data);
248 $this->assertEquals(false, $assign->has_submissions_or_grades());
249
250 // Reload the instance data.
251 $instance = $DB->get_record('assign', array('id'=>$assign->get_instance()->id));
252 $this->assertEquals($now + 24*60*60, $instance->duedate);
e63515ba
RT
253
254 // Test reset using assign_reset_userdata().
255 $assignduedate = $instance->duedate; // Keep old updated value for comparison.
256 $data->timeshift = 2*24*60*60;
257 assign_reset_userdata($data);
258 $instance = $DB->get_record('assign', array('id' => $assign->get_instance()->id));
259 $this->assertEquals($assignduedate + 2*24*60*60, $instance->duedate);
260
261 // Create one more assignment and reset, make sure time shifted for previous assignment is not changed.
262 $assign2 = $this->create_instance(array('assignsubmission_onlinetext_enabled' => 1,
263 'duedate' => $now));
264 $assignduedate = $instance->duedate;
265 $data->timeshift = 3*24*60*60;
266 $assign2->reset_userdata($data);
267 $instance = $DB->get_record('assign', array('id' => $assign->get_instance()->id));
268 $this->assertEquals($assignduedate, $instance->duedate);
269 $instance2 = $DB->get_record('assign', array('id' => $assign2->get_instance()->id));
270 $this->assertEquals($now + 3*24*60*60, $instance2->duedate);
271
272 // Reset both assignments using assign_reset_userdata() and make sure both assignments have same date.
273 $assignduedate = $instance->duedate;
274 $assign2duedate = $instance2->duedate;
275 $data->timeshift = 4*24*60*60;
276 assign_reset_userdata($data);
277 $instance = $DB->get_record('assign', array('id' => $assign->get_instance()->id));
278 $this->assertEquals($assignduedate + 4*24*60*60, $instance->duedate);
279 $instance2 = $DB->get_record('assign', array('id' => $assign2->get_instance()->id));
280 $this->assertEquals($assign2duedate + 4*24*60*60, $instance2->duedate);
47f48152
DW
281 }
282
283 public function test_plugin_settings() {
284 global $DB;
285
286 $now = time();
287 $this->setUser($this->editingteachers[0]);
288 $assign = $this->create_instance(array('assignsubmission_file_enabled'=>1,
289 'assignsubmission_file_maxfiles'=>12,
290 'assignsubmission_file_maxsizebytes'=>10));
291
292 $plugin = $assign->get_submission_plugin_by_type('file');
293 $this->assertEquals('12', $plugin->get_config('maxfilesubmissions'));
294 }
295
296 public function test_update_calendar() {
297 global $DB;
298
299 $now = time();
300 $this->setUser($this->editingteachers[0]);
301 $assign = $this->create_instance(array('duedate'=>$now));
302
303 // See if there is an event in the calendar.
304 $params = array('modulename'=>'assign', 'instance'=>$assign->get_instance()->id);
305 $id = $DB->get_field('event', 'id', $params);
306
307 $this->assertEquals(false, empty($id));
308 }
309
310 public function test_update_instance() {
311 global $DB;
312
313 $this->setUser($this->editingteachers[0]);
314 $assign = $this->create_instance(array('assignsubmission_onlinetext_enabled'=>1));
315
316 $now = time();
317 $instance = $assign->get_instance();
318 $instance->duedate = $now;
319 $instance->instance = $instance->id;
320 $instance->assignsubmission_onlinetext_enabled = 1;
47f48152
DW
321
322 $assign->update_instance($instance);
323
324 $instance = $DB->get_record('assign', array('id'=>$assign->get_instance()->id));
325 $this->assertEquals($now, $instance->duedate);
326 }
327
8fd00a24
DW
328 public function test_cannot_submit_empty() {
329 global $PAGE;
330
331 $this->setUser($this->editingteachers[0]);
332 $assign = $this->create_instance(array('submissiondrafts'=>1));
333
334 $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
335
336 // Test you cannot see the submit button for an offline assignment regardless.
337 $this->setUser($this->students[0]);
338 $output = $assign->view_student_summary($this->students[0], true);
339 $this->assertNotContains(get_string('submitassignment', 'assign'), $output, 'Can submit empty offline assignment');
340
341 // Test you cannot see the submit button for an online text assignment with no submission.
342 $this->setUser($this->editingteachers[0]);
343 $instance = $assign->get_instance();
344 $instance->instance = $instance->id;
345 $instance->assignsubmission_onlinetext_enabled = 1;
346
347 $assign->update_instance($instance);
348 $this->setUser($this->students[0]);
349 $output = $assign->view_student_summary($this->students[0], true);
350 $this->assertNotContains(get_string('submitassignment', 'assign'), $output, 'Cannot submit empty onlinetext assignment');
351
352 // Simulate a submission.
353 $submission = $assign->get_user_submission($this->students[0]->id, true);
354 $data = new stdClass();
355 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
356 'text'=>'Submission text',
357 'format'=>FORMAT_MOODLE);
358 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
359 $plugin->save($submission, $data);
360 // Test you can see the submit button for an online text assignment with a submission.
361 $output = $assign->view_student_summary($this->students[0], true);
362 $this->assertContains(get_string('submitassignment', 'assign'), $output, 'Can submit non empty onlinetext assignment');
363 }
364
47f48152 365 public function test_list_participants() {
df211804 366 $this->create_extra_users();
47f48152
DW
367 $this->setUser($this->editingteachers[0]);
368 $assign = $this->create_instance(array('grade'=>100));
369
a5c793c3 370 $this->assertEquals(self::DEFAULT_STUDENT_COUNT + self::EXTRA_STUDENT_COUNT, count($assign->list_participants(null, true)));
e6cc5347
RT
371
372 // Teacher with user preference set should see suspended users as well.
373 set_user_preference('grade_report_showonlyactiveenrol', false);
374 $assign = $this->create_instance(array('grade'=>100));
375 $this->assertEquals(self::DEFAULT_STUDENT_COUNT + self::EXTRA_STUDENT_COUNT + self::EXTRA_SUSPENDED_COUNT,
376 count($assign->list_participants(null, true)));
377
1ecb8044 378 // Non-editing teacher should not see suspended users, even if user preference is set.
e6cc5347
RT
379 $this->setUser($this->teachers[0]);
380 set_user_preference('grade_report_showonlyactiveenrol', false);
381 $assign = $this->create_instance(array('grade'=>100));
382 $this->assertEquals(self::DEFAULT_STUDENT_COUNT + self::EXTRA_STUDENT_COUNT, count($assign->list_participants(null, true)));
47f48152
DW
383 }
384
385 public function test_count_teams() {
df211804 386 $this->create_extra_users();
47f48152
DW
387 $this->setUser($this->editingteachers[0]);
388 $assign = $this->create_instance(array('teamsubmission'=>1));
389
a5c793c3 390 $this->assertEquals(self::GROUP_COUNT + 1, $assign->count_teams());
47f48152
DW
391 }
392
393 public function test_count_submissions() {
df211804 394 $this->create_extra_users();
47f48152
DW
395 $this->setUser($this->editingteachers[0]);
396 $assign = $this->create_instance(array('assignsubmission_onlinetext_enabled'=>1));
397
398 // Simulate a submission.
a5c793c3
DW
399 $this->setUser($this->extrastudents[0]);
400 $submission = $assign->get_user_submission($this->extrastudents[0]->id, true);
47f48152
DW
401 // Leave this one as DRAFT.
402 $data = new stdClass();
403 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
404 'text'=>'Submission text',
405 'format'=>FORMAT_MOODLE);
406 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
407 $plugin->save($submission, $data);
408
409 // Simulate adding a grade.
410 $this->setUser($this->teachers[0]);
411 $data = new stdClass();
412 $data->grade = '50.0';
df211804 413 $assign->testable_apply_grade_to_user($data, $this->extrastudents[0]->id, 0);
47f48152
DW
414
415 // Simulate a submission.
a5c793c3
DW
416 $this->setUser($this->extrastudents[1]);
417 $submission = $assign->get_user_submission($this->extrastudents[1]->id, true);
47f48152 418 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
a5c793c3 419 $assign->testable_update_submission($submission, $this->extrastudents[1]->id, true, false);
47f48152
DW
420 $data = new stdClass();
421 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
422 'text'=>'Submission text',
423 'format'=>FORMAT_MOODLE);
424 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
425 $plugin->save($submission, $data);
426
427 // Simulate a submission.
a5c793c3
DW
428 $this->setUser($this->extrastudents[2]);
429 $submission = $assign->get_user_submission($this->extrastudents[2]->id, true);
47f48152 430 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
a5c793c3 431 $assign->testable_update_submission($submission, $this->extrastudents[2]->id, true, false);
47f48152
DW
432 $data = new stdClass();
433 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
434 'text'=>'Submission text',
435 'format'=>FORMAT_MOODLE);
436 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
437 $plugin->save($submission, $data);
438
439 // Simulate a submission.
a5c793c3
DW
440 $this->setUser($this->extrastudents[3]);
441 $submission = $assign->get_user_submission($this->extrastudents[3]->id, true);
47f48152 442 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
a5c793c3 443 $assign->testable_update_submission($submission, $this->extrastudents[3]->id, true, false);
47f48152
DW
444 $data = new stdClass();
445 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
446 'text'=>'Submission text',
447 'format'=>FORMAT_MOODLE);
448 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
449 $plugin->save($submission, $data);
450
e6cc5347
RT
451 // Simulate a submission for suspended user, this will never be counted.
452 $this->setUser($this->extrastudents[3]);
453 $submission = $assign->get_user_submission($this->extrasuspendedstudents[0]->id, true);
454 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
455 $assign->testable_update_submission($submission, $this->extrasuspendedstudents[0]->id, true, false);
456 $data = new stdClass();
457 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
458 'text'=>'Submission text',
459 'format'=>FORMAT_MOODLE);
460 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
461 $plugin->save($submission, $data);
462
47f48152
DW
463 // Simulate adding a grade.
464 $this->setUser($this->teachers[0]);
465 $data = new stdClass();
466 $data->grade = '50.0';
df211804 467 $assign->testable_apply_grade_to_user($data, $this->extrastudents[3]->id, 0);
e6cc5347 468 $assign->testable_apply_grade_to_user($data, $this->extrasuspendedstudents[0]->id, 0);
47f48152
DW
469
470 $this->assertEquals(2, $assign->count_grades());
471 $this->assertEquals(4, $assign->count_submissions());
472 $this->assertEquals(2, $assign->count_submissions_need_grading());
473 $this->assertEquals(3, $assign->count_submissions_with_status(ASSIGN_SUBMISSION_STATUS_SUBMITTED));
474 $this->assertEquals(1, $assign->count_submissions_with_status(ASSIGN_SUBMISSION_STATUS_DRAFT));
475 }
476
477 public function test_get_grading_userid_list() {
df211804 478 $this->create_extra_users();
47f48152
DW
479 $this->setUser($this->editingteachers[0]);
480 $assign = $this->create_instance();
481
482 $users = $assign->testable_get_grading_userid_list();
a5c793c3 483 $this->assertEquals(self::DEFAULT_STUDENT_COUNT + self::EXTRA_STUDENT_COUNT, count($users));
e6cc5347
RT
484
485 $this->setUser($this->editingteachers[0]);
486 set_user_preference('grade_report_showonlyactiveenrol', false);
487 $assign = $this->create_instance();
488
489 $users = $assign->testable_get_grading_userid_list();
490 $this->assertEquals(self::DEFAULT_STUDENT_COUNT + self::EXTRA_STUDENT_COUNT + self::EXTRA_SUSPENDED_COUNT, count($users));
47f48152
DW
491 }
492
493 public function test_cron() {
494 // First run cron so there are no messages waiting to be sent (from other tests).
495 cron_setup_user();
496 assign::cron();
497
498 // Now create an assignment and add some feedback.
499 $this->setUser($this->editingteachers[0]);
500 $assign = $this->create_instance();
501
502 // Simulate adding a grade.
503 $this->setUser($this->teachers[0]);
504 $data = new stdClass();
505 $data->grade = '50.0';
df211804 506 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
4d2bd673 507 $assign->testable_apply_grade_to_user($data, $this->students[1]->id, 0);
47f48152
DW
508
509 // Now run cron and see that one message was sent.
510 $this->preventResetByRollback();
511 $sink = $this->redirectMessages();
512 cron_setup_user();
4d2bd673 513 $this->expectOutputRegex('/Done processing 2 assignment submissions/');
47f48152
DW
514 assign::cron();
515
516 $messages = $sink->get_messages();
4d2bd673 517 $this->assertEquals(2, count($messages));
47f48152
DW
518 $this->assertEquals(1, $messages[0]->notification);
519 $this->assertEquals($assign->get_instance()->name, $messages[0]->contexturlname);
520 }
521
522 public function test_is_graded() {
523 $this->setUser($this->editingteachers[0]);
524 $assign = $this->create_instance();
525
526 // Simulate adding a grade.
527 $this->setUser($this->teachers[0]);
528 $data = new stdClass();
529 $data->grade = '50.0';
df211804 530 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
47f48152
DW
531
532 $this->assertEquals(true, $assign->testable_is_graded($this->students[0]->id));
533 $this->assertEquals(false, $assign->testable_is_graded($this->students[1]->id));
534 }
535
536 public function test_can_view_submission() {
e6cc5347 537 $this->create_extra_users();
47f48152
DW
538 $this->setUser($this->editingteachers[0]);
539 $assign = $this->create_instance();
540
541 $this->setUser($this->students[0]);
542 $this->assertEquals(true, $assign->can_view_submission($this->students[0]->id));
543 $this->assertEquals(false, $assign->can_view_submission($this->students[1]->id));
544 $this->assertEquals(false, $assign->can_view_submission($this->teachers[0]->id));
545 $this->setUser($this->teachers[0]);
546 $this->assertEquals(true, $assign->can_view_submission($this->students[0]->id));
547 $this->assertEquals(true, $assign->can_view_submission($this->students[1]->id));
548 $this->assertEquals(true, $assign->can_view_submission($this->teachers[0]->id));
e6cc5347 549 $this->assertEquals(false, $assign->can_view_submission($this->extrasuspendedstudents[0]->id));
47f48152
DW
550 $this->setUser($this->editingteachers[0]);
551 $this->assertEquals(true, $assign->can_view_submission($this->students[0]->id));
552 $this->assertEquals(true, $assign->can_view_submission($this->students[1]->id));
553 $this->assertEquals(true, $assign->can_view_submission($this->teachers[0]->id));
e6cc5347 554 $this->assertEquals(true, $assign->can_view_submission($this->extrasuspendedstudents[0]->id));
47f48152
DW
555 }
556
557
558 public function test_update_submission() {
df211804 559 $this->create_extra_users();
47f48152
DW
560 $this->setUser($this->editingteachers[0]);
561 $assign = $this->create_instance();
562
a5c793c3 563 $this->setUser($this->extrastudents[0]);
47f48152 564 $now = time();
a5c793c3
DW
565 $submission = $assign->get_user_submission($this->extrastudents[0]->id, true);
566 $assign->testable_update_submission($submission, $this->extrastudents[0]->id, true, false);
47f48152
DW
567
568 $this->setUser($this->teachers[0]);
569 // Verify the gradebook update.
570 $gradinginfo = grade_get_grades($this->course->id,
571 'mod',
572 'assign',
573 $assign->get_instance()->id,
a5c793c3 574 $this->extrastudents[0]->id);
47f48152 575
a5c793c3
DW
576 $this->assertEquals($this->extrastudents[0]->id,
577 $gradinginfo->items[0]->grades[$this->extrastudents[0]->id]->usermodified);
47f48152
DW
578
579 // Now verify group assignments.
580 $this->setUser($this->editingteachers[0]);
581 $assign = $this->create_instance(array('teamsubmission'=>1));
582
a5c793c3 583 $this->setUser($this->extrastudents[0]);
47f48152 584 $now = time();
a5c793c3
DW
585 $submission = $assign->get_group_submission($this->extrastudents[0]->id, 0, true);
586 $assign->testable_update_submission($submission, $this->extrastudents[0]->id, true, true);
47f48152 587
e6cc5347 588 // Check that at least 2 active members and 1 suspended member of the submission group had their submission updated.
47f48152
DW
589
590 $this->setUser($this->editingteachers[0]);
591 $gradinginfo = grade_get_grades($this->course->id,
592 'mod',
593 'assign',
594 $assign->get_instance()->id,
a5c793c3 595 $this->extrastudents[0]->id);
47f48152 596
a5c793c3
DW
597 $this->assertEquals($this->extrastudents[0]->id,
598 $gradinginfo->items[0]->grades[$this->extrastudents[0]->id]->usermodified);
47f48152
DW
599
600 $gradinginfo = grade_get_grades($this->course->id,
601 'mod',
602 'assign',
603 $assign->get_instance()->id,
a5c793c3 604 $this->extrastudents[self::GROUP_COUNT]->id);
47f48152 605
a5c793c3
DW
606 $this->assertEquals($this->extrastudents[self::GROUP_COUNT]->id,
607 $gradinginfo->items[0]->grades[$this->extrastudents[self::GROUP_COUNT]->id]->usermodified);
47f48152 608
e6cc5347
RT
609 $gradinginfo = grade_get_grades($this->course->id,
610 'mod',
611 'assign',
612 $assign->get_instance()->id,
613 $this->extrasuspendedstudents[0]->id);
614 $this->assertEquals($this->extrasuspendedstudents[0]->id,
615 $gradinginfo->items[0]->grades[$this->extrasuspendedstudents[0]->id]->usermodified);
616
617 // Check the same with non-editing teacher and make sure submission is not updated for suspended user.
618 $this->setUser($this->editingteachers[0]);
619 $assign = $this->create_instance(array('teamsubmission'=>1));
620
621 $this->setUser($this->extrastudents[1]);
622 $now = time();
623 $submission = $assign->get_group_submission($this->extrastudents[1]->id, 0, true);
624 $assign->testable_update_submission($submission, $this->extrastudents[1]->id, true, true);
625
626 $this->setUser($this->teachers[0]);
627 $gradinginfo = grade_get_grades($this->course->id,
628 'mod',
629 'assign',
630 $assign->get_instance()->id,
631 $this->extrastudents[1]->id);
632
633 $this->assertEquals($this->extrastudents[1]->id,
634 $gradinginfo->items[0]->grades[$this->extrastudents[1]->id]->usermodified);
635
636 $gradinginfo = grade_get_grades($this->course->id,
637 'mod',
638 'assign',
639 $assign->get_instance()->id,
640 $this->extrastudents[self::GROUP_COUNT+1]->id);
641
642 $this->assertEquals($this->extrastudents[self::GROUP_COUNT+1]->id,
643 $gradinginfo->items[0]->grades[$this->extrastudents[self::GROUP_COUNT+1]->id]->usermodified);
644
645 $gradinginfo = grade_get_grades($this->course->id,
646 'mod',
647 'assign',
648 $assign->get_instance()->id,
649 $this->extrasuspendedstudents[1]->id);
650 $this->assertEquals($this->extrasuspendedstudents[1]->id,
651 $gradinginfo->items[0]->grades[$this->extrasuspendedstudents[1]->id]->usermodified);
652
47f48152
DW
653 // Now verify blind marking.
654 $this->setUser($this->editingteachers[0]);
655 $assign = $this->create_instance(array('blindmarking'=>1));
656
a5c793c3 657 $this->setUser($this->extrastudents[0]);
47f48152 658 $now = time();
a5c793c3
DW
659 $submission = $assign->get_user_submission($this->extrastudents[0]->id, true);
660 $assign->testable_update_submission($submission, $this->extrastudents[0]->id, true, false);
47f48152
DW
661
662 $this->setUser($this->editingteachers[0]);
663 $gradinginfo = grade_get_grades($this->course->id,
664 'mod',
665 'assign',
666 $assign->get_instance()->id,
a5c793c3 667 $this->extrastudents[0]->id);
47f48152 668
a5c793c3 669 $this->assertEquals(null, $gradinginfo->items[0]->grades[$this->extrastudents[0]->id]->datesubmitted);
47f48152
DW
670 }
671
2bd27878
DW
672 public function test_group_submissions_submit_for_marking() {
673 global $PAGE;
674
675 $this->create_extra_users();
676 // Now verify group assignments.
677 $this->setUser($this->editingteachers[0]);
678 $assign = $this->create_instance(array('teamsubmission'=>1,
679 'assignsubmission_onlinetext_enabled'=>1,
680 'submissiondrafts'=>1,
681 'requireallteammemberssubmit'=>1));
682 $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
683
684 $this->setUser($this->extrastudents[0]);
685 // Add a submission.
686 $data = new stdClass();
687 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
688 'text'=>'Submission text',
689 'format'=>FORMAT_MOODLE);
690
691 $notices = array();
692 $assign->save_submission($data, $notices);
693
694 // Check we can see the submit button.
695 $output = $assign->view_student_summary($this->extrastudents[0], true);
696 $this->assertContains(get_string('submitassignment', 'assign'), $output);
697
698 $submission = $assign->get_group_submission($this->extrastudents[0]->id, 0, true);
699 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
700 $assign->testable_update_submission($submission, $this->extrastudents[0]->id, true, true);
701
702 // Check that the student does not see "Submit" button.
703 $output = $assign->view_student_summary($this->extrastudents[0], true);
704 $this->assertNotContains(get_string('submitassignment', 'assign'), $output);
705
706 // Change to another user in the same group.
707 $this->setUser($this->extrastudents[self::GROUP_COUNT]);
708 $output = $assign->view_student_summary($this->extrastudents[self::GROUP_COUNT], true);
709 $this->assertContains(get_string('submitassignment', 'assign'), $output);
710
711 $submission = $assign->get_group_submission($this->extrastudents[self::GROUP_COUNT]->id, 0, true);
712 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
713 $assign->testable_update_submission($submission, $this->extrastudents[self::GROUP_COUNT]->id, true, true);
714 $output = $assign->view_student_summary($this->extrastudents[self::GROUP_COUNT], true);
715 $this->assertNotContains(get_string('submitassignment', 'assign'), $output);
716 }
717
47f48152
DW
718 public function test_submissions_open() {
719 $this->setUser($this->editingteachers[0]);
720
721 $now = time();
722 $tomorrow = $now + 24*60*60;
723 $oneweek = $now + 7*24*60*60;
724 $yesterday = $now - 24*60*60;
725
726 $assign = $this->create_instance();
727 $this->assertEquals(true, $assign->testable_submissions_open($this->students[0]->id));
728
729 $assign = $this->create_instance(array('duedate'=>$tomorrow));
730 $this->assertEquals(true, $assign->testable_submissions_open($this->students[0]->id));
731
732 $assign = $this->create_instance(array('duedate'=>$yesterday));
733 $this->assertEquals(true, $assign->testable_submissions_open($this->students[0]->id));
734
735 $assign = $this->create_instance(array('duedate'=>$yesterday, 'cutoffdate'=>$tomorrow));
736 $this->assertEquals(true, $assign->testable_submissions_open($this->students[0]->id));
737
738 $assign = $this->create_instance(array('duedate'=>$yesterday, 'cutoffdate'=>$yesterday));
739 $this->assertEquals(false, $assign->testable_submissions_open($this->students[0]->id));
740
741 $assign->testable_save_user_extension($this->students[0]->id, $tomorrow);
742 $this->assertEquals(true, $assign->testable_submissions_open($this->students[0]->id));
743
744 $assign = $this->create_instance(array('submissiondrafts'=>1));
745 $this->assertEquals(true, $assign->testable_submissions_open($this->students[0]->id));
746
747 $this->setUser($this->students[0]);
748 $now = time();
749 $submission = $assign->get_user_submission($this->students[0]->id, true);
750 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
751 $assign->testable_update_submission($submission, $this->students[0]->id, true, false);
752 $this->setUser($this->editingteachers[0]);
753 $this->assertEquals(false, $assign->testable_submissions_open($this->students[0]->id));
754 }
755
756 public function test_get_graders() {
df211804 757 $this->create_extra_users();
47f48152
DW
758 $this->setUser($this->editingteachers[0]);
759 $assign = $this->create_instance();
760
a5c793c3
DW
761 $this->assertCount(self::DEFAULT_TEACHER_COUNT +
762 self::DEFAULT_EDITING_TEACHER_COUNT +
763 self::EXTRA_TEACHER_COUNT +
764 self::EXTRA_EDITING_TEACHER_COUNT,
765 $assign->testable_get_graders($this->students[0]->id));
47f48152
DW
766
767 $assign = $this->create_instance();
768 // Force create an assignment with SEPARATEGROUPS.
769 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
770 $params = array('course'=>$this->course->id);
771 $instance = $generator->create_instance($params);
772 $cm = get_coursemodule_from_instance('assign', $instance->id);
773 set_coursemodule_groupmode($cm->id, SEPARATEGROUPS);
774 $cm->groupmode = SEPARATEGROUPS;
775 $context = context_module::instance($cm->id);
776 $assign = new testable_assign($context, $cm, $this->course);
777
778 $this->setUser($this->students[1]);
a5c793c3 779 $this->assertCount(4, $assign->testable_get_graders($this->students[0]->id));
47f48152
DW
780 }
781
20b7dcfc
DW
782 public function test_group_members_only() {
783 global $CFG;
784
785 $this->setAdminUser();
786 $this->create_extra_users();
787 $CFG->enablegroupmembersonly = true;
788 $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $this->course->id));
789 groups_assign_grouping($grouping->id, $this->groups[0]->id);
790
791 // Force create an assignment with SEPARATEGROUPS.
792 $instance = $this->getDataGenerator()->create_module('assign', array('course'=>$this->course->id),
793 array('groupmembersonly' => SEPARATEGROUPS, 'groupingid' => $grouping->id));
794
795 $cm = get_coursemodule_from_instance('assign', $instance->id);
796 $context = context_module::instance($cm->id);
797 $assign = new testable_assign($context, $cm, $this->course);
798
799 $this->setUser($this->teachers[0]);
800 $this->assertCount(5, $assign->list_participants(0, true));
801
802 }
803
47f48152
DW
804 public function test_get_uniqueid_for_user() {
805 $this->setUser($this->editingteachers[0]);
806 $assign = $this->create_instance();
807
808 foreach ($this->students as $student) {
809 $uniqueid = $assign->get_uniqueid_for_user($student->id);
810 $this->assertEquals($student->id, $assign->get_user_id_for_uniqueid($uniqueid));
811 }
812 }
813
46692c3a 814 public function test_show_student_summary() {
c2114099 815 global $CFG, $PAGE;
46692c3a
DW
816
817 $this->setUser($this->editingteachers[0]);
818 $assign = $this->create_instance();
c2114099 819 $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
46692c3a
DW
820
821 // No feedback should be available because this student has not been graded.
822 $this->setUser($this->students[0]);
823 $output = $assign->view_student_summary($this->students[0], true);
824 $this->assertEquals(false, strpos($output, 'Feedback'), 'Do not show feedback if there is no grade');
825 // Simulate adding a grade.
826 $this->setUser($this->teachers[0]);
827 $data = new stdClass();
828 $data->grade = '50.0';
df211804 829 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
46692c3a 830
b0da618b 831 // Now we should see the feedback.
46692c3a
DW
832 $this->setUser($this->students[0]);
833 $output = $assign->view_student_summary($this->students[0], true);
834 $this->assertNotEquals(false, strpos($output, 'Feedback'), 'Show feedback if there is a grade');
835
836 // Now hide the grade in gradebook.
837 $this->setUser($this->teachers[0]);
838 require_once($CFG->libdir.'/gradelib.php');
839 $gradeitem = new grade_item(array(
840 'itemtype' => 'mod',
841 'itemmodule' => 'assign',
842 'iteminstance' => $assign->get_instance()->id,
843 'courseid' => $this->course->id));
844
845 $gradeitem->set_hidden(1, false);
846
847 // No feedback should be available because the grade is hidden.
848 $this->setUser($this->students[0]);
849 $output = $assign->view_student_summary($this->students[0], true);
850 $this->assertEquals(false, strpos($output, 'Feedback'), 'Do not show feedback if the grade is hidden in the gradebook');
851
b0da618b 852 // Do the same but add feedback.
46692c3a
DW
853 $assign = $this->create_instance(array('assignfeedback_comments_enabled' => 1));
854
855 $this->setUser($this->teachers[0]);
856 $grade = $assign->get_user_grade($this->students[0]->id, true);
857 $data = new stdClass();
858 $data->assignfeedbackcomments_editor = array('text'=>'Tomato sauce',
859 'format'=>FORMAT_MOODLE);
860 $plugin = $assign->get_feedback_plugin_by_type('comments');
861 $plugin->save($grade, $data);
862
b0da618b 863 // Should have feedback but no grade.
46692c3a
DW
864 $this->setUser($this->students[0]);
865 $output = $assign->view_student_summary($this->students[0], true);
ea698284 866 $this->assertNotEquals(false, strpos($output, 'Feedback'), 'Show feedback even if there is no grade');
46692c3a
DW
867 $this->assertEquals(false, strpos($output, 'Grade'), 'Do not show grade when there is no grade.');
868 $this->assertEquals(false, strpos($output, 'Graded on'), 'Do not show graded date when there is no grade.');
45aac51a 869
ea698284
AD
870 // Now hide the grade in gradebook.
871 $this->setUser($this->teachers[0]);
872 $gradeitem = new grade_item(array(
873 'itemtype' => 'mod',
874 'itemmodule' => 'assign',
875 'iteminstance' => $assign->get_instance()->id,
876 'courseid' => $this->course->id));
877
878 $gradeitem->set_hidden(1, false);
879
880 // No feedback should be available because the grade is hidden.
881 $this->setUser($this->students[0]);
882 $output = $assign->view_student_summary($this->students[0], true);
883 $this->assertEquals(false, strpos($output, 'Feedback'), 'Do not show feedback if the grade is hidden in the gradebook');
46692c3a
DW
884 }
885
df211804
DW
886 public function test_attempt_reopen_method_manual() {
887 global $PAGE;
888
889 $this->setUser($this->editingteachers[0]);
890 $assign = $this->create_instance(array('attemptreopenmethod'=>ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL,
891 'maxattempts'=>3,
892 'submissiondrafts'=>1,
893 'assignsubmission_onlinetext_enabled'=>1));
894 $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
895
896 // Student should be able to see an add submission button.
897 $this->setUser($this->students[0]);
898 $output = $assign->view_student_summary($this->students[0], true);
899 $this->assertNotEquals(false, strpos($output, get_string('addsubmission', 'assign')));
900
901 // Add a submission.
902 $now = time();
903 $submission = $assign->get_user_submission($this->students[0]->id, true);
904 $data = new stdClass();
905 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
906 'text'=>'Submission text',
907 'format'=>FORMAT_MOODLE);
908 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
909 $plugin->save($submission, $data);
910
911 // And now submit it for marking.
912 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
913 $assign->testable_update_submission($submission, $this->students[0]->id, true, false);
914
915 // Verify the student cannot make changes to the submission.
916 $output = $assign->view_student_summary($this->students[0], true);
917 $this->assertEquals(false, strpos($output, get_string('addsubmission', 'assign')));
918
919 // Mark the submission.
920 $this->setUser($this->teachers[0]);
921 $data = new stdClass();
922 $data->grade = '50.0';
923 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
924
925 // Check the student can see the grade.
926 $this->setUser($this->students[0]);
927 $output = $assign->view_student_summary($this->students[0], true);
928 $this->assertNotEquals(false, strpos($output, '50.0'));
929
930 // Allow the student another attempt.
931 $this->teachers[0]->ignoresesskey = true;
932 $this->setUser($this->teachers[0]);
933 $result = $assign->testable_process_add_attempt($this->students[0]->id);
934 $this->assertEquals(true, $result);
935
936 // Check that the previous attempt is now in the submission history table.
937 $this->setUser($this->students[0]);
938 $output = $assign->view_student_summary($this->students[0], true);
939 // Need a better check.
940 $this->assertNotEquals(false, strpos($output, 'Submission text'), 'Contains: Submission text');
941
942 // Check that the student now has a button for Add a new attempt".
943 $this->assertNotEquals(false, strpos($output, get_string('addnewattempt', 'assign')));
944 // Check that the student now does not have a button for Submit.
945 $this->assertEquals(false, strpos($output, get_string('submitassignment', 'assign')));
946
947 // Check that the student now has a submission history.
948 $this->assertNotEquals(false, strpos($output, get_string('attempthistory', 'assign')));
949
950 $this->setUser($this->teachers[0]);
951 // Check that the grading table loads correctly and contains this user.
952 // This is also testing that we do not get duplicate rows in the grading table.
953 $gradingtable = new assign_grading_table($assign, 100, '', 0, true);
954 $output = $assign->get_renderer()->render($gradingtable);
955 $this->assertEquals(true, strpos($output, $this->students[0]->lastname));
956
957 // Should be 1 not 2.
958 $this->assertEquals(1, $assign->count_submissions());
959 $this->assertEquals(1, $assign->count_submissions_with_status('reopened'));
960 $this->assertEquals(0, $assign->count_submissions_need_grading());
961 $this->assertEquals(1, $assign->count_grades());
962
963 // Change max attempts to unlimited.
964 $formdata = clone($assign->get_instance());
965 $formdata->maxattempts = ASSIGN_UNLIMITED_ATTEMPTS;
966 $formdata->instance = $formdata->id;
967 $assign->update_instance($formdata);
968
969 // Check we can repopen still.
970 $result = $assign->testable_process_add_attempt($this->students[0]->id);
971 $this->assertEquals(true, $result);
972
973 $grades = $assign->get_user_grades_for_gradebook($this->students[0]->id);
974 $this->assertEquals(50, (int)$grades[$this->students[0]->id]->rawgrade);
975
df211804 976 }
46692c3a 977
f8d107b3
DM
978 public function test_markingworkflow() {
979 global $PAGE;
980
981 $this->setUser($this->editingteachers[0]);
982 $assign = $this->create_instance(array('markingworkflow'=>1));
983 $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
984
13e82f1c 985 // Mark the submission and set to notmarked.
f8d107b3
DM
986 $this->setUser($this->teachers[0]);
987 $data = new stdClass();
988 $data->grade = '50.0';
989 $data->workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED;
990 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
991
992 // Check the student can't see the grade.
993 $this->setUser($this->students[0]);
994 $output = $assign->view_student_summary($this->students[0], true);
995 $this->assertEquals(false, strpos($output, '50.0'));
996
13e82f1c 997 // Mark the submission and set to inmarking.
f8d107b3
DM
998 $this->setUser($this->teachers[0]);
999 $data = new stdClass();
1000 $data->grade = '50.0';
1001 $data->workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_INMARKING;
1002 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
1003
1004 // Check the student can't see the grade.
1005 $this->setUser($this->students[0]);
1006 $output = $assign->view_student_summary($this->students[0], true);
1007 $this->assertEquals(false, strpos($output, '50.0'));
1008
13e82f1c 1009 // Mark the submission and set to readyforreview.
f8d107b3
DM
1010 $this->setUser($this->teachers[0]);
1011 $data = new stdClass();
1012 $data->grade = '50.0';
1013 $data->workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_READYFORREVIEW;
1014 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
1015
1016 // Check the student can't see the grade.
1017 $this->setUser($this->students[0]);
1018 $output = $assign->view_student_summary($this->students[0], true);
1019 $this->assertEquals(false, strpos($output, '50.0'));
1020
13e82f1c 1021 // Mark the submission and set to inreview.
f8d107b3
DM
1022 $this->setUser($this->teachers[0]);
1023 $data = new stdClass();
1024 $data->grade = '50.0';
1025 $data->workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_INREVIEW;
1026 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
1027
1028 // Check the student can't see the grade.
1029 $this->setUser($this->students[0]);
1030 $output = $assign->view_student_summary($this->students[0], true);
1031 $this->assertEquals(false, strpos($output, '50.0'));
1032
13e82f1c 1033 // Mark the submission and set to readyforrelease.
f8d107b3
DM
1034 $this->setUser($this->teachers[0]);
1035 $data = new stdClass();
1036 $data->grade = '50.0';
1037 $data->workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_READYFORRELEASE;
1038 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
1039
1040 // Check the student can't see the grade.
1041 $this->setUser($this->students[0]);
1042 $output = $assign->view_student_summary($this->students[0], true);
1043 $this->assertEquals(false, strpos($output, '50.0'));
1044
13e82f1c 1045 // Mark the submission and set to released.
f8d107b3
DM
1046 $this->setUser($this->teachers[0]);
1047 $data = new stdClass();
1048 $data->grade = '50.0';
1049 $data->workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_RELEASED;
1050 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
1051
1052 // Check the student can see the grade.
1053 $this->setUser($this->students[0]);
1054 $output = $assign->view_student_summary($this->students[0], true);
1055 $this->assertNotEquals(false, strpos($output, '50.0'));
1056 }
1057
1058 public function test_markerallocation() {
1059 global $PAGE;
1060
1061 $this->setUser($this->editingteachers[0]);
13e82f1c 1062 $assign = $this->create_instance(array('markingworkflow'=>1, 'markingallocation'=>1));
f8d107b3
DM
1063 $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
1064
13e82f1c 1065 // Allocate marker to submission.
f8d107b3
DM
1066 $data = new stdClass();
1067 $data->allocatedmarker = $this->teachers[0]->id;
1068 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
1069
13e82f1c 1070 // Check the allocated marker can view the submission.
f8d107b3
DM
1071 $this->setUser($this->teachers[0]);
1072 $gradingtable = new assign_grading_table($assign, 100, '', 0, true);
1073 $output = $assign->get_renderer()->render($gradingtable);
1074 $this->assertEquals(true, strpos($output, $this->students[0]->lastname));
1075
13e82f1c 1076 // Check that other teachers can't view this submission.
f8d107b3
DM
1077 $this->setUser($this->teachers[1]);
1078 $gradingtable = new assign_grading_table($assign, 100, '', 0, true);
1079 $output = $assign->get_renderer()->render($gradingtable);
1080 $this->assertNotEquals(true, strpos($output, $this->students[0]->lastname));
1081 }
76e77b05
FM
1082
1083 public function test_extension_granted_event() {
1084 $this->setUser($this->editingteachers[0]);
1085
1086 $tomorrow = time() + 24*60*60;
1087 $yesterday = time() - 24*60*60;
1088
1089 $assign = $this->create_instance(array('duedate' => $yesterday, 'cutoffdate' => $yesterday));
1090 $sink = $this->redirectEvents();
1091
1092 $assign->testable_save_user_extension($this->students[0]->id, $tomorrow);
1093
1094 $events = $sink->get_events();
1095 $this->assertCount(1, $events);
1096 $event = reset($events);
1097 $this->assertInstanceOf('\mod_assign\event\extension_granted', $event);
1098 $this->assertEquals($assign->get_context(), $event->get_context());
1099 $this->assertEquals($assign->get_instance()->id, $event->objectid);
1100 $this->assertEquals($this->students[0]->id, $event->relateduserid);
1101 $expected = array(
1102 $assign->get_course()->id,
1103 'assign',
1104 'grant extension',
1105 'view.php?id=' . $assign->get_course_module()->id,
1106 $this->students[0]->id,
1107 $assign->get_course_module()->id,
1108 $this->editingteachers[0]->id
1109 );
1110 $this->assertEventLegacyLogData($expected, $event);
1111 $sink->close();
1112 }
1113
3d1331be
FM
1114 public function test_submission_locked_event() {
1115 $this->editingteachers[0]->ignoresesskey = true;
1116 $this->setUser($this->editingteachers[0]);
1117
1118 $assign = $this->create_instance();
1119 $sink = $this->redirectEvents();
1120
05a6445a 1121 $assign->lock_submission($this->students[0]->id);
3d1331be
FM
1122
1123 $events = $sink->get_events();
1124 $this->assertCount(1, $events);
1125 $event = reset($events);
1126 $this->assertInstanceOf('\mod_assign\event\submission_locked', $event);
1127 $this->assertEquals($assign->get_context(), $event->get_context());
1128 $this->assertEquals($assign->get_instance()->id, $event->objectid);
1129 $this->assertEquals($this->students[0]->id, $event->relateduserid);
1130 $expected = array(
1131 $assign->get_course()->id,
1132 'assign',
1133 'lock submission',
1134 'view.php?id=' . $assign->get_course_module()->id,
1135 get_string('locksubmissionforstudent', 'assign', array('id' => $this->students[0]->id,
1136 'fullname' => fullname($this->students[0]))),
1137 $assign->get_course_module()->id,
1138 $this->editingteachers[0]->id
1139 );
1140 $this->assertEventLegacyLogData($expected, $event);
1141 $sink->close();
1142
1143 // Revert to defaults.
1144 $this->editingteachers[0]->ignoresesskey = false;
1145 }
1146
ad10ad14
FM
1147 public function test_identities_revealed_event() {
1148 $this->editingteachers[0]->ignoresesskey = true;
1149 $this->setUser($this->editingteachers[0]);
1150
1151 $assign = $this->create_instance(array('blindmarking'=>1));
1152 $sink = $this->redirectEvents();
1153
05a6445a 1154 $assign->reveal_identities();
ad10ad14
FM
1155
1156 $events = $sink->get_events();
1157 $this->assertCount(1, $events);
1158 $event = reset($events);
1159 $this->assertInstanceOf('\mod_assign\event\identities_revealed', $event);
1160 $this->assertEquals($assign->get_context(), $event->get_context());
1161 $this->assertEquals($assign->get_instance()->id, $event->objectid);
1162 $expected = array(
1163 $assign->get_course()->id,
1164 'assign',
1165 'reveal identities',
1166 'view.php?id=' . $assign->get_course_module()->id,
1167 get_string('revealidentities', 'assign'),
1168 $assign->get_course_module()->id,
1169 $this->editingteachers[0]->id
1170 );
1171 $this->assertEventLegacyLogData($expected, $event);
1172 $sink->close();
1173
1174 // Revert to defaults.
1175 $this->editingteachers[0]->ignoresesskey = false;
1176 }
1177
159b7f40
FM
1178 public function test_submission_status_updated_event() {
1179 $this->editingteachers[0]->ignoresesskey = true;
1180 $this->setUser($this->editingteachers[0]);
1181
1182 $assign = $this->create_instance();
1183 $submission = $assign->get_user_submission($this->students[0]->id, true);
1184 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
1185 $assign->testable_update_submission($submission, $this->students[0]->id, true, false);
1186
1187 $sink = $this->redirectEvents();
05a6445a 1188 $assign->revert_to_draft($this->students[0]->id);
159b7f40
FM
1189
1190 $events = $sink->get_events();
1191 $this->assertCount(1, $events);
1192 $event = reset($events);
1193 $this->assertInstanceOf('\mod_assign\event\submission_status_updated', $event);
1194 $this->assertEquals($assign->get_context(), $event->get_context());
1195 $this->assertEquals($submission->id, $event->objectid);
1196 $this->assertEquals($this->students[0]->id, $event->relateduserid);
1197 $this->assertEquals(ASSIGN_SUBMISSION_STATUS_DRAFT, $event->other['newstatus']);
1198 $expected = array(
1199 $assign->get_course()->id,
1200 'assign',
1201 'revert submission to draft',
1202 'view.php?id=' . $assign->get_course_module()->id,
1203 get_string('reverttodraftforstudent', 'assign', array('id' => $this->students[0]->id,
1204 'fullname' => fullname($this->students[0]))),
1205 $assign->get_course_module()->id,
1206 $this->editingteachers[0]->id
1207 );
1208 $this->assertEventLegacyLogData($expected, $event);
1209 $sink->close();
1210
1211 // Revert to defaults.
1212 $this->editingteachers[0]->ignoresesskey = false;
1213 }
1214
e59a85ba
FM
1215 public function test_marker_updated_event() {
1216 $this->editingteachers[0]->ignoresesskey = true;
1217 $this->setUser($this->editingteachers[0]);
1218
1219 $assign = $this->create_instance();
1220
1221 $sink = $this->redirectEvents();
1222 $assign->testable_process_set_batch_marking_allocation($this->students[0]->id, $this->teachers[0]->id);
1223
1224 $events = $sink->get_events();
1225 $this->assertCount(1, $events);
1226 $event = reset($events);
1227 $this->assertInstanceOf('\mod_assign\event\marker_updated', $event);
1228 $this->assertEquals($assign->get_context(), $event->get_context());
1229 $this->assertEquals($assign->get_instance()->id, $event->objectid);
1230 $this->assertEquals($this->students[0]->id, $event->relateduserid);
1231 $this->assertEquals($this->editingteachers[0]->id, $event->userid);
1232 $this->assertEquals($this->teachers[0]->id, $event->other['markerid']);
1233 $expected = array(
1234 $assign->get_course()->id,
1235 'assign',
1236 'set marking allocation',
1237 'view.php?id=' . $assign->get_course_module()->id,
1238 get_string('setmarkerallocationforlog', 'assign', array('id' => $this->students[0]->id,
1239 'fullname' => fullname($this->students[0]), 'marker' => fullname($this->teachers[0]))),
1240 $assign->get_course_module()->id,
1241 $this->editingteachers[0]->id
1242 );
1243 $this->assertEventLegacyLogData($expected, $event);
1244 $sink->close();
1245
1246 // Revert to defaults.
1247 $this->editingteachers[0]->ignoresesskey = false;
1248 }
1249
8bb213eb
FM
1250 public function test_workflow_state_updated_event() {
1251 $this->editingteachers[0]->ignoresesskey = true;
1252 $this->setUser($this->editingteachers[0]);
1253
1254 $assign = $this->create_instance();
1255
1256 $sink = $this->redirectEvents();
1257 $assign->testable_process_set_batch_marking_workflow_state($this->students[0]->id, ASSIGN_MARKING_WORKFLOW_STATE_INREVIEW);
1258
1259 $events = $sink->get_events();
1260 $this->assertCount(1, $events);
1261 $event = reset($events);
1262 $this->assertInstanceOf('\mod_assign\event\workflow_state_updated', $event);
1263 $this->assertEquals($assign->get_context(), $event->get_context());
1264 $this->assertEquals($assign->get_instance()->id, $event->objectid);
1265 $this->assertEquals($this->students[0]->id, $event->relateduserid);
1266 $this->assertEquals($this->editingteachers[0]->id, $event->userid);
1267 $this->assertEquals(ASSIGN_MARKING_WORKFLOW_STATE_INREVIEW, $event->other['newstate']);
1268 $expected = array(
1269 $assign->get_course()->id,
1270 'assign',
1271 'set marking workflow state',
1272 'view.php?id=' . $assign->get_course_module()->id,
1273 get_string('setmarkingworkflowstateforlog', 'assign', array('id' => $this->students[0]->id,
1274 'fullname' => fullname($this->students[0]), 'state' => ASSIGN_MARKING_WORKFLOW_STATE_INREVIEW)),
1275 $assign->get_course_module()->id,
1276 $this->editingteachers[0]->id
1277 );
1278 $this->assertEventLegacyLogData($expected, $event);
1279 $sink->close();
1280
1281 // Revert to defaults.
1282 $this->editingteachers[0]->ignoresesskey = false;
1283 }
89fbc202
FM
1284
1285 public function test_submission_duplicated_event() {
1286 $this->setUser($this->students[0]);
1287
1288 $assign = $this->create_instance();
1289 $submission1 = $assign->get_user_submission($this->students[0]->id, true, 0);
1290 $submission2 = $assign->get_user_submission($this->students[0]->id, true, 1);
1291 $submission2->status = ASSIGN_SUBMISSION_STATUS_REOPENED;
1292 $assign->testable_update_submission($submission2, $this->students[0]->id, time(), $assign->get_instance()->teamsubmission);
1293
1294 $sink = $this->redirectEvents();
1295 $notices = null;
05a6445a 1296 $assign->copy_previous_attempt($notices);
89fbc202
FM
1297
1298 $events = $sink->get_events();
1299 $this->assertCount(1, $events);
1300 $event = reset($events);
1301 $this->assertInstanceOf('\mod_assign\event\submission_duplicated', $event);
1302 $this->assertEquals($assign->get_context(), $event->get_context());
1303 $this->assertEquals($submission2->id, $event->objectid);
1304 $this->assertEquals($this->students[0]->id, $event->userid);
1305 $submission2->status = ASSIGN_SUBMISSION_STATUS_DRAFT;
1306 $expected = array(
1307 $assign->get_course()->id,
1308 'assign',
1309 'submissioncopied',
1310 'view.php?id=' . $assign->get_course_module()->id,
1311 $assign->testable_format_submission_for_log($submission2),
1312 $assign->get_course_module()->id,
1313 $this->students[0]->id
1314 );
1315 $this->assertEventLegacyLogData($expected, $event);
1316 $sink->close();
1317 }
1318
9a289c3d
FM
1319 public function test_submission_unlocked_event() {
1320 $this->editingteachers[0]->ignoresesskey = true;
1321 $this->setUser($this->editingteachers[0]);
1322
1323 $assign = $this->create_instance();
1324 $sink = $this->redirectEvents();
1325
05a6445a 1326 $assign->unlock_submission($this->students[0]->id);
9a289c3d
FM
1327
1328 $events = $sink->get_events();
1329 $this->assertCount(1, $events);
1330 $event = reset($events);
1331 $this->assertInstanceOf('\mod_assign\event\submission_unlocked', $event);
1332 $this->assertEquals($assign->get_context(), $event->get_context());
1333 $this->assertEquals($assign->get_instance()->id, $event->objectid);
1334 $this->assertEquals($this->students[0]->id, $event->relateduserid);
1335 $expected = array(
1336 $assign->get_course()->id,
1337 'assign',
1338 'unlock submission',
1339 'view.php?id=' . $assign->get_course_module()->id,
1340 get_string('unlocksubmissionforstudent', 'assign', array('id' => $this->students[0]->id,
1341 'fullname' => fullname($this->students[0]))),
1342 $assign->get_course_module()->id,
1343 $this->editingteachers[0]->id
1344 );
1345 $this->assertEventLegacyLogData($expected, $event);
1346 $sink->close();
1347
1348 // Revert to defaults.
1349 $this->editingteachers[0]->ignoresesskey = false;
1350 }
1351
bd2e9829
FM
1352 public function test_submission_graded_event() {
1353 $this->setUser($this->editingteachers[0]);
1354 $assign = $this->create_instance();
1355
1356 // Test apply_grade_to_user.
1357 $sink = $this->redirectEvents();
1358
1359 $data = new stdClass();
1360 $data->grade = '50.0';
1361 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
1362 $grade = $assign->get_user_grade($this->students[0]->id, false, 0);
1363
1364 $events = $sink->get_events();
1365 $this->assertCount(1, $events);
1366 $event = reset($events);
1367 $this->assertInstanceOf('\mod_assign\event\submission_graded', $event);
1368 $this->assertEquals($assign->get_context(), $event->get_context());
1369 $this->assertEquals($grade->id, $event->objectid);
1370 $this->assertEquals($this->students[0]->id, $event->relateduserid);
1371 $expected = array(
1372 $assign->get_course()->id,
1373 'assign',
1374 'grade submission',
1375 'view.php?id=' . $assign->get_course_module()->id,
1376 $assign->format_grade_for_log($grade),
1377 $assign->get_course_module()->id,
1378 $this->editingteachers[0]->id
1379 );
1380 $this->assertEventLegacyLogData($expected, $event);
1381 $sink->close();
1382
1383 // Test process_save_quick_grades.
1384 $sink = $this->redirectEvents();
1385
1386 $data = array(
1387 'grademodified_' . $this->students[0]->id => time(),
1388 'quickgrade_' . $this->students[0]->id => '60.0'
1389 );
1390 $assign->testable_process_save_quick_grades($data);
1391 $grade = $assign->get_user_grade($this->students[0]->id, false);
1392 $this->assertEquals('60.0', $grade->grade);
1393
1394 $events = $sink->get_events();
1395 $this->assertCount(1, $events);
1396 $event = reset($events);
1397 $this->assertInstanceOf('\mod_assign\event\submission_graded', $event);
1398 $this->assertEquals($assign->get_context(), $event->get_context());
1399 $this->assertEquals($grade->id, $event->objectid);
1400 $this->assertEquals($this->students[0]->id, $event->relateduserid);
1401 $expected = array(
1402 $assign->get_course()->id,
1403 'assign',
1404 'grade submission',
1405 'view.php?id=' . $assign->get_course_module()->id,
1406 $assign->format_grade_for_log($grade),
1407 $assign->get_course_module()->id,
1408 $this->editingteachers[0]->id
1409 );
1410 $this->assertEventLegacyLogData($expected, $event);
1411 $sink->close();
1412 }
1413
9054c04d 1414 public function test_disable_submit_after_cutoff_date() {
1415 global $PAGE;
1416
1417 $this->setUser($this->editingteachers[0]);
1418 $now = time();
1419 $tomorrow = $now + 24*60*60;
1420 $lastweek = $now - 7*24*60*60;
1421 $yesterday = $now - 24*60*60;
1422
1423 $assign = $this->create_instance(array('duedate'=>$yesterday,
1424 'cutoffdate'=>$tomorrow,
1425 'assignsubmission_onlinetext_enabled'=>1));
1426 $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
1427
1428 // Student should be able to see an add submission button.
1429 $this->setUser($this->students[0]);
1430 $output = $assign->view_student_summary($this->students[0], true);
1431 $this->assertNotEquals(false, strpos($output, get_string('addsubmission', 'assign')));
1432
1433 // Add a submission but don't submit now.
1434 $submission = $assign->get_user_submission($this->students[0]->id, true);
1435 $data = new stdClass();
1436 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
1437 'text'=>'Submission text',
1438 'format'=>FORMAT_MOODLE);
1439 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
1440 $plugin->save($submission, $data);
1441
1442 // Create another instance with cut-off and due-date already passed.
1443 $this->setUser($this->editingteachers[0]);
1444 $now = time();
1445 $assign = $this->create_instance(array('duedate'=>$lastweek,
1446 'cutoffdate'=>$yesterday,
1447 'assignsubmission_onlinetext_enabled'=>1));
1448
1449 $this->setUser($this->students[0]);
1450 $output = $assign->view_student_summary($this->students[0], true);
1451 $this->assertNotContains($output, get_string('editsubmission', 'assign'),
1452 'Should not be able to edit after cutoff date.');
1453 $this->assertNotContains($output, get_string('submitassignment', 'assign'),
1454 'Should not be able to submit after cutoff date.');
1455 }
f159ad73 1456 /**
1457 * Testing for submission comment plugin settings
1458 */
1459 public function test_submission_comment_plugin_settings() {
1460 global $CFG;
1461
1462 $commentconfig = false;
1463 if (!empty($CFG->usecomments)) {
1464 $commentconfig = $CFG->usecomments;
1465 }
1466
1467 $CFG->usecomments = true;
1468 $assign = $this->create_instance();
1469 $plugin = $assign->get_submission_plugin_by_type('comments');
1470 $this->assertEquals(1, $plugin->is_enabled('enabled'));
1471
1472 $assign = $this->create_instance(array('assignsubmission_comments_enabled' => 0));
1473 $plugin = $assign->get_submission_plugin_by_type('comments');
1474 $this->assertEquals(1, $plugin->is_enabled('enabled'));
1475
1476 $assign = $this->create_instance(array('assignsubmission_comments_enabled' => 1));
1477 $plugin = $assign->get_submission_plugin_by_type('comments');
1478 $this->assertEquals(1, $plugin->is_enabled('enabled'));
1479
1480 $CFG->usecomments = false;
1481 $assign = $this->create_instance();
1482 $plugin = $assign->get_submission_plugin_by_type('comments');
1483 $this->assertEquals(0, $plugin->is_enabled('enabled'));
1484
1485 $assign = $this->create_instance(array('assignsubmission_comments_enabled' => 0));
1486 $plugin = $assign->get_submission_plugin_by_type('comments');
1487 $this->assertEquals(0, $plugin->is_enabled('enabled'));
1488
1489 $assign = $this->create_instance(array('assignsubmission_comments_enabled' => 1));
1490 $plugin = $assign->get_submission_plugin_by_type('comments');
1491 $this->assertEquals(0, $plugin->is_enabled('enabled'));
1492
1493 $CFG->usecomments = $commentconfig;
1494 }
1495
1496 /**
1497 * Testing for feedback comment plugin settings
1498 */
1499 public function test_feedback_plugin_settings() {
1500
1501 $assign = $this->create_instance();
1502 $plugin = $assign->get_feedback_plugin_by_type('comments');
1503 $this->assertEquals(0, $plugin->is_enabled('enabled'));
1504
1505 $assign = $this->create_instance(array('assignfeedback_comments_enabled' => 0));
1506 $plugin = $assign->get_feedback_plugin_by_type('comments');
1507 $this->assertEquals(0, $plugin->is_enabled('enabled'));
1508
1509 $assign = $this->create_instance(array('assignfeedback_comments_enabled' => 1));
1510 $plugin = $assign->get_feedback_plugin_by_type('comments');
1511 $this->assertEquals(1, $plugin->is_enabled('enabled'));
1512 }
47f48152
DW
1513}
1514