Merge branch 'MDL-40912_master' of https://github.com/markn86/moodle
[moodle.git] / mod / assign / tests / locallib_test.php
CommitLineData
47f48152
DW
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Unit tests for (some of) mod/assign/locallib.php.
19 *
20 * @package mod_assign
21 * @category phpunit
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30require_once($CFG->dirroot . '/mod/assign/locallib.php');
31require_once($CFG->dirroot . '/mod/assign/upgradelib.php');
9c986ee0 32require_once($CFG->dirroot . '/mod/assign/tests/base_test.php');
47f48152
DW
33
34/**
35 * Unit tests for (some of) mod/assign/locallib.php.
36 *
37 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
9c986ee0 40class mod_assign_locallib_testcase extends mod_assign_base_testcase {
47f48152
DW
41
42 public function test_return_links() {
c2114099 43 global $PAGE;
47f48152
DW
44 $this->setUser($this->editingteachers[0]);
45 $returnaction = 'RETURNACTION';
c2114099 46 $returnparams = array('param'=>'1');
47f48152 47 $assign = $this->create_instance();
c2114099 48 $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
47f48152
DW
49 $assign->register_return_link($returnaction, $returnparams);
50 $this->assertEquals($returnaction, $assign->get_return_action());
51 $this->assertEquals($returnparams, $assign->get_return_params());
52 }
53
54 public function test_get_feedback_plugins() {
55 $this->setUser($this->editingteachers[0]);
56 $assign = $this->create_instance();
bd3b3bba 57 $installedplugins = array_keys(core_component::get_plugin_list('assignfeedback'));
11527706
DW
58
59 foreach ($assign->get_feedback_plugins() as $plugin) {
60 $this->assertContains($plugin->get_type(), $installedplugins, 'Feedback plugin not in list of installed plugins');
61 }
47f48152
DW
62 }
63
64 public function test_get_submission_plugins() {
65 $this->setUser($this->editingteachers[0]);
66 $assign = $this->create_instance();
bd3b3bba 67 $installedplugins = array_keys(core_component::get_plugin_list('assignsubmission'));
11527706
DW
68
69 foreach ($assign->get_submission_plugins() as $plugin) {
70 $this->assertContains($plugin->get_type(), $installedplugins, 'Submission plugin not in list of installed plugins');
71 }
47f48152
DW
72 }
73
74 public function test_is_blind_marking() {
75 $this->setUser($this->editingteachers[0]);
76 $assign = $this->create_instance(array('blindmarking'=>1));
77 $this->assertEquals(true, $assign->is_blind_marking());
78
79 // Test cannot see student names.
80 $gradingtable = new assign_grading_table($assign, 1, '', 0, true);
81 $output = $assign->get_renderer()->render($gradingtable);
82 $this->assertEquals(true, strpos($output, get_string('hiddenuser', 'assign')));
83
84 // Test students cannot reveal identities.
85 $nopermission = false;
05a6445a 86 $this->students[0]->ignoresesskey = true;
47f48152
DW
87 $this->setUser($this->students[0]);
88 $this->setExpectedException('required_capability_exception');
05a6445a
DW
89 $assign->reveal_identities();
90 $this->students[0]->ignoresesskey = false;
47f48152
DW
91
92 // Test teachers cannot reveal identities.
93 $nopermission = false;
05a6445a 94 $this->teachers[0]->ignoresesskey = true;
47f48152
DW
95 $this->setUser($this->teachers[0]);
96 $this->setExpectedException('required_capability_exception');
05a6445a
DW
97 $assign->reveal_identities();
98 $this->teachers[0]->ignoresesskey = false;
47f48152
DW
99
100 // Test sesskey is required.
47f48152
DW
101 $this->setUser($this->editingteachers[0]);
102 $this->setExpectedException('moodle_exception');
05a6445a 103 $assign->reveal_identities();
47f48152
DW
104
105 // Test editingteacher can reveal identities if sesskey is ignored.
106 $this->editingteachers[0]->ignoresesskey = true;
107 $this->setUser($this->editingteachers[0]);
05a6445a 108 $assign->reveal_identities();
47f48152 109 $this->assertEquals(false, $assign->is_blind_marking());
05a6445a 110 $this->editingteachers[0]->ignoresesskey = false;
47f48152
DW
111
112 // Test student names are visible.
113 $gradingtable = new assign_grading_table($assign, 1, '', 0, true);
114 $output = $assign->get_renderer()->render($gradingtable);
115 $this->assertEquals(false, strpos($output, get_string('hiddenuser', 'assign')));
116
117 // Set this back to default.
118 $this->editingteachers[0]->ignoresesskey = false;
119 }
120
121 public function test_show_intro() {
122 // Test whether we are showing the intro at the correct times.
123 $this->setUser($this->editingteachers[0]);
124 $assign = $this->create_instance(array('alwaysshowdescription'=>1));
125
126 $this->assertEquals(true, $assign->testable_show_intro());
127
128 $tomorrow = time() + (24*60*60);
129
130 $assign = $this->create_instance(array('alwaysshowdescription'=>0,
131 'allowsubmissionsfromdate'=>$tomorrow));
132 $this->assertEquals(false, $assign->testable_show_intro());
133 $yesterday = time() - (24*60*60);
134 $assign = $this->create_instance(array('alwaysshowdescription'=>0,
135 'allowsubmissionsfromdate'=>$yesterday));
136 $this->assertEquals(true, $assign->testable_show_intro());
137 }
138
139 public function test_has_submissions_or_grades() {
140 $this->setUser($this->editingteachers[0]);
141 $assign = $this->create_instance(array('assignsubmission_onlinetext_enabled'=>1));
142
143 $instance = $assign->get_instance();
144
145 // Should start empty.
146 $this->assertEquals(false, $assign->has_submissions_or_grades());
147
148 // Simulate a submission.
149 $this->setUser($this->students[0]);
150 $submission = $assign->get_user_submission($this->students[0]->id, true);
151 $data = new stdClass();
152 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
153 'text'=>'Submission text',
154 'format'=>FORMAT_MOODLE);
155 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
156 $plugin->save($submission, $data);
157
158 // Now test again.
159 $this->assertEquals(true, $assign->has_submissions_or_grades());
160 // Set this back to default.
161 $this->students[0]->ignoresesskey = false;
162 }
163
164 public function test_delete_grades() {
165 $this->setUser($this->editingteachers[0]);
166 $assign = $this->create_instance();
167
168 // Simulate adding a grade.
169 $this->setUser($this->teachers[0]);
170 $data = new stdClass();
171 $data->grade = '50.0';
df211804 172 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
47f48152
DW
173
174 // Now see if the data is in the gradebook.
175 $gradinginfo = grade_get_grades($this->course->id,
176 'mod',
177 'assign',
178 $assign->get_instance()->id);
179
180 $this->assertNotEquals(0, count($gradinginfo->items));
181
182 $assign->testable_delete_grades();
183 $gradinginfo = grade_get_grades($this->course->id,
184 'mod',
185 'assign',
186 $assign->get_instance()->id);
187
188 $this->assertEquals(0, count($gradinginfo->items));
189 }
190
191 public function test_delete_instance() {
192 $this->setUser($this->editingteachers[0]);
193 $assign = $this->create_instance(array('assignsubmission_onlinetext_enabled'=>1));
194
195 // Simulate adding a grade.
196 $this->setUser($this->teachers[0]);
197 $data = new stdClass();
198 $data->grade = '50.0';
df211804 199 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
47f48152
DW
200
201 // Simulate a submission.
202 $this->setUser($this->students[0]);
203 $submission = $assign->get_user_submission($this->students[0]->id, true);
204 $data = new stdClass();
205 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
206 'text'=>'Submission text',
207 'format'=>FORMAT_MOODLE);
208 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
209 $plugin->save($submission, $data);
210
211 // Now try and delete.
212 $this->assertEquals(true, $assign->delete_instance());
213 }
214
215 public function test_reset_userdata() {
216 global $DB;
217
218 $now = time();
219 $this->setUser($this->editingteachers[0]);
220 $assign = $this->create_instance(array('assignsubmission_onlinetext_enabled'=>1,
221 'duedate'=>$now));
222
223 // Simulate adding a grade.
224 $this->setUser($this->teachers[0]);
225 $data = new stdClass();
226 $data->grade = '50.0';
df211804 227 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
47f48152
DW
228
229 // Simulate a submission.
230 $this->setUser($this->students[0]);
231 $submission = $assign->get_user_submission($this->students[0]->id, true);
232 $data = new stdClass();
233 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
234 'text'=>'Submission text',
235 'format'=>FORMAT_MOODLE);
236 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
237 $plugin->save($submission, $data);
238
239 $this->assertEquals(true, $assign->has_submissions_or_grades());
240 // Now try and reset.
241 $data = new stdClass();
242 $data->reset_assign_submissions = 1;
243 $data->reset_gradebook_grades = 1;
244 $data->courseid = $this->course->id;
245 $data->timeshift = 24*60*60;
246 $this->setUser($this->editingteachers[0]);
247 $assign->reset_userdata($data);
248 $this->assertEquals(false, $assign->has_submissions_or_grades());
249
250 // Reload the instance data.
251 $instance = $DB->get_record('assign', array('id'=>$assign->get_instance()->id));
252 $this->assertEquals($now + 24*60*60, $instance->duedate);
e63515ba
RT
253
254 // Test reset using assign_reset_userdata().
255 $assignduedate = $instance->duedate; // Keep old updated value for comparison.
256 $data->timeshift = 2*24*60*60;
257 assign_reset_userdata($data);
258 $instance = $DB->get_record('assign', array('id' => $assign->get_instance()->id));
259 $this->assertEquals($assignduedate + 2*24*60*60, $instance->duedate);
260
261 // Create one more assignment and reset, make sure time shifted for previous assignment is not changed.
262 $assign2 = $this->create_instance(array('assignsubmission_onlinetext_enabled' => 1,
263 'duedate' => $now));
264 $assignduedate = $instance->duedate;
265 $data->timeshift = 3*24*60*60;
266 $assign2->reset_userdata($data);
267 $instance = $DB->get_record('assign', array('id' => $assign->get_instance()->id));
268 $this->assertEquals($assignduedate, $instance->duedate);
269 $instance2 = $DB->get_record('assign', array('id' => $assign2->get_instance()->id));
270 $this->assertEquals($now + 3*24*60*60, $instance2->duedate);
271
272 // Reset both assignments using assign_reset_userdata() and make sure both assignments have same date.
273 $assignduedate = $instance->duedate;
274 $assign2duedate = $instance2->duedate;
275 $data->timeshift = 4*24*60*60;
276 assign_reset_userdata($data);
277 $instance = $DB->get_record('assign', array('id' => $assign->get_instance()->id));
278 $this->assertEquals($assignduedate + 4*24*60*60, $instance->duedate);
279 $instance2 = $DB->get_record('assign', array('id' => $assign2->get_instance()->id));
280 $this->assertEquals($assign2duedate + 4*24*60*60, $instance2->duedate);
47f48152
DW
281 }
282
283 public function test_plugin_settings() {
284 global $DB;
285
286 $now = time();
287 $this->setUser($this->editingteachers[0]);
288 $assign = $this->create_instance(array('assignsubmission_file_enabled'=>1,
289 'assignsubmission_file_maxfiles'=>12,
290 'assignsubmission_file_maxsizebytes'=>10));
291
292 $plugin = $assign->get_submission_plugin_by_type('file');
293 $this->assertEquals('12', $plugin->get_config('maxfilesubmissions'));
294 }
295
296 public function test_update_calendar() {
297 global $DB;
298
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]);
866 $this->assertCount(5, $assign->list_participants(0, true));
867
868 }
869
47f48152
DW
870 public function test_get_uniqueid_for_user() {
871 $this->setUser($this->editingteachers[0]);
872 $assign = $this->create_instance();
873
874 foreach ($this->students as $student) {
875 $uniqueid = $assign->get_uniqueid_for_user($student->id);
876 $this->assertEquals($student->id, $assign->get_user_id_for_uniqueid($uniqueid));
877 }
878 }
879
46692c3a 880 public function test_show_student_summary() {
c2114099 881 global $CFG, $PAGE;
46692c3a
DW
882
883 $this->setUser($this->editingteachers[0]);
884 $assign = $this->create_instance();
c2114099 885 $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
46692c3a
DW
886
887 // No feedback should be available because this student has not been graded.
888 $this->setUser($this->students[0]);
889 $output = $assign->view_student_summary($this->students[0], true);
890 $this->assertEquals(false, strpos($output, 'Feedback'), 'Do not show feedback if there is no grade');
891 // Simulate adding a grade.
892 $this->setUser($this->teachers[0]);
893 $data = new stdClass();
894 $data->grade = '50.0';
df211804 895 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
46692c3a 896
b0da618b 897 // Now we should see the feedback.
46692c3a
DW
898 $this->setUser($this->students[0]);
899 $output = $assign->view_student_summary($this->students[0], true);
900 $this->assertNotEquals(false, strpos($output, 'Feedback'), 'Show feedback if there is a grade');
901
902 // Now hide the grade in gradebook.
903 $this->setUser($this->teachers[0]);
904 require_once($CFG->libdir.'/gradelib.php');
905 $gradeitem = new grade_item(array(
906 'itemtype' => 'mod',
907 'itemmodule' => 'assign',
908 'iteminstance' => $assign->get_instance()->id,
909 'courseid' => $this->course->id));
910
911 $gradeitem->set_hidden(1, false);
912
913 // No feedback should be available because the grade is hidden.
914 $this->setUser($this->students[0]);
915 $output = $assign->view_student_summary($this->students[0], true);
916 $this->assertEquals(false, strpos($output, 'Feedback'), 'Do not show feedback if the grade is hidden in the gradebook');
917
b0da618b 918 // Do the same but add feedback.
46692c3a
DW
919 $assign = $this->create_instance(array('assignfeedback_comments_enabled' => 1));
920
921 $this->setUser($this->teachers[0]);
922 $grade = $assign->get_user_grade($this->students[0]->id, true);
923 $data = new stdClass();
924 $data->assignfeedbackcomments_editor = array('text'=>'Tomato sauce',
925 'format'=>FORMAT_MOODLE);
926 $plugin = $assign->get_feedback_plugin_by_type('comments');
927 $plugin->save($grade, $data);
928
b0da618b 929 // Should have feedback but no grade.
46692c3a
DW
930 $this->setUser($this->students[0]);
931 $output = $assign->view_student_summary($this->students[0], true);
ea698284 932 $this->assertNotEquals(false, strpos($output, 'Feedback'), 'Show feedback even if there is no grade');
46692c3a
DW
933 $this->assertEquals(false, strpos($output, 'Grade'), 'Do not show grade when there is no grade.');
934 $this->assertEquals(false, strpos($output, 'Graded on'), 'Do not show graded date when there is no grade.');
45aac51a 935
ea698284
AD
936 // Now hide the grade in gradebook.
937 $this->setUser($this->teachers[0]);
938 $gradeitem = new grade_item(array(
939 'itemtype' => 'mod',
940 'itemmodule' => 'assign',
941 'iteminstance' => $assign->get_instance()->id,
942 'courseid' => $this->course->id));
943
944 $gradeitem->set_hidden(1, false);
945
946 // No feedback should be available because the grade is hidden.
947 $this->setUser($this->students[0]);
948 $output = $assign->view_student_summary($this->students[0], true);
949 $this->assertEquals(false, strpos($output, 'Feedback'), 'Do not show feedback if the grade is hidden in the gradebook');
46692c3a
DW
950 }
951
df211804
DW
952 public function test_attempt_reopen_method_manual() {
953 global $PAGE;
954
955 $this->setUser($this->editingteachers[0]);
956 $assign = $this->create_instance(array('attemptreopenmethod'=>ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL,
957 'maxattempts'=>3,
958 'submissiondrafts'=>1,
959 'assignsubmission_onlinetext_enabled'=>1));
960 $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
961
962 // Student should be able to see an add submission button.
963 $this->setUser($this->students[0]);
964 $output = $assign->view_student_summary($this->students[0], true);
965 $this->assertNotEquals(false, strpos($output, get_string('addsubmission', 'assign')));
966
967 // Add a submission.
968 $now = time();
969 $submission = $assign->get_user_submission($this->students[0]->id, true);
970 $data = new stdClass();
971 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
972 'text'=>'Submission text',
973 'format'=>FORMAT_MOODLE);
974 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
975 $plugin->save($submission, $data);
976
977 // And now submit it for marking.
978 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
979 $assign->testable_update_submission($submission, $this->students[0]->id, true, false);
980
981 // Verify the student cannot make changes to the submission.
982 $output = $assign->view_student_summary($this->students[0], true);
983 $this->assertEquals(false, strpos($output, get_string('addsubmission', 'assign')));
984
985 // Mark the submission.
986 $this->setUser($this->teachers[0]);
987 $data = new stdClass();
988 $data->grade = '50.0';
989 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
990
991 // Check the student can see the grade.
992 $this->setUser($this->students[0]);
993 $output = $assign->view_student_summary($this->students[0], true);
994 $this->assertNotEquals(false, strpos($output, '50.0'));
995
996 // Allow the student another attempt.
997 $this->teachers[0]->ignoresesskey = true;
998 $this->setUser($this->teachers[0]);
999 $result = $assign->testable_process_add_attempt($this->students[0]->id);
1000 $this->assertEquals(true, $result);
1001
1002 // Check that the previous attempt is now in the submission history table.
1003 $this->setUser($this->students[0]);
1004 $output = $assign->view_student_summary($this->students[0], true);
1005 // Need a better check.
1006 $this->assertNotEquals(false, strpos($output, 'Submission text'), 'Contains: Submission text');
1007
1008 // Check that the student now has a button for Add a new attempt".
1009 $this->assertNotEquals(false, strpos($output, get_string('addnewattempt', 'assign')));
1010 // Check that the student now does not have a button for Submit.
1011 $this->assertEquals(false, strpos($output, get_string('submitassignment', 'assign')));
1012
1013 // Check that the student now has a submission history.
1014 $this->assertNotEquals(false, strpos($output, get_string('attempthistory', 'assign')));
1015
1016 $this->setUser($this->teachers[0]);
1017 // Check that the grading table loads correctly and contains this user.
1018 // This is also testing that we do not get duplicate rows in the grading table.
1019 $gradingtable = new assign_grading_table($assign, 100, '', 0, true);
1020 $output = $assign->get_renderer()->render($gradingtable);
1021 $this->assertEquals(true, strpos($output, $this->students[0]->lastname));
1022
1023 // Should be 1 not 2.
1024 $this->assertEquals(1, $assign->count_submissions());
1025 $this->assertEquals(1, $assign->count_submissions_with_status('reopened'));
1026 $this->assertEquals(0, $assign->count_submissions_need_grading());
1027 $this->assertEquals(1, $assign->count_grades());
1028
1029 // Change max attempts to unlimited.
1030 $formdata = clone($assign->get_instance());
1031 $formdata->maxattempts = ASSIGN_UNLIMITED_ATTEMPTS;
1032 $formdata->instance = $formdata->id;
1033 $assign->update_instance($formdata);
1034
1035 // Check we can repopen still.
1036 $result = $assign->testable_process_add_attempt($this->students[0]->id);
1037 $this->assertEquals(true, $result);
1038
1039 $grades = $assign->get_user_grades_for_gradebook($this->students[0]->id);
1040 $this->assertEquals(50, (int)$grades[$this->students[0]->id]->rawgrade);
1041
df211804 1042 }
46692c3a 1043
f8d107b3
DM
1044 public function test_markingworkflow() {
1045 global $PAGE;
1046
1047 $this->setUser($this->editingteachers[0]);
1048 $assign = $this->create_instance(array('markingworkflow'=>1));
1049 $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
1050
13e82f1c 1051 // Mark the submission and set to notmarked.
f8d107b3
DM
1052 $this->setUser($this->teachers[0]);
1053 $data = new stdClass();
1054 $data->grade = '50.0';
1055 $data->workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED;
1056 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
1057
1058 // Check the student can't see the grade.
1059 $this->setUser($this->students[0]);
1060 $output = $assign->view_student_summary($this->students[0], true);
1061 $this->assertEquals(false, strpos($output, '50.0'));
1062
13e82f1c 1063 // Mark the submission and set to inmarking.
f8d107b3
DM
1064 $this->setUser($this->teachers[0]);
1065 $data = new stdClass();
1066 $data->grade = '50.0';
1067 $data->workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_INMARKING;
1068 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
1069
1070 // Check the student can't see the grade.
1071 $this->setUser($this->students[0]);
1072 $output = $assign->view_student_summary($this->students[0], true);
1073 $this->assertEquals(false, strpos($output, '50.0'));
1074
13e82f1c 1075 // Mark the submission and set to readyforreview.
f8d107b3
DM
1076 $this->setUser($this->teachers[0]);
1077 $data = new stdClass();
1078 $data->grade = '50.0';
1079 $data->workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_READYFORREVIEW;
1080 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
1081
1082 // Check the student can't see the grade.
1083 $this->setUser($this->students[0]);
1084 $output = $assign->view_student_summary($this->students[0], true);
1085 $this->assertEquals(false, strpos($output, '50.0'));
1086
13e82f1c 1087 // Mark the submission and set to inreview.
f8d107b3
DM
1088 $this->setUser($this->teachers[0]);
1089 $data = new stdClass();
1090 $data->grade = '50.0';
1091 $data->workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_INREVIEW;
1092 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
1093
1094 // Check the student can't see the grade.
1095 $this->setUser($this->students[0]);
1096 $output = $assign->view_student_summary($this->students[0], true);
1097 $this->assertEquals(false, strpos($output, '50.0'));
1098
13e82f1c 1099 // Mark the submission and set to readyforrelease.
f8d107b3
DM
1100 $this->setUser($this->teachers[0]);
1101 $data = new stdClass();
1102 $data->grade = '50.0';
1103 $data->workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_READYFORRELEASE;
1104 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
1105
1106 // Check the student can't see the grade.
1107 $this->setUser($this->students[0]);
1108 $output = $assign->view_student_summary($this->students[0], true);
1109 $this->assertEquals(false, strpos($output, '50.0'));
1110
13e82f1c 1111 // Mark the submission and set to released.
f8d107b3
DM
1112 $this->setUser($this->teachers[0]);
1113 $data = new stdClass();
1114 $data->grade = '50.0';
1115 $data->workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_RELEASED;
1116 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
1117
1118 // Check the student can see the grade.
1119 $this->setUser($this->students[0]);
1120 $output = $assign->view_student_summary($this->students[0], true);
1121 $this->assertNotEquals(false, strpos($output, '50.0'));
1122 }
1123
1124 public function test_markerallocation() {
1125 global $PAGE;
1126
1127 $this->setUser($this->editingteachers[0]);
13e82f1c 1128 $assign = $this->create_instance(array('markingworkflow'=>1, 'markingallocation'=>1));
f8d107b3
DM
1129 $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
1130
13e82f1c 1131 // Allocate marker to submission.
f8d107b3
DM
1132 $data = new stdClass();
1133 $data->allocatedmarker = $this->teachers[0]->id;
1134 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
1135
13e82f1c 1136 // Check the allocated marker can view the submission.
f8d107b3
DM
1137 $this->setUser($this->teachers[0]);
1138 $gradingtable = new assign_grading_table($assign, 100, '', 0, true);
1139 $output = $assign->get_renderer()->render($gradingtable);
1140 $this->assertEquals(true, strpos($output, $this->students[0]->lastname));
1141
13e82f1c 1142 // Check that other teachers can't view this submission.
f8d107b3
DM
1143 $this->setUser($this->teachers[1]);
1144 $gradingtable = new assign_grading_table($assign, 100, '', 0, true);
1145 $output = $assign->get_renderer()->render($gradingtable);
1146 $this->assertNotEquals(true, strpos($output, $this->students[0]->lastname));
1147 }
76e77b05
FM
1148
1149 public function test_extension_granted_event() {
1150 $this->setUser($this->editingteachers[0]);
1151
1152 $tomorrow = time() + 24*60*60;
1153 $yesterday = time() - 24*60*60;
1154
1155 $assign = $this->create_instance(array('duedate' => $yesterday, 'cutoffdate' => $yesterday));
1156 $sink = $this->redirectEvents();
1157
1158 $assign->testable_save_user_extension($this->students[0]->id, $tomorrow);
1159
1160 $events = $sink->get_events();
1161 $this->assertCount(1, $events);
1162 $event = reset($events);
1163 $this->assertInstanceOf('\mod_assign\event\extension_granted', $event);
1164 $this->assertEquals($assign->get_context(), $event->get_context());
1165 $this->assertEquals($assign->get_instance()->id, $event->objectid);
1166 $this->assertEquals($this->students[0]->id, $event->relateduserid);
1167 $expected = array(
1168 $assign->get_course()->id,
1169 'assign',
1170 'grant extension',
1171 'view.php?id=' . $assign->get_course_module()->id,
1172 $this->students[0]->id,
1173 $assign->get_course_module()->id,
1174 $this->editingteachers[0]->id
1175 );
1176 $this->assertEventLegacyLogData($expected, $event);
1177 $sink->close();
1178 }
1179
3d1331be
FM
1180 public function test_submission_locked_event() {
1181 $this->editingteachers[0]->ignoresesskey = true;
1182 $this->setUser($this->editingteachers[0]);
1183
1184 $assign = $this->create_instance();
1185 $sink = $this->redirectEvents();
1186
05a6445a 1187 $assign->lock_submission($this->students[0]->id);
3d1331be
FM
1188
1189 $events = $sink->get_events();
1190 $this->assertCount(1, $events);
1191 $event = reset($events);
1192 $this->assertInstanceOf('\mod_assign\event\submission_locked', $event);
1193 $this->assertEquals($assign->get_context(), $event->get_context());
1194 $this->assertEquals($assign->get_instance()->id, $event->objectid);
1195 $this->assertEquals($this->students[0]->id, $event->relateduserid);
1196 $expected = array(
1197 $assign->get_course()->id,
1198 'assign',
1199 'lock submission',
1200 'view.php?id=' . $assign->get_course_module()->id,
1201 get_string('locksubmissionforstudent', 'assign', array('id' => $this->students[0]->id,
1202 'fullname' => fullname($this->students[0]))),
1203 $assign->get_course_module()->id,
1204 $this->editingteachers[0]->id
1205 );
1206 $this->assertEventLegacyLogData($expected, $event);
1207 $sink->close();
1208
1209 // Revert to defaults.
1210 $this->editingteachers[0]->ignoresesskey = false;
1211 }
1212
ad10ad14
FM
1213 public function test_identities_revealed_event() {
1214 $this->editingteachers[0]->ignoresesskey = true;
1215 $this->setUser($this->editingteachers[0]);
1216
1217 $assign = $this->create_instance(array('blindmarking'=>1));
1218 $sink = $this->redirectEvents();
1219
05a6445a 1220 $assign->reveal_identities();
ad10ad14
FM
1221
1222 $events = $sink->get_events();
1223 $this->assertCount(1, $events);
1224 $event = reset($events);
1225 $this->assertInstanceOf('\mod_assign\event\identities_revealed', $event);
1226 $this->assertEquals($assign->get_context(), $event->get_context());
1227 $this->assertEquals($assign->get_instance()->id, $event->objectid);
1228 $expected = array(
1229 $assign->get_course()->id,
1230 'assign',
1231 'reveal identities',
1232 'view.php?id=' . $assign->get_course_module()->id,
1233 get_string('revealidentities', 'assign'),
1234 $assign->get_course_module()->id,
1235 $this->editingteachers[0]->id
1236 );
1237 $this->assertEventLegacyLogData($expected, $event);
1238 $sink->close();
1239
1240 // Revert to defaults.
1241 $this->editingteachers[0]->ignoresesskey = false;
1242 }
1243
159b7f40
FM
1244 public function test_submission_status_updated_event() {
1245 $this->editingteachers[0]->ignoresesskey = true;
1246 $this->setUser($this->editingteachers[0]);
1247
1248 $assign = $this->create_instance();
1249 $submission = $assign->get_user_submission($this->students[0]->id, true);
1250 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
1251 $assign->testable_update_submission($submission, $this->students[0]->id, true, false);
1252
1253 $sink = $this->redirectEvents();
05a6445a 1254 $assign->revert_to_draft($this->students[0]->id);
159b7f40
FM
1255
1256 $events = $sink->get_events();
1257 $this->assertCount(1, $events);
1258 $event = reset($events);
1259 $this->assertInstanceOf('\mod_assign\event\submission_status_updated', $event);
1260 $this->assertEquals($assign->get_context(), $event->get_context());
1261 $this->assertEquals($submission->id, $event->objectid);
1262 $this->assertEquals($this->students[0]->id, $event->relateduserid);
1263 $this->assertEquals(ASSIGN_SUBMISSION_STATUS_DRAFT, $event->other['newstatus']);
1264 $expected = array(
1265 $assign->get_course()->id,
1266 'assign',
1267 'revert submission to draft',
1268 'view.php?id=' . $assign->get_course_module()->id,
1269 get_string('reverttodraftforstudent', 'assign', array('id' => $this->students[0]->id,
1270 'fullname' => fullname($this->students[0]))),
1271 $assign->get_course_module()->id,
1272 $this->editingteachers[0]->id
1273 );
1274 $this->assertEventLegacyLogData($expected, $event);
1275 $sink->close();
1276
1277 // Revert to defaults.
1278 $this->editingteachers[0]->ignoresesskey = false;
1279 }
1280
e59a85ba
FM
1281 public function test_marker_updated_event() {
1282 $this->editingteachers[0]->ignoresesskey = true;
1283 $this->setUser($this->editingteachers[0]);
1284
1285 $assign = $this->create_instance();
1286
1287 $sink = $this->redirectEvents();
1288 $assign->testable_process_set_batch_marking_allocation($this->students[0]->id, $this->teachers[0]->id);
1289
1290 $events = $sink->get_events();
1291 $this->assertCount(1, $events);
1292 $event = reset($events);
1293 $this->assertInstanceOf('\mod_assign\event\marker_updated', $event);
1294 $this->assertEquals($assign->get_context(), $event->get_context());
1295 $this->assertEquals($assign->get_instance()->id, $event->objectid);
1296 $this->assertEquals($this->students[0]->id, $event->relateduserid);
1297 $this->assertEquals($this->editingteachers[0]->id, $event->userid);
1298 $this->assertEquals($this->teachers[0]->id, $event->other['markerid']);
1299 $expected = array(
1300 $assign->get_course()->id,
1301 'assign',
1302 'set marking allocation',
1303 'view.php?id=' . $assign->get_course_module()->id,
1304 get_string('setmarkerallocationforlog', 'assign', array('id' => $this->students[0]->id,
1305 'fullname' => fullname($this->students[0]), 'marker' => fullname($this->teachers[0]))),
1306 $assign->get_course_module()->id,
1307 $this->editingteachers[0]->id
1308 );
1309 $this->assertEventLegacyLogData($expected, $event);
1310 $sink->close();
1311
1312 // Revert to defaults.
1313 $this->editingteachers[0]->ignoresesskey = false;
1314 }
1315
8bb213eb
FM
1316 public function test_workflow_state_updated_event() {
1317 $this->editingteachers[0]->ignoresesskey = true;
1318 $this->setUser($this->editingteachers[0]);
1319
1320 $assign = $this->create_instance();
1321
1322 $sink = $this->redirectEvents();
1323 $assign->testable_process_set_batch_marking_workflow_state($this->students[0]->id, ASSIGN_MARKING_WORKFLOW_STATE_INREVIEW);
1324
1325 $events = $sink->get_events();
1326 $this->assertCount(1, $events);
1327 $event = reset($events);
1328 $this->assertInstanceOf('\mod_assign\event\workflow_state_updated', $event);
1329 $this->assertEquals($assign->get_context(), $event->get_context());
1330 $this->assertEquals($assign->get_instance()->id, $event->objectid);
1331 $this->assertEquals($this->students[0]->id, $event->relateduserid);
1332 $this->assertEquals($this->editingteachers[0]->id, $event->userid);
1333 $this->assertEquals(ASSIGN_MARKING_WORKFLOW_STATE_INREVIEW, $event->other['newstate']);
1334 $expected = array(
1335 $assign->get_course()->id,
1336 'assign',
1337 'set marking workflow state',
1338 'view.php?id=' . $assign->get_course_module()->id,
1339 get_string('setmarkingworkflowstateforlog', 'assign', array('id' => $this->students[0]->id,
1340 'fullname' => fullname($this->students[0]), 'state' => ASSIGN_MARKING_WORKFLOW_STATE_INREVIEW)),
1341 $assign->get_course_module()->id,
1342 $this->editingteachers[0]->id
1343 );
1344 $this->assertEventLegacyLogData($expected, $event);
1345 $sink->close();
1346
1347 // Revert to defaults.
1348 $this->editingteachers[0]->ignoresesskey = false;
1349 }
89fbc202
FM
1350
1351 public function test_submission_duplicated_event() {
1352 $this->setUser($this->students[0]);
1353
1354 $assign = $this->create_instance();
1355 $submission1 = $assign->get_user_submission($this->students[0]->id, true, 0);
1356 $submission2 = $assign->get_user_submission($this->students[0]->id, true, 1);
1357 $submission2->status = ASSIGN_SUBMISSION_STATUS_REOPENED;
1358 $assign->testable_update_submission($submission2, $this->students[0]->id, time(), $assign->get_instance()->teamsubmission);
1359
1360 $sink = $this->redirectEvents();
1361 $notices = null;
05a6445a 1362 $assign->copy_previous_attempt($notices);
89fbc202
FM
1363
1364 $events = $sink->get_events();
1365 $this->assertCount(1, $events);
1366 $event = reset($events);
1367 $this->assertInstanceOf('\mod_assign\event\submission_duplicated', $event);
1368 $this->assertEquals($assign->get_context(), $event->get_context());
1369 $this->assertEquals($submission2->id, $event->objectid);
1370 $this->assertEquals($this->students[0]->id, $event->userid);
1371 $submission2->status = ASSIGN_SUBMISSION_STATUS_DRAFT;
1372 $expected = array(
1373 $assign->get_course()->id,
1374 'assign',
1375 'submissioncopied',
1376 'view.php?id=' . $assign->get_course_module()->id,
1377 $assign->testable_format_submission_for_log($submission2),
1378 $assign->get_course_module()->id,
1379 $this->students[0]->id
1380 );
1381 $this->assertEventLegacyLogData($expected, $event);
1382 $sink->close();
1383 }
1384
9a289c3d
FM
1385 public function test_submission_unlocked_event() {
1386 $this->editingteachers[0]->ignoresesskey = true;
1387 $this->setUser($this->editingteachers[0]);
1388
1389 $assign = $this->create_instance();
1390 $sink = $this->redirectEvents();
1391
05a6445a 1392 $assign->unlock_submission($this->students[0]->id);
9a289c3d
FM
1393
1394 $events = $sink->get_events();
1395 $this->assertCount(1, $events);
1396 $event = reset($events);
1397 $this->assertInstanceOf('\mod_assign\event\submission_unlocked', $event);
1398 $this->assertEquals($assign->get_context(), $event->get_context());
1399 $this->assertEquals($assign->get_instance()->id, $event->objectid);
1400 $this->assertEquals($this->students[0]->id, $event->relateduserid);
1401 $expected = array(
1402 $assign->get_course()->id,
1403 'assign',
1404 'unlock submission',
1405 'view.php?id=' . $assign->get_course_module()->id,
1406 get_string('unlocksubmissionforstudent', 'assign', array('id' => $this->students[0]->id,
1407 'fullname' => fullname($this->students[0]))),
1408 $assign->get_course_module()->id,
1409 $this->editingteachers[0]->id
1410 );
1411 $this->assertEventLegacyLogData($expected, $event);
1412 $sink->close();
1413
1414 // Revert to defaults.
1415 $this->editingteachers[0]->ignoresesskey = false;
1416 }
1417
bd2e9829
FM
1418 public function test_submission_graded_event() {
1419 $this->setUser($this->editingteachers[0]);
1420 $assign = $this->create_instance();
1421
1422 // Test apply_grade_to_user.
1423 $sink = $this->redirectEvents();
1424
1425 $data = new stdClass();
1426 $data->grade = '50.0';
1427 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
1428 $grade = $assign->get_user_grade($this->students[0]->id, false, 0);
1429
1430 $events = $sink->get_events();
1431 $this->assertCount(1, $events);
1432 $event = reset($events);
1433 $this->assertInstanceOf('\mod_assign\event\submission_graded', $event);
1434 $this->assertEquals($assign->get_context(), $event->get_context());
1435 $this->assertEquals($grade->id, $event->objectid);
1436 $this->assertEquals($this->students[0]->id, $event->relateduserid);
1437 $expected = array(
1438 $assign->get_course()->id,
1439 'assign',
1440 'grade submission',
1441 'view.php?id=' . $assign->get_course_module()->id,
1442 $assign->format_grade_for_log($grade),
1443 $assign->get_course_module()->id,
1444 $this->editingteachers[0]->id
1445 );
1446 $this->assertEventLegacyLogData($expected, $event);
1447 $sink->close();
1448
1449 // Test process_save_quick_grades.
1450 $sink = $this->redirectEvents();
1451
1452 $data = array(
1453 'grademodified_' . $this->students[0]->id => time(),
1454 'quickgrade_' . $this->students[0]->id => '60.0'
1455 );
1456 $assign->testable_process_save_quick_grades($data);
1457 $grade = $assign->get_user_grade($this->students[0]->id, false);
1458 $this->assertEquals('60.0', $grade->grade);
1459
1460 $events = $sink->get_events();
1461 $this->assertCount(1, $events);
1462 $event = reset($events);
1463 $this->assertInstanceOf('\mod_assign\event\submission_graded', $event);
1464 $this->assertEquals($assign->get_context(), $event->get_context());
1465 $this->assertEquals($grade->id, $event->objectid);
1466 $this->assertEquals($this->students[0]->id, $event->relateduserid);
1467 $expected = array(
1468 $assign->get_course()->id,
1469 'assign',
1470 'grade submission',
1471 'view.php?id=' . $assign->get_course_module()->id,
1472 $assign->format_grade_for_log($grade),
1473 $assign->get_course_module()->id,
1474 $this->editingteachers[0]->id
1475 );
1476 $this->assertEventLegacyLogData($expected, $event);
1477 $sink->close();
1478 }
1479
9054c04d 1480 public function test_disable_submit_after_cutoff_date() {
1481 global $PAGE;
1482
1483 $this->setUser($this->editingteachers[0]);
1484 $now = time();
1485 $tomorrow = $now + 24*60*60;
1486 $lastweek = $now - 7*24*60*60;
1487 $yesterday = $now - 24*60*60;
1488
1489 $assign = $this->create_instance(array('duedate'=>$yesterday,
1490 'cutoffdate'=>$tomorrow,
1491 'assignsubmission_onlinetext_enabled'=>1));
1492 $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
1493
1494 // Student should be able to see an add submission button.
1495 $this->setUser($this->students[0]);
1496 $output = $assign->view_student_summary($this->students[0], true);
1497 $this->assertNotEquals(false, strpos($output, get_string('addsubmission', 'assign')));
1498
1499 // Add a submission but don't submit now.
1500 $submission = $assign->get_user_submission($this->students[0]->id, true);
1501 $data = new stdClass();
1502 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
1503 'text'=>'Submission text',
1504 'format'=>FORMAT_MOODLE);
1505 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
1506 $plugin->save($submission, $data);
1507
1508 // Create another instance with cut-off and due-date already passed.
1509 $this->setUser($this->editingteachers[0]);
1510 $now = time();
1511 $assign = $this->create_instance(array('duedate'=>$lastweek,
1512 'cutoffdate'=>$yesterday,
1513 'assignsubmission_onlinetext_enabled'=>1));
1514
1515 $this->setUser($this->students[0]);
1516 $output = $assign->view_student_summary($this->students[0], true);
1517 $this->assertNotContains($output, get_string('editsubmission', 'assign'),
1518 'Should not be able to edit after cutoff date.');
1519 $this->assertNotContains($output, get_string('submitassignment', 'assign'),
1520 'Should not be able to submit after cutoff date.');
1521 }
f159ad73 1522 /**
1523 * Testing for submission comment plugin settings
1524 */
1525 public function test_submission_comment_plugin_settings() {
1526 global $CFG;
1527
1528 $commentconfig = false;
1529 if (!empty($CFG->usecomments)) {
1530 $commentconfig = $CFG->usecomments;
1531 }
1532
1533 $CFG->usecomments = true;
1534 $assign = $this->create_instance();
1535 $plugin = $assign->get_submission_plugin_by_type('comments');
1536 $this->assertEquals(1, $plugin->is_enabled('enabled'));
1537
1538 $assign = $this->create_instance(array('assignsubmission_comments_enabled' => 0));
1539 $plugin = $assign->get_submission_plugin_by_type('comments');
1540 $this->assertEquals(1, $plugin->is_enabled('enabled'));
1541
1542 $assign = $this->create_instance(array('assignsubmission_comments_enabled' => 1));
1543 $plugin = $assign->get_submission_plugin_by_type('comments');
1544 $this->assertEquals(1, $plugin->is_enabled('enabled'));
1545
1546 $CFG->usecomments = false;
1547 $assign = $this->create_instance();
1548 $plugin = $assign->get_submission_plugin_by_type('comments');
1549 $this->assertEquals(0, $plugin->is_enabled('enabled'));
1550
1551 $assign = $this->create_instance(array('assignsubmission_comments_enabled' => 0));
1552 $plugin = $assign->get_submission_plugin_by_type('comments');
1553 $this->assertEquals(0, $plugin->is_enabled('enabled'));
1554
1555 $assign = $this->create_instance(array('assignsubmission_comments_enabled' => 1));
1556 $plugin = $assign->get_submission_plugin_by_type('comments');
1557 $this->assertEquals(0, $plugin->is_enabled('enabled'));
1558
1559 $CFG->usecomments = $commentconfig;
1560 }
1561
c7a73689
DW
1562 /**
1563 * Testing for comment inline settings
1564 */
1565 public function test_feedback_comment_commentinline() {
1566 global $CFG;
1567
1568 $sourcetext = "Hello!
1569
1570I'm writing to you from the Moodle Majlis in Muscat, Oman, where we just had several days of Moodle community goodness.
1571
1572URL outside a tag: https://moodle.org/logo/logo-240x60.gif
1573Plugin url outside a tag: @@PLUGINFILE@@/logo-240x60.gif
1574
1575External link 1:<img src='https://moodle.org/logo/logo-240x60.gif' alt='Moodle'/>
1576External link 2:<img alt=\"Moodle\" src=\"https://moodle.org/logo/logo-240x60.gif\"/>
1577Internal link 1:<img src='@@PLUGINFILE@@/logo-240x60.gif' alt='Moodle'/>
1578Internal link 2:<img alt=\"Moodle\" src=\"@@PLUGINFILE@@logo-240x60.gif\"/>
1579Anchor link 1:<a href=\"@@PLUGINFILE@@logo-240x60.gif\" alt=\"bananas\">Link text</a>
1580Anchor link 2:<a title=\"bananas\" href=\"../logo-240x60.gif\">Link text</a>
1581";
1582
1583 // Note the internal images have been stripped and the html is purified (quotes fixed in this case).
1584 $filteredtext = "Hello!
1585
1586I'm writing to you from the Moodle Majlis in Muscat, Oman, where we just had several days of Moodle community goodness.
1587
1588URL outside a tag: https://moodle.org/logo/logo-240x60.gif
1589Plugin url outside a tag: @@PLUGINFILE@@/logo-240x60.gif
1590
1591External link 1:<img src=\"https://moodle.org/logo/logo-240x60.gif\" alt=\"Moodle\" />
1592External link 2:<img alt=\"Moodle\" src=\"https://moodle.org/logo/logo-240x60.gif\" />
1593Internal link 1:
1594Internal link 2:
1595Anchor link 1:Link text
1596Anchor link 2:<a title=\"bananas\" href=\"../logo-240x60.gif\">Link text</a>
1597";
1598
1599 $this->setUser($this->editingteachers[0]);
1600 $params = array('assignsubmission_onlinetext_enabled' => 1,
1601 'assignfeedback_comments_enabled' => 1,
1602 'assignfeedback_comments_commentinline' => 1);
1603 $assign = $this->create_instance($params);
1604
1605 $this->setUser($this->students[0]);
1606 // Add a submission but don't submit now.
1607 $submission = $assign->get_user_submission($this->students[0]->id, true);
1608 $data = new stdClass();
1609
1610 // Test the internal link is stripped, but the external one is not.
1611 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
1612 'text'=>$sourcetext,
1613 'format'=>FORMAT_MOODLE);
1614
1615 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
1616 $plugin->save($submission, $data);
1617
1618 $this->setUser($this->editingteachers[0]);
1619
1620 $data = new stdClass();
1621 require_once($CFG->dirroot . '/mod/assign/gradeform.php');
1622 $pagination = array('userid'=>$this->students[0]->id,
1623 'rownum'=>0,
1624 'last'=>true,
1625 'useridlistid'=>time(),
1626 'attemptnumber'=>0);
1627 $formparams = array($assign, $data, $pagination);
1628 $mform = new mod_assign_grade_form(null, $formparams);
1629
1630 $this->assertEquals($filteredtext, $data->assignfeedbackcomments_editor['text']);
1631 }
1632
f159ad73 1633 /**
1634 * Testing for feedback comment plugin settings
1635 */
1636 public function test_feedback_plugin_settings() {
1637
1638 $assign = $this->create_instance();
1639 $plugin = $assign->get_feedback_plugin_by_type('comments');
1640 $this->assertEquals(0, $plugin->is_enabled('enabled'));
1641
1642 $assign = $this->create_instance(array('assignfeedback_comments_enabled' => 0));
1643 $plugin = $assign->get_feedback_plugin_by_type('comments');
1644 $this->assertEquals(0, $plugin->is_enabled('enabled'));
1645
1646 $assign = $this->create_instance(array('assignfeedback_comments_enabled' => 1));
1647 $plugin = $assign->get_feedback_plugin_by_type('comments');
1648 $this->assertEquals(1, $plugin->is_enabled('enabled'));
1649 }
456d7bc7
RT
1650
1651 /**
1652 * Testing if gradebook feedback plugin is enabled.
1653 */
1654 public function test_is_gradebook_feedback_enabled() {
1655 $adminconfig = get_config('assign');
1656 $gradebookplugin = $adminconfig->feedback_plugin_for_gradebook;
1657
1658 // Create assignment with gradebook feedback enabled and grade = 0.
1659 $assign = $this->create_instance(array($gradebookplugin . '_enabled' => 1, 'grades' => 0));
1660
1661 // Get gradebook feedback plugin.
1662 $gradebookplugintype = str_replace('assignfeedback_', '', $gradebookplugin);
1663 $plugin = $assign->get_feedback_plugin_by_type($gradebookplugintype);
1664 $this->assertEquals(1, $plugin->is_enabled('enabled'));
1665 $this->assertEquals(1, $assign->is_gradebook_feedback_enabled());
1666
1667 // Create assignment with gradebook feedback disabled and grade = 0.
1668 $assign = $this->create_instance(array($gradebookplugin . '_enabled' => 0, 'grades' => 0));
1669 $plugin = $assign->get_feedback_plugin_by_type($gradebookplugintype);
1670 $this->assertEquals(0, $plugin->is_enabled('enabled'));
1671 }
47f48152
DW
1672}
1673