forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-40908 core_tag: replaced 'update' add_to_log calls with an event
- Loading branch information
Showing
5 changed files
with
266 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
// This file is part of Moodle - http://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
/** | ||
* Tag updated event. | ||
* | ||
* @property-read array $other { | ||
* Extra information about event. | ||
* | ||
* - string name: the name of the tag. | ||
* - string rawname: the raw name of the tag. | ||
* } | ||
* | ||
* @package core | ||
* @copyright 2014 Mark Nelson <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
namespace core\event; | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
class tag_updated extends base { | ||
|
||
/** @var array The legacy log data. */ | ||
private $legacylogdata; | ||
|
||
/** | ||
* Initialise the event data. | ||
*/ | ||
protected function init() { | ||
$this->data['objecttable'] = 'tag'; | ||
$this->data['crud'] = 'u'; | ||
$this->data['edulevel'] = self::LEVEL_OTHER; | ||
} | ||
|
||
/** | ||
* Returns localised general event name. | ||
* | ||
* @return string | ||
*/ | ||
public static function get_name() { | ||
return get_string('eventtagupdated', 'tag'); | ||
} | ||
|
||
/** | ||
* Returns non-localised description of what happened. | ||
* | ||
* @return string | ||
*/ | ||
public function get_description() { | ||
return 'The tag with the id ' . $this->objectid . ' was updated by the user with the id ' . $this->userid; | ||
} | ||
|
||
/** | ||
* Set the legacy data used for add_to_log(). | ||
* | ||
* @param array $logdata | ||
*/ | ||
public function set_legacy_logdata($logdata) { | ||
$this->legacylogdata = $logdata; | ||
} | ||
|
||
/** | ||
* Return legacy data for add_to_log(). | ||
* | ||
* @return array | ||
*/ | ||
protected function get_legacy_logdata() { | ||
if (isset($this->legacylogdata)) { | ||
return $this->legacylogdata; | ||
} | ||
|
||
return array($this->courseid, 'tag', 'update', 'index.php?id='. $this->objectid, $this->other['name']); | ||
} | ||
|
||
/** | ||
* Custom validation. | ||
* | ||
* @throws \coding_exception | ||
* @return void | ||
*/ | ||
protected function validate_data() { | ||
parent::validate_data(); | ||
|
||
if (!isset($this->other['name'])) { | ||
throw new \coding_exception('The name must be set in $other.'); | ||
} | ||
|
||
if (!isset($this->other['rawname'])) { | ||
throw new \coding_exception('The rawname must be set in $other.'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
// This file is part of Moodle - http://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
/** | ||
* Events tests. | ||
* | ||
* @package core_tag | ||
* @category test | ||
* @copyright 2014 Mark Nelson <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
global $CFG; | ||
|
||
require_once($CFG->dirroot . '/tag/lib.php'); | ||
|
||
class core_tag_events_testcase extends advanced_testcase { | ||
|
||
/** | ||
* Test set up. | ||
* | ||
* This is executed before running any test in this file. | ||
*/ | ||
public function setUp() { | ||
$this->resetAfterTest(); | ||
} | ||
|
||
/** | ||
* Test the tag updated event. | ||
*/ | ||
public function test_tag_updated() { | ||
$this->setAdminUser(); | ||
|
||
// Save the system context. | ||
$systemcontext = context_system::instance(); | ||
|
||
// Create a tag we are going to update. | ||
$tag = $this->getDataGenerator()->create_tag(); | ||
|
||
// Store the name before we change it. | ||
$oldname = $tag->name; | ||
|
||
// Trigger and capture the event when renaming a tag. | ||
$sink = $this->redirectEvents(); | ||
tag_rename($tag->id, 'newname'); | ||
// Update the tag's name since we have renamed it. | ||
$tag->name = 'newname'; | ||
$events = $sink->get_events(); | ||
$event = reset($events); | ||
|
||
// Check that the event data is valid. | ||
$this->assertInstanceOf('\core\event\tag_updated', $event); | ||
$this->assertEquals($systemcontext, $event->get_context()); | ||
$expected = array(SITEID, 'tag', 'update', 'index.php?id=' . $tag->id, $oldname . '->'. $tag->name); | ||
$this->assertEventLegacyLogData($expected, $event); | ||
|
||
// Trigger and capture the event when setting the type of a tag. | ||
$sink = $this->redirectEvents(); | ||
tag_type_set($tag->id, 'official'); | ||
$events = $sink->get_events(); | ||
$event = reset($events); | ||
|
||
// Check that the event data is valid. | ||
$this->assertInstanceOf('\core\event\tag_updated', $event); | ||
$this->assertEquals($systemcontext, $event->get_context()); | ||
$expected = array(0, 'tag', 'update', 'index.php?id=' . $tag->id, $tag->name); | ||
$this->assertEventLegacyLogData($expected, $event); | ||
|
||
// Trigger and capture the event for setting the description of a tag. | ||
$sink = $this->redirectEvents(); | ||
tag_description_set($tag->id, 'description', FORMAT_MOODLE); | ||
$events = $sink->get_events(); | ||
$event = reset($events); | ||
|
||
// Check that the event data is valid. | ||
$this->assertInstanceOf('\core\event\tag_updated', $event); | ||
$this->assertEquals($systemcontext, $event->get_context()); | ||
$expected = array(0, 'tag', 'update', 'index.php?id=' . $tag->id, $tag->name); | ||
$this->assertEventLegacyLogData($expected, $event); | ||
} | ||
} |