Merge branch 'MDL-37621-master' of git://github.com/damyon/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();
11527706
DW
57 $installedplugins = array_keys(get_plugin_list('assignfeedback'));
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();
11527706
DW
67 $installedplugins = array_keys(get_plugin_list('assignsubmission'));
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;
86 $this->setUser($this->students[0]);
87 $this->setExpectedException('required_capability_exception');
88 $assign->testable_process_reveal_identities();
89
90 // Test teachers cannot reveal identities.
91 $nopermission = false;
92 $this->setUser($this->teachers[0]);
93 $this->setExpectedException('required_capability_exception');
94 $assign->testable_process_reveal_identities();
95
96 // Test sesskey is required.
47f48152
DW
97 $this->setUser($this->editingteachers[0]);
98 $this->setExpectedException('moodle_exception');
99 $assign->testable_process_reveal_identities();
100
101 // Test editingteacher can reveal identities if sesskey is ignored.
102 $this->editingteachers[0]->ignoresesskey = true;
103 $this->setUser($this->editingteachers[0]);
104 $assign->testable_process_reveal_identities();
105 $this->assertEquals(false, $assign->is_blind_marking());
106
107 // Test student names are visible.
108 $gradingtable = new assign_grading_table($assign, 1, '', 0, true);
109 $output = $assign->get_renderer()->render($gradingtable);
110 $this->assertEquals(false, strpos($output, get_string('hiddenuser', 'assign')));
111
112 // Set this back to default.
113 $this->editingteachers[0]->ignoresesskey = false;
114 }
115
116 public function test_show_intro() {
117 // Test whether we are showing the intro at the correct times.
118 $this->setUser($this->editingteachers[0]);
119 $assign = $this->create_instance(array('alwaysshowdescription'=>1));
120
121 $this->assertEquals(true, $assign->testable_show_intro());
122
123 $tomorrow = time() + (24*60*60);
124
125 $assign = $this->create_instance(array('alwaysshowdescription'=>0,
126 'allowsubmissionsfromdate'=>$tomorrow));
127 $this->assertEquals(false, $assign->testable_show_intro());
128 $yesterday = time() - (24*60*60);
129 $assign = $this->create_instance(array('alwaysshowdescription'=>0,
130 'allowsubmissionsfromdate'=>$yesterday));
131 $this->assertEquals(true, $assign->testable_show_intro());
132 }
133
134 public function test_has_submissions_or_grades() {
135 $this->setUser($this->editingteachers[0]);
136 $assign = $this->create_instance(array('assignsubmission_onlinetext_enabled'=>1));
137
138 $instance = $assign->get_instance();
139
140 // Should start empty.
141 $this->assertEquals(false, $assign->has_submissions_or_grades());
142
143 // Simulate a submission.
144 $this->setUser($this->students[0]);
145 $submission = $assign->get_user_submission($this->students[0]->id, true);
146 $data = new stdClass();
147 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
148 'text'=>'Submission text',
149 'format'=>FORMAT_MOODLE);
150 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
151 $plugin->save($submission, $data);
152
153 // Now test again.
154 $this->assertEquals(true, $assign->has_submissions_or_grades());
155 // Set this back to default.
156 $this->students[0]->ignoresesskey = false;
157 }
158
159 public function test_delete_grades() {
160 $this->setUser($this->editingteachers[0]);
161 $assign = $this->create_instance();
162
163 // Simulate adding a grade.
164 $this->setUser($this->teachers[0]);
165 $data = new stdClass();
166 $data->grade = '50.0';
df211804 167 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
47f48152
DW
168
169 // Now see if the data is in the gradebook.
170 $gradinginfo = grade_get_grades($this->course->id,
171 'mod',
172 'assign',
173 $assign->get_instance()->id);
174
175 $this->assertNotEquals(0, count($gradinginfo->items));
176
177 $assign->testable_delete_grades();
178 $gradinginfo = grade_get_grades($this->course->id,
179 'mod',
180 'assign',
181 $assign->get_instance()->id);
182
183 $this->assertEquals(0, count($gradinginfo->items));
184 }
185
186 public function test_delete_instance() {
187 $this->setUser($this->editingteachers[0]);
188 $assign = $this->create_instance(array('assignsubmission_onlinetext_enabled'=>1));
189
190 // Simulate adding a grade.
191 $this->setUser($this->teachers[0]);
192 $data = new stdClass();
193 $data->grade = '50.0';
df211804 194 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
47f48152
DW
195
196 // Simulate a submission.
197 $this->setUser($this->students[0]);
198 $submission = $assign->get_user_submission($this->students[0]->id, true);
199 $data = new stdClass();
200 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
201 'text'=>'Submission text',
202 'format'=>FORMAT_MOODLE);
203 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
204 $plugin->save($submission, $data);
205
206 // Now try and delete.
207 $this->assertEquals(true, $assign->delete_instance());
208 }
209
210 public function test_reset_userdata() {
211 global $DB;
212
213 $now = time();
214 $this->setUser($this->editingteachers[0]);
215 $assign = $this->create_instance(array('assignsubmission_onlinetext_enabled'=>1,
216 'duedate'=>$now));
217
218 // Simulate adding a grade.
219 $this->setUser($this->teachers[0]);
220 $data = new stdClass();
221 $data->grade = '50.0';
df211804 222 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
47f48152
DW
223
224 // Simulate a submission.
225 $this->setUser($this->students[0]);
226 $submission = $assign->get_user_submission($this->students[0]->id, true);
227 $data = new stdClass();
228 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
229 'text'=>'Submission text',
230 'format'=>FORMAT_MOODLE);
231 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
232 $plugin->save($submission, $data);
233
234 $this->assertEquals(true, $assign->has_submissions_or_grades());
235 // Now try and reset.
236 $data = new stdClass();
237 $data->reset_assign_submissions = 1;
238 $data->reset_gradebook_grades = 1;
239 $data->courseid = $this->course->id;
240 $data->timeshift = 24*60*60;
241 $this->setUser($this->editingteachers[0]);
242 $assign->reset_userdata($data);
243 $this->assertEquals(false, $assign->has_submissions_or_grades());
244
245 // Reload the instance data.
246 $instance = $DB->get_record('assign', array('id'=>$assign->get_instance()->id));
247 $this->assertEquals($now + 24*60*60, $instance->duedate);
248 }
249
250 public function test_plugin_settings() {
251 global $DB;
252
253 $now = time();
254 $this->setUser($this->editingteachers[0]);
255 $assign = $this->create_instance(array('assignsubmission_file_enabled'=>1,
256 'assignsubmission_file_maxfiles'=>12,
257 'assignsubmission_file_maxsizebytes'=>10));
258
259 $plugin = $assign->get_submission_plugin_by_type('file');
260 $this->assertEquals('12', $plugin->get_config('maxfilesubmissions'));
261 }
262
263 public function test_update_calendar() {
264 global $DB;
265
266 $now = time();
267 $this->setUser($this->editingteachers[0]);
268 $assign = $this->create_instance(array('duedate'=>$now));
269
270 // See if there is an event in the calendar.
271 $params = array('modulename'=>'assign', 'instance'=>$assign->get_instance()->id);
272 $id = $DB->get_field('event', 'id', $params);
273
274 $this->assertEquals(false, empty($id));
275 }
276
277 public function test_update_instance() {
278 global $DB;
279
280 $this->setUser($this->editingteachers[0]);
281 $assign = $this->create_instance(array('assignsubmission_onlinetext_enabled'=>1));
282
283 $now = time();
284 $instance = $assign->get_instance();
285 $instance->duedate = $now;
286 $instance->instance = $instance->id;
287 $instance->assignsubmission_onlinetext_enabled = 1;
47f48152
DW
288
289 $assign->update_instance($instance);
290
291 $instance = $DB->get_record('assign', array('id'=>$assign->get_instance()->id));
292 $this->assertEquals($now, $instance->duedate);
293 }
294
295 public function test_list_participants() {
df211804 296 $this->create_extra_users();
47f48152
DW
297 $this->setUser($this->editingteachers[0]);
298 $assign = $this->create_instance(array('grade'=>100));
299
a5c793c3 300 $this->assertEquals(self::DEFAULT_STUDENT_COUNT + self::EXTRA_STUDENT_COUNT, count($assign->list_participants(null, true)));
47f48152
DW
301 }
302
303 public function test_count_teams() {
df211804 304 $this->create_extra_users();
47f48152
DW
305 $this->setUser($this->editingteachers[0]);
306 $assign = $this->create_instance(array('teamsubmission'=>1));
307
a5c793c3 308 $this->assertEquals(self::GROUP_COUNT + 1, $assign->count_teams());
47f48152
DW
309 }
310
311 public function test_count_submissions() {
df211804 312 $this->create_extra_users();
47f48152
DW
313 $this->setUser($this->editingteachers[0]);
314 $assign = $this->create_instance(array('assignsubmission_onlinetext_enabled'=>1));
315
316 // Simulate a submission.
a5c793c3
DW
317 $this->setUser($this->extrastudents[0]);
318 $submission = $assign->get_user_submission($this->extrastudents[0]->id, true);
47f48152
DW
319 // Leave this one as DRAFT.
320 $data = new stdClass();
321 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
322 'text'=>'Submission text',
323 'format'=>FORMAT_MOODLE);
324 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
325 $plugin->save($submission, $data);
326
327 // Simulate adding a grade.
328 $this->setUser($this->teachers[0]);
329 $data = new stdClass();
330 $data->grade = '50.0';
df211804 331 $assign->testable_apply_grade_to_user($data, $this->extrastudents[0]->id, 0);
47f48152
DW
332
333 // Simulate a submission.
a5c793c3
DW
334 $this->setUser($this->extrastudents[1]);
335 $submission = $assign->get_user_submission($this->extrastudents[1]->id, true);
47f48152 336 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
a5c793c3 337 $assign->testable_update_submission($submission, $this->extrastudents[1]->id, true, false);
47f48152
DW
338 $data = new stdClass();
339 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
340 'text'=>'Submission text',
341 'format'=>FORMAT_MOODLE);
342 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
343 $plugin->save($submission, $data);
344
345 // Simulate a submission.
a5c793c3
DW
346 $this->setUser($this->extrastudents[2]);
347 $submission = $assign->get_user_submission($this->extrastudents[2]->id, true);
47f48152 348 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
a5c793c3 349 $assign->testable_update_submission($submission, $this->extrastudents[2]->id, true, false);
47f48152
DW
350 $data = new stdClass();
351 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
352 'text'=>'Submission text',
353 'format'=>FORMAT_MOODLE);
354 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
355 $plugin->save($submission, $data);
356
357 // Simulate a submission.
a5c793c3
DW
358 $this->setUser($this->extrastudents[3]);
359 $submission = $assign->get_user_submission($this->extrastudents[3]->id, true);
47f48152 360 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
a5c793c3 361 $assign->testable_update_submission($submission, $this->extrastudents[3]->id, true, false);
47f48152
DW
362 $data = new stdClass();
363 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
364 'text'=>'Submission text',
365 'format'=>FORMAT_MOODLE);
366 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
367 $plugin->save($submission, $data);
368
369 // Simulate adding a grade.
370 $this->setUser($this->teachers[0]);
371 $data = new stdClass();
372 $data->grade = '50.0';
df211804 373 $assign->testable_apply_grade_to_user($data, $this->extrastudents[3]->id, 0);
47f48152
DW
374
375 $this->assertEquals(2, $assign->count_grades());
376 $this->assertEquals(4, $assign->count_submissions());
377 $this->assertEquals(2, $assign->count_submissions_need_grading());
378 $this->assertEquals(3, $assign->count_submissions_with_status(ASSIGN_SUBMISSION_STATUS_SUBMITTED));
379 $this->assertEquals(1, $assign->count_submissions_with_status(ASSIGN_SUBMISSION_STATUS_DRAFT));
380 }
381
382 public function test_get_grading_userid_list() {
df211804 383 $this->create_extra_users();
47f48152
DW
384 $this->setUser($this->editingteachers[0]);
385 $assign = $this->create_instance();
386
387 $users = $assign->testable_get_grading_userid_list();
a5c793c3 388 $this->assertEquals(self::DEFAULT_STUDENT_COUNT + self::EXTRA_STUDENT_COUNT, count($users));
47f48152
DW
389 }
390
391 public function test_cron() {
392 // First run cron so there are no messages waiting to be sent (from other tests).
393 cron_setup_user();
394 assign::cron();
395
396 // Now create an assignment and add some feedback.
397 $this->setUser($this->editingteachers[0]);
398 $assign = $this->create_instance();
399
400 // Simulate adding a grade.
401 $this->setUser($this->teachers[0]);
402 $data = new stdClass();
403 $data->grade = '50.0';
df211804 404 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
47f48152
DW
405
406 // Now run cron and see that one message was sent.
407 $this->preventResetByRollback();
408 $sink = $this->redirectMessages();
409 cron_setup_user();
410 $this->expectOutputRegex('/Done processing 1 assignment submissions/');
411 assign::cron();
412
413 $messages = $sink->get_messages();
414 $this->assertEquals(1, count($messages));
415 $this->assertEquals(1, $messages[0]->notification);
416 $this->assertEquals($assign->get_instance()->name, $messages[0]->contexturlname);
417 }
418
419 public function test_is_graded() {
420 $this->setUser($this->editingteachers[0]);
421 $assign = $this->create_instance();
422
423 // Simulate adding a grade.
424 $this->setUser($this->teachers[0]);
425 $data = new stdClass();
426 $data->grade = '50.0';
df211804 427 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
47f48152
DW
428
429 $this->assertEquals(true, $assign->testable_is_graded($this->students[0]->id));
430 $this->assertEquals(false, $assign->testable_is_graded($this->students[1]->id));
431 }
432
433 public function test_can_view_submission() {
434 $this->setUser($this->editingteachers[0]);
435 $assign = $this->create_instance();
436
437 $this->setUser($this->students[0]);
438 $this->assertEquals(true, $assign->can_view_submission($this->students[0]->id));
439 $this->assertEquals(false, $assign->can_view_submission($this->students[1]->id));
440 $this->assertEquals(false, $assign->can_view_submission($this->teachers[0]->id));
441 $this->setUser($this->teachers[0]);
442 $this->assertEquals(true, $assign->can_view_submission($this->students[0]->id));
443 $this->assertEquals(true, $assign->can_view_submission($this->students[1]->id));
444 $this->assertEquals(true, $assign->can_view_submission($this->teachers[0]->id));
445 $this->setUser($this->editingteachers[0]);
446 $this->assertEquals(true, $assign->can_view_submission($this->students[0]->id));
447 $this->assertEquals(true, $assign->can_view_submission($this->students[1]->id));
448 $this->assertEquals(true, $assign->can_view_submission($this->teachers[0]->id));
449 }
450
451
452 public function test_update_submission() {
df211804 453 $this->create_extra_users();
47f48152
DW
454 $this->setUser($this->editingteachers[0]);
455 $assign = $this->create_instance();
456
a5c793c3 457 $this->setUser($this->extrastudents[0]);
47f48152 458 $now = time();
a5c793c3
DW
459 $submission = $assign->get_user_submission($this->extrastudents[0]->id, true);
460 $assign->testable_update_submission($submission, $this->extrastudents[0]->id, true, false);
47f48152
DW
461
462 $this->setUser($this->teachers[0]);
463 // Verify the gradebook update.
464 $gradinginfo = grade_get_grades($this->course->id,
465 'mod',
466 'assign',
467 $assign->get_instance()->id,
a5c793c3 468 $this->extrastudents[0]->id);
47f48152 469
a5c793c3
DW
470 $this->assertEquals($this->extrastudents[0]->id,
471 $gradinginfo->items[0]->grades[$this->extrastudents[0]->id]->usermodified);
47f48152
DW
472
473 // Now verify group assignments.
474 $this->setUser($this->editingteachers[0]);
475 $assign = $this->create_instance(array('teamsubmission'=>1));
476
a5c793c3 477 $this->setUser($this->extrastudents[0]);
47f48152 478 $now = time();
a5c793c3
DW
479 $submission = $assign->get_group_submission($this->extrastudents[0]->id, 0, true);
480 $assign->testable_update_submission($submission, $this->extrastudents[0]->id, true, true);
47f48152
DW
481
482 // Check that at least 2 members of the submission group had their submission updated.
483
484 $this->setUser($this->editingteachers[0]);
485 $gradinginfo = grade_get_grades($this->course->id,
486 'mod',
487 'assign',
488 $assign->get_instance()->id,
a5c793c3 489 $this->extrastudents[0]->id);
47f48152 490
a5c793c3
DW
491 $this->assertEquals($this->extrastudents[0]->id,
492 $gradinginfo->items[0]->grades[$this->extrastudents[0]->id]->usermodified);
47f48152
DW
493
494 $gradinginfo = grade_get_grades($this->course->id,
495 'mod',
496 'assign',
497 $assign->get_instance()->id,
a5c793c3 498 $this->extrastudents[self::GROUP_COUNT]->id);
47f48152 499
a5c793c3
DW
500 $this->assertEquals($this->extrastudents[self::GROUP_COUNT]->id,
501 $gradinginfo->items[0]->grades[$this->extrastudents[self::GROUP_COUNT]->id]->usermodified);
47f48152
DW
502
503 // Now verify blind marking.
504 $this->setUser($this->editingteachers[0]);
505 $assign = $this->create_instance(array('blindmarking'=>1));
506
a5c793c3 507 $this->setUser($this->extrastudents[0]);
47f48152 508 $now = time();
a5c793c3
DW
509 $submission = $assign->get_user_submission($this->extrastudents[0]->id, true);
510 $assign->testable_update_submission($submission, $this->extrastudents[0]->id, true, false);
47f48152
DW
511
512 $this->setUser($this->editingteachers[0]);
513 $gradinginfo = grade_get_grades($this->course->id,
514 'mod',
515 'assign',
516 $assign->get_instance()->id,
a5c793c3 517 $this->extrastudents[0]->id);
47f48152 518
a5c793c3 519 $this->assertEquals(null, $gradinginfo->items[0]->grades[$this->extrastudents[0]->id]->datesubmitted);
47f48152
DW
520 }
521
522 public function test_submissions_open() {
523 $this->setUser($this->editingteachers[0]);
524
525 $now = time();
526 $tomorrow = $now + 24*60*60;
527 $oneweek = $now + 7*24*60*60;
528 $yesterday = $now - 24*60*60;
529
530 $assign = $this->create_instance();
531 $this->assertEquals(true, $assign->testable_submissions_open($this->students[0]->id));
532
533 $assign = $this->create_instance(array('duedate'=>$tomorrow));
534 $this->assertEquals(true, $assign->testable_submissions_open($this->students[0]->id));
535
536 $assign = $this->create_instance(array('duedate'=>$yesterday));
537 $this->assertEquals(true, $assign->testable_submissions_open($this->students[0]->id));
538
539 $assign = $this->create_instance(array('duedate'=>$yesterday, 'cutoffdate'=>$tomorrow));
540 $this->assertEquals(true, $assign->testable_submissions_open($this->students[0]->id));
541
542 $assign = $this->create_instance(array('duedate'=>$yesterday, 'cutoffdate'=>$yesterday));
543 $this->assertEquals(false, $assign->testable_submissions_open($this->students[0]->id));
544
545 $assign->testable_save_user_extension($this->students[0]->id, $tomorrow);
546 $this->assertEquals(true, $assign->testable_submissions_open($this->students[0]->id));
547
548 $assign = $this->create_instance(array('submissiondrafts'=>1));
549 $this->assertEquals(true, $assign->testable_submissions_open($this->students[0]->id));
550
551 $this->setUser($this->students[0]);
552 $now = time();
553 $submission = $assign->get_user_submission($this->students[0]->id, true);
554 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
555 $assign->testable_update_submission($submission, $this->students[0]->id, true, false);
556 $this->setUser($this->editingteachers[0]);
557 $this->assertEquals(false, $assign->testable_submissions_open($this->students[0]->id));
558 }
559
560 public function test_get_graders() {
df211804 561 $this->create_extra_users();
47f48152
DW
562 $this->setUser($this->editingteachers[0]);
563 $assign = $this->create_instance();
564
a5c793c3
DW
565 $this->assertCount(self::DEFAULT_TEACHER_COUNT +
566 self::DEFAULT_EDITING_TEACHER_COUNT +
567 self::EXTRA_TEACHER_COUNT +
568 self::EXTRA_EDITING_TEACHER_COUNT,
569 $assign->testable_get_graders($this->students[0]->id));
47f48152
DW
570
571 $assign = $this->create_instance();
572 // Force create an assignment with SEPARATEGROUPS.
573 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
574 $params = array('course'=>$this->course->id);
575 $instance = $generator->create_instance($params);
576 $cm = get_coursemodule_from_instance('assign', $instance->id);
577 set_coursemodule_groupmode($cm->id, SEPARATEGROUPS);
578 $cm->groupmode = SEPARATEGROUPS;
579 $context = context_module::instance($cm->id);
580 $assign = new testable_assign($context, $cm, $this->course);
581
582 $this->setUser($this->students[1]);
a5c793c3 583 $this->assertCount(4, $assign->testable_get_graders($this->students[0]->id));
47f48152
DW
584 }
585
586 public function test_get_uniqueid_for_user() {
587 $this->setUser($this->editingteachers[0]);
588 $assign = $this->create_instance();
589
590 foreach ($this->students as $student) {
591 $uniqueid = $assign->get_uniqueid_for_user($student->id);
592 $this->assertEquals($student->id, $assign->get_user_id_for_uniqueid($uniqueid));
593 }
594 }
595
46692c3a 596 public function test_show_student_summary() {
c2114099 597 global $CFG, $PAGE;
46692c3a
DW
598
599 $this->setUser($this->editingteachers[0]);
600 $assign = $this->create_instance();
c2114099 601 $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
46692c3a
DW
602
603 // No feedback should be available because this student has not been graded.
604 $this->setUser($this->students[0]);
605 $output = $assign->view_student_summary($this->students[0], true);
606 $this->assertEquals(false, strpos($output, 'Feedback'), 'Do not show feedback if there is no grade');
607 // Simulate adding a grade.
608 $this->setUser($this->teachers[0]);
609 $data = new stdClass();
610 $data->grade = '50.0';
df211804 611 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
46692c3a 612
b0da618b 613 // Now we should see the feedback.
46692c3a
DW
614 $this->setUser($this->students[0]);
615 $output = $assign->view_student_summary($this->students[0], true);
616 $this->assertNotEquals(false, strpos($output, 'Feedback'), 'Show feedback if there is a grade');
617
618 // Now hide the grade in gradebook.
619 $this->setUser($this->teachers[0]);
620 require_once($CFG->libdir.'/gradelib.php');
621 $gradeitem = new grade_item(array(
622 'itemtype' => 'mod',
623 'itemmodule' => 'assign',
624 'iteminstance' => $assign->get_instance()->id,
625 'courseid' => $this->course->id));
626
627 $gradeitem->set_hidden(1, false);
628
629 // No feedback should be available because the grade is hidden.
630 $this->setUser($this->students[0]);
631 $output = $assign->view_student_summary($this->students[0], true);
632 $this->assertEquals(false, strpos($output, 'Feedback'), 'Do not show feedback if the grade is hidden in the gradebook');
633
b0da618b 634 // Do the same but add feedback.
46692c3a
DW
635 $assign = $this->create_instance(array('assignfeedback_comments_enabled' => 1));
636
637 $this->setUser($this->teachers[0]);
638 $grade = $assign->get_user_grade($this->students[0]->id, true);
639 $data = new stdClass();
640 $data->assignfeedbackcomments_editor = array('text'=>'Tomato sauce',
641 'format'=>FORMAT_MOODLE);
642 $plugin = $assign->get_feedback_plugin_by_type('comments');
643 $plugin->save($grade, $data);
644
b0da618b 645 // Should have feedback but no grade.
46692c3a
DW
646 $this->setUser($this->students[0]);
647 $output = $assign->view_student_summary($this->students[0], true);
648 $this->assertNotEquals(false, strpos($output, 'Tomato sauce'), 'Show feedback even if there is no grade');
649 $this->assertEquals(false, strpos($output, 'Grade'), 'Do not show grade when there is no grade.');
650 $this->assertEquals(false, strpos($output, 'Graded on'), 'Do not show graded date when there is no grade.');
651 }
652
df211804
DW
653 public function test_attempt_reopen_method_manual() {
654 global $PAGE;
655
656 $this->setUser($this->editingteachers[0]);
657 $assign = $this->create_instance(array('attemptreopenmethod'=>ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL,
658 'maxattempts'=>3,
659 'submissiondrafts'=>1,
660 'assignsubmission_onlinetext_enabled'=>1));
661 $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
662
663 // Student should be able to see an add submission button.
664 $this->setUser($this->students[0]);
665 $output = $assign->view_student_summary($this->students[0], true);
666 $this->assertNotEquals(false, strpos($output, get_string('addsubmission', 'assign')));
667
668 // Add a submission.
669 $now = time();
670 $submission = $assign->get_user_submission($this->students[0]->id, true);
671 $data = new stdClass();
672 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
673 'text'=>'Submission text',
674 'format'=>FORMAT_MOODLE);
675 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
676 $plugin->save($submission, $data);
677
678 // And now submit it for marking.
679 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
680 $assign->testable_update_submission($submission, $this->students[0]->id, true, false);
681
682 // Verify the student cannot make changes to the submission.
683 $output = $assign->view_student_summary($this->students[0], true);
684 $this->assertEquals(false, strpos($output, get_string('addsubmission', 'assign')));
685
686 // Mark the submission.
687 $this->setUser($this->teachers[0]);
688 $data = new stdClass();
689 $data->grade = '50.0';
690 $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
691
692 // Check the student can see the grade.
693 $this->setUser($this->students[0]);
694 $output = $assign->view_student_summary($this->students[0], true);
695 $this->assertNotEquals(false, strpos($output, '50.0'));
696
697 // Allow the student another attempt.
698 $this->teachers[0]->ignoresesskey = true;
699 $this->setUser($this->teachers[0]);
700 $result = $assign->testable_process_add_attempt($this->students[0]->id);
701 $this->assertEquals(true, $result);
702
703 // Check that the previous attempt is now in the submission history table.
704 $this->setUser($this->students[0]);
705 $output = $assign->view_student_summary($this->students[0], true);
706 // Need a better check.
707 $this->assertNotEquals(false, strpos($output, 'Submission text'), 'Contains: Submission text');
708
709 // Check that the student now has a button for Add a new attempt".
710 $this->assertNotEquals(false, strpos($output, get_string('addnewattempt', 'assign')));
711 // Check that the student now does not have a button for Submit.
712 $this->assertEquals(false, strpos($output, get_string('submitassignment', 'assign')));
713
714 // Check that the student now has a submission history.
715 $this->assertNotEquals(false, strpos($output, get_string('attempthistory', 'assign')));
716
717 $this->setUser($this->teachers[0]);
718 // Check that the grading table loads correctly and contains this user.
719 // This is also testing that we do not get duplicate rows in the grading table.
720 $gradingtable = new assign_grading_table($assign, 100, '', 0, true);
721 $output = $assign->get_renderer()->render($gradingtable);
722 $this->assertEquals(true, strpos($output, $this->students[0]->lastname));
723
724 // Should be 1 not 2.
725 $this->assertEquals(1, $assign->count_submissions());
726 $this->assertEquals(1, $assign->count_submissions_with_status('reopened'));
727 $this->assertEquals(0, $assign->count_submissions_need_grading());
728 $this->assertEquals(1, $assign->count_grades());
729
730 // Change max attempts to unlimited.
731 $formdata = clone($assign->get_instance());
732 $formdata->maxattempts = ASSIGN_UNLIMITED_ATTEMPTS;
733 $formdata->instance = $formdata->id;
734 $assign->update_instance($formdata);
735
736 // Check we can repopen still.
737 $result = $assign->testable_process_add_attempt($this->students[0]->id);
738 $this->assertEquals(true, $result);
739
740 $grades = $assign->get_user_grades_for_gradebook($this->students[0]->id);
741 $this->assertEquals(50, (int)$grades[$this->students[0]->id]->rawgrade);
742
743
744 }
46692c3a 745
47f48152
DW
746}
747