MDL-42350 mod_assign: fixed display of submission button
[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
672 public function test_submissions_open() {
673 $this->setUser($this->editingteachers[0]);
674
675 $now = time();
676 $tomorrow = $now + 24*60*60;
677 $oneweek = $now + 7*24*60*60;
678 $yesterday = $now - 24*60*60;
679
680 $assign = $this->create_instance();
681 $this->assertEquals(true, $assign->testable_submissions_open($this->students[0]->id));
682
683 $assign = $this->create_instance(array('duedate'=>$tomorrow));
684 $this->assertEquals(true, $assign->testable_submissions_open($this->students[0]->id));
685
686 $assign = $this->create_instance(array('duedate'=>$yesterday));
687 $this->assertEquals(true, $assign->testable_submissions_open($this->students[0]->id));
688
689 $assign = $this->create_instance(array('duedate'=>$yesterday, 'cutoffdate'=>$tomorrow));
690 $this->assertEquals(true, $assign->testable_submissions_open($this->students[0]->id));
691
692 $assign = $this->create_instance(array('duedate'=>$yesterday, 'cutoffdate'=>$yesterday));
693 $this->assertEquals(false, $assign->testable_submissions_open($this->students[0]->id));
694
695 $assign->testable_save_user_extension($this->students[0]->id, $tomorrow);
696 $this->assertEquals(true, $assign->testable_submissions_open($this->students[0]->id));
697
698 $assign = $this->create_instance(array('submissiondrafts'=>1));
699 $this->assertEquals(true, $assign->testable_submissions_open($this->students[0]->id));
700
701 $this->setUser($this->students[0]);
702 $now = time();
703 $submission = $assign->get_user_submission($this->students[0]->id, true);
704 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
705 $assign->testable_update_submission($submission, $this->students[0]->id, true, false);
706 $this->setUser($this->editingteachers[0]);
707 $this->assertEquals(false, $assign->testable_submissions_open($this->students[0]->id));
708 }
709
710 public function test_get_graders() {
df211804 711 $this->create_extra_users();
47f48152
DW
712 $this->setUser($this->editingteachers[0]);
713 $assign = $this->create_instance();
714
a5c793c3
DW
715 $this->assertCount(self::DEFAULT_TEACHER_COUNT +
716 self::DEFAULT_EDITING_TEACHER_COUNT +
717 self::EXTRA_TEACHER_COUNT +
718 self::EXTRA_EDITING_TEACHER_COUNT,
719 $assign->testable_get_graders($this->students[0]->id));
47f48152
DW
720
721 $assign = $this->create_instance();
722 // Force create an assignment with SEPARATEGROUPS.
723 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
724 $params = array('course'=>$this->course->id);
725 $instance = $generator->create_instance($params);
726 $cm = get_coursemodule_from_instance('assign', $instance->id);
727 set_coursemodule_groupmode($cm->id, SEPARATEGROUPS);
728 $cm->groupmode = SEPARATEGROUPS;
729 $context = context_module::instance($cm->id);
730 $assign = new testable_assign($context, $cm, $this->course);
731
732 $this->setUser($this->students[1]);
a5c793c3 733 $this->assertCount(4, $assign->testable_get_graders($this->students[0]->id));
47f48152
DW
734 }
735
20b7dcfc
DW
736 public function test_group_members_only() {
737 global $CFG;
738
739 $this->setAdminUser();
740 $this->create_extra_users();
741 $CFG->enablegroupmembersonly = true;
742 $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $this->course->id));
743 groups_assign_grouping($grouping->id, $this->groups[0]->id);
744
745 // Force create an assignment with SEPARATEGROUPS.
746 $instance = $this->getDataGenerator()->create_module('assign', array('course'=>$this->course->id),
747 array('groupmembersonly' => SEPARATEGROUPS, 'groupingid' => $grouping->id));
748
749 $cm = get_coursemodule_from_instance('assign', $instance->id);
750 $context = context_module::instance($cm->id);
751 $assign = new testable_assign($context, $cm, $this->course);
752
753 $this->setUser($this->teachers[0]);
754 $this->assertCount(5, $assign->list_participants(0, true));
755
756 }
757
47f48152
DW
758 public function test_get_uniqueid_for_user() {
759 $this->setUser($this->editingteachers[0]);
760 $assign = $this->create_instance();
761
762 foreach ($this->students as $student) {
763 $uniqueid = $assign->get_uniqueid_for_user($student->id);
764 $this->assertEquals($student->id, $assign->get_user_id_for_uniqueid($uniqueid));
765 }
766 }
767
46692c3a 768 public function test_show_student_summary() {
c2114099 769 global $CFG, $PAGE;
46692c3a
DW
770
771 $this->setUser($this->editingteachers[0]);
772 $assign = $this->create_instance();
c2114099 773 $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
46692c3a
DW
774
775 // No feedback should be available because this student has not been graded.
776 $this->setUser($this->students[0]);
777 $output = $assign->view_student_summary($this->students[0], true);
778 $this->assertEquals(false, strpos($output, 'Feedback'), 'Do not show feedback if there is no grade');
779 // Simulate adding a grade.
780 $this->setUser($this->teachers[0]);
781 $data = new stdClass();
782 $data->grade = '50.0';
df211804 783 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
46692c3a 784
b0da618b 785 // Now we should see the feedback.
46692c3a
DW
786 $this->setUser($this->students[0]);
787 $output = $assign->view_student_summary($this->students[0], true);
788 $this->assertNotEquals(false, strpos($output, 'Feedback'), 'Show feedback if there is a grade');
789
790 // Now hide the grade in gradebook.
791 $this->setUser($this->teachers[0]);
792 require_once($CFG->libdir.'/gradelib.php');
793 $gradeitem = new grade_item(array(
794 'itemtype' => 'mod',
795 'itemmodule' => 'assign',
796 'iteminstance' => $assign->get_instance()->id,
797 'courseid' => $this->course->id));
798
799 $gradeitem->set_hidden(1, false);
800
801 // No feedback should be available because the grade is hidden.
802 $this->setUser($this->students[0]);
803 $output = $assign->view_student_summary($this->students[0], true);
804 $this->assertEquals(false, strpos($output, 'Feedback'), 'Do not show feedback if the grade is hidden in the gradebook');
805
b0da618b 806 // Do the same but add feedback.
46692c3a
DW
807 $assign = $this->create_instance(array('assignfeedback_comments_enabled' => 1));
808
809 $this->setUser($this->teachers[0]);
810 $grade = $assign->get_user_grade($this->students[0]->id, true);
811 $data = new stdClass();
812 $data->assignfeedbackcomments_editor = array('text'=>'Tomato sauce',
813 'format'=>FORMAT_MOODLE);
814 $plugin = $assign->get_feedback_plugin_by_type('comments');
815 $plugin->save($grade, $data);
816
b0da618b 817 // Should have feedback but no grade.
46692c3a
DW
818 $this->setUser($this->students[0]);
819 $output = $assign->view_student_summary($this->students[0], true);
ea698284 820 $this->assertNotEquals(false, strpos($output, 'Feedback'), 'Show feedback even if there is no grade');
46692c3a
DW
821 $this->assertEquals(false, strpos($output, 'Grade'), 'Do not show grade when there is no grade.');
822 $this->assertEquals(false, strpos($output, 'Graded on'), 'Do not show graded date when there is no grade.');
45aac51a 823
ea698284
AD
824 // Now hide the grade in gradebook.
825 $this->setUser($this->teachers[0]);
826 $gradeitem = new grade_item(array(
827 'itemtype' => 'mod',
828 'itemmodule' => 'assign',
829 'iteminstance' => $assign->get_instance()->id,
830 'courseid' => $this->course->id));
831
832 $gradeitem->set_hidden(1, false);
833
834 // No feedback should be available because the grade is hidden.
835 $this->setUser($this->students[0]);
836 $output = $assign->view_student_summary($this->students[0], true);
837 $this->assertEquals(false, strpos($output, 'Feedback'), 'Do not show feedback if the grade is hidden in the gradebook');
46692c3a
DW
838 }
839
df211804
DW
840 public function test_attempt_reopen_method_manual() {
841 global $PAGE;
842
843 $this->setUser($this->editingteachers[0]);
844 $assign = $this->create_instance(array('attemptreopenmethod'=>ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL,
845 'maxattempts'=>3,
846 'submissiondrafts'=>1,
847 'assignsubmission_onlinetext_enabled'=>1));
848 $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
849
850 // Student should be able to see an add submission button.
851 $this->setUser($this->students[0]);
852 $output = $assign->view_student_summary($this->students[0], true);
853 $this->assertNotEquals(false, strpos($output, get_string('addsubmission', 'assign')));
854
855 // Add a submission.
856 $now = time();
857 $submission = $assign->get_user_submission($this->students[0]->id, true);
858 $data = new stdClass();
859 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
860 'text'=>'Submission text',
861 'format'=>FORMAT_MOODLE);
862 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
863 $plugin->save($submission, $data);
864
865 // And now submit it for marking.
866 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
867 $assign->testable_update_submission($submission, $this->students[0]->id, true, false);
868
869 // Verify the student cannot make changes to the submission.
870 $output = $assign->view_student_summary($this->students[0], true);
871 $this->assertEquals(false, strpos($output, get_string('addsubmission', 'assign')));
872
873 // Mark the submission.
874 $this->setUser($this->teachers[0]);
875 $data = new stdClass();
876 $data->grade = '50.0';
877 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
878
879 // Check the student can see the grade.
880 $this->setUser($this->students[0]);
881 $output = $assign->view_student_summary($this->students[0], true);
882 $this->assertNotEquals(false, strpos($output, '50.0'));
883
884 // Allow the student another attempt.
885 $this->teachers[0]->ignoresesskey = true;
886 $this->setUser($this->teachers[0]);
887 $result = $assign->testable_process_add_attempt($this->students[0]->id);
888 $this->assertEquals(true, $result);
889
890 // Check that the previous attempt is now in the submission history table.
891 $this->setUser($this->students[0]);
892 $output = $assign->view_student_summary($this->students[0], true);
893 // Need a better check.
894 $this->assertNotEquals(false, strpos($output, 'Submission text'), 'Contains: Submission text');
895
896 // Check that the student now has a button for Add a new attempt".
897 $this->assertNotEquals(false, strpos($output, get_string('addnewattempt', 'assign')));
898 // Check that the student now does not have a button for Submit.
899 $this->assertEquals(false, strpos($output, get_string('submitassignment', 'assign')));
900
901 // Check that the student now has a submission history.
902 $this->assertNotEquals(false, strpos($output, get_string('attempthistory', 'assign')));
903
904 $this->setUser($this->teachers[0]);
905 // Check that the grading table loads correctly and contains this user.
906 // This is also testing that we do not get duplicate rows in the grading table.
907 $gradingtable = new assign_grading_table($assign, 100, '', 0, true);
908 $output = $assign->get_renderer()->render($gradingtable);
909 $this->assertEquals(true, strpos($output, $this->students[0]->lastname));
910
911 // Should be 1 not 2.
912 $this->assertEquals(1, $assign->count_submissions());
913 $this->assertEquals(1, $assign->count_submissions_with_status('reopened'));
914 $this->assertEquals(0, $assign->count_submissions_need_grading());
915 $this->assertEquals(1, $assign->count_grades());
916
917 // Change max attempts to unlimited.
918 $formdata = clone($assign->get_instance());
919 $formdata->maxattempts = ASSIGN_UNLIMITED_ATTEMPTS;
920 $formdata->instance = $formdata->id;
921 $assign->update_instance($formdata);
922
923 // Check we can repopen still.
924 $result = $assign->testable_process_add_attempt($this->students[0]->id);
925 $this->assertEquals(true, $result);
926
927 $grades = $assign->get_user_grades_for_gradebook($this->students[0]->id);
928 $this->assertEquals(50, (int)$grades[$this->students[0]->id]->rawgrade);
929
df211804 930 }
46692c3a 931
f8d107b3
DM
932 public function test_markingworkflow() {
933 global $PAGE;
934
935 $this->setUser($this->editingteachers[0]);
936 $assign = $this->create_instance(array('markingworkflow'=>1));
937 $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
938
13e82f1c 939 // Mark the submission and set to notmarked.
f8d107b3
DM
940 $this->setUser($this->teachers[0]);
941 $data = new stdClass();
942 $data->grade = '50.0';
943 $data->workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED;
944 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
945
946 // Check the student can't see the grade.
947 $this->setUser($this->students[0]);
948 $output = $assign->view_student_summary($this->students[0], true);
949 $this->assertEquals(false, strpos($output, '50.0'));
950
13e82f1c 951 // Mark the submission and set to inmarking.
f8d107b3
DM
952 $this->setUser($this->teachers[0]);
953 $data = new stdClass();
954 $data->grade = '50.0';
955 $data->workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_INMARKING;
956 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
957
958 // Check the student can't see the grade.
959 $this->setUser($this->students[0]);
960 $output = $assign->view_student_summary($this->students[0], true);
961 $this->assertEquals(false, strpos($output, '50.0'));
962
13e82f1c 963 // Mark the submission and set to readyforreview.
f8d107b3
DM
964 $this->setUser($this->teachers[0]);
965 $data = new stdClass();
966 $data->grade = '50.0';
967 $data->workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_READYFORREVIEW;
968 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
969
970 // Check the student can't see the grade.
971 $this->setUser($this->students[0]);
972 $output = $assign->view_student_summary($this->students[0], true);
973 $this->assertEquals(false, strpos($output, '50.0'));
974
13e82f1c 975 // Mark the submission and set to inreview.
f8d107b3
DM
976 $this->setUser($this->teachers[0]);
977 $data = new stdClass();
978 $data->grade = '50.0';
979 $data->workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_INREVIEW;
980 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
981
982 // Check the student can't see the grade.
983 $this->setUser($this->students[0]);
984 $output = $assign->view_student_summary($this->students[0], true);
985 $this->assertEquals(false, strpos($output, '50.0'));
986
13e82f1c 987 // Mark the submission and set to readyforrelease.
f8d107b3
DM
988 $this->setUser($this->teachers[0]);
989 $data = new stdClass();
990 $data->grade = '50.0';
991 $data->workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_READYFORRELEASE;
992 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
993
994 // Check the student can't see the grade.
995 $this->setUser($this->students[0]);
996 $output = $assign->view_student_summary($this->students[0], true);
997 $this->assertEquals(false, strpos($output, '50.0'));
998
13e82f1c 999 // Mark the submission and set to released.
f8d107b3
DM
1000 $this->setUser($this->teachers[0]);
1001 $data = new stdClass();
1002 $data->grade = '50.0';
1003 $data->workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_RELEASED;
1004 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
1005
1006 // Check the student can see the grade.
1007 $this->setUser($this->students[0]);
1008 $output = $assign->view_student_summary($this->students[0], true);
1009 $this->assertNotEquals(false, strpos($output, '50.0'));
1010 }
1011
1012 public function test_markerallocation() {
1013 global $PAGE;
1014
1015 $this->setUser($this->editingteachers[0]);
13e82f1c 1016 $assign = $this->create_instance(array('markingworkflow'=>1, 'markingallocation'=>1));
f8d107b3
DM
1017 $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
1018
13e82f1c 1019 // Allocate marker to submission.
f8d107b3
DM
1020 $data = new stdClass();
1021 $data->allocatedmarker = $this->teachers[0]->id;
1022 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
1023
13e82f1c 1024 // Check the allocated marker can view the submission.
f8d107b3
DM
1025 $this->setUser($this->teachers[0]);
1026 $gradingtable = new assign_grading_table($assign, 100, '', 0, true);
1027 $output = $assign->get_renderer()->render($gradingtable);
1028 $this->assertEquals(true, strpos($output, $this->students[0]->lastname));
1029
13e82f1c 1030 // Check that other teachers can't view this submission.
f8d107b3
DM
1031 $this->setUser($this->teachers[1]);
1032 $gradingtable = new assign_grading_table($assign, 100, '', 0, true);
1033 $output = $assign->get_renderer()->render($gradingtable);
1034 $this->assertNotEquals(true, strpos($output, $this->students[0]->lastname));
1035 }
76e77b05
FM
1036
1037 public function test_extension_granted_event() {
1038 $this->setUser($this->editingteachers[0]);
1039
1040 $tomorrow = time() + 24*60*60;
1041 $yesterday = time() - 24*60*60;
1042
1043 $assign = $this->create_instance(array('duedate' => $yesterday, 'cutoffdate' => $yesterday));
1044 $sink = $this->redirectEvents();
1045
1046 $assign->testable_save_user_extension($this->students[0]->id, $tomorrow);
1047
1048 $events = $sink->get_events();
1049 $this->assertCount(1, $events);
1050 $event = reset($events);
1051 $this->assertInstanceOf('\mod_assign\event\extension_granted', $event);
1052 $this->assertEquals($assign->get_context(), $event->get_context());
1053 $this->assertEquals($assign->get_instance()->id, $event->objectid);
1054 $this->assertEquals($this->students[0]->id, $event->relateduserid);
1055 $expected = array(
1056 $assign->get_course()->id,
1057 'assign',
1058 'grant extension',
1059 'view.php?id=' . $assign->get_course_module()->id,
1060 $this->students[0]->id,
1061 $assign->get_course_module()->id,
1062 $this->editingteachers[0]->id
1063 );
1064 $this->assertEventLegacyLogData($expected, $event);
1065 $sink->close();
1066 }
1067
3d1331be
FM
1068 public function test_submission_locked_event() {
1069 $this->editingteachers[0]->ignoresesskey = true;
1070 $this->setUser($this->editingteachers[0]);
1071
1072 $assign = $this->create_instance();
1073 $sink = $this->redirectEvents();
1074
05a6445a 1075 $assign->lock_submission($this->students[0]->id);
3d1331be
FM
1076
1077 $events = $sink->get_events();
1078 $this->assertCount(1, $events);
1079 $event = reset($events);
1080 $this->assertInstanceOf('\mod_assign\event\submission_locked', $event);
1081 $this->assertEquals($assign->get_context(), $event->get_context());
1082 $this->assertEquals($assign->get_instance()->id, $event->objectid);
1083 $this->assertEquals($this->students[0]->id, $event->relateduserid);
1084 $expected = array(
1085 $assign->get_course()->id,
1086 'assign',
1087 'lock submission',
1088 'view.php?id=' . $assign->get_course_module()->id,
1089 get_string('locksubmissionforstudent', 'assign', array('id' => $this->students[0]->id,
1090 'fullname' => fullname($this->students[0]))),
1091 $assign->get_course_module()->id,
1092 $this->editingteachers[0]->id
1093 );
1094 $this->assertEventLegacyLogData($expected, $event);
1095 $sink->close();
1096
1097 // Revert to defaults.
1098 $this->editingteachers[0]->ignoresesskey = false;
1099 }
1100
ad10ad14
FM
1101 public function test_identities_revealed_event() {
1102 $this->editingteachers[0]->ignoresesskey = true;
1103 $this->setUser($this->editingteachers[0]);
1104
1105 $assign = $this->create_instance(array('blindmarking'=>1));
1106 $sink = $this->redirectEvents();
1107
05a6445a 1108 $assign->reveal_identities();
ad10ad14
FM
1109
1110 $events = $sink->get_events();
1111 $this->assertCount(1, $events);
1112 $event = reset($events);
1113 $this->assertInstanceOf('\mod_assign\event\identities_revealed', $event);
1114 $this->assertEquals($assign->get_context(), $event->get_context());
1115 $this->assertEquals($assign->get_instance()->id, $event->objectid);
1116 $expected = array(
1117 $assign->get_course()->id,
1118 'assign',
1119 'reveal identities',
1120 'view.php?id=' . $assign->get_course_module()->id,
1121 get_string('revealidentities', 'assign'),
1122 $assign->get_course_module()->id,
1123 $this->editingteachers[0]->id
1124 );
1125 $this->assertEventLegacyLogData($expected, $event);
1126 $sink->close();
1127
1128 // Revert to defaults.
1129 $this->editingteachers[0]->ignoresesskey = false;
1130 }
1131
159b7f40
FM
1132 public function test_submission_status_updated_event() {
1133 $this->editingteachers[0]->ignoresesskey = true;
1134 $this->setUser($this->editingteachers[0]);
1135
1136 $assign = $this->create_instance();
1137 $submission = $assign->get_user_submission($this->students[0]->id, true);
1138 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
1139 $assign->testable_update_submission($submission, $this->students[0]->id, true, false);
1140
1141 $sink = $this->redirectEvents();
05a6445a 1142 $assign->revert_to_draft($this->students[0]->id);
159b7f40
FM
1143
1144 $events = $sink->get_events();
1145 $this->assertCount(1, $events);
1146 $event = reset($events);
1147 $this->assertInstanceOf('\mod_assign\event\submission_status_updated', $event);
1148 $this->assertEquals($assign->get_context(), $event->get_context());
1149 $this->assertEquals($submission->id, $event->objectid);
1150 $this->assertEquals($this->students[0]->id, $event->relateduserid);
1151 $this->assertEquals(ASSIGN_SUBMISSION_STATUS_DRAFT, $event->other['newstatus']);
1152 $expected = array(
1153 $assign->get_course()->id,
1154 'assign',
1155 'revert submission to draft',
1156 'view.php?id=' . $assign->get_course_module()->id,
1157 get_string('reverttodraftforstudent', 'assign', array('id' => $this->students[0]->id,
1158 'fullname' => fullname($this->students[0]))),
1159 $assign->get_course_module()->id,
1160 $this->editingteachers[0]->id
1161 );
1162 $this->assertEventLegacyLogData($expected, $event);
1163 $sink->close();
1164
1165 // Revert to defaults.
1166 $this->editingteachers[0]->ignoresesskey = false;
1167 }
1168
e59a85ba
FM
1169 public function test_marker_updated_event() {
1170 $this->editingteachers[0]->ignoresesskey = true;
1171 $this->setUser($this->editingteachers[0]);
1172
1173 $assign = $this->create_instance();
1174
1175 $sink = $this->redirectEvents();
1176 $assign->testable_process_set_batch_marking_allocation($this->students[0]->id, $this->teachers[0]->id);
1177
1178 $events = $sink->get_events();
1179 $this->assertCount(1, $events);
1180 $event = reset($events);
1181 $this->assertInstanceOf('\mod_assign\event\marker_updated', $event);
1182 $this->assertEquals($assign->get_context(), $event->get_context());
1183 $this->assertEquals($assign->get_instance()->id, $event->objectid);
1184 $this->assertEquals($this->students[0]->id, $event->relateduserid);
1185 $this->assertEquals($this->editingteachers[0]->id, $event->userid);
1186 $this->assertEquals($this->teachers[0]->id, $event->other['markerid']);
1187 $expected = array(
1188 $assign->get_course()->id,
1189 'assign',
1190 'set marking allocation',
1191 'view.php?id=' . $assign->get_course_module()->id,
1192 get_string('setmarkerallocationforlog', 'assign', array('id' => $this->students[0]->id,
1193 'fullname' => fullname($this->students[0]), 'marker' => fullname($this->teachers[0]))),
1194 $assign->get_course_module()->id,
1195 $this->editingteachers[0]->id
1196 );
1197 $this->assertEventLegacyLogData($expected, $event);
1198 $sink->close();
1199
1200 // Revert to defaults.
1201 $this->editingteachers[0]->ignoresesskey = false;
1202 }
1203
8bb213eb
FM
1204 public function test_workflow_state_updated_event() {
1205 $this->editingteachers[0]->ignoresesskey = true;
1206 $this->setUser($this->editingteachers[0]);
1207
1208 $assign = $this->create_instance();
1209
1210 $sink = $this->redirectEvents();
1211 $assign->testable_process_set_batch_marking_workflow_state($this->students[0]->id, ASSIGN_MARKING_WORKFLOW_STATE_INREVIEW);
1212
1213 $events = $sink->get_events();
1214 $this->assertCount(1, $events);
1215 $event = reset($events);
1216 $this->assertInstanceOf('\mod_assign\event\workflow_state_updated', $event);
1217 $this->assertEquals($assign->get_context(), $event->get_context());
1218 $this->assertEquals($assign->get_instance()->id, $event->objectid);
1219 $this->assertEquals($this->students[0]->id, $event->relateduserid);
1220 $this->assertEquals($this->editingteachers[0]->id, $event->userid);
1221 $this->assertEquals(ASSIGN_MARKING_WORKFLOW_STATE_INREVIEW, $event->other['newstate']);
1222 $expected = array(
1223 $assign->get_course()->id,
1224 'assign',
1225 'set marking workflow state',
1226 'view.php?id=' . $assign->get_course_module()->id,
1227 get_string('setmarkingworkflowstateforlog', 'assign', array('id' => $this->students[0]->id,
1228 'fullname' => fullname($this->students[0]), 'state' => ASSIGN_MARKING_WORKFLOW_STATE_INREVIEW)),
1229 $assign->get_course_module()->id,
1230 $this->editingteachers[0]->id
1231 );
1232 $this->assertEventLegacyLogData($expected, $event);
1233 $sink->close();
1234
1235 // Revert to defaults.
1236 $this->editingteachers[0]->ignoresesskey = false;
1237 }
89fbc202
FM
1238
1239 public function test_submission_duplicated_event() {
1240 $this->setUser($this->students[0]);
1241
1242 $assign = $this->create_instance();
1243 $submission1 = $assign->get_user_submission($this->students[0]->id, true, 0);
1244 $submission2 = $assign->get_user_submission($this->students[0]->id, true, 1);
1245 $submission2->status = ASSIGN_SUBMISSION_STATUS_REOPENED;
1246 $assign->testable_update_submission($submission2, $this->students[0]->id, time(), $assign->get_instance()->teamsubmission);
1247
1248 $sink = $this->redirectEvents();
1249 $notices = null;
05a6445a 1250 $assign->copy_previous_attempt($notices);
89fbc202
FM
1251
1252 $events = $sink->get_events();
1253 $this->assertCount(1, $events);
1254 $event = reset($events);
1255 $this->assertInstanceOf('\mod_assign\event\submission_duplicated', $event);
1256 $this->assertEquals($assign->get_context(), $event->get_context());
1257 $this->assertEquals($submission2->id, $event->objectid);
1258 $this->assertEquals($this->students[0]->id, $event->userid);
1259 $submission2->status = ASSIGN_SUBMISSION_STATUS_DRAFT;
1260 $expected = array(
1261 $assign->get_course()->id,
1262 'assign',
1263 'submissioncopied',
1264 'view.php?id=' . $assign->get_course_module()->id,
1265 $assign->testable_format_submission_for_log($submission2),
1266 $assign->get_course_module()->id,
1267 $this->students[0]->id
1268 );
1269 $this->assertEventLegacyLogData($expected, $event);
1270 $sink->close();
1271 }
1272
9a289c3d
FM
1273 public function test_submission_unlocked_event() {
1274 $this->editingteachers[0]->ignoresesskey = true;
1275 $this->setUser($this->editingteachers[0]);
1276
1277 $assign = $this->create_instance();
1278 $sink = $this->redirectEvents();
1279
05a6445a 1280 $assign->unlock_submission($this->students[0]->id);
9a289c3d
FM
1281
1282 $events = $sink->get_events();
1283 $this->assertCount(1, $events);
1284 $event = reset($events);
1285 $this->assertInstanceOf('\mod_assign\event\submission_unlocked', $event);
1286 $this->assertEquals($assign->get_context(), $event->get_context());
1287 $this->assertEquals($assign->get_instance()->id, $event->objectid);
1288 $this->assertEquals($this->students[0]->id, $event->relateduserid);
1289 $expected = array(
1290 $assign->get_course()->id,
1291 'assign',
1292 'unlock submission',
1293 'view.php?id=' . $assign->get_course_module()->id,
1294 get_string('unlocksubmissionforstudent', 'assign', array('id' => $this->students[0]->id,
1295 'fullname' => fullname($this->students[0]))),
1296 $assign->get_course_module()->id,
1297 $this->editingteachers[0]->id
1298 );
1299 $this->assertEventLegacyLogData($expected, $event);
1300 $sink->close();
1301
1302 // Revert to defaults.
1303 $this->editingteachers[0]->ignoresesskey = false;
1304 }
1305
bd2e9829
FM
1306 public function test_submission_graded_event() {
1307 $this->setUser($this->editingteachers[0]);
1308 $assign = $this->create_instance();
1309
1310 // Test apply_grade_to_user.
1311 $sink = $this->redirectEvents();
1312
1313 $data = new stdClass();
1314 $data->grade = '50.0';
1315 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
1316 $grade = $assign->get_user_grade($this->students[0]->id, false, 0);
1317
1318 $events = $sink->get_events();
1319 $this->assertCount(1, $events);
1320 $event = reset($events);
1321 $this->assertInstanceOf('\mod_assign\event\submission_graded', $event);
1322 $this->assertEquals($assign->get_context(), $event->get_context());
1323 $this->assertEquals($grade->id, $event->objectid);
1324 $this->assertEquals($this->students[0]->id, $event->relateduserid);
1325 $expected = array(
1326 $assign->get_course()->id,
1327 'assign',
1328 'grade submission',
1329 'view.php?id=' . $assign->get_course_module()->id,
1330 $assign->format_grade_for_log($grade),
1331 $assign->get_course_module()->id,
1332 $this->editingteachers[0]->id
1333 );
1334 $this->assertEventLegacyLogData($expected, $event);
1335 $sink->close();
1336
1337 // Test process_save_quick_grades.
1338 $sink = $this->redirectEvents();
1339
1340 $data = array(
1341 'grademodified_' . $this->students[0]->id => time(),
1342 'quickgrade_' . $this->students[0]->id => '60.0'
1343 );
1344 $assign->testable_process_save_quick_grades($data);
1345 $grade = $assign->get_user_grade($this->students[0]->id, false);
1346 $this->assertEquals('60.0', $grade->grade);
1347
1348 $events = $sink->get_events();
1349 $this->assertCount(1, $events);
1350 $event = reset($events);
1351 $this->assertInstanceOf('\mod_assign\event\submission_graded', $event);
1352 $this->assertEquals($assign->get_context(), $event->get_context());
1353 $this->assertEquals($grade->id, $event->objectid);
1354 $this->assertEquals($this->students[0]->id, $event->relateduserid);
1355 $expected = array(
1356 $assign->get_course()->id,
1357 'assign',
1358 'grade submission',
1359 'view.php?id=' . $assign->get_course_module()->id,
1360 $assign->format_grade_for_log($grade),
1361 $assign->get_course_module()->id,
1362 $this->editingteachers[0]->id
1363 );
1364 $this->assertEventLegacyLogData($expected, $event);
1365 $sink->close();
1366 }
1367
9054c04d 1368 public function test_disable_submit_after_cutoff_date() {
1369 global $PAGE;
1370
1371 $this->setUser($this->editingteachers[0]);
1372 $now = time();
1373 $tomorrow = $now + 24*60*60;
1374 $lastweek = $now - 7*24*60*60;
1375 $yesterday = $now - 24*60*60;
1376
1377 $assign = $this->create_instance(array('duedate'=>$yesterday,
1378 'cutoffdate'=>$tomorrow,
1379 'assignsubmission_onlinetext_enabled'=>1));
1380 $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
1381
1382 // Student should be able to see an add submission button.
1383 $this->setUser($this->students[0]);
1384 $output = $assign->view_student_summary($this->students[0], true);
1385 $this->assertNotEquals(false, strpos($output, get_string('addsubmission', 'assign')));
1386
1387 // Add a submission but don't submit now.
1388 $submission = $assign->get_user_submission($this->students[0]->id, true);
1389 $data = new stdClass();
1390 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
1391 'text'=>'Submission text',
1392 'format'=>FORMAT_MOODLE);
1393 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
1394 $plugin->save($submission, $data);
1395
1396 // Create another instance with cut-off and due-date already passed.
1397 $this->setUser($this->editingteachers[0]);
1398 $now = time();
1399 $assign = $this->create_instance(array('duedate'=>$lastweek,
1400 'cutoffdate'=>$yesterday,
1401 'assignsubmission_onlinetext_enabled'=>1));
1402
1403 $this->setUser($this->students[0]);
1404 $output = $assign->view_student_summary($this->students[0], true);
1405 $this->assertNotContains($output, get_string('editsubmission', 'assign'),
1406 'Should not be able to edit after cutoff date.');
1407 $this->assertNotContains($output, get_string('submitassignment', 'assign'),
1408 'Should not be able to submit after cutoff date.');
1409 }
f159ad73 1410 /**
1411 * Testing for submission comment plugin settings
1412 */
1413 public function test_submission_comment_plugin_settings() {
1414 global $CFG;
1415
1416 $commentconfig = false;
1417 if (!empty($CFG->usecomments)) {
1418 $commentconfig = $CFG->usecomments;
1419 }
1420
1421 $CFG->usecomments = true;
1422 $assign = $this->create_instance();
1423 $plugin = $assign->get_submission_plugin_by_type('comments');
1424 $this->assertEquals(1, $plugin->is_enabled('enabled'));
1425
1426 $assign = $this->create_instance(array('assignsubmission_comments_enabled' => 0));
1427 $plugin = $assign->get_submission_plugin_by_type('comments');
1428 $this->assertEquals(1, $plugin->is_enabled('enabled'));
1429
1430 $assign = $this->create_instance(array('assignsubmission_comments_enabled' => 1));
1431 $plugin = $assign->get_submission_plugin_by_type('comments');
1432 $this->assertEquals(1, $plugin->is_enabled('enabled'));
1433
1434 $CFG->usecomments = false;
1435 $assign = $this->create_instance();
1436 $plugin = $assign->get_submission_plugin_by_type('comments');
1437 $this->assertEquals(0, $plugin->is_enabled('enabled'));
1438
1439 $assign = $this->create_instance(array('assignsubmission_comments_enabled' => 0));
1440 $plugin = $assign->get_submission_plugin_by_type('comments');
1441 $this->assertEquals(0, $plugin->is_enabled('enabled'));
1442
1443 $assign = $this->create_instance(array('assignsubmission_comments_enabled' => 1));
1444 $plugin = $assign->get_submission_plugin_by_type('comments');
1445 $this->assertEquals(0, $plugin->is_enabled('enabled'));
1446
1447 $CFG->usecomments = $commentconfig;
1448 }
1449
1450 /**
1451 * Testing for feedback comment plugin settings
1452 */
1453 public function test_feedback_plugin_settings() {
1454
1455 $assign = $this->create_instance();
1456 $plugin = $assign->get_feedback_plugin_by_type('comments');
1457 $this->assertEquals(0, $plugin->is_enabled('enabled'));
1458
1459 $assign = $this->create_instance(array('assignfeedback_comments_enabled' => 0));
1460 $plugin = $assign->get_feedback_plugin_by_type('comments');
1461 $this->assertEquals(0, $plugin->is_enabled('enabled'));
1462
1463 $assign = $this->create_instance(array('assignfeedback_comments_enabled' => 1));
1464 $plugin = $assign->get_feedback_plugin_by_type('comments');
1465 $this->assertEquals(1, $plugin->is_enabled('enabled'));
1466 }
47f48152
DW
1467}
1468