|
| 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 events found in /grade/letter and /grade/scale. |
| 19 | + * |
| 20 | + * @package core_grades |
| 21 | + * @category test |
| 22 | + * @copyright 2017 Stephen Bourget |
| 23 | + * @license http://www.gnu.org/copyleft/gpl.html GNU Public License |
| 24 | + */ |
| 25 | + |
| 26 | +defined('MOODLE_INTERNAL') || die(); |
| 27 | + |
| 28 | +global $CFG; |
| 29 | +require_once($CFG->dirroot . '/grade/lib.php'); |
| 30 | + |
| 31 | +/** |
| 32 | + * Unit tests for grade events. |
| 33 | + * |
| 34 | + * @package core_grades |
| 35 | + * @category test |
| 36 | + * @copyright 2017 Stephen Bourget |
| 37 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 38 | + */ |
| 39 | +class core_grade_events_test extends advanced_testcase { |
| 40 | + |
| 41 | + /** @var stdClass the course used for testing */ |
| 42 | + private $course; |
| 43 | + |
| 44 | + /** |
| 45 | + * Test set up. |
| 46 | + * |
| 47 | + * This is executed before running any test in this file. |
| 48 | + */ |
| 49 | + public function setUp() { |
| 50 | + $this->resetAfterTest(); |
| 51 | + |
| 52 | + $this->setAdminUser(); |
| 53 | + $this->course = $this->getDataGenerator()->create_course(); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Test the grade letter created event. |
| 58 | + * |
| 59 | + * There is no external API for triggering this event, so the unit test will simply |
| 60 | + * create and trigger the event and ensure the data is returned as expected. |
| 61 | + */ |
| 62 | + public function test_grade_letter_created() { |
| 63 | + // Create a grade letter created event. |
| 64 | + $event = \core\event\grade_letter_created::create(array( |
| 65 | + 'objectid' => 10, |
| 66 | + 'context' => context_course::instance($this->course->id) |
| 67 | + )); |
| 68 | + |
| 69 | + // Trigger and capture the event. |
| 70 | + $sink = $this->redirectEvents(); |
| 71 | + $event->trigger(); |
| 72 | + $events = $sink->get_events(); |
| 73 | + $event = reset($events); |
| 74 | + |
| 75 | + // Check that the event data is valid. |
| 76 | + $this->assertInstanceOf('\core\event\grade_letter_created', $event); |
| 77 | + $this->assertEquals(context_course::instance($this->course->id), $event->get_context()); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Test the grade letter deleted event. |
| 82 | + * |
| 83 | + * There is no external API for triggering this event, so the unit test will simply |
| 84 | + * create and trigger the event and ensure the data is returned as expected. |
| 85 | + */ |
| 86 | + public function test_grade_letter_deleted() { |
| 87 | + // Create a grade letter deleted event. |
| 88 | + $event = \core\event\grade_letter_deleted::create(array( |
| 89 | + 'objectid' => 10, |
| 90 | + 'context' => context_course::instance($this->course->id) |
| 91 | + )); |
| 92 | + |
| 93 | + // Trigger and capture the event. |
| 94 | + $sink = $this->redirectEvents(); |
| 95 | + $event->trigger(); |
| 96 | + $events = $sink->get_events(); |
| 97 | + $event = reset($events); |
| 98 | + |
| 99 | + // Check that the event data is valid. |
| 100 | + $this->assertInstanceOf('\core\event\grade_letter_deleted', $event); |
| 101 | + $this->assertEquals(context_course::instance($this->course->id), $event->get_context()); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Test the grade letter updated event. |
| 106 | + * |
| 107 | + * There is no external API for triggering this event, so the unit test will simply |
| 108 | + * create and trigger the event and ensure the data is returned as expected. |
| 109 | + */ |
| 110 | + public function test_grade_letter_updated() { |
| 111 | + // Create a grade letter updated event. |
| 112 | + $event = \core\event\grade_letter_updated::create(array( |
| 113 | + 'objectid' => 10, |
| 114 | + 'context' => context_course::instance($this->course->id) |
| 115 | + )); |
| 116 | + |
| 117 | + // Trigger and capture the event. |
| 118 | + $sink = $this->redirectEvents(); |
| 119 | + $event->trigger(); |
| 120 | + $events = $sink->get_events(); |
| 121 | + $event = reset($events); |
| 122 | + |
| 123 | + // Check that the event data is valid. |
| 124 | + $this->assertInstanceOf('\core\event\grade_letter_updated', $event); |
| 125 | + $this->assertEquals(context_course::instance($this->course->id), $event->get_context()); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Test the scale created event. |
| 130 | + */ |
| 131 | + public function test_scale_created() { |
| 132 | + $gradescale = new grade_scale(); |
| 133 | + $gradescale->name = 'unittestscale3'; |
| 134 | + $gradescale->courseid = $this->course->id; |
| 135 | + $gradescale->userid = 317; |
| 136 | + $gradescale->scale = 'Distinction, Very Good, Good, Pass, Fail'; |
| 137 | + $gradescale->description = 'This scale is used to mark standard assignments.'; |
| 138 | + |
| 139 | + $url = new \moodle_url('/grade/edit/scale/index.php', array('id' => $this->course->id)); |
| 140 | + |
| 141 | + // Trigger and capture the event. |
| 142 | + $sink = $this->redirectEvents(); |
| 143 | + $id = $gradescale->insert(); |
| 144 | + $events = $sink->get_events(); |
| 145 | + $event = reset($events); |
| 146 | + |
| 147 | + // Check that the event data is valid. |
| 148 | + $this->assertInstanceOf('\core\event\scale_created', $event); |
| 149 | + $this->assertEquals($id, $event->objectid); |
| 150 | + $this->assertEquals($url, $event->get_url()); |
| 151 | + $this->assertEquals(context_course::instance($this->course->id), $event->get_context()); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Test the scale deleted event. |
| 156 | + */ |
| 157 | + public function test_scale_deleted() { |
| 158 | + $gradescale = new grade_scale(); |
| 159 | + $gradescale->name = 'unittestscale3'; |
| 160 | + $gradescale->courseid = $this->course->id; |
| 161 | + $gradescale->userid = 317; |
| 162 | + $gradescale->scale = 'Distinction, Very Good, Good, Pass, Fail'; |
| 163 | + $gradescale->description = 'This scale is used to mark standard assignments.'; |
| 164 | + $gradescale->insert(); |
| 165 | + |
| 166 | + // Trigger and capture the event. |
| 167 | + $sink = $this->redirectEvents(); |
| 168 | + $gradescale->delete(); |
| 169 | + $events = $sink->get_events(); |
| 170 | + $event = reset($events); |
| 171 | + |
| 172 | + // Check that the event data is valid. |
| 173 | + $this->assertInstanceOf('\core\event\scale_deleted', $event); |
| 174 | + $this->assertEquals(context_course::instance($this->course->id), $event->get_context()); |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Test the scale updated event. |
| 179 | + */ |
| 180 | + public function test_scale_updated() { |
| 181 | + $gradescale = new grade_scale(); |
| 182 | + $gradescale->name = 'unittestscale3'; |
| 183 | + $gradescale->courseid = $this->course->id; |
| 184 | + $gradescale->userid = 317; |
| 185 | + $gradescale->scale = 'Distinction, Very Good, Good, Pass, Fail'; |
| 186 | + $gradescale->description = 'This scale is used to mark standard assignments.'; |
| 187 | + $id = $gradescale->insert(); |
| 188 | + |
| 189 | + $gradescale->name = 'Updated info for this unittest grade_scale'; |
| 190 | + $url = new \moodle_url('/grade/edit/scale/index.php', array('id' => $this->course->id)); |
| 191 | + |
| 192 | + // Trigger and capture the event. |
| 193 | + $sink = $this->redirectEvents(); |
| 194 | + $gradescale->update(); |
| 195 | + $events = $sink->get_events(); |
| 196 | + $event = reset($events); |
| 197 | + |
| 198 | + // Check that the event data is valid. |
| 199 | + $this->assertInstanceOf('\core\event\scale_updated', $event); |
| 200 | + $this->assertEquals($id, $event->objectid); |
| 201 | + $this->assertEquals($url, $event->get_url()); |
| 202 | + $this->assertEquals(context_course::instance($this->course->id), $event->get_context()); |
| 203 | + } |
| 204 | +} |
0 commit comments