[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/assign/tests/ -> lib_test.php (source)

   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/lib.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  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  require_once($CFG->dirroot . '/mod/assign/lib.php');
  31  require_once($CFG->dirroot . '/mod/assign/locallib.php');
  32  require_once($CFG->dirroot . '/mod/assign/tests/base_test.php');
  33  
  34  /**
  35   * Unit tests for (some of) mod/assign/lib.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   */
  40  class mod_assign_lib_testcase extends mod_assign_base_testcase {
  41  
  42      protected function setUp() {
  43          parent::setUp();
  44  
  45          // Add additional default data (some real attempts and stuff).
  46          $this->setUser($this->editingteachers[0]);
  47          $this->create_instance();
  48          $assign = $this->create_instance(array('duedate' => time(),
  49                                                 'attemptreopenmethod' => ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL,
  50                                                 'maxattempts' => 3,
  51                                                 'submissiondrafts' => 1,
  52                                                 'assignsubmission_onlinetext_enabled' => 1));
  53  
  54          // Add a submission.
  55          $this->setUser($this->students[0]);
  56          $submission = $assign->get_user_submission($this->students[0]->id, true);
  57          $data = new stdClass();
  58          $data->onlinetext_editor = array('itemid' => file_get_unused_draft_itemid(),
  59                                           'text' => 'Submission text',
  60                                           'format' => FORMAT_HTML);
  61          $plugin = $assign->get_submission_plugin_by_type('onlinetext');
  62          $plugin->save($submission, $data);
  63  
  64          // And now submit it for marking.
  65          $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
  66          $assign->testable_update_submission($submission, $this->students[0]->id, true, false);
  67  
  68          // Mark the submission.
  69          $this->setUser($this->teachers[0]);
  70          $data = new stdClass();
  71          $data->grade = '50.0';
  72          $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
  73  
  74          // This is required so that the submissions timemodified > the grade timemodified.
  75          sleep(2);
  76  
  77          // Edit the submission again.
  78          $this->setUser($this->students[0]);
  79          $submission = $assign->get_user_submission($this->students[0]->id, true);
  80          $assign->testable_update_submission($submission, $this->students[0]->id, true, false);
  81  
  82          // This is required so that the submissions timemodified > the grade timemodified.
  83          sleep(2);
  84  
  85          // Allow the student another attempt.
  86          $this->teachers[0]->ignoresesskey = true;
  87          $this->setUser($this->teachers[0]);
  88          $result = $assign->testable_process_add_attempt($this->students[0]->id);
  89          // Add another submission.
  90          $this->setUser($this->students[0]);
  91          $submission = $assign->get_user_submission($this->students[0]->id, true);
  92          $data = new stdClass();
  93          $data->onlinetext_editor = array('itemid' => file_get_unused_draft_itemid(),
  94                                           'text' => 'Submission text 2',
  95                                           'format' => FORMAT_HTML);
  96          $plugin = $assign->get_submission_plugin_by_type('onlinetext');
  97          $plugin->save($submission, $data);
  98  
  99          // And now submit it for marking (again).
 100          $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
 101          $assign->testable_update_submission($submission, $this->students[0]->id, true, false);
 102      }
 103  
 104      public function test_assign_print_overview() {
 105          global $DB;
 106  
 107          // Create one more assignment instance.
 108          $this->setAdminUser();
 109          $courses = $DB->get_records('course', array('id' => $this->course->id));
 110          // Past assignments should not show up.
 111          $pastassign = $this->create_instance(array('duedate' => time(),
 112                                                     'cutoffdate' => time() - 370000,
 113                                                     'nosubmissions' => 0,
 114                                                     'assignsubmission_onlinetext_enabled' => 1));
 115          // Open assignments should show up only if relevant.
 116          $openassign = $this->create_instance(array('duedate' => time(),
 117                                                     'cutoffdate' => time() + 370000,
 118                                                     'nosubmissions' => 0,
 119                                                     'assignsubmission_onlinetext_enabled' => 1));
 120          $pastsubmission = $pastassign->get_user_submission($this->students[0]->id, true);
 121          $opensubmission = $openassign->get_user_submission($this->students[0]->id, true);
 122  
 123          // Check the overview as the different users.
 124          // For students , open assignments should show only when there are no valid submissions.
 125          $this->setUser($this->students[0]);
 126          $overview = array();
 127          assign_print_overview($courses, $overview);
 128          $this->assertEquals(1, count($overview));
 129          $this->assertRegExp('/.*Assignment 4.*/', $overview[$this->course->id]['assign']); // No valid submission.
 130          $this->assertNotRegExp('/.*Assignment 1.*/', $overview[$this->course->id]['assign']); // Has valid submission.
 131  
 132          // And now submit the submission.
 133          $opensubmission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
 134          $openassign->testable_update_submission($opensubmission, $this->students[0]->id, true, false);
 135  
 136          $overview = array();
 137          assign_print_overview($courses, $overview);
 138          $this->assertEquals(0, count($overview));
 139  
 140          $this->setUser($this->teachers[0]);
 141          $overview = array();
 142          assign_print_overview($courses, $overview);
 143          $this->assertEquals(1, count($overview));
 144          // Submissions without a grade.
 145          $this->assertRegExp('/.*Assignment 4.*/', $overview[$this->course->id]['assign']);
 146          $this->assertRegExp('/.*Assignment 2.*/', $overview[$this->course->id]['assign']);
 147  
 148          $this->setUser($this->editingteachers[0]);
 149          $overview = array();
 150          assign_print_overview($courses, $overview);
 151          $this->assertEquals(1, count($overview));
 152          // Submissions without a grade.
 153          $this->assertRegExp('/.*Assignment 4.*/', $overview[$this->course->id]['assign']);
 154          $this->assertRegExp('/.*Assignment 2.*/', $overview[$this->course->id]['assign']);
 155  
 156          // Let us grade a submission.
 157          $this->setUser($this->teachers[0]);
 158          $data = new stdClass();
 159          $data->grade = '50.0';
 160          $openassign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
 161          $overview = array();
 162          assign_print_overview($courses, $overview);
 163          $this->assertEquals(1, count($overview));
 164          // Now assignment 4 should not show up.
 165          $this->assertNotRegExp('/.*Assignment 4.*/', $overview[$this->course->id]['assign']);
 166          $this->assertRegExp('/.*Assignment 2.*/', $overview[$this->course->id]['assign']);
 167  
 168          $this->setUser($this->editingteachers[0]);
 169          $overview = array();
 170          assign_print_overview($courses, $overview);
 171          $this->assertEquals(1, count($overview));
 172          // Now assignment 4 should not show up.
 173          $this->assertNotRegExp('/.*Assignment 4.*/', $overview[$this->course->id]['assign']);
 174          $this->assertRegExp('/.*Assignment 2.*/', $overview[$this->course->id]['assign']);
 175  
 176          // Open offline assignments should not show any notification to students.
 177          $openassign = $this->create_instance(array('duedate' => time(),
 178                                                     'cutoffdate' => time() + 370000));
 179          $this->setUser($this->students[0]);
 180          $overview = array();
 181          assign_print_overview($courses, $overview);
 182          $this->assertEquals(0, count($overview));
 183      }
 184  
 185      public function test_print_recent_activity() {
 186          // Submitting an assignment generates a notification.
 187          $this->preventResetByRollback();
 188          $sink = $this->redirectMessages();
 189  
 190          $this->setUser($this->editingteachers[0]);
 191          $assign = $this->create_instance();
 192          $data = new stdClass();
 193          $data->userid = $this->students[0]->id;
 194          $notices = array();
 195          $this->setUser($this->students[0]);
 196          $assign->submit_for_grading($data, $notices);
 197  
 198          $this->setUser($this->editingteachers[0]);
 199          $this->expectOutputRegex('/submitted:/');
 200          assign_print_recent_activity($this->course, true, time() - 3600);
 201  
 202          $sink->close();
 203      }
 204  
 205      /** Make sure fullname dosn't trigger any warnings when assign_print_recent_activity is triggered. */
 206      public function test_print_recent_activity_fullname() {
 207          // Submitting an assignment generates a notification.
 208          $this->preventResetByRollback();
 209          $sink = $this->redirectMessages();
 210  
 211          $this->setUser($this->editingteachers[0]);
 212          $assign = $this->create_instance();
 213  
 214          $data = new stdClass();
 215          $data->userid = $this->students[0]->id;
 216          $notices = array();
 217          $this->setUser($this->students[0]);
 218          $assign->submit_for_grading($data, $notices);
 219  
 220          $this->setUser($this->editingteachers[0]);
 221          $this->expectOutputRegex('/submitted:/');
 222          set_config('fullnamedisplay', 'firstname, lastnamephonetic');
 223          assign_print_recent_activity($this->course, false, time() - 3600);
 224  
 225          $sink->close();
 226      }
 227  
 228      /** Make sure blind marking shows participant \d+ not fullname when assign_print_recent_activity is triggered. */
 229      public function test_print_recent_activity_fullname_blind_marking() {
 230          // Submitting an assignment generates a notification in blind marking.
 231          $this->preventResetByRollback();
 232          $sink = $this->redirectMessages();
 233  
 234          $this->setUser($this->editingteachers[0]);
 235          $assign = $this->create_instance(array('blindmarking' => 1));
 236  
 237          $data = new stdClass();
 238          $data->userid = $this->students[0]->id;
 239          $notices = array();
 240          $this->setUser($this->students[0]);
 241          $assign->submit_for_grading($data, $notices);
 242  
 243          $this->setUser($this->editingteachers[0]);
 244          $uniqueid = $assign->get_uniqueid_for_user($data->userid);
 245          $expectedstr = preg_quote(get_string('participant', 'mod_assign'), '/') . '.*' . $uniqueid;
 246          $this->expectOutputRegex("/{$expectedstr}/");
 247          assign_print_recent_activity($this->course, false, time() - 3600);
 248  
 249          $sink->close();
 250      }
 251  
 252      public function test_assign_get_recent_mod_activity() {
 253          // Submitting an assignment generates a notification.
 254          $this->preventResetByRollback();
 255          $sink = $this->redirectMessages();
 256  
 257          $this->setUser($this->editingteachers[0]);
 258          $assign = $this->create_instance();
 259  
 260          $data = new stdClass();
 261          $data->userid = $this->students[0]->id;
 262          $notices = array();
 263          $this->setUser($this->students[0]);
 264          $assign->submit_for_grading($data, $notices);
 265  
 266          $this->setUser($this->editingteachers[0]);
 267          $activities = array();
 268          $index = 0;
 269  
 270          $activity = new stdClass();
 271          $activity->type    = 'activity';
 272          $activity->cmid    = $assign->get_course_module()->id;
 273          $activities[$index++] = $activity;
 274  
 275          assign_get_recent_mod_activity( $activities,
 276                                          $index,
 277                                          time() - 3600,
 278                                          $this->course->id,
 279                                          $assign->get_course_module()->id);
 280  
 281          $this->assertEquals("assign", $activities[1]->type);
 282          $sink->close();
 283      }
 284  
 285      public function test_assign_user_complete() {
 286          global $PAGE, $DB;
 287  
 288          $this->setUser($this->editingteachers[0]);
 289          $assign = $this->create_instance(array('submissiondrafts' => 1));
 290          $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
 291  
 292          $submission = $assign->get_user_submission($this->students[0]->id, true);
 293          $submission->status = ASSIGN_SUBMISSION_STATUS_DRAFT;
 294          $DB->update_record('assign_submission', $submission);
 295  
 296          $this->expectOutputRegex('/Draft/');
 297          assign_user_complete($this->course, $this->students[0], $assign->get_course_module(), $assign->get_instance());
 298      }
 299  
 300      public function test_assign_user_outline() {
 301          $this->setUser($this->editingteachers[0]);
 302          $assign = $this->create_instance();
 303  
 304          $this->setUser($this->teachers[0]);
 305          $data = $assign->get_user_grade($this->students[0]->id, true);
 306          $data->grade = '50.5';
 307          $assign->update_grade($data);
 308  
 309          $result = assign_user_outline($this->course, $this->students[0], $assign->get_course_module(), $assign->get_instance());
 310  
 311          $this->assertRegExp('/50.5/', $result->info);
 312      }
 313  
 314      public function test_assign_get_completion_state() {
 315          global $DB;
 316          $assign = $this->create_instance(array('submissiondrafts' => 0, 'completionsubmit' => 1));
 317  
 318          $this->setUser($this->students[0]);
 319          $result = assign_get_completion_state($this->course, $assign->get_course_module(), $this->students[0]->id, false);
 320          $this->assertFalse($result);
 321          $submission = $assign->get_user_submission($this->students[0]->id, true);
 322          $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
 323          $DB->update_record('assign_submission', $submission);
 324  
 325          $result = assign_get_completion_state($this->course, $assign->get_course_module(), $this->students[0]->id, false);
 326  
 327          $this->assertTrue($result);
 328      }
 329  
 330      /**
 331       * Tests for mod_assign_refresh_events.
 332       */
 333      public function test_assign_refresh_events() {
 334          global $DB;
 335          $duedate = time();
 336          $this->setAdminUser();
 337  
 338          $assign = $this->create_instance(array('duedate' => $duedate));
 339  
 340          // Normal case, with existing course.
 341          $this->assertTrue(assign_refresh_events($this->course->id));
 342  
 343          $instance = $assign->get_instance();
 344          $eventparams = array('modulename' => 'assign', 'instance' => $instance->id);
 345          $event = $DB->get_record('event', $eventparams, '*', MUST_EXIST);
 346          $this->assertEquals($event->timestart, $duedate);
 347  
 348          // In case the course ID is passed as a numeric string.
 349          $this->assertTrue(assign_refresh_events('' . $this->course->id));
 350  
 351          // Course ID not provided.
 352          $this->assertTrue(assign_refresh_events());
 353  
 354          $eventparams = array('modulename' => 'assign');
 355          $events = $DB->get_records('event', $eventparams);
 356          foreach ($events as $event) {
 357              if ($event->modulename === 'assign' && $event->instance === $instance->id) {
 358                  $this->assertEquals($event->timestart, $duedate);
 359              }
 360          }
 361  
 362          // Non-existing course ID.
 363          $this->assertFalse(assign_refresh_events(-1));
 364  
 365          // Invalid course ID.
 366          $this->assertFalse(assign_refresh_events('aaa'));
 367      }
 368  
 369  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1