MDL-55609 mod_assign: Remove shared setUp for all tests
[moodle.git] / mod / assign / tests / generator.php
blob96ea18cf1764dfd8a7b27d15eb7c9c91b1cd61c3
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.
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/>.
17 /**
18 * Base class for unit tests for mod_assign.
20 * @package mod_assign
21 * @category phpunit
22 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->dirroot . '/mod/assign/locallib.php');
30 require_once($CFG->dirroot . '/mod/assign/upgradelib.php');
31 require_once(__DIR__ . '/fixtures/testable_assign.php');
33 /**
34 * Generator helper trait.
36 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 trait mod_assign_test_generator {
41 /**
42 * Convenience function to create a testable instance of an assignment.
44 * @param array $params Array of parameters to pass to the generator
45 * @return testable_assign Testable wrapper around the assign class.
47 protected function create_instance($course, $params = [], $options = []) {
48 $params['course'] = $course->id;
50 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
51 $instance = $generator->create_instance($params, $options);
52 $cm = get_coursemodule_from_instance('assign', $instance->id);
53 $context = context_module::instance($cm->id);
55 return new mod_assign_testable_assign($context, $cm, $course);
58 /**
59 * Add a user submission to the assignment.
61 * @param \stdClass $student The user to submit for
62 * @param \assign $assign The assignment to submit to
63 * @param string $onlinetext The text tobe submitted
64 * @param bool $changeuser Whether to switch user to the user being submitted as.
66 protected function add_submission($student, $assign, $onlinetext = null, $changeuser = true) {
67 // Add a submission.
68 if ($changeuser) {
69 $this->setUser($student);
72 if ($onlinetext === null) {
73 $onlinetext = 'Submission text';
76 $data = (object) [
77 'userid' => $student->id,
79 'onlinetext_editor' => [
80 'itemid' => file_get_unused_draft_itemid(),
81 'text' => $onlinetext,
82 'format' => FORMAT_HTML,
86 $assign->save_submission($data, $notices);
89 /**
90 * Submit the assignemnt for grading.
92 * @param \stdClass $student The user to submit for
93 * @param \assign $assign The assignment to submit to
94 * @param array $data Additional data to set
95 * @param bool $changeuser Whether to switch user to the user being submitted as.
97 public function submit_for_grading($student, $assign, $data = [], $changeuser = true) {
98 if ($changeuser) {
99 $this->setUser($student);
102 $data = (object) array_merge($data, [
103 'userid' => $student->id,
106 $sink = $this->redirectMessages();
107 $assign->submit_for_grading($data, []);
108 $sink->close();
110 return $data;
114 * Mark the submission.
116 * @param \stdClass $teacher The user to mark as
117 * @param \assign $assign The assignment to mark
118 * @param \stdClass $student The user to grade
119 * @param array $data Additional data to set
120 * @param bool $changeuser Whether to switch user to the user being submitted as.
122 protected function mark_submission($teacher, $assign, $student, $grade = 50.0, $data = [], $attempt = 0) {
123 // Mark the submission.
124 $this->setUser($teacher);
125 $data = (object) array_merge($data, [
126 'grade' => $grade,
129 $assign->testable_apply_grade_to_user($data, $student->id, $attempt);