Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   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  namespace mod_assign;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  global $CFG;
  22  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  23  require_once($CFG->dirroot . '/mod/assign/externallib.php');
  24  require_once (__DIR__ . '/fixtures/testable_assign.php');
  25  
  26  /**
  27   * Base class for unit tests for external functions in mod_assign.
  28   *
  29   * @package    mod_assign
  30   * @author     Andrew Madden <andrewmadden@catalyst-au.net>
  31   * @copyright  2021 Catalyst IT
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  abstract class externallib_advanced_testcase extends \externallib_advanced_testcase {
  35  
  36      /**
  37       * Create a submission for testing the get_submission_status function.
  38       * @param  bool $submitforgrading whether to submit for grading the submission
  39       * @param  array $params Optional params to use for creating assignment instance.
  40       * @return array an array containing all the required data for testing
  41       */
  42      protected function create_submission_for_testing_status(bool $submitforgrading = false, array $params = []): array {
  43          global $DB;
  44  
  45          // Create a course and assignment and users.
  46          $course = self::getDataGenerator()->create_course(['groupmode' => SEPARATEGROUPS, 'groupmodeforce' => 1]);
  47  
  48          $group1 = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
  49          $group2 = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
  50  
  51          $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
  52          $params = array_merge([
  53              'course' => $course->id,
  54              'assignsubmission_file_maxfiles' => 1,
  55              'assignsubmission_file_maxsizebytes' => 1024 * 1024,
  56              'assignsubmission_onlinetext_enabled' => 1,
  57              'assignsubmission_file_enabled' => 1,
  58              'submissiondrafts' => 1,
  59              'assignfeedback_file_enabled' => 1,
  60              'assignfeedback_comments_enabled' => 1,
  61              'attemptreopenmethod' => ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL,
  62              'sendnotifications' => 0
  63          ], $params);
  64  
  65          set_config('submissionreceipts', 0, 'assign');
  66  
  67          $instance = $generator->create_instance($params);
  68          $cm = get_coursemodule_from_instance('assign', $instance->id);
  69          $context = \context_module::instance($cm->id);
  70  
  71          $assign = new \mod_assign_testable_assign($context, $cm, $course);
  72  
  73          $student1 = self::getDataGenerator()->create_user();
  74          $student2 = self::getDataGenerator()->create_user();
  75          $studentrole = $DB->get_record('role', ['shortname' => 'student']);
  76          $this->getDataGenerator()->enrol_user($student1->id, $course->id, $studentrole->id);
  77          $this->getDataGenerator()->enrol_user($student2->id, $course->id, $studentrole->id);
  78          $teacher = self::getDataGenerator()->create_user();
  79          $teacherrole = $DB->get_record('role', ['shortname' => 'teacher']);
  80          $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
  81  
  82          $this->getDataGenerator()->create_group_member(['groupid' => $group1->id, 'userid' => $student1->id]);
  83          $this->getDataGenerator()->create_group_member(['groupid' => $group1->id, 'userid' => $teacher->id]);
  84          $this->getDataGenerator()->create_group_member(['groupid' => $group2->id, 'userid' => $student2->id]);
  85          $this->getDataGenerator()->create_group_member(['groupid' => $group2->id, 'userid' => $teacher->id]);
  86  
  87          $this->setUser($student1);
  88  
  89          // Create a student1 with an online text submission.
  90          // Simulate a submission.
  91          $assign->get_user_submission($student1->id, true);
  92  
  93          $data = new \stdClass();
  94          $data->onlinetext_editor = [
  95              'itemid' => file_get_unused_draft_itemid(),
  96              'text' => 'Submission text with a <a href="@@PLUGINFILE@@/intro.txt">link</a>',
  97              'format' => FORMAT_MOODLE,
  98          ];
  99  
 100          $draftidfile = file_get_unused_draft_itemid();
 101          $usercontext = \context_user::instance($student1->id);
 102          $filerecord = [
 103              'contextid' => $usercontext->id,
 104              'component' => 'user',
 105              'filearea'  => 'draft',
 106              'itemid'    => $draftidfile,
 107              'filepath'  => '/',
 108              'filename'  => 't.txt',
 109          ];
 110          $fs = get_file_storage();
 111          $fs->create_file_from_string($filerecord, 'text contents');
 112  
 113          $data->files_filemanager = $draftidfile;
 114  
 115          $notices = [];
 116          $assign->save_submission($data, $notices);
 117  
 118          if ($submitforgrading) {
 119              // Now, submit the draft for grading.
 120              $notices = [];
 121  
 122              $data = new \stdClass;
 123              $data->userid = $student1->id;
 124              $assign->submit_for_grading($data, $notices);
 125          }
 126  
 127          return [$assign, $instance, $student1, $student2, $teacher, $group1, $group2];
 128      }
 129  
 130      /**
 131       * Create a course, assignment module instance, student and teacher and enrol them in
 132       * the course.
 133       *
 134       * @param array $params parameters to be provided to the assignment module creation
 135       * @return array containing the course, assignment module, student and teacher
 136       */
 137      protected function create_assign_with_student_and_teacher(array $params = []): array {
 138          global $DB;
 139  
 140          $course = $this->getDataGenerator()->create_course();
 141          $params = array_merge([
 142              'course' => $course->id,
 143              'name' => 'assignment',
 144              'intro' => 'assignment intro text',
 145          ], $params);
 146  
 147          // Create a course and assignment and users.
 148          $assign = $this->getDataGenerator()->create_module('assign', $params);
 149  
 150          $cm = get_coursemodule_from_instance('assign', $assign->id);
 151          $context = \context_module::instance($cm->id);
 152  
 153          $student = $this->getDataGenerator()->create_user();
 154          $studentrole = $DB->get_record('role', ['shortname' => 'student']);
 155          $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id);
 156          $teacher = $this->getDataGenerator()->create_user();
 157          $teacherrole = $DB->get_record('role', ['shortname' => 'teacher']);
 158          $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
 159  
 160          assign_capability('mod/assign:view', CAP_ALLOW, $teacherrole->id, $context->id, true);
 161          assign_capability('mod/assign:viewgrades', CAP_ALLOW, $teacherrole->id, $context->id, true);
 162          assign_capability('mod/assign:grade', CAP_ALLOW, $teacherrole->id, $context->id, true);
 163          accesslib_clear_all_caches_for_unit_testing();
 164  
 165          return [
 166              'course' => $course,
 167              'assign' => $assign,
 168              'student' => $student,
 169              'teacher' => $teacher,
 170          ];
 171      }
 172  }