[ Index ]

PHP Cross Reference of moodle-2.8

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          $courses = $DB->get_records('course', array('id' => $this->course->id));
 107  
 108          // Check the overview as the different users.
 109          $this->setUser($this->students[0]);
 110          $overview = array();
 111          assign_print_overview($courses, $overview);
 112          $this->assertEquals(count($overview), 1);
 113  
 114          $this->setUser($this->teachers[0]);
 115          $overview = array();
 116          assign_print_overview($courses, $overview);
 117          $this->assertEquals(count($overview), 1);
 118  
 119          $this->setUser($this->editingteachers[0]);
 120          $overview = array();
 121          assign_print_overview($courses, $overview);
 122          $this->assertEquals(1, count($overview));
 123      }
 124  
 125      public function test_print_recent_activity() {
 126          // Submitting an assignment generates a notification.
 127          $this->preventResetByRollback();
 128          $sink = $this->redirectMessages();
 129  
 130          $this->setUser($this->editingteachers[0]);
 131          $assign = $this->create_instance();
 132          $data = new stdClass();
 133          $data->userid = $this->students[0]->id;
 134          $notices = array();
 135          $this->setUser($this->students[0]);
 136          $assign->submit_for_grading($data, $notices);
 137  
 138          $this->setUser($this->editingteachers[0]);
 139          $this->expectOutputRegex('/submitted:/');
 140          assign_print_recent_activity($this->course, true, time() - 3600);
 141  
 142          $sink->close();
 143      }
 144  
 145      /** Make sure fullname dosn't trigger any warnings when assign_print_recent_activity is triggered. */
 146      public function test_print_recent_activity_fullname() {
 147          // Submitting an assignment generates a notification.
 148          $this->preventResetByRollback();
 149          $sink = $this->redirectMessages();
 150  
 151          $this->setUser($this->editingteachers[0]);
 152          $assign = $this->create_instance();
 153  
 154          $data = new stdClass();
 155          $data->userid = $this->students[0]->id;
 156          $notices = array();
 157          $this->setUser($this->students[0]);
 158          $assign->submit_for_grading($data, $notices);
 159  
 160          $this->setUser($this->editingteachers[0]);
 161          $this->expectOutputRegex('/submitted:/');
 162          set_config('fullnamedisplay', 'firstname, lastnamephonetic');
 163          assign_print_recent_activity($this->course, false, time() - 3600);
 164  
 165          $sink->close();
 166      }
 167  
 168      public function test_assign_get_recent_mod_activity() {
 169          // Submitting an assignment generates a notification.
 170          $this->preventResetByRollback();
 171          $sink = $this->redirectMessages();
 172  
 173          $this->setUser($this->editingteachers[0]);
 174          $assign = $this->create_instance();
 175  
 176          $data = new stdClass();
 177          $data->userid = $this->students[0]->id;
 178          $notices = array();
 179          $this->setUser($this->students[0]);
 180          $assign->submit_for_grading($data, $notices);
 181  
 182          $this->setUser($this->editingteachers[0]);
 183          $activities = array();
 184          $index = 0;
 185  
 186          $activity = new stdClass();
 187          $activity->type    = 'activity';
 188          $activity->cmid    = $assign->get_course_module()->id;
 189          $activities[$index++] = $activity;
 190  
 191          assign_get_recent_mod_activity( $activities,
 192                                          $index,
 193                                          time() - 3600,
 194                                          $this->course->id,
 195                                          $assign->get_course_module()->id);
 196  
 197          $this->assertEquals("assign", $activities[1]->type);
 198          $sink->close();
 199      }
 200  
 201      public function test_assign_user_complete() {
 202          global $PAGE, $DB;
 203  
 204          $this->setUser($this->editingteachers[0]);
 205          $assign = $this->create_instance(array('submissiondrafts' => 1));
 206          $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
 207  
 208          $submission = $assign->get_user_submission($this->students[0]->id, true);
 209          $submission->status = ASSIGN_SUBMISSION_STATUS_DRAFT;
 210          $DB->update_record('assign_submission', $submission);
 211  
 212          $this->expectOutputRegex('/Draft/');
 213          assign_user_complete($this->course, $this->students[0], $assign->get_course_module(), $assign->get_instance());
 214      }
 215  
 216      public function test_assign_user_outline() {
 217          $this->setUser($this->editingteachers[0]);
 218          $assign = $this->create_instance();
 219  
 220          $this->setUser($this->teachers[0]);
 221          $data = $assign->get_user_grade($this->students[0]->id, true);
 222          $data->grade = '50.5';
 223          $assign->update_grade($data);
 224  
 225          $result = assign_user_outline($this->course, $this->students[0], $assign->get_course_module(), $assign->get_instance());
 226  
 227          $this->assertRegExp('/50.5/', $result->info);
 228      }
 229  
 230      public function test_assign_get_completion_state() {
 231          global $DB;
 232          $assign = $this->create_instance(array('submissiondrafts' => 0, 'completionsubmit' => 1));
 233  
 234          $this->setUser($this->students[0]);
 235          $result = assign_get_completion_state($this->course, $assign->get_course_module(), $this->students[0]->id, false);
 236          $this->assertFalse($result);
 237          $submission = $assign->get_user_submission($this->students[0]->id, true);
 238          $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
 239          $DB->update_record('assign_submission', $submission);
 240  
 241          $result = assign_get_completion_state($this->course, $assign->get_course_module(), $this->students[0]->id, false);
 242  
 243          $this->assertTrue($result);
 244      }
 245  
 246  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1