MDL-1626 mod_forum: Update discussion subscriptions when moving discussions
[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
47f48152 299 $this->setUser($this->editingteachers[0]);
f588ea50
FM
300 $userctx = context_user::instance($this->editingteachers[0]->id)->id;
301
302 // Hack to pretend that there was an editor involved. We need both $_POST and $_REQUEST, and a sesskey.
303 $draftid = file_get_unused_draft_itemid();
304 $_REQUEST['introeditor'] = $draftid;
305 $_POST['introeditor'] = $draftid;
306 $_POST['sesskey'] = sesskey();
307
308 // Write links to a draft area.
309 $fakearealink1 = file_rewrite_pluginfile_urls('<a href="@@PLUGINFILE@@/pic.gif">link</a>', 'draftfile.php', $userctx,
310 'user', 'draft', $draftid);
311 $fakearealink2 = file_rewrite_pluginfile_urls('<a href="@@PLUGINFILE@@/pic.gif">new</a>', 'draftfile.php', $userctx,
312 'user', 'draft', $draftid);
313
314 // Create a new assignment with links to a draft area.
315 $now = time();
316 $assign = $this->create_instance(array(
317 'duedate' => $now,
318 'intro' => $fakearealink1,
319 'introformat' => FORMAT_HTML
320 ));
47f48152
DW
321
322 // See if there is an event in the calendar.
323 $params = array('modulename'=>'assign', 'instance'=>$assign->get_instance()->id);
f588ea50
FM
324 $event = $DB->get_record('event', $params);
325 $this->assertNotEmpty($event);
326 $this->assertSame('link', $event->description); // The pluginfile links are removed.
47f48152 327
f588ea50
FM
328 // Make sure the same works when updating the assignment.
329 $instance = $assign->get_instance();
330 $instance->instance = $instance->id;
331 $instance->intro = $fakearealink2;
332 $instance->introformat = FORMAT_HTML;
333 $assign->update_instance($instance);
334 $params = array('modulename' => 'assign', 'instance' => $assign->get_instance()->id);
335 $event = $DB->get_record('event', $params);
336 $this->assertNotEmpty($event);
337 $this->assertSame('new', $event->description); // The pluginfile links are removed.
47f48152
DW
338 }
339
340 public function test_update_instance() {
341 global $DB;
342
343 $this->setUser($this->editingteachers[0]);
344 $assign = $this->create_instance(array('assignsubmission_onlinetext_enabled'=>1));
345
346 $now = time();
347 $instance = $assign->get_instance();
348 $instance->duedate = $now;
349 $instance->instance = $instance->id;
350 $instance->assignsubmission_onlinetext_enabled = 1;
47f48152
DW
351
352 $assign->update_instance($instance);
353
354 $instance = $DB->get_record('assign', array('id'=>$assign->get_instance()->id));
355 $this->assertEquals($now, $instance->duedate);
356 }
357
8fd00a24
DW
358 public function test_cannot_submit_empty() {
359 global $PAGE;
360
361 $this->setUser($this->editingteachers[0]);
362 $assign = $this->create_instance(array('submissiondrafts'=>1));
363
364 $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
365
366 // Test you cannot see the submit button for an offline assignment regardless.
367 $this->setUser($this->students[0]);
368 $output = $assign->view_student_summary($this->students[0], true);
369 $this->assertNotContains(get_string('submitassignment', 'assign'), $output, 'Can submit empty offline assignment');
370
371 // Test you cannot see the submit button for an online text assignment with no submission.
372 $this->setUser($this->editingteachers[0]);
373 $instance = $assign->get_instance();
374 $instance->instance = $instance->id;
375 $instance->assignsubmission_onlinetext_enabled = 1;
376
377 $assign->update_instance($instance);
378 $this->setUser($this->students[0]);
379 $output = $assign->view_student_summary($this->students[0], true);
380 $this->assertNotContains(get_string('submitassignment', 'assign'), $output, 'Cannot submit empty onlinetext assignment');
381
382 // Simulate a submission.
383 $submission = $assign->get_user_submission($this->students[0]->id, true);
384 $data = new stdClass();
385 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
386 'text'=>'Submission text',
387 'format'=>FORMAT_MOODLE);
388 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
389 $plugin->save($submission, $data);
390 // Test you can see the submit button for an online text assignment with a submission.
391 $output = $assign->view_student_summary($this->students[0], true);
392 $this->assertContains(get_string('submitassignment', 'assign'), $output, 'Can submit non empty onlinetext assignment');
393 }
394
47f48152 395 public function test_list_participants() {
df211804 396 $this->create_extra_users();
47f48152
DW
397 $this->setUser($this->editingteachers[0]);
398 $assign = $this->create_instance(array('grade'=>100));
399
a5c793c3 400 $this->assertEquals(self::DEFAULT_STUDENT_COUNT + self::EXTRA_STUDENT_COUNT, count($assign->list_participants(null, true)));
e6cc5347
RT
401
402 // Teacher with user preference set should see suspended users as well.
403 set_user_preference('grade_report_showonlyactiveenrol', false);
404 $assign = $this->create_instance(array('grade'=>100));
405 $this->assertEquals(self::DEFAULT_STUDENT_COUNT + self::EXTRA_STUDENT_COUNT + self::EXTRA_SUSPENDED_COUNT,
406 count($assign->list_participants(null, true)));
407
1ecb8044 408 // Non-editing teacher should not see suspended users, even if user preference is set.
e6cc5347
RT
409 $this->setUser($this->teachers[0]);
410 set_user_preference('grade_report_showonlyactiveenrol', false);
411 $assign = $this->create_instance(array('grade'=>100));
412 $this->assertEquals(self::DEFAULT_STUDENT_COUNT + self::EXTRA_STUDENT_COUNT, count($assign->list_participants(null, true)));
47f48152
DW
413 }
414
415 public function test_count_teams() {
df211804 416 $this->create_extra_users();
47f48152
DW
417 $this->setUser($this->editingteachers[0]);
418 $assign = $this->create_instance(array('teamsubmission'=>1));
419
a5c793c3 420 $this->assertEquals(self::GROUP_COUNT + 1, $assign->count_teams());
47f48152
DW
421 }
422
423 public function test_count_submissions() {
df211804 424 $this->create_extra_users();
47f48152
DW
425 $this->setUser($this->editingteachers[0]);
426 $assign = $this->create_instance(array('assignsubmission_onlinetext_enabled'=>1));
427
428 // Simulate a submission.
a5c793c3
DW
429 $this->setUser($this->extrastudents[0]);
430 $submission = $assign->get_user_submission($this->extrastudents[0]->id, true);
47f48152
DW
431 // Leave this one as DRAFT.
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 adding a grade.
440 $this->setUser($this->teachers[0]);
441 $data = new stdClass();
442 $data->grade = '50.0';
df211804 443 $assign->testable_apply_grade_to_user($data, $this->extrastudents[0]->id, 0);
47f48152
DW
444
445 // Simulate a submission.
a5c793c3
DW
446 $this->setUser($this->extrastudents[1]);
447 $submission = $assign->get_user_submission($this->extrastudents[1]->id, true);
47f48152 448 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
a5c793c3 449 $assign->testable_update_submission($submission, $this->extrastudents[1]->id, true, false);
47f48152
DW
450 $data = new stdClass();
451 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
452 'text'=>'Submission text',
453 'format'=>FORMAT_MOODLE);
454 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
455 $plugin->save($submission, $data);
456
457 // Simulate a submission.
a5c793c3
DW
458 $this->setUser($this->extrastudents[2]);
459 $submission = $assign->get_user_submission($this->extrastudents[2]->id, true);
47f48152 460 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
a5c793c3 461 $assign->testable_update_submission($submission, $this->extrastudents[2]->id, true, false);
47f48152
DW
462 $data = new stdClass();
463 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
464 'text'=>'Submission text',
465 'format'=>FORMAT_MOODLE);
466 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
467 $plugin->save($submission, $data);
468
469 // Simulate a submission.
a5c793c3
DW
470 $this->setUser($this->extrastudents[3]);
471 $submission = $assign->get_user_submission($this->extrastudents[3]->id, true);
47f48152 472 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
a5c793c3 473 $assign->testable_update_submission($submission, $this->extrastudents[3]->id, true, false);
47f48152
DW
474 $data = new stdClass();
475 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
476 'text'=>'Submission text',
477 'format'=>FORMAT_MOODLE);
478 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
479 $plugin->save($submission, $data);
480
e6cc5347
RT
481 // Simulate a submission for suspended user, this will never be counted.
482 $this->setUser($this->extrastudents[3]);
483 $submission = $assign->get_user_submission($this->extrasuspendedstudents[0]->id, true);
484 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
485 $assign->testable_update_submission($submission, $this->extrasuspendedstudents[0]->id, true, false);
486 $data = new stdClass();
487 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
488 'text'=>'Submission text',
489 'format'=>FORMAT_MOODLE);
490 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
491 $plugin->save($submission, $data);
492
47f48152
DW
493 // Simulate adding a grade.
494 $this->setUser($this->teachers[0]);
495 $data = new stdClass();
496 $data->grade = '50.0';
df211804 497 $assign->testable_apply_grade_to_user($data, $this->extrastudents[3]->id, 0);
e6cc5347 498 $assign->testable_apply_grade_to_user($data, $this->extrasuspendedstudents[0]->id, 0);
47f48152
DW
499
500 $this->assertEquals(2, $assign->count_grades());
501 $this->assertEquals(4, $assign->count_submissions());
502 $this->assertEquals(2, $assign->count_submissions_need_grading());
503 $this->assertEquals(3, $assign->count_submissions_with_status(ASSIGN_SUBMISSION_STATUS_SUBMITTED));
504 $this->assertEquals(1, $assign->count_submissions_with_status(ASSIGN_SUBMISSION_STATUS_DRAFT));
505 }
506
507 public function test_get_grading_userid_list() {
df211804 508 $this->create_extra_users();
47f48152
DW
509 $this->setUser($this->editingteachers[0]);
510 $assign = $this->create_instance();
511
512 $users = $assign->testable_get_grading_userid_list();
a5c793c3 513 $this->assertEquals(self::DEFAULT_STUDENT_COUNT + self::EXTRA_STUDENT_COUNT, count($users));
e6cc5347
RT
514
515 $this->setUser($this->editingteachers[0]);
516 set_user_preference('grade_report_showonlyactiveenrol', false);
517 $assign = $this->create_instance();
518
519 $users = $assign->testable_get_grading_userid_list();
520 $this->assertEquals(self::DEFAULT_STUDENT_COUNT + self::EXTRA_STUDENT_COUNT + self::EXTRA_SUSPENDED_COUNT, count($users));
47f48152
DW
521 }
522
523 public function test_cron() {
524 // First run cron so there are no messages waiting to be sent (from other tests).
525 cron_setup_user();
526 assign::cron();
527
528 // Now create an assignment and add some feedback.
529 $this->setUser($this->editingteachers[0]);
8e1266bf 530 $assign = $this->create_instance(array('sendstudentnotifications'=>1));
47f48152
DW
531
532 // Simulate adding a grade.
533 $this->setUser($this->teachers[0]);
534 $data = new stdClass();
535 $data->grade = '50.0';
df211804 536 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
4d2bd673 537 $assign->testable_apply_grade_to_user($data, $this->students[1]->id, 0);
47f48152 538
f7dc9871
DW
539 $data->sendstudentnotifications = false;
540 $assign->testable_apply_grade_to_user($data, $this->students[2]->id, 0);
541
47f48152
DW
542 // Now run cron and see that one message was sent.
543 $this->preventResetByRollback();
544 $sink = $this->redirectMessages();
545 cron_setup_user();
4d2bd673 546 $this->expectOutputRegex('/Done processing 2 assignment submissions/');
47f48152
DW
547 assign::cron();
548
549 $messages = $sink->get_messages();
f7dc9871 550 // The sent count should be 2, because the 3rd one was marked as do not send notifications.
4d2bd673 551 $this->assertEquals(2, count($messages));
47f48152
DW
552 $this->assertEquals(1, $messages[0]->notification);
553 $this->assertEquals($assign->get_instance()->name, $messages[0]->contexturlname);
554 }
555
556 public function test_is_graded() {
557 $this->setUser($this->editingteachers[0]);
558 $assign = $this->create_instance();
559
560 // Simulate adding a grade.
561 $this->setUser($this->teachers[0]);
562 $data = new stdClass();
563 $data->grade = '50.0';
df211804 564 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
47f48152
DW
565
566 $this->assertEquals(true, $assign->testable_is_graded($this->students[0]->id));
567 $this->assertEquals(false, $assign->testable_is_graded($this->students[1]->id));
568 }
569
4a47008c
DW
570 public function test_can_grade() {
571 global $DB;
572
573 $this->setUser($this->editingteachers[0]);
574 $assign = $this->create_instance();
575
576 $this->setUser($this->students[0]);
577 $this->assertEquals(false, $assign->can_grade());
578 $this->setUser($this->editingteachers[0]);
579 $this->assertEquals(true, $assign->can_grade());
580 $this->setUser($this->teachers[0]);
581 $this->assertEquals(true, $assign->can_grade());
582
583 // Test the viewgrades capability - without mod/assign:grade.
584 $this->setUser($this->students[0]);
585 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
586 assign_capability('mod/assign:viewgrades', CAP_ALLOW, $studentrole->id, $assign->get_context()->id);
587 $this->assertEquals(false, $assign->can_grade());
588 }
589
47f48152 590 public function test_can_view_submission() {
4a47008c
DW
591 global $DB;
592
e6cc5347 593 $this->create_extra_users();
47f48152
DW
594 $this->setUser($this->editingteachers[0]);
595 $assign = $this->create_instance();
596
597 $this->setUser($this->students[0]);
598 $this->assertEquals(true, $assign->can_view_submission($this->students[0]->id));
599 $this->assertEquals(false, $assign->can_view_submission($this->students[1]->id));
600 $this->assertEquals(false, $assign->can_view_submission($this->teachers[0]->id));
601 $this->setUser($this->teachers[0]);
602 $this->assertEquals(true, $assign->can_view_submission($this->students[0]->id));
603 $this->assertEquals(true, $assign->can_view_submission($this->students[1]->id));
604 $this->assertEquals(true, $assign->can_view_submission($this->teachers[0]->id));
e6cc5347 605 $this->assertEquals(false, $assign->can_view_submission($this->extrasuspendedstudents[0]->id));
47f48152
DW
606 $this->setUser($this->editingteachers[0]);
607 $this->assertEquals(true, $assign->can_view_submission($this->students[0]->id));
608 $this->assertEquals(true, $assign->can_view_submission($this->students[1]->id));
609 $this->assertEquals(true, $assign->can_view_submission($this->teachers[0]->id));
e6cc5347 610 $this->assertEquals(true, $assign->can_view_submission($this->extrasuspendedstudents[0]->id));
4a47008c
DW
611
612 // Test the viewgrades capability - without mod/assign:grade.
613 $this->setUser($this->students[0]);
614 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
615 assign_capability('mod/assign:viewgrades', CAP_ALLOW, $studentrole->id, $assign->get_context()->id);
616 $this->assertEquals(true, $assign->can_view_submission($this->students[0]->id));
617 $this->assertEquals(true, $assign->can_view_submission($this->students[1]->id));
618 $this->assertEquals(true, $assign->can_view_submission($this->teachers[0]->id));
619 $this->assertEquals(false, $assign->can_view_submission($this->extrasuspendedstudents[0]->id));
47f48152
DW
620 }
621
622
623 public function test_update_submission() {
df211804 624 $this->create_extra_users();
47f48152
DW
625 $this->setUser($this->editingteachers[0]);
626 $assign = $this->create_instance();
627
a5c793c3 628 $this->setUser($this->extrastudents[0]);
47f48152 629 $now = time();
a5c793c3
DW
630 $submission = $assign->get_user_submission($this->extrastudents[0]->id, true);
631 $assign->testable_update_submission($submission, $this->extrastudents[0]->id, true, false);
47f48152
DW
632
633 $this->setUser($this->teachers[0]);
634 // Verify the gradebook update.
635 $gradinginfo = grade_get_grades($this->course->id,
636 'mod',
637 'assign',
638 $assign->get_instance()->id,
a5c793c3 639 $this->extrastudents[0]->id);
47f48152 640
a5c793c3
DW
641 $this->assertEquals($this->extrastudents[0]->id,
642 $gradinginfo->items[0]->grades[$this->extrastudents[0]->id]->usermodified);
47f48152
DW
643
644 // Now verify group assignments.
645 $this->setUser($this->editingteachers[0]);
646 $assign = $this->create_instance(array('teamsubmission'=>1));
647
a5c793c3 648 $this->setUser($this->extrastudents[0]);
47f48152 649 $now = time();
a5c793c3
DW
650 $submission = $assign->get_group_submission($this->extrastudents[0]->id, 0, true);
651 $assign->testable_update_submission($submission, $this->extrastudents[0]->id, true, true);
47f48152 652
e6cc5347 653 // Check that at least 2 active members and 1 suspended member of the submission group had their submission updated.
47f48152
DW
654
655 $this->setUser($this->editingteachers[0]);
656 $gradinginfo = grade_get_grades($this->course->id,
657 'mod',
658 'assign',
659 $assign->get_instance()->id,
a5c793c3 660 $this->extrastudents[0]->id);
47f48152 661
a5c793c3
DW
662 $this->assertEquals($this->extrastudents[0]->id,
663 $gradinginfo->items[0]->grades[$this->extrastudents[0]->id]->usermodified);
47f48152
DW
664
665 $gradinginfo = grade_get_grades($this->course->id,
666 'mod',
667 'assign',
668 $assign->get_instance()->id,
a5c793c3 669 $this->extrastudents[self::GROUP_COUNT]->id);
47f48152 670
a5c793c3
DW
671 $this->assertEquals($this->extrastudents[self::GROUP_COUNT]->id,
672 $gradinginfo->items[0]->grades[$this->extrastudents[self::GROUP_COUNT]->id]->usermodified);
47f48152 673
e6cc5347
RT
674 $gradinginfo = grade_get_grades($this->course->id,
675 'mod',
676 'assign',
677 $assign->get_instance()->id,
678 $this->extrasuspendedstudents[0]->id);
679 $this->assertEquals($this->extrasuspendedstudents[0]->id,
680 $gradinginfo->items[0]->grades[$this->extrasuspendedstudents[0]->id]->usermodified);
681
682 // Check the same with non-editing teacher and make sure submission is not updated for suspended user.
683 $this->setUser($this->editingteachers[0]);
684 $assign = $this->create_instance(array('teamsubmission'=>1));
685
686 $this->setUser($this->extrastudents[1]);
687 $now = time();
688 $submission = $assign->get_group_submission($this->extrastudents[1]->id, 0, true);
689 $assign->testable_update_submission($submission, $this->extrastudents[1]->id, true, true);
690
691 $this->setUser($this->teachers[0]);
692 $gradinginfo = grade_get_grades($this->course->id,
693 'mod',
694 'assign',
695 $assign->get_instance()->id,
696 $this->extrastudents[1]->id);
697
698 $this->assertEquals($this->extrastudents[1]->id,
699 $gradinginfo->items[0]->grades[$this->extrastudents[1]->id]->usermodified);
700
701 $gradinginfo = grade_get_grades($this->course->id,
702 'mod',
703 'assign',
704 $assign->get_instance()->id,
705 $this->extrastudents[self::GROUP_COUNT+1]->id);
706
707 $this->assertEquals($this->extrastudents[self::GROUP_COUNT+1]->id,
708 $gradinginfo->items[0]->grades[$this->extrastudents[self::GROUP_COUNT+1]->id]->usermodified);
709
710 $gradinginfo = grade_get_grades($this->course->id,
711 'mod',
712 'assign',
713 $assign->get_instance()->id,
714 $this->extrasuspendedstudents[1]->id);
715 $this->assertEquals($this->extrasuspendedstudents[1]->id,
716 $gradinginfo->items[0]->grades[$this->extrasuspendedstudents[1]->id]->usermodified);
717
47f48152
DW
718 // Now verify blind marking.
719 $this->setUser($this->editingteachers[0]);
720 $assign = $this->create_instance(array('blindmarking'=>1));
721
a5c793c3 722 $this->setUser($this->extrastudents[0]);
47f48152 723 $now = time();
a5c793c3
DW
724 $submission = $assign->get_user_submission($this->extrastudents[0]->id, true);
725 $assign->testable_update_submission($submission, $this->extrastudents[0]->id, true, false);
47f48152
DW
726
727 $this->setUser($this->editingteachers[0]);
728 $gradinginfo = grade_get_grades($this->course->id,
729 'mod',
730 'assign',
731 $assign->get_instance()->id,
a5c793c3 732 $this->extrastudents[0]->id);
47f48152 733
a5c793c3 734 $this->assertEquals(null, $gradinginfo->items[0]->grades[$this->extrastudents[0]->id]->datesubmitted);
47f48152
DW
735 }
736
2bd27878
DW
737 public function test_group_submissions_submit_for_marking() {
738 global $PAGE;
739
740 $this->create_extra_users();
741 // Now verify group assignments.
742 $this->setUser($this->editingteachers[0]);
743 $assign = $this->create_instance(array('teamsubmission'=>1,
744 'assignsubmission_onlinetext_enabled'=>1,
745 'submissiondrafts'=>1,
746 'requireallteammemberssubmit'=>1));
747 $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
748
749 $this->setUser($this->extrastudents[0]);
750 // Add a submission.
751 $data = new stdClass();
752 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
753 'text'=>'Submission text',
754 'format'=>FORMAT_MOODLE);
755
756 $notices = array();
757 $assign->save_submission($data, $notices);
758
759 // Check we can see the submit button.
760 $output = $assign->view_student_summary($this->extrastudents[0], true);
761 $this->assertContains(get_string('submitassignment', 'assign'), $output);
762
763 $submission = $assign->get_group_submission($this->extrastudents[0]->id, 0, true);
764 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
765 $assign->testable_update_submission($submission, $this->extrastudents[0]->id, true, true);
766
767 // Check that the student does not see "Submit" button.
768 $output = $assign->view_student_summary($this->extrastudents[0], true);
769 $this->assertNotContains(get_string('submitassignment', 'assign'), $output);
770
771 // Change to another user in the same group.
772 $this->setUser($this->extrastudents[self::GROUP_COUNT]);
773 $output = $assign->view_student_summary($this->extrastudents[self::GROUP_COUNT], true);
774 $this->assertContains(get_string('submitassignment', 'assign'), $output);
775
776 $submission = $assign->get_group_submission($this->extrastudents[self::GROUP_COUNT]->id, 0, true);
777 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
778 $assign->testable_update_submission($submission, $this->extrastudents[self::GROUP_COUNT]->id, true, true);
779 $output = $assign->view_student_summary($this->extrastudents[self::GROUP_COUNT], true);
780 $this->assertNotContains(get_string('submitassignment', 'assign'), $output);
781 }
782
47f48152
DW
783 public function test_submissions_open() {
784 $this->setUser($this->editingteachers[0]);
785
786 $now = time();
787 $tomorrow = $now + 24*60*60;
788 $oneweek = $now + 7*24*60*60;
789 $yesterday = $now - 24*60*60;
790
791 $assign = $this->create_instance();
792 $this->assertEquals(true, $assign->testable_submissions_open($this->students[0]->id));
793
794 $assign = $this->create_instance(array('duedate'=>$tomorrow));
795 $this->assertEquals(true, $assign->testable_submissions_open($this->students[0]->id));
796
797 $assign = $this->create_instance(array('duedate'=>$yesterday));
798 $this->assertEquals(true, $assign->testable_submissions_open($this->students[0]->id));
799
800 $assign = $this->create_instance(array('duedate'=>$yesterday, 'cutoffdate'=>$tomorrow));
801 $this->assertEquals(true, $assign->testable_submissions_open($this->students[0]->id));
802
803 $assign = $this->create_instance(array('duedate'=>$yesterday, 'cutoffdate'=>$yesterday));
804 $this->assertEquals(false, $assign->testable_submissions_open($this->students[0]->id));
805
806 $assign->testable_save_user_extension($this->students[0]->id, $tomorrow);
807 $this->assertEquals(true, $assign->testable_submissions_open($this->students[0]->id));
808
809 $assign = $this->create_instance(array('submissiondrafts'=>1));
810 $this->assertEquals(true, $assign->testable_submissions_open($this->students[0]->id));
811
812 $this->setUser($this->students[0]);
813 $now = time();
814 $submission = $assign->get_user_submission($this->students[0]->id, true);
815 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
816 $assign->testable_update_submission($submission, $this->students[0]->id, true, false);
817 $this->setUser($this->editingteachers[0]);
818 $this->assertEquals(false, $assign->testable_submissions_open($this->students[0]->id));
819 }
820
821 public function test_get_graders() {
df211804 822 $this->create_extra_users();
47f48152 823 $this->setUser($this->editingteachers[0]);
47f48152 824
f268fb5d
DW
825 // Create an assignment with no groups.
826 $assign = $this->create_instance();
a5c793c3
DW
827 $this->assertCount(self::DEFAULT_TEACHER_COUNT +
828 self::DEFAULT_EDITING_TEACHER_COUNT +
829 self::EXTRA_TEACHER_COUNT +
830 self::EXTRA_EDITING_TEACHER_COUNT,
831 $assign->testable_get_graders($this->students[0]->id));
47f48152 832
47f48152 833 // Force create an assignment with SEPARATEGROUPS.
f268fb5d
DW
834 $data = new stdClass();
835 $data->courseid = $this->course->id;
836 $data->name = 'Grouping';
837 $groupingid = groups_create_grouping($data);
838 groups_assign_grouping($groupingid, $this->groups[0]->id);
839 $assign = $this->create_instance(array('groupingid' => $groupingid, 'groupmode' => SEPARATEGROUPS));
47f48152
DW
840
841 $this->setUser($this->students[1]);
a5c793c3 842 $this->assertCount(4, $assign->testable_get_graders($this->students[0]->id));
f268fb5d
DW
843 // Note the second student is in a group that is not in the grouping.
844 // This means that we get all graders that are not in a group in the grouping.
845 $this->assertCount(10, $assign->testable_get_graders($this->students[1]->id));
47f48152
DW
846 }
847
20b7dcfc
DW
848 public function test_group_members_only() {
849 global $CFG;
850
851 $this->setAdminUser();
852 $this->create_extra_users();
853 $CFG->enablegroupmembersonly = true;
854 $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $this->course->id));
855 groups_assign_grouping($grouping->id, $this->groups[0]->id);
856
857 // Force create an assignment with SEPARATEGROUPS.
858 $instance = $this->getDataGenerator()->create_module('assign', array('course'=>$this->course->id),
859 array('groupmembersonly' => SEPARATEGROUPS, 'groupingid' => $grouping->id));
860
861 $cm = get_coursemodule_from_instance('assign', $instance->id);
862 $context = context_module::instance($cm->id);
863 $assign = new testable_assign($context, $cm, $this->course);
864
865 $this->setUser($this->teachers[0]);
8270f0d0 866 get_fast_modinfo($this->course, 0, true);
20b7dcfc
DW
867 $this->assertCount(5, $assign->list_participants(0, true));
868
869 }
870
47f48152
DW
871 public function test_get_uniqueid_for_user() {
872 $this->setUser($this->editingteachers[0]);
873 $assign = $this->create_instance();
874
875 foreach ($this->students as $student) {
876 $uniqueid = $assign->get_uniqueid_for_user($student->id);
877 $this->assertEquals($student->id, $assign->get_user_id_for_uniqueid($uniqueid));
878 }
879 }
880
46692c3a 881 public function test_show_student_summary() {
c2114099 882 global $CFG, $PAGE;
46692c3a
DW
883
884 $this->setUser($this->editingteachers[0]);
885 $assign = $this->create_instance();
c2114099 886 $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
46692c3a
DW
887
888 // No feedback should be available because this student has not been graded.
889 $this->setUser($this->students[0]);
890 $output = $assign->view_student_summary($this->students[0], true);
891 $this->assertEquals(false, strpos($output, 'Feedback'), 'Do not show feedback if there is no grade');
892 // Simulate adding a grade.
893 $this->setUser($this->teachers[0]);
894 $data = new stdClass();
895 $data->grade = '50.0';
df211804 896 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
46692c3a 897
b0da618b 898 // Now we should see the feedback.
46692c3a
DW
899 $this->setUser($this->students[0]);
900 $output = $assign->view_student_summary($this->students[0], true);
901 $this->assertNotEquals(false, strpos($output, 'Feedback'), 'Show feedback if there is a grade');
902
903 // Now hide the grade in gradebook.
904 $this->setUser($this->teachers[0]);
905 require_once($CFG->libdir.'/gradelib.php');
906 $gradeitem = new grade_item(array(
907 'itemtype' => 'mod',
908 'itemmodule' => 'assign',
909 'iteminstance' => $assign->get_instance()->id,
910 'courseid' => $this->course->id));
911
912 $gradeitem->set_hidden(1, false);
913
914 // No feedback should be available because the grade is hidden.
915 $this->setUser($this->students[0]);
916 $output = $assign->view_student_summary($this->students[0], true);
917 $this->assertEquals(false, strpos($output, 'Feedback'), 'Do not show feedback if the grade is hidden in the gradebook');
918
b0da618b 919 // Do the same but add feedback.
46692c3a
DW
920 $assign = $this->create_instance(array('assignfeedback_comments_enabled' => 1));
921
922 $this->setUser($this->teachers[0]);
923 $grade = $assign->get_user_grade($this->students[0]->id, true);
924 $data = new stdClass();
925 $data->assignfeedbackcomments_editor = array('text'=>'Tomato sauce',
926 'format'=>FORMAT_MOODLE);
927 $plugin = $assign->get_feedback_plugin_by_type('comments');
928 $plugin->save($grade, $data);
929
b0da618b 930 // Should have feedback but no grade.
46692c3a
DW
931 $this->setUser($this->students[0]);
932 $output = $assign->view_student_summary($this->students[0], true);
ea698284 933 $this->assertNotEquals(false, strpos($output, 'Feedback'), 'Show feedback even if there is no grade');
46692c3a
DW
934 $this->assertEquals(false, strpos($output, 'Grade'), 'Do not show grade when there is no grade.');
935 $this->assertEquals(false, strpos($output, 'Graded on'), 'Do not show graded date when there is no grade.');
45aac51a 936
ea698284
AD
937 // Now hide the grade in gradebook.
938 $this->setUser($this->teachers[0]);
939 $gradeitem = new grade_item(array(
940 'itemtype' => 'mod',
941 'itemmodule' => 'assign',
942 'iteminstance' => $assign->get_instance()->id,
943 'courseid' => $this->course->id));
944
945 $gradeitem->set_hidden(1, false);
946
947 // No feedback should be available because the grade is hidden.
948 $this->setUser($this->students[0]);
949 $output = $assign->view_student_summary($this->students[0], true);
950 $this->assertEquals(false, strpos($output, 'Feedback'), 'Do not show feedback if the grade is hidden in the gradebook');
46692c3a
DW
951 }
952
df211804
DW
953 public function test_attempt_reopen_method_manual() {
954 global $PAGE;
955
956 $this->setUser($this->editingteachers[0]);
957 $assign = $this->create_instance(array('attemptreopenmethod'=>ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL,
958 'maxattempts'=>3,
959 'submissiondrafts'=>1,
960 'assignsubmission_onlinetext_enabled'=>1));
961 $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
962
963 // Student should be able to see an add submission button.
964 $this->setUser($this->students[0]);
965 $output = $assign->view_student_summary($this->students[0], true);
966 $this->assertNotEquals(false, strpos($output, get_string('addsubmission', 'assign')));
967
968 // Add a submission.
969 $now = time();
970 $submission = $assign->get_user_submission($this->students[0]->id, true);
971 $data = new stdClass();
972 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
973 'text'=>'Submission text',
974 'format'=>FORMAT_MOODLE);
975 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
976 $plugin->save($submission, $data);
977
978 // And now submit it for marking.
979 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
980 $assign->testable_update_submission($submission, $this->students[0]->id, true, false);
981
982 // Verify the student cannot make changes to the submission.
983 $output = $assign->view_student_summary($this->students[0], true);
984 $this->assertEquals(false, strpos($output, get_string('addsubmission', 'assign')));
985
986 // Mark the submission.
987 $this->setUser($this->teachers[0]);
988 $data = new stdClass();
989 $data->grade = '50.0';
990 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
991
992 // Check the student can see the grade.
993 $this->setUser($this->students[0]);
994 $output = $assign->view_student_summary($this->students[0], true);
995 $this->assertNotEquals(false, strpos($output, '50.0'));
996
997 // Allow the student another attempt.
998 $this->teachers[0]->ignoresesskey = true;
999 $this->setUser($this->teachers[0]);
1000 $result = $assign->testable_process_add_attempt($this->students[0]->id);
1001 $this->assertEquals(true, $result);
1002
1003 // Check that the previous attempt is now in the submission history table.
1004 $this->setUser($this->students[0]);
1005 $output = $assign->view_student_summary($this->students[0], true);
1006 // Need a better check.
1007 $this->assertNotEquals(false, strpos($output, 'Submission text'), 'Contains: Submission text');
1008
1009 // Check that the student now has a button for Add a new attempt".
1010 $this->assertNotEquals(false, strpos($output, get_string('addnewattempt', 'assign')));
1011 // Check that the student now does not have a button for Submit.
1012 $this->assertEquals(false, strpos($output, get_string('submitassignment', 'assign')));
1013
1014 // Check that the student now has a submission history.
1015 $this->assertNotEquals(false, strpos($output, get_string('attempthistory', 'assign')));
1016
1017 $this->setUser($this->teachers[0]);
1018 // Check that the grading table loads correctly and contains this user.
1019 // This is also testing that we do not get duplicate rows in the grading table.
1020 $gradingtable = new assign_grading_table($assign, 100, '', 0, true);
1021 $output = $assign->get_renderer()->render($gradingtable);
1022 $this->assertEquals(true, strpos($output, $this->students[0]->lastname));
1023
1024 // Should be 1 not 2.
1025 $this->assertEquals(1, $assign->count_submissions());
1026 $this->assertEquals(1, $assign->count_submissions_with_status('reopened'));
1027 $this->assertEquals(0, $assign->count_submissions_need_grading());
1028 $this->assertEquals(1, $assign->count_grades());
1029
1030 // Change max attempts to unlimited.
1031 $formdata = clone($assign->get_instance());
1032 $formdata->maxattempts = ASSIGN_UNLIMITED_ATTEMPTS;
1033 $formdata->instance = $formdata->id;
1034 $assign->update_instance($formdata);
1035
1036 // Check we can repopen still.
1037 $result = $assign->testable_process_add_attempt($this->students[0]->id);
1038 $this->assertEquals(true, $result);
1039
1040 $grades = $assign->get_user_grades_for_gradebook($this->students[0]->id);
1041 $this->assertEquals(50, (int)$grades[$this->students[0]->id]->rawgrade);
1042
df211804 1043 }
46692c3a 1044
f8d107b3
DM
1045 public function test_markingworkflow() {
1046 global $PAGE;
1047
1048 $this->setUser($this->editingteachers[0]);
1049 $assign = $this->create_instance(array('markingworkflow'=>1));
1050 $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
1051
13e82f1c 1052 // Mark the submission and set to notmarked.
f8d107b3
DM
1053 $this->setUser($this->teachers[0]);
1054 $data = new stdClass();
1055 $data->grade = '50.0';
1056 $data->workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED;
1057 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
1058
1059 // Check the student can't see the grade.
1060 $this->setUser($this->students[0]);
1061 $output = $assign->view_student_summary($this->students[0], true);
1062 $this->assertEquals(false, strpos($output, '50.0'));
1063
13e82f1c 1064 // Mark the submission and set to inmarking.
f8d107b3
DM
1065 $this->setUser($this->teachers[0]);
1066 $data = new stdClass();
1067 $data->grade = '50.0';
1068 $data->workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_INMARKING;
1069 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
1070
1071 // Check the student can't see the grade.
1072 $this->setUser($this->students[0]);
1073 $output = $assign->view_student_summary($this->students[0], true);
1074 $this->assertEquals(false, strpos($output, '50.0'));
1075
13e82f1c 1076 // Mark the submission and set to readyforreview.
f8d107b3
DM
1077 $this->setUser($this->teachers[0]);
1078 $data = new stdClass();
1079 $data->grade = '50.0';
1080 $data->workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_READYFORREVIEW;
1081 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
1082
1083 // Check the student can't see the grade.
1084 $this->setUser($this->students[0]);
1085 $output = $assign->view_student_summary($this->students[0], true);
1086 $this->assertEquals(false, strpos($output, '50.0'));
1087
13e82f1c 1088 // Mark the submission and set to inreview.
f8d107b3
DM
1089 $this->setUser($this->teachers[0]);
1090 $data = new stdClass();
1091 $data->grade = '50.0';
1092 $data->workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_INREVIEW;
1093 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
1094
1095 // Check the student can't see the grade.
1096 $this->setUser($this->students[0]);
1097 $output = $assign->view_student_summary($this->students[0], true);
1098 $this->assertEquals(false, strpos($output, '50.0'));
1099
13e82f1c 1100 // Mark the submission and set to readyforrelease.
f8d107b3
DM
1101 $this->setUser($this->teachers[0]);
1102 $data = new stdClass();
1103 $data->grade = '50.0';
1104 $data->workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_READYFORRELEASE;
1105 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
1106
1107 // Check the student can't see the grade.
1108 $this->setUser($this->students[0]);
1109 $output = $assign->view_student_summary($this->students[0], true);
1110 $this->assertEquals(false, strpos($output, '50.0'));
1111
13e82f1c 1112 // Mark the submission and set to released.
f8d107b3
DM
1113 $this->setUser($this->teachers[0]);
1114 $data = new stdClass();
1115 $data->grade = '50.0';
1116 $data->workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_RELEASED;
1117 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
1118
1119 // Check the student can see the grade.
1120 $this->setUser($this->students[0]);
1121 $output = $assign->view_student_summary($this->students[0], true);
1122 $this->assertNotEquals(false, strpos($output, '50.0'));
1123 }
1124
1125 public function test_markerallocation() {
1126 global $PAGE;
1127
1128 $this->setUser($this->editingteachers[0]);
13e82f1c 1129 $assign = $this->create_instance(array('markingworkflow'=>1, 'markingallocation'=>1));
f8d107b3
DM
1130 $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
1131
13e82f1c 1132 // Allocate marker to submission.
f8d107b3
DM
1133 $data = new stdClass();
1134 $data->allocatedmarker = $this->teachers[0]->id;
1135 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
1136
13e82f1c 1137 // Check the allocated marker can view the submission.
f8d107b3
DM
1138 $this->setUser($this->teachers[0]);
1139 $gradingtable = new assign_grading_table($assign, 100, '', 0, true);
1140 $output = $assign->get_renderer()->render($gradingtable);
1141 $this->assertEquals(true, strpos($output, $this->students[0]->lastname));
1142
13e82f1c 1143 // Check that other teachers can't view this submission.
f8d107b3
DM
1144 $this->setUser($this->teachers[1]);
1145 $gradingtable = new assign_grading_table($assign, 100, '', 0, true);
1146 $output = $assign->get_renderer()->render($gradingtable);
1147 $this->assertNotEquals(true, strpos($output, $this->students[0]->lastname));
1148 }
76e77b05 1149
3d1331be 1150
159b7f40 1151
57fbd5f9
DW
1152 public function test_teacher_submit_for_student() {
1153 global $PAGE;
1154
1155 $this->preventResetByRollback();
1156 $sink = $this->redirectMessages();
1157
1158 $this->setUser($this->editingteachers[0]);
1159
1160 $assign = $this->create_instance(array('assignsubmission_onlinetext_enabled'=>1, 'submissiondrafts'=>1));
1161 $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
1162
1163 $this->setUser($this->students[0]);
1164 // Simulate a submission.
1165 $data = new stdClass();
1166 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
1167 'text'=>'Student submission text',
1168 'format'=>FORMAT_MOODLE);
1169
1170 $notices = array();
1171 $assign->save_submission($data, $notices);
1172
1173 // Check that the submission text was saved.
1174 $output = $assign->view_student_summary($this->students[0], true);
1175 $this->assertContains('Student submission text', $output, 'Contains student submission text');
1176
1177 // Check that a teacher teacher with the extra capability can edit a students submission.
1178 $this->setUser($this->teachers[0]);
1179 $data = new stdClass();
1180 $data->userid = $this->students[0]->id;
1181 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
1182 'text'=>'Teacher edited submission text',
1183 'format'=>FORMAT_MOODLE);
1184
1185 // Add the required capability.
1186 $roleid = create_role('Dummy role', 'dummyrole', 'dummy role description');
1187 assign_capability('mod/assign:editothersubmission', CAP_ALLOW, $roleid, $assign->get_context()->id);
1188 role_assign($roleid, $this->teachers[0]->id, $assign->get_context()->id);
1189 accesslib_clear_all_caches_for_unit_testing();
1190
1191 // Try to save the submission.
1192 $notices = array();
1193 $assign->save_submission($data, $notices);
1194
1195 // Check that the teacher can submit the students work.
1196 $data = new stdClass();
1197 $data->userid = $this->students[0]->id;
1198 $notices = array();
1199 $assign->submit_for_grading($data, $notices);
1200
1201 // Revert to draft so the student can edit it.
1202 $assign->revert_to_draft($this->students[0]->id);
1203
1204 $this->setUser($this->students[0]);
1205
1206 // Check that the submission text was saved.
1207 $output = $assign->view_student_summary($this->students[0], true);
1208 $this->assertContains('Teacher edited submission text', $output, 'Contains student submission text');
1209
1210 // Check that the student can submit their work.
1211 $data = new stdClass();
1212 $assign->submit_for_grading($data, $notices);
1213
1214 $output = $assign->view_student_summary($this->students[0], true);
1215 $this->assertNotContains(get_string('addsubmission', 'assign'), $output);
1216
1217 // Set to a default editing teacher who should not be able to edit this submission.
1218 $this->setUser($this->editingteachers[1]);
1219
1220 // Revert to draft so the submission is editable.
1221 $assign->revert_to_draft($this->students[0]->id);
1222
1223 $data = new stdClass();
1224 $data->userid = $this->students[0]->id;
1225 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
1226 'text'=>'Teacher 2 edited submission text',
1227 'format'=>FORMAT_MOODLE);
1228
1229 $notices = array();
1230 $this->setExpectedException('moodle_exception');
1231 $assign->save_submission($data, $notices);
1232
1233 $sink->close();
1234 }
1235
9054c04d 1236 public function test_disable_submit_after_cutoff_date() {
1237 global $PAGE;
1238
1239 $this->setUser($this->editingteachers[0]);
1240 $now = time();
1241 $tomorrow = $now + 24*60*60;
1242 $lastweek = $now - 7*24*60*60;
1243 $yesterday = $now - 24*60*60;
1244
1245 $assign = $this->create_instance(array('duedate'=>$yesterday,
1246 'cutoffdate'=>$tomorrow,
1247 'assignsubmission_onlinetext_enabled'=>1));
1248 $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
1249
1250 // Student should be able to see an add submission button.
1251 $this->setUser($this->students[0]);
1252 $output = $assign->view_student_summary($this->students[0], true);
1253 $this->assertNotEquals(false, strpos($output, get_string('addsubmission', 'assign')));
1254
1255 // Add a submission but don't submit now.
1256 $submission = $assign->get_user_submission($this->students[0]->id, true);
1257 $data = new stdClass();
1258 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
1259 'text'=>'Submission text',
1260 'format'=>FORMAT_MOODLE);
1261 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
1262 $plugin->save($submission, $data);
1263
1264 // Create another instance with cut-off and due-date already passed.
1265 $this->setUser($this->editingteachers[0]);
1266 $now = time();
1267 $assign = $this->create_instance(array('duedate'=>$lastweek,
1268 'cutoffdate'=>$yesterday,
1269 'assignsubmission_onlinetext_enabled'=>1));
1270
1271 $this->setUser($this->students[0]);
1272 $output = $assign->view_student_summary($this->students[0], true);
1273 $this->assertNotContains($output, get_string('editsubmission', 'assign'),
1274 'Should not be able to edit after cutoff date.');
1275 $this->assertNotContains($output, get_string('submitassignment', 'assign'),
1276 'Should not be able to submit after cutoff date.');
1277 }
f159ad73 1278 /**
1279 * Testing for submission comment plugin settings
1280 */
1281 public function test_submission_comment_plugin_settings() {
1282 global $CFG;
1283
1284 $commentconfig = false;
1285 if (!empty($CFG->usecomments)) {
1286 $commentconfig = $CFG->usecomments;
1287 }
1288
1289 $CFG->usecomments = true;
1290 $assign = $this->create_instance();
1291 $plugin = $assign->get_submission_plugin_by_type('comments');
1292 $this->assertEquals(1, $plugin->is_enabled('enabled'));
1293
1294 $assign = $this->create_instance(array('assignsubmission_comments_enabled' => 0));
1295 $plugin = $assign->get_submission_plugin_by_type('comments');
1296 $this->assertEquals(1, $plugin->is_enabled('enabled'));
1297
1298 $assign = $this->create_instance(array('assignsubmission_comments_enabled' => 1));
1299 $plugin = $assign->get_submission_plugin_by_type('comments');
1300 $this->assertEquals(1, $plugin->is_enabled('enabled'));
1301
1302 $CFG->usecomments = false;
1303 $assign = $this->create_instance();
1304 $plugin = $assign->get_submission_plugin_by_type('comments');
1305 $this->assertEquals(0, $plugin->is_enabled('enabled'));
1306
1307 $assign = $this->create_instance(array('assignsubmission_comments_enabled' => 0));
1308 $plugin = $assign->get_submission_plugin_by_type('comments');
1309 $this->assertEquals(0, $plugin->is_enabled('enabled'));
1310
1311 $assign = $this->create_instance(array('assignsubmission_comments_enabled' => 1));
1312 $plugin = $assign->get_submission_plugin_by_type('comments');
1313 $this->assertEquals(0, $plugin->is_enabled('enabled'));
1314
1315 $CFG->usecomments = $commentconfig;
1316 }
1317
c7a73689
DW
1318 /**
1319 * Testing for comment inline settings
1320 */
1321 public function test_feedback_comment_commentinline() {
1322 global $CFG;
1323
1324 $sourcetext = "Hello!
1325
1326I'm writing to you from the Moodle Majlis in Muscat, Oman, where we just had several days of Moodle community goodness.
1327
1328URL outside a tag: https://moodle.org/logo/logo-240x60.gif
1329Plugin url outside a tag: @@PLUGINFILE@@/logo-240x60.gif
1330
1331External link 1:<img src='https://moodle.org/logo/logo-240x60.gif' alt='Moodle'/>
1332External link 2:<img alt=\"Moodle\" src=\"https://moodle.org/logo/logo-240x60.gif\"/>
1333Internal link 1:<img src='@@PLUGINFILE@@/logo-240x60.gif' alt='Moodle'/>
1334Internal link 2:<img alt=\"Moodle\" src=\"@@PLUGINFILE@@logo-240x60.gif\"/>
1335Anchor link 1:<a href=\"@@PLUGINFILE@@logo-240x60.gif\" alt=\"bananas\">Link text</a>
1336Anchor link 2:<a title=\"bananas\" href=\"../logo-240x60.gif\">Link text</a>
1337";
1338
1339 // Note the internal images have been stripped and the html is purified (quotes fixed in this case).
1340 $filteredtext = "Hello!
1341
1342I'm writing to you from the Moodle Majlis in Muscat, Oman, where we just had several days of Moodle community goodness.
1343
1344URL outside a tag: https://moodle.org/logo/logo-240x60.gif
1345Plugin url outside a tag: @@PLUGINFILE@@/logo-240x60.gif
1346
1347External link 1:<img src=\"https://moodle.org/logo/logo-240x60.gif\" alt=\"Moodle\" />
1348External link 2:<img alt=\"Moodle\" src=\"https://moodle.org/logo/logo-240x60.gif\" />
1349Internal link 1:
1350Internal link 2:
1351Anchor link 1:Link text
1352Anchor link 2:<a title=\"bananas\" href=\"../logo-240x60.gif\">Link text</a>
1353";
1354
1355 $this->setUser($this->editingteachers[0]);
1356 $params = array('assignsubmission_onlinetext_enabled' => 1,
1357 'assignfeedback_comments_enabled' => 1,
1358 'assignfeedback_comments_commentinline' => 1);
1359 $assign = $this->create_instance($params);
1360
1361 $this->setUser($this->students[0]);
1362 // Add a submission but don't submit now.
1363 $submission = $assign->get_user_submission($this->students[0]->id, true);
1364 $data = new stdClass();
1365
1366 // Test the internal link is stripped, but the external one is not.
1367 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
1368 'text'=>$sourcetext,
1369 'format'=>FORMAT_MOODLE);
1370
1371 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
1372 $plugin->save($submission, $data);
1373
1374 $this->setUser($this->editingteachers[0]);
1375
1376 $data = new stdClass();
1377 require_once($CFG->dirroot . '/mod/assign/gradeform.php');
1378 $pagination = array('userid'=>$this->students[0]->id,
1379 'rownum'=>0,
1380 'last'=>true,
1381 'useridlistid'=>time(),
1382 'attemptnumber'=>0);
1383 $formparams = array($assign, $data, $pagination);
1384 $mform = new mod_assign_grade_form(null, $formparams);
1385
1386 $this->assertEquals($filteredtext, $data->assignfeedbackcomments_editor['text']);
1387 }
1388
f159ad73 1389 /**
1390 * Testing for feedback comment plugin settings
1391 */
1392 public function test_feedback_plugin_settings() {
1393
1394 $assign = $this->create_instance();
1395 $plugin = $assign->get_feedback_plugin_by_type('comments');
1396 $this->assertEquals(0, $plugin->is_enabled('enabled'));
1397
1398 $assign = $this->create_instance(array('assignfeedback_comments_enabled' => 0));
1399 $plugin = $assign->get_feedback_plugin_by_type('comments');
1400 $this->assertEquals(0, $plugin->is_enabled('enabled'));
1401
1402 $assign = $this->create_instance(array('assignfeedback_comments_enabled' => 1));
1403 $plugin = $assign->get_feedback_plugin_by_type('comments');
1404 $this->assertEquals(1, $plugin->is_enabled('enabled'));
1405 }
456d7bc7
RT
1406
1407 /**
1408 * Testing if gradebook feedback plugin is enabled.
1409 */
1410 public function test_is_gradebook_feedback_enabled() {
1411 $adminconfig = get_config('assign');
1412 $gradebookplugin = $adminconfig->feedback_plugin_for_gradebook;
1413
1414 // Create assignment with gradebook feedback enabled and grade = 0.
1415 $assign = $this->create_instance(array($gradebookplugin . '_enabled' => 1, 'grades' => 0));
1416
1417 // Get gradebook feedback plugin.
1418 $gradebookplugintype = str_replace('assignfeedback_', '', $gradebookplugin);
1419 $plugin = $assign->get_feedback_plugin_by_type($gradebookplugintype);
1420 $this->assertEquals(1, $plugin->is_enabled('enabled'));
1421 $this->assertEquals(1, $assign->is_gradebook_feedback_enabled());
1422
1423 // Create assignment with gradebook feedback disabled and grade = 0.
1424 $assign = $this->create_instance(array($gradebookplugin . '_enabled' => 0, 'grades' => 0));
1425 $plugin = $assign->get_feedback_plugin_by_type($gradebookplugintype);
1426 $this->assertEquals(0, $plugin->is_enabled('enabled'));
1427 }
57fbd5f9
DW
1428
1429 /**
1430 * Testing can_edit_submission
1431 */
1432 public function test_can_edit_submission() {
1433 global $PAGE, $DB;
1434 $this->create_extra_users();
1435
1436 $this->setAdminUser();
1437 // Create assignment (onlinetext).
1438 $assign = $this->create_instance(array('assignsubmission_onlinetext_enabled'=>1, 'submissiondrafts'=>1));
1439 $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
1440
1441 // Check student can edit their own submission.
1442 $this->assertTrue($assign->can_edit_submission($this->students[0]->id, $this->students[0]->id));
1443 // Check student cannot edit others submission.
1444 $this->assertFalse($assign->can_edit_submission($this->students[0]->id, $this->students[1]->id));
1445
1446 // Check teacher cannot (by default) edit a students submission.
1447 $this->assertFalse($assign->can_edit_submission($this->students[0]->id, $this->teachers[0]->id));
1448
1449 // Add the required capability to edit a student submission.
1450 $roleid = create_role('Dummy role', 'dummyrole', 'dummy role description');
1451 assign_capability('mod/assign:editothersubmission', CAP_ALLOW, $roleid, $assign->get_context()->id);
1452 role_assign($roleid, $this->teachers[0]->id, $assign->get_context()->id);
1453 accesslib_clear_all_caches_for_unit_testing();
1454 // Retest - should now have access.
1455 $this->assertTrue($assign->can_edit_submission($this->students[0]->id, $this->teachers[0]->id));
1456
1457 // Force create an assignment with SEPARATEGROUPS.
1458 $data = new stdClass();
1459 $data->courseid = $this->course->id;
1460 $data->name = 'Grouping';
1461 $groupingid = groups_create_grouping($data);
1462 groups_assign_grouping($groupingid, $this->groups[0]->id);
1463 groups_assign_grouping($groupingid, $this->groups[1]->id);
1464 $assign = $this->create_instance(array('groupingid' => $groupingid, 'groupmode' => SEPARATEGROUPS));
1465
1466 // Add the capability to the new assignment for extra students 0 and 1.
1467 assign_capability('mod/assign:editothersubmission', CAP_ALLOW, $roleid, $assign->get_context()->id);
1468 role_assign($roleid, $this->extrastudents[0]->id, $assign->get_context()->id);
1469 role_assign($roleid, $this->extrastudents[1]->id, $assign->get_context()->id);
1470 accesslib_clear_all_caches_for_unit_testing();
1471
1472 // Verify the extra student does not have the capability to edit a submission not in their group.
1473 $this->assertFalse($assign->can_edit_submission($this->students[0]->id, $this->extrastudents[1]->id));
1474 // Verify the extra student does have the capability to edit a submission in their group.
1475 $this->assertTrue($assign->can_edit_submission($this->students[0]->id, $this->extrastudents[0]->id));
1476
1477 }
47f48152
DW
1478}
1479