Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   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   * Assign feedback unit tests.
  19   *
  20   * @package     mod_assign
  21   * @category    test
  22   * @copyright   2019 Ilya Tregubov ilyatregubov@catalyst-au.net
  23   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once($CFG->dirroot . '/mod/assign/locallib.php');
  30  require_once($CFG->dirroot . '/mod/assign/tests/generator.php');
  31  
  32  use \assignfeedback_editpdf\document_services;
  33  use \assignfeedback_editpdf\combined_document;
  34  
  35  /**
  36   * Provides the unit tests for feedback.
  37   *
  38   * @package     mod_assign
  39   * @category    test
  40   * @copyright   2019 Ilya Tregubov ilyatregubov@catalyst-au.net
  41   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  class mod_assign_feedback_testcase extends advanced_testcase {
  44  
  45      // Use the generator helper.
  46      use mod_assign_test_generator;
  47  
  48      /**
  49       * Helper to create a stored file object with the given supplied content.
  50       *
  51       * @param   int $contextid context id for assigment
  52       * @param   int $itemid item id from assigment grade
  53       * @param   string $filearea File area
  54       * @param   int $timemodified Time modified
  55       * @param   string $filecontent The content of the mocked file
  56       * @param   string $filename The file name to use in the stored_file
  57       * @param   string $filerecord Any overrides to the filerecord
  58       * @return  stored_file
  59       */
  60      protected function create_stored_file($contextid, $itemid, $filearea, $timemodified,
  61                                            $filecontent = 'content', $filename = 'combined.pdf', $filerecord = []) {
  62          $filerecord = array_merge([
  63              'contextid' => $contextid,
  64              'component' => 'assignfeedback_editpdf',
  65              'filearea'  => $filearea,
  66              'itemid'    => $itemid,
  67              'filepath'  => '/',
  68              'filename'  => $filename,
  69              'timemodified' => $timemodified,
  70          ], $filerecord);
  71  
  72          $fs = get_file_storage();
  73          $file = $fs->create_file_from_string($filerecord, $filecontent);
  74  
  75          return $file;
  76      }
  77  
  78      /**
  79       * Convenience function to create an instance of an assignment.
  80       *
  81       * @param array $params Array of parameters to pass to the generator
  82       * @return assign The assign class.
  83       */
  84      protected function create_instance($params = array()) {
  85          $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
  86          $instance = $generator->create_instance($params);
  87          $cm = get_coursemodule_from_instance('assign', $instance->id);
  88          $context = \context_module::instance($cm->id);
  89          return new \assign($context, $cm, $params['course']);
  90      }
  91  
  92      /**
  93       * Test fetching combined.pdf for state checking.
  94       */
  95      public function test_get_combined_document_for_attempt() {
  96  
  97          $this->resetAfterTest(true);
  98  
  99          $course = $this->getDataGenerator()->create_course();
 100  
 101          $user = $this->getDataGenerator()->create_user();
 102          $this->getDataGenerator()->enrol_user($user->id, $course->id, 'student');
 103  
 104          $teacher = $this->getDataGenerator()->create_user();
 105          $this->getDataGenerator()->enrol_user($teacher->id, $course->id, 'editingteacher');
 106  
 107          $assign = $this->create_instance([
 108              'course' => $course,
 109              'name' => 'Assign 1',
 110              'attemptreopenmethod' => ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL,
 111              'maxattempts' => 3,
 112              'assignsubmission_onlinetext_enabled' => true,
 113              'assignfeedback_comments_enabled' => true
 114          ]);
 115  
 116          $submission = new \stdClass();
 117          $submission->assignment = $assign->get_instance()->id;
 118          $submission->userid = $user->id;
 119          $submission->timecreated = time();
 120          $submission->timemodified = time();
 121          $submission->onlinetext_editor = ['text' => 'Submission text',
 122              'format' => FORMAT_MOODLE];
 123  
 124          $this->setUser($user);
 125          $notices = [];
 126          $assign->save_submission($submission, $notices);
 127  
 128          $this->setUser($teacher);
 129  
 130          $grade = '3.14';
 131          $teachercommenttext = 'This is better. Thanks.';
 132          $data = new \stdClass();
 133          $data->attemptnumber = 1;
 134          $data->grade = $grade;
 135          $data->assignfeedbackcomments_editor = ['text' => $teachercommenttext, 'format' => FORMAT_MOODLE];
 136  
 137          // Give the submission a grade.
 138          $assign->save_grade($user->id, $data);
 139  
 140          $grade = $assign->get_user_grade($user->id, true, -1);
 141  
 142          $contextid = $assign->get_context()->id;
 143          $itemid = $grade->id;
 144  
 145          // Create combined document in combined area.
 146          $this->create_stored_file($contextid, $itemid, 'combined', time());
 147  
 148          $document = document_services::get_combined_document_for_attempt($assign, $user->id, -1);
 149          $status = $document->get_status();
 150  
 151          $this->assertEquals($status, combined_document::STATUS_COMPLETE);
 152  
 153          // Create orphaned combined document in partial area.
 154          $this->create_stored_file($contextid, $itemid, 'partial', time() - 3600);
 155  
 156          $document = document_services::get_combined_document_for_attempt($assign, $user->id, -1);
 157          $status = $document->get_status();
 158  
 159          $this->assertEquals($status, combined_document::STATUS_FAILED);
 160      }
 161  
 162  }