From 2f3b70985956a987232178123a52850c153f9484 Mon Sep 17 00:00:00 2001 From: Stephen Bourget Date: Wed, 7 Feb 2018 12:34:24 -0500 Subject: [PATCH 1/4] MDL-45837 Grades: Add events for scales --- lang/en/grades.php | 3 + lib/classes/event/scale_created.php | 93 +++++++++++++++++++++++++++++ lib/classes/event/scale_deleted.php | 81 +++++++++++++++++++++++++ lib/classes/event/scale_updated.php | 93 +++++++++++++++++++++++++++++ lib/grade/grade_scale.php | 60 ++++++++++++++++++- 5 files changed, 327 insertions(+), 3 deletions(-) create mode 100644 lib/classes/event/scale_created.php create mode 100644 lib/classes/event/scale_deleted.php create mode 100644 lib/classes/event/scale_updated.php diff --git a/lang/en/grades.php b/lang/en/grades.php index b166512456c..a8df6cd77df 100644 --- a/lang/en/grades.php +++ b/lang/en/grades.php @@ -195,6 +195,9 @@ $string['errorupdatinggradecategoryaggregation'] = 'Error updating the aggregati $string['errorupdatinggradeitemaggregationcoef'] = 'Error updating the aggregation coefficient (weight or extra credit) of grade item ID {$a->id}'; $string['eventgradedeleted'] = 'Grade deleted'; $string['eventgradeviewed'] = 'Grades were viewed in the gradebook'; +$string['eventscalecreated'] = 'Scale created'; +$string['eventscaledeleted'] = 'Scale deleted'; +$string['eventscaleupdated'] = 'Scale updated'; $string['eventusergraded'] = 'User graded'; $string['excluded'] = 'Excluded'; $string['excluded_help'] = 'If ticked, the grade will not be included in any aggregation.'; diff --git a/lib/classes/event/scale_created.php b/lib/classes/event/scale_created.php new file mode 100644 index 00000000000..9156184b95e --- /dev/null +++ b/lib/classes/event/scale_created.php @@ -0,0 +1,93 @@ +. + +/** + * Scale created event. + * + * @package core + * @copyright 2017 Stephen Bourget + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core\event; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Scale created event class. + * + * @package core + * @since Moodle 3.5 + * @copyright 2017 Stephen Bourget + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class scale_created extends base { + + /** + * Init method. + * + * @return void + */ + protected function init() { + $this->data['objecttable'] = 'scale'; + $this->data['crud'] = 'c'; + $this->data['edulevel'] = self::LEVEL_OTHER; + } + + /** + * Return localised event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventscalecreated', 'core_grades'); + } + + /** + * Returns description of what happened. + * + * @return string + */ + public function get_description() { + if ($this->courseid) { + return "The user with id '$this->userid' created the custom scale with id '$this->objectid'". + " from the course in the id '".$this->courseid."'."; + } else { + return "The user with id '$this->userid' created the standard scale with id '$this->objectid'."; + } + } + + /** + * Returns relevant URL. + * @return \moodle_url + */ + public function get_url() { + $url = new \moodle_url('/grade/edit/scale/index.php'); + if ($this->courseid) { + $url->param('id', $this->courseid); + } + return $url; + } + + /** + * Used for mapping events on restore + * @return array + */ + public static function get_objectid_mapping() { + return array('db' => 'scale', 'restore' => 'scale'); + } + +} diff --git a/lib/classes/event/scale_deleted.php b/lib/classes/event/scale_deleted.php new file mode 100644 index 00000000000..d14acf75215 --- /dev/null +++ b/lib/classes/event/scale_deleted.php @@ -0,0 +1,81 @@ +. + +/** + * Scale deleted event. + * + * @package core + * @copyright 2017 Stephen Bourget + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core\event; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Scale deleted event class. + * + * @package core + * @since Moodle 3.5 + * @copyright 2017 Stephen Bourget + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class scale_deleted extends base { + + /** + * Init method. + * + * @return void + */ + protected function init() { + $this->data['objecttable'] = 'scale'; + $this->data['crud'] = 'd'; + $this->data['edulevel'] = self::LEVEL_OTHER; + } + + /** + * Return localised event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventscaledeleted', 'core_grades'); + } + + /** + * Returns description of what happened. + * + * @return string + */ + public function get_description() { + if ($this->courseid) { + return "The user with id '$this->userid' deleted the custom scale with id '$this->objectid'". + " from the course with the id '".$this->courseid."'."; + } else { + return "The user with id '$this->userid' deleted the standard scale with id '$this->objectid'."; + } + } + + /** + * Used for mapping events on restore + * @return array + */ + public static function get_objectid_mapping() { + return array('db' => 'scale', 'restore' => 'scale'); + } + +} diff --git a/lib/classes/event/scale_updated.php b/lib/classes/event/scale_updated.php new file mode 100644 index 00000000000..e363ea2d8a4 --- /dev/null +++ b/lib/classes/event/scale_updated.php @@ -0,0 +1,93 @@ +. + +/** + * Scale updated event. + * + * @package core + * @copyright 2017 Stephen Bourget + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core\event; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Scale added event class. + * + * @package core + * @since Moodle 3.5 + * @copyright 2017 Stephen Bourget + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class scale_updated extends base { + + /** + * Init method. + * + * @return void + */ + protected function init() { + $this->data['objecttable'] = 'scale'; + $this->data['crud'] = 'u'; + $this->data['edulevel'] = self::LEVEL_OTHER; + } + + /** + * Return localised event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventscaleupdated', 'core_grades'); + } + + /** + * Returns description of what happened. + * + * @return string + */ + public function get_description() { + if ($this->courseid) { + return "The user with id '$this->userid' updated the custom scale with id '$this->objectid'". + " from the course in the id '".$this->courseid."'."; + } else { + return "The user with id '$this->userid' updated the standard scale with id '$this->objectid'."; + } + } + + /** + * Returns relevant URL. + * @return \moodle_url + */ + public function get_url() { + $url = new \moodle_url('/grade/edit/scale/index.php'); + if ($this->courseid) { + $url->param('id', $this->courseid); + } + return $url; + } + + /** + * Used for mapping events on restore + * @return array + */ + public static function get_objectid_mapping() { + return array('db' => 'scale', 'restore' => 'scale'); + } + +} diff --git a/lib/grade/grade_scale.php b/lib/grade/grade_scale.php index 450fa67e0e4..96850088082 100644 --- a/lib/grade/grade_scale.php +++ b/lib/grade/grade_scale.php @@ -119,7 +119,26 @@ class grade_scale extends grade_object { public function insert($source=null) { $this->timecreated = time(); $this->timemodified = time(); - return parent::insert($source); + + $result = parent::insert($source); + if ($result) { + // Trigger the scale created event. + if (!empty($this->standard)) { + $eventcontext = context_system::instance(); + } else { + if ((!empty($this->courseid)) && ($this->courseid != SITEID)) { + $eventcontext = context_course::instance($this->courseid); + } else { + $eventcontext = context_system::instance(); + } + } + $event = \core\event\scale_created::create(array( + 'objectid' => $result, + 'context' => $eventcontext + )); + $event->trigger(); + } + return $result; } /** @@ -130,17 +149,52 @@ class grade_scale extends grade_object { */ public function update($source=null) { $this->timemodified = time(); - return parent::update($source); + + $result = parent::update($source); + if ($result) { + // Trigger the scale updated event. + if (!empty($this->standard)) { + $eventcontext = context_system::instance(); + } else { + if ((!empty($this->courseid)) && ($this->courseid != SITEID)) { + $eventcontext = context_course::instance($this->courseid); + } else { + $eventcontext = context_system::instance(); + } + } + $event = \core\event\scale_updated::create(array( + 'objectid' => $this->id, + 'context' => $eventcontext + )); + $event->trigger(); + } + return $result; } /** - * Deletes this outcome from the database. + * Deletes this scale from the database. * * @param string $source from where was the object deleted (mod/forum, manual, etc.) * @return bool success */ public function delete($source=null) { global $DB; + + // Trigger the scale deleted event. + if (!empty($this->standard)) { + $eventcontext = context_system::instance(); + } else { + if ((!empty($this->courseid)) && ($this->courseid != SITEID)) { + $eventcontext = context_course::instance($this->courseid); + } else { + $eventcontext = context_system::instance(); + } + } + $event = \core\event\scale_deleted::create(array( + 'objectid' => $this->id, + 'context' => $eventcontext + )); + $event->trigger(); if (parent::delete($source)) { $context = context_system::instance(); $fs = get_file_storage(); From 31647f0cc99b426405425fb968f045102c1603e3 Mon Sep 17 00:00:00 2001 From: Stephen Bourget Date: Wed, 7 Feb 2018 12:38:07 -0500 Subject: [PATCH 2/4] MDL-45837 Grades: Add events for grade letters --- grade/edit/letter/index.php | 26 +++++- lang/en/grades.php | 3 + lib/classes/event/grade_letter_created.php | 93 ++++++++++++++++++++++ lib/classes/event/grade_letter_deleted.php | 81 +++++++++++++++++++ lib/classes/event/grade_letter_updated.php | 93 ++++++++++++++++++++++ 5 files changed, 295 insertions(+), 1 deletion(-) create mode 100644 lib/classes/event/grade_letter_created.php create mode 100644 lib/classes/event/grade_letter_deleted.php create mode 100644 lib/classes/event/grade_letter_updated.php diff --git a/grade/edit/letter/index.php b/grade/edit/letter/index.php index 025584bc825..3113407abcb 100644 --- a/grade/edit/letter/index.php +++ b/grade/edit/letter/index.php @@ -184,21 +184,45 @@ if (!$edit) { // The letter has been assigned to another boundary, we update it. $record->id = $pool[$boundary]->id; $DB->update_record('grade_letters', $record); + // Trigger the letter grade updated event. + $event = \core\event\grade_letter_updated::create(array( + 'objectid' => $record->id, + 'context' => $context, + )); + $event->trigger(); } unset($pool[$boundary]); // Remove the letter from the pool. } else if ($candidate = array_pop($pool)) { // The boundary is new, we update a random record from the pool. $record->id = $candidate->id; $DB->update_record('grade_letters', $record); + // Trigger the letter grade updated event. + $event = \core\event\grade_letter_updated::create(array( + 'objectid' => $record->id, + 'context' => $context, + )); + $event->trigger(); } else { // No records were found, this must be a new letter. - $DB->insert_record('grade_letters', $record); + $newid = $DB->insert_record('grade_letters', $record); + // Trigger the letter grade added event. + $event = \core\event\grade_letter_created::create(array( + 'objectid' => $newid, + 'context' => $context, + )); + $event->trigger(); } } // Delete the unused records. foreach($pool as $leftover) { $DB->delete_records('grade_letters', array('id' => $leftover->id)); + // Trigger the letter grade deleted event. + $event = \core\event\grade_letter_deleted::create(array( + 'objectid' => $leftover->id, + 'context' => $context, + )); + $event->trigger(); } redirect($returnurl); diff --git a/lang/en/grades.php b/lang/en/grades.php index a8df6cd77df..7037a00dc81 100644 --- a/lang/en/grades.php +++ b/lang/en/grades.php @@ -194,6 +194,9 @@ $string['errorupdatinggradecategoryaggregateoutcomes'] = 'Error updating the "In $string['errorupdatinggradecategoryaggregation'] = 'Error updating the aggregation type of grade category ID {$a->id}'; $string['errorupdatinggradeitemaggregationcoef'] = 'Error updating the aggregation coefficient (weight or extra credit) of grade item ID {$a->id}'; $string['eventgradedeleted'] = 'Grade deleted'; +$string['eventgradelettercreated'] = 'Grade letter created'; +$string['eventgradeletterdeleted'] = 'Grade letter deleted'; +$string['eventgradeletterupdated'] = 'Grade letter updated'; $string['eventgradeviewed'] = 'Grades were viewed in the gradebook'; $string['eventscalecreated'] = 'Scale created'; $string['eventscaledeleted'] = 'Scale deleted'; diff --git a/lib/classes/event/grade_letter_created.php b/lib/classes/event/grade_letter_created.php new file mode 100644 index 00000000000..ff19f65a3e8 --- /dev/null +++ b/lib/classes/event/grade_letter_created.php @@ -0,0 +1,93 @@ +. + +/** + * Grade letter added created. + * + * @package core + * @copyright 2017 Stephen Bourget + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core\event; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Grade letter added event class. + * + * @package core + * @since Moodle 3.5 + * @copyright 2017 Stephen Bourget + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class grade_letter_created extends base { + + /** + * Init method. + * + * @return void + */ + protected function init() { + $this->data['objecttable'] = 'grade_letters'; + $this->data['crud'] = 'c'; + $this->data['edulevel'] = self::LEVEL_OTHER; + } + + /** + * Return localised event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventgradelettercreated', 'core_grades'); + } + + /** + * Returns description of what happened. + * + * @return string + */ + public function get_description() { + if ($this->courseid) { + return "The user with id '$this->userid' created the letter grade with id '$this->objectid'". + " in the course in the id '".$this->courseid."'."; + } else { + return "The user with id '$this->userid' created the letter grade with id '$this->objectid'."; + } + } + + /** + * Returns relevant URL. + * @return \moodle_url + */ + public function get_url() { + $url = new \moodle_url('/grade/edit/letter/index.php'); + if ($this->courseid) { + $url->param('id', $this->contextid); + } + return $url; + } + + /** + * Used for mapping events on restore + * @return array + */ + public static function get_objectid_mapping() { + return array('db' => 'grade_letters', 'restore' => 'grade_letters'); + } + +} diff --git a/lib/classes/event/grade_letter_deleted.php b/lib/classes/event/grade_letter_deleted.php new file mode 100644 index 00000000000..f31b39e656b --- /dev/null +++ b/lib/classes/event/grade_letter_deleted.php @@ -0,0 +1,81 @@ +. + +/** + * Grade letter deleted event. + * + * @package core + * @copyright 2017 Stephen Bourget + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core\event; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Grade letter deleted event class. + * + * @package core + * @since Moodle 3.5 + * @copyright 2017 Stephen Bourget + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class grade_letter_deleted extends base { + + /** + * Init method. + * + * @return void + */ + protected function init() { + $this->data['objecttable'] = 'grade_letters'; + $this->data['crud'] = 'd'; + $this->data['edulevel'] = self::LEVEL_OTHER; + } + + /** + * Return localised event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventgradeletterdeleted', 'core_grades'); + } + + /** + * Returns description of what happened. + * + * @return string + */ + public function get_description() { + if ($this->courseid) { + return "The user with id '$this->userid' deleted the letter grade with id '$this->objectid'". + " from the course in the id '".$this->courseid."'."; + } else { + return "The user with id '$this->userid' deleted the letter grade with id '$this->objectid'."; + } + } + + /** + * Used for mapping events on restore + * @return array + */ + public static function get_objectid_mapping() { + return array('db' => 'grade_letters', 'restore' => 'grade_letters'); + } + +} diff --git a/lib/classes/event/grade_letter_updated.php b/lib/classes/event/grade_letter_updated.php new file mode 100644 index 00000000000..a7a856855e0 --- /dev/null +++ b/lib/classes/event/grade_letter_updated.php @@ -0,0 +1,93 @@ +. + +/** + * Grade letter updated event. + * + * @package core + * @copyright 2017 Stephen Bourget + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core\event; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Grade letter added event class. + * + * @package core + * @since Moodle 3.5 + * @copyright 2017 Stephen Bourget + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class grade_letter_updated extends base { + + /** + * Init method. + * + * @return void + */ + protected function init() { + $this->data['objecttable'] = 'grade_letters'; + $this->data['crud'] = 'u'; + $this->data['edulevel'] = self::LEVEL_OTHER; + } + + /** + * Return localised event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventgradeletterupdated', 'core_grades'); + } + + /** + * Returns description of what happened. + * + * @return string + */ + public function get_description() { + if ($this->courseid) { + return "The user with id '$this->userid' updated the letter grade with id '$this->objectid'". + " in the course in the id '".$this->courseid."'."; + } else { + return "The user with id '$this->userid' updated the letter grade with id '$this->objectid'."; + } + } + + /** + * Returns relevant URL. + * @return \moodle_url + */ + public function get_url() { + $url = new \moodle_url('/grade/edit/letter/index.php'); + if ($this->courseid) { + $url->param('id', $this->contextid); + } + return $url; + } + + /** + * Used for mapping events on restore + * @return array + */ + public static function get_objectid_mapping() { + return array('db' => 'grade_letters', 'restore' => 'grade_letters'); + } + +} From b302d434ae763cfcff3c250cdfebe7e0929d2199 Mon Sep 17 00:00:00 2001 From: Stephen Bourget Date: Wed, 7 Feb 2018 13:00:37 -0500 Subject: [PATCH 3/4] MDL-45837 Grades: Add unit & behat tests for events --- .../tests/behat/grade_letter_logging.feature | 37 +++ .../tests/behat/grade_scales_logging.feature | 27 +++ grade/tests/events_test.php | 222 ++++++++++++++++++ 3 files changed, 286 insertions(+) create mode 100644 grade/tests/behat/grade_letter_logging.feature create mode 100644 grade/tests/behat/grade_scales_logging.feature create mode 100644 grade/tests/events_test.php diff --git a/grade/tests/behat/grade_letter_logging.feature b/grade/tests/behat/grade_letter_logging.feature new file mode 100644 index 00000000000..db472f05de5 --- /dev/null +++ b/grade/tests/behat/grade_letter_logging.feature @@ -0,0 +1,37 @@ +@core @core_grades +Feature: We can view the logs for any changes to grade letters. + In order to view changes the letter boundary of a course + As an administrator + I need to add make changes and then view the logs. + + Scenario: I override the letter boundaries and check the logs. + Given the following "courses" exist: + | fullname | shortname | category | + | Course 1 | C1 | 0 | + And I log in as "admin" + And I am on "Course 1" course homepage + And I navigate to "Setup > Course grade settings" in the course gradebook + And I set the following fields to these values: + | Grade display type | Letter | + And I press "Save changes" + And I navigate to "Letters" in the course gradebook + And I follow "Edit grade letters" + And I set the following fields to these values: + | id_override | 1 | + | id_gradeboundary10 | 57 | + And I press "Save changes" + And I follow "Edit grade letters" + And I set the following fields to these values: + | id_override | 1 | + | id_gradeboundary10 | 50 | + And I press "Save changes" + And I follow "Edit grade letters" + And I set the following fields to these values: + | id_override | 1 | + | id_gradeletter11 | | + | id_gradeboundary11 | | + And I press "Save changes" + When I navigate to "Live logs" node in "Site administration > Reports" + Then I should see "Grade letter created" + And I should see "Grade letter updated" + And I should see "Grade letter deleted" diff --git a/grade/tests/behat/grade_scales_logging.feature b/grade/tests/behat/grade_scales_logging.feature new file mode 100644 index 00000000000..0af66d2c213 --- /dev/null +++ b/grade/tests/behat/grade_scales_logging.feature @@ -0,0 +1,27 @@ +@core @core_grades +Feature: We can view the logs for any changes to grade scales. + In order to view changes grade scales + As an administrator + I need to add make changes and then view the logs. + + Scenario: I edit scales and then view the logs. + Given I log in as "admin" + And I navigate to "Scales" node in "Site administration > Grades" + # Add a scale + And I press "Add a new scale" + And I set the following fields to these values: + | Name | Letterscale | + | Scale | F,D,C,B,A | + And I press "Save changes" + # Delete first scale + And I follow "Delete" + And I press "Continue" + # Edit first scale + And I follow "Edit" + And I set the following fields to these values: + | id_scale | ONE,TWO,THREE | + And I press "Save changes" + When I navigate to "Live logs" node in "Site administration > Reports" + Then I should see "Scale created" + And I should see "Scale updated" + And I should see "Scale deleted" diff --git a/grade/tests/events_test.php b/grade/tests/events_test.php new file mode 100644 index 00000000000..33204f93471 --- /dev/null +++ b/grade/tests/events_test.php @@ -0,0 +1,222 @@ +. + +/** + * Unit tests for events found in /grade/letter and /grade/scale. + * + * @package core_grades + * @category test + * @copyright 2017 Stephen Bourget + * @license http://www.gnu.org/copyleft/gpl.html GNU Public License + */ + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once($CFG->dirroot . '/grade/lib.php'); + +/** + * Unit tests for grade/lib.php. + * + * @package core_grades + * @category test + * @copyright 2017 Stephen Bourget + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class core_grade_events_test extends advanced_testcase { + + /** @var stdClass the course used for testing */ + private $course; + + /** + * Test set up. + * + * This is executed before running any test in this file. + */ + public function setUp() { + $this->resetAfterTest(); + + $this->setAdminUser(); + $this->course = $this->getDataGenerator()->create_course(); + } + + /** + * Test the grade_letter added event. + * + * There is no external API for triggering this event, so the unit test will simply + * create and trigger the event and ensure the data is returned as expected. + */ + public function test_grade_letter_created() { + + // Create a scale aded event. + $event = \core\event\grade_letter_created::create(array( + 'objectid' => 10, + 'context' => context_course::instance($this->course->id) + )); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + $event->trigger(); + $events = $sink->get_events(); + $event = reset($events); + + // Check that the event data is valid. + $this->assertInstanceOf('\core\event\grade_letter_created', $event); + $this->assertEquals(context_course::instance($this->course->id), $event->get_context()); + } + + /** + * Test the grade_letter deleted event. + * + * There is no external API for triggering this event, so the unit test will simply + * create and trigger the event and ensure the data is returned as expected. + */ + public function test_grade_letter_deleted() { + + // Create a grade_letter deleted event. + $event = \core\event\grade_letter_deleted::create(array( + 'objectid' => 10, + 'context' => context_course::instance($this->course->id) + )); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + $event->trigger(); + $events = $sink->get_events(); + $event = reset($events); + + // Check that the event data is valid. + $this->assertInstanceOf('\core\event\grade_letter_deleted', $event); + $this->assertEquals(context_course::instance($this->course->id), $event->get_context()); + } + + /** + * Test the grade_letter updated event. + * + * There is no external API for triggering this event, so the unit test will simply + * create and trigger the event and ensure the data is returned as expected. + */ + public function test_grade_letter_updated() { + + // Create a scale updated event. + $event = \core\event\grade_letter_updated::create(array( + 'objectid' => 10, + 'context' => context_course::instance($this->course->id) + )); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + $event->trigger(); + $events = $sink->get_events(); + $event = reset($events); + + // Check that the event data is valid. + $this->assertInstanceOf('\core\event\grade_letter_updated', $event); + $this->assertEquals(context_course::instance($this->course->id), $event->get_context()); + } + + + /** + * Test the scale added event. + * + * There is no external API for triggering this event, so the unit test will simply + * create and trigger the event and ensure the data is returned as expected. + */ + public function test_scale_created() { + + $gradescale = new grade_scale(); + + $gradescale->name = 'unittestscale3'; + $gradescale->courseid = $this->course->id; + $gradescale->userid = 317; + $gradescale->scale = 'Distinction, Very Good, Good, Pass, Fail'; + $gradescale->description = 'This scale is used to mark standard assignments.'; + + $url = new \moodle_url('/grade/edit/scale/index.php', array('id' => $this->course->id)); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + $id = $gradescale->insert(); + $events = $sink->get_events(); + $event = reset($events); + + // Check that the event data is valid. + $this->assertInstanceOf('\core\event\scale_created', $event); + $this->assertEquals($id, $event->objectid); + $this->assertEquals($url, $event->get_url()); + $this->assertEquals(context_course::instance($this->course->id), $event->get_context()); + } + + /** + * Test the scale deleted event. + * + * There is no external API for triggering this event, so the unit test will simply + * create and trigger the event and ensure the data is returned as expected. + */ + public function test_scale_deleted() { + + $gradescale = new grade_scale(); + + $gradescale->name = 'unittestscale3'; + $gradescale->courseid = $this->course->id; + $gradescale->userid = 317; + $gradescale->scale = 'Distinction, Very Good, Good, Pass, Fail'; + $gradescale->description = 'This scale is used to mark standard assignments.'; + $gradescale->insert(); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + $gradescale->delete(); + $events = $sink->get_events(); + $event = reset($events); + + // Check that the event data is valid. + $this->assertInstanceOf('\core\event\scale_deleted', $event); + $this->assertEquals(context_course::instance($this->course->id), $event->get_context()); + } + + /** + * Test the scale updated event. + * + * There is no external API for triggering this event, so the unit test will simply + * create and trigger the event and ensure the data is returned as expected. + */ + public function test_scale_updated() { + + $gradescale = new grade_scale(); + $gradescale->name = 'unittestscale3'; + $gradescale->courseid = $this->course->id; + $gradescale->userid = 317; + $gradescale->scale = 'Distinction, Very Good, Good, Pass, Fail'; + $gradescale->description = 'This scale is used to mark standard assignments.'; + $id = $gradescale->insert(); + + $gradescale->name = 'Updated info for this unittest grade_scale'; + $url = new \moodle_url('/grade/edit/scale/index.php', array('id' => $this->course->id)); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + $gradescale->update(); + $events = $sink->get_events(); + $event = reset($events); + + // Check that the event data is valid. + $this->assertInstanceOf('\core\event\scale_updated', $event); + $this->assertEquals($id, $event->objectid); + $this->assertEquals($url, $event->get_url()); + $this->assertEquals(context_course::instance($this->course->id), $event->get_context()); + } +} From e96015214146c602069cfce203a1aa9da6f6f2ae Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Fri, 9 Feb 2018 11:58:02 +0800 Subject: [PATCH 4/4] MDL-45837 core: minor tidy up to events --- grade/tests/events_test.php | 34 +++++----------------- lib/classes/event/grade_letter_created.php | 4 +-- lib/classes/event/grade_letter_deleted.php | 5 ++-- lib/classes/event/grade_letter_updated.php | 4 ++- lib/classes/event/scale_created.php | 6 ++-- lib/classes/event/scale_deleted.php | 5 ++-- lib/classes/event/scale_updated.php | 8 +++-- 7 files changed, 28 insertions(+), 38 deletions(-) diff --git a/grade/tests/events_test.php b/grade/tests/events_test.php index 33204f93471..5789fcd3f48 100644 --- a/grade/tests/events_test.php +++ b/grade/tests/events_test.php @@ -29,7 +29,7 @@ global $CFG; require_once($CFG->dirroot . '/grade/lib.php'); /** - * Unit tests for grade/lib.php. + * Unit tests for grade events. * * @package core_grades * @category test @@ -54,14 +54,13 @@ class core_grade_events_test extends advanced_testcase { } /** - * Test the grade_letter added event. + * Test the grade letter created event. * * There is no external API for triggering this event, so the unit test will simply * create and trigger the event and ensure the data is returned as expected. */ public function test_grade_letter_created() { - - // Create a scale aded event. + // Create a grade letter created event. $event = \core\event\grade_letter_created::create(array( 'objectid' => 10, 'context' => context_course::instance($this->course->id) @@ -79,14 +78,13 @@ class core_grade_events_test extends advanced_testcase { } /** - * Test the grade_letter deleted event. + * Test the grade letter deleted event. * * There is no external API for triggering this event, so the unit test will simply * create and trigger the event and ensure the data is returned as expected. */ public function test_grade_letter_deleted() { - - // Create a grade_letter deleted event. + // Create a grade letter deleted event. $event = \core\event\grade_letter_deleted::create(array( 'objectid' => 10, 'context' => context_course::instance($this->course->id) @@ -104,14 +102,13 @@ class core_grade_events_test extends advanced_testcase { } /** - * Test the grade_letter updated event. + * Test the grade letter updated event. * * There is no external API for triggering this event, so the unit test will simply * create and trigger the event and ensure the data is returned as expected. */ public function test_grade_letter_updated() { - - // Create a scale updated event. + // Create a grade letter updated event. $event = \core\event\grade_letter_updated::create(array( 'objectid' => 10, 'context' => context_course::instance($this->course->id) @@ -128,17 +125,11 @@ class core_grade_events_test extends advanced_testcase { $this->assertEquals(context_course::instance($this->course->id), $event->get_context()); } - /** - * Test the scale added event. - * - * There is no external API for triggering this event, so the unit test will simply - * create and trigger the event and ensure the data is returned as expected. + * Test the scale created event. */ public function test_scale_created() { - $gradescale = new grade_scale(); - $gradescale->name = 'unittestscale3'; $gradescale->courseid = $this->course->id; $gradescale->userid = 317; @@ -162,14 +153,9 @@ class core_grade_events_test extends advanced_testcase { /** * Test the scale deleted event. - * - * There is no external API for triggering this event, so the unit test will simply - * create and trigger the event and ensure the data is returned as expected. */ public function test_scale_deleted() { - $gradescale = new grade_scale(); - $gradescale->name = 'unittestscale3'; $gradescale->courseid = $this->course->id; $gradescale->userid = 317; @@ -190,12 +176,8 @@ class core_grade_events_test extends advanced_testcase { /** * Test the scale updated event. - * - * There is no external API for triggering this event, so the unit test will simply - * create and trigger the event and ensure the data is returned as expected. */ public function test_scale_updated() { - $gradescale = new grade_scale(); $gradescale->name = 'unittestscale3'; $gradescale->courseid = $this->course->id; diff --git a/lib/classes/event/grade_letter_created.php b/lib/classes/event/grade_letter_created.php index ff19f65a3e8..f91399094fe 100644 --- a/lib/classes/event/grade_letter_created.php +++ b/lib/classes/event/grade_letter_created.php @@ -15,7 +15,7 @@ // along with Moodle. If not, see . /** - * Grade letter added created. + * Grade letter created event. * * @package core * @copyright 2017 Stephen Bourget @@ -27,7 +27,7 @@ namespace core\event; defined('MOODLE_INTERNAL') || die(); /** - * Grade letter added event class. + * Grade letter created event class. * * @package core * @since Moodle 3.5 diff --git a/lib/classes/event/grade_letter_deleted.php b/lib/classes/event/grade_letter_deleted.php index f31b39e656b..e5dca9c0ddf 100644 --- a/lib/classes/event/grade_letter_deleted.php +++ b/lib/classes/event/grade_letter_deleted.php @@ -65,13 +65,14 @@ class grade_letter_deleted extends base { if ($this->courseid) { return "The user with id '$this->userid' deleted the letter grade with id '$this->objectid'". " from the course in the id '".$this->courseid."'."; - } else { - return "The user with id '$this->userid' deleted the letter grade with id '$this->objectid'."; } + + return "The user with id '$this->userid' deleted the letter grade with id '$this->objectid'."; } /** * Used for mapping events on restore + * * @return array */ public static function get_objectid_mapping() { diff --git a/lib/classes/event/grade_letter_updated.php b/lib/classes/event/grade_letter_updated.php index a7a856855e0..b3ecaf12b04 100644 --- a/lib/classes/event/grade_letter_updated.php +++ b/lib/classes/event/grade_letter_updated.php @@ -27,7 +27,7 @@ namespace core\event; defined('MOODLE_INTERNAL') || die(); /** - * Grade letter added event class. + * Grade letter updated event class. * * @package core * @since Moodle 3.5 @@ -72,6 +72,7 @@ class grade_letter_updated extends base { /** * Returns relevant URL. + * * @return \moodle_url */ public function get_url() { @@ -84,6 +85,7 @@ class grade_letter_updated extends base { /** * Used for mapping events on restore + * * @return array */ public static function get_objectid_mapping() { diff --git a/lib/classes/event/scale_created.php b/lib/classes/event/scale_created.php index 9156184b95e..b1b8750e7a5 100644 --- a/lib/classes/event/scale_created.php +++ b/lib/classes/event/scale_created.php @@ -65,13 +65,14 @@ class scale_created extends base { if ($this->courseid) { return "The user with id '$this->userid' created the custom scale with id '$this->objectid'". " from the course in the id '".$this->courseid."'."; - } else { - return "The user with id '$this->userid' created the standard scale with id '$this->objectid'."; } + + return "The user with id '$this->userid' created the standard scale with id '$this->objectid'."; } /** * Returns relevant URL. + * * @return \moodle_url */ public function get_url() { @@ -84,6 +85,7 @@ class scale_created extends base { /** * Used for mapping events on restore + * * @return array */ public static function get_objectid_mapping() { diff --git a/lib/classes/event/scale_deleted.php b/lib/classes/event/scale_deleted.php index d14acf75215..8ab883c1a44 100644 --- a/lib/classes/event/scale_deleted.php +++ b/lib/classes/event/scale_deleted.php @@ -65,13 +65,14 @@ class scale_deleted extends base { if ($this->courseid) { return "The user with id '$this->userid' deleted the custom scale with id '$this->objectid'". " from the course with the id '".$this->courseid."'."; - } else { - return "The user with id '$this->userid' deleted the standard scale with id '$this->objectid'."; } + + return "The user with id '$this->userid' deleted the standard scale with id '$this->objectid'."; } /** * Used for mapping events on restore + * * @return array */ public static function get_objectid_mapping() { diff --git a/lib/classes/event/scale_updated.php b/lib/classes/event/scale_updated.php index e363ea2d8a4..f99e0d3cbeb 100644 --- a/lib/classes/event/scale_updated.php +++ b/lib/classes/event/scale_updated.php @@ -27,7 +27,7 @@ namespace core\event; defined('MOODLE_INTERNAL') || die(); /** - * Scale added event class. + * Scale updated event class. * * @package core * @since Moodle 3.5 @@ -65,13 +65,14 @@ class scale_updated extends base { if ($this->courseid) { return "The user with id '$this->userid' updated the custom scale with id '$this->objectid'". " from the course in the id '".$this->courseid."'."; - } else { - return "The user with id '$this->userid' updated the standard scale with id '$this->objectid'."; } + + return "The user with id '$this->userid' updated the standard scale with id '$this->objectid'."; } /** * Returns relevant URL. + * * @return \moodle_url */ public function get_url() { @@ -84,6 +85,7 @@ class scale_updated extends base { /** * Used for mapping events on restore + * * @return array */ public static function get_objectid_mapping() {