Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure events are only triggered when necessary #595

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 18 additions & 32 deletions classes/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function save($data) {
$DB->update_record('customcert_templates', $savedata);

// Only trigger event if the name has changed.
if ($savedata->name != $data->name) {
mdjnelson marked this conversation as resolved.
Show resolved Hide resolved
if ($this->get_name() != $data->name) {
\mod_customcert\event\template_updated::create_from_template($this)->trigger();
}
}
Expand Down Expand Up @@ -155,9 +155,9 @@ public function save_page($data) {
// Update the page.
$DB->update_record('customcert_pages', $p);

// Calling code is expected to trigger template_updated
// after this method.
\mod_customcert\event\page_updated::create_from_page($p, $this)->trigger();

\mod_customcert\event\template_updated::create_from_template($this)->trigger();
mdjnelson marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand All @@ -171,27 +171,11 @@ public function save_page($data) {
public function delete() {
global $DB;

// Delete the elements.
$sql = "SELECT e.*
FROM {customcert_elements} e
INNER JOIN {customcert_pages} p
ON e.pageid = p.id
WHERE p.templateid = :templateid";
if ($elements = $DB->get_records_sql($sql, array('templateid' => $this->id))) {
foreach ($elements as $element) {
// Get an instance of the element class.
if ($e = \mod_customcert\element_factory::get_element_instance($element)) {
$e->delete();
} else {
// The plugin files are missing, so just remove the entry from the DB.
$DB->delete_records('customcert_elements', array('id' => $element->id));
}
}
}

// Delete the pages.
if (!$DB->delete_records('customcert_pages', array('templateid' => $this->id))) {
return false;
if ($pages = $DB->get_records('customcert_pages', array('templateid' => $this->id))) {
foreach ($pages as $page) {
$this->delete_page($page->id, false);
}
}

// Now, finally delete the actual template.
Expand All @@ -208,8 +192,10 @@ public function delete() {
* Handles deleting a page from the template.
*
* @param int $pageid the template page
* @param bool $triggertemplateupdatedevent False if page is being deleted
* during deletion of template.
*/
public function delete_page($pageid) {
public function delete_page(int $pageid, bool $triggertemplateupdatedevent = true): void {
global $DB;

// Get the page.
Expand Down Expand Up @@ -241,7 +227,9 @@ public function delete_page($pageid) {
AND sequence > :sequence";
$DB->execute($sql, array('templateid' => $this->id, 'sequence' => $page->sequence));

\mod_customcert\event\template_updated::create_from_template($this)->trigger();
if ($triggertemplateupdatedevent) {
\mod_customcert\event\template_updated::create_from_template($this)->trigger();
}
}

/**
Expand Down Expand Up @@ -402,7 +390,7 @@ public function copy_to_template($copytotemplate) {
$page->timemodified = $page->timecreated;
// Insert into the database.
$page->id = $DB->insert_record('customcert_pages', $page);
\mod_customcert\event\page_created::create_from_page($page, $this)->trigger();
\mod_customcert\event\page_created::create_from_page($page, $copytotemplate)->trigger();
// Now go through the elements we want to load.
if ($templateelements = $DB->get_records('customcert_elements', array('pageid' => $templatepage->id))) {
foreach ($templateelements as $templateelement) {
Expand All @@ -425,12 +413,10 @@ public function copy_to_template($copytotemplate) {
}
}

// Trigger event for template instance being copied to.
if ($copytotemplate->get_context() == \context_system::instance()) {
// If CONTEXT_SYSTEM we're creating a new template.
\mod_customcert\event\template_created::create_from_template($copytotemplate)->trigger();
} else {
// Otherwise we're loading template in a course module instance.
// Trigger event if loading a template in a course module instance.
// (No event triggered if copying a system-wide template as
// create() triggers this).
if ($copytotemplate->get_context() != \context_system::instance()) {
\mod_customcert\event\template_updated::create_from_template($copytotemplate)->trigger();
}
}
Expand Down
2 changes: 1 addition & 1 deletion edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@
$template = \mod_customcert\template::create($data->name, $contextid);

// Create a page for this template.
$pageid = $template->add_page();
$pageid = $template->add_page(false);

// Associate all the data from the form to the newly created page.
$width = 'pagewidth_' . $pageid;
Expand Down
17 changes: 3 additions & 14 deletions load_template.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,12 @@
// Check that they have confirmed they wish to load the template.
if ($confirm && confirm_sesskey()) {
// First, remove all the existing elements and pages.
$sql = "SELECT e.*
FROM {customcert_elements} e
INNER JOIN {customcert_pages} p
ON e.pageid = p.id
WHERE p.templateid = :templateid";
if ($elements = $DB->get_records_sql($sql, array('templateid' => $template->get_id()))) {
foreach ($elements as $element) {
// Get an instance of the element class.
if ($e = \mod_customcert\element_factory::get_element_instance($element)) {
$e->delete();
}
if ($pages = $DB->get_records('customcert_pages', ['templateid' => $template->get_id()])) {
foreach ($pages as $page) {
$template->delete_page($page->id, false);
}
}

// Delete the pages.
$DB->delete_records('customcert_pages', array('templateid' => $template->get_id()));

// Copy the items across.
$loadtemplate->copy_to_template($template);

Expand Down
142 changes: 131 additions & 11 deletions tests/event/events_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function test_creating_a_template(): void {
// Check that the event data is valid.
$this->assertInstanceOf('\mod_customcert\event\template_created', $event);
$this->assertEquals($template->get_id(), $event->objectid);
$this->assertEquals(\context_system::instance()->id, $event->contextid);
}

/**
Expand All @@ -74,10 +75,12 @@ public function test_creating_a_page(): void {
// Check that the event data is valid.
$this->assertInstanceOf('\mod_customcert\event\page_created', $pagecreatedevent);
$this->assertEquals($page, $pagecreatedevent->objectid);
$this->assertEquals(\context_system::instance()->id, $pagecreatedevent->contextid);
$this->assertDebuggingNotCalled();

$this->assertInstanceOf('\mod_customcert\event\template_updated', $templateupdateevent);
$this->assertEquals($template->get_id(), $templateupdateevent->objectid);
$this->assertEquals(\context_system::instance()->id, $templateupdateevent->contextid);
$this->assertDebuggingNotCalled();
}

Expand All @@ -98,25 +101,96 @@ public function test_moving_item(): void {
$event = reset($events);
$this->assertInstanceOf('\mod_customcert\event\template_updated', $event);
$this->assertEquals($template->get_id(), $event->objectid);
$this->assertEquals(\context_system::instance()->id, $event->contextid);
$this->assertDebuggingNotCalled();
}

/**
* Tests the events are fired correctly when updating a template.
*
* @covers \mod_customcert\template::save
*/
public function test_updating_a_template(): void {
$template = \mod_customcert\template::create('Test name', \context_system::instance()->id);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$data = new \stdClass();
$data->id = $template->get_id();
$data->name = 'Test name 2';
$template->save($data);
$events = $sink->get_events();
$event = reset($events);

// Check that the event data is valid.
$this->assertInstanceOf('\mod_customcert\event\template_updated', $event);
$this->assertEquals($template->get_id(), $event->objectid);
$this->assertEquals(\context_system::instance()->id, $event->contextid);
}

/**
* Tests the events are fired correctly when updating a template with no
* changes.
*
* @covers \mod_customcert\template::save
*/
public function test_updating_a_template_no_change(): void {
$template = \mod_customcert\template::create('Test name', \context_system::instance()->id);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$data = new \stdClass();
$data->id = $template->get_id();
$data->name = $template->get_name();
$template->save($data);
$events = $sink->get_events();

// Check that no events were triggered.
$this->assertCount(0, $events);
}

/**
* Tests the events are fired correctly when deleting a template.
*
* @covers \mod_customcert\template::delete
*/
public function test_deleting_a_template(): void {
global $DB;

$template = \mod_customcert\template::create('Test name', \context_system::instance()->id);
$data = new \stdClass();
$data->name = $template->get_name();
$template->save($data);
$page1id = $template->add_page();

// Check the created objects exist in the database as we will check the
// triggered events correspond to the deletion of these records.
$templates = $DB->get_records('customcert_templates', ['id' => $template->get_id()]);
$this->assertEquals(1, count($templates));
$pages = $DB->get_records('customcert_pages', ['templateid' => $template->get_id()]);
$this->assertEquals(1, count($pages));

$sink = $this->redirectEvents();
$template->delete();
$events = $sink->get_events();
$this->assertCount(2, $events);

$event = reset($events);
$event = array_shift($events);
$this->assertInstanceOf('\mod_customcert\event\page_deleted', $event);
$this->assertEquals($page1id, $event->objectid);
$this->assertEquals(\context_system::instance()->id, $event->contextid);
$this->assertDebuggingNotCalled();

$event = array_shift($events);
$this->assertInstanceOf('\mod_customcert\event\template_deleted', $event);
$this->assertEquals($template->get_id(), $event->objectid);
$this->assertEquals(\context_system::instance()->id, $event->contextid);
$this->assertDebuggingNotCalled();

// Check the above page_deleted and template_deleted events correspond
// to actual deletions in the database.
$templates = $DB->get_records('customcert_templates', ['id' => $template->get_id()]);
$this->assertEquals(0, count($templates));
$pages = $DB->get_records('customcert_pages', ['templateid' => $template->get_id()]);
$this->assertEquals(0, count($pages));
}

/**
Expand All @@ -138,10 +212,12 @@ public function test_deleting_a_page(): void {
// Check that the event data is valid.
$this->assertInstanceOf('\mod_customcert\event\page_deleted', $pagedeletedevent);
$this->assertEquals($page1id, $pagedeletedevent->objectid);
$this->assertEquals(\context_system::instance()->id, $pagedeletedevent->contextid);
$this->assertDebuggingNotCalled();

$this->assertInstanceOf('\mod_customcert\event\template_updated', $templatedeletedevent);
$this->assertEquals($template->get_id(), $templatedeletedevent->objectid);
$this->assertEquals(\context_system::instance()->id, $templatedeletedevent->contextid);
$this->assertDebuggingNotCalled();
}

Expand Down Expand Up @@ -176,10 +252,7 @@ public function test_updating_a_page() {
// Check that the event data is valid.
$this->assertInstanceOf('\mod_customcert\event\page_updated', $pageupdatedevent);
$this->assertEquals($pageid, $pageupdatedevent->objectid);
$this->assertDebuggingNotCalled();

$this->assertInstanceOf('\mod_customcert\event\template_updated', $templateupdatedevent);
$this->assertEquals($template->get_id(), $templateupdatedevent->objectid);
$this->assertEquals(\context_system::instance()->id, $pageupdatedevent->contextid);
$this->assertDebuggingNotCalled();
}

Expand Down Expand Up @@ -215,6 +288,7 @@ public function test_save_form_elements_insert() {
// Check that the event data is valid.
$this->assertInstanceOf('\mod_customcert\event\element_created', $event);
$this->assertEquals($e->get_id(), $event->objectid);
$this->assertEquals(\context_system::instance()->id, $event->contextid);
$this->assertDebuggingNotCalled();
}

Expand Down Expand Up @@ -252,6 +326,7 @@ public function test_save_form_elements_update() {
// Check that the event data is valid.
$this->assertInstanceOf('\mod_customcert\event\element_updated', $event);
$this->assertEquals($element->get_id(), $event->objectid);
$this->assertEquals(\context_system::instance()->id, $event->contextid);
$this->assertDebuggingNotCalled();
}

Expand All @@ -272,31 +347,74 @@ public function test_copy_to_template() {
$element->name = 'image';
$element->element = 'image';
$element->data = '';
$DB->insert_record('customcert_elements', $element);
$element->id = $DB->insert_record('customcert_elements', $element);

// Add another template.
$template2 = \mod_customcert\template::create('Test name 2', \context_system::instance()->id);

$sink = $this->redirectEvents();
$template->copy_to_template($template2);
$events = $sink->get_events();
$this->assertCount(2, $events);

$pagecreatedevent = array_shift($events);
$elementcreatedevent = array_shift($events);

// Check that the event data is valid.
$this->assertInstanceOf('\mod_customcert\event\page_created', $pagecreatedevent);
$this->assertEquals(\context_system::instance()->id, $pagecreatedevent->contextid);
$this->assertDebuggingNotCalled();

$this->assertInstanceOf('\mod_customcert\event\element_created', $elementcreatedevent);
$this->assertEquals(\context_system::instance()->id, $elementcreatedevent->contextid);
$this->assertDebuggingNotCalled();
}

/**
* Tests the events are fired correctly when loading a template into a
* course-level certificate.
*
* @covers \mod_customcert\element::copy_to_template
*/
public function test_load_template(): void {
global $DB;

$template = \mod_customcert\template::create('Test name', \context_system::instance()->id);
$page1id = $template->add_page();

// Add an element to the page.
$element = new \stdClass();
$element->pageid = $page1id;
$element->name = 'image';
$element->element = 'image';
$element->data = '';
$DB->insert_record('customcert_elements', $element);

$course = $this->getDataGenerator()->create_course();
$activity = $this->getDataGenerator()->create_module('customcert', ['course' => $course->id]);
$contextid = \context_module::instance($activity->cmid)->id;
$template2 = \mod_customcert\template::create($activity->name, $contextid);

$sink = $this->redirectEvents();
$template->copy_to_template($template2);
$events = $sink->get_events();
$this->assertCount(3, $events);

$pagecreatedevent = array_shift($events);
$elementcreatedevent = array_shift($events);
$templatecreatedevent = array_shift($events);
$templateupdatedevent = array_shift($events);

// Check that the event data is valid.
$this->assertInstanceOf('\mod_customcert\event\page_created', $pagecreatedevent);
$this->assertEquals($pagecreatedevent->objectid, $pagecreatedevent->objectid);
$this->assertEquals($contextid, $pagecreatedevent->contextid);
$this->assertDebuggingNotCalled();

$this->assertInstanceOf('\mod_customcert\event\element_created', $elementcreatedevent);
$this->assertEquals($elementcreatedevent->objectid, $elementcreatedevent->objectid);
$this->assertEquals($contextid, $elementcreatedevent->contextid);
$this->assertDebuggingNotCalled();

$this->assertInstanceOf('\mod_customcert\event\template_created', $templatecreatedevent);
$this->assertEquals($template2->get_id(), $templatecreatedevent->objectid);
$this->assertInstanceOf('\mod_customcert\event\template_updated', $templateupdatedevent);
$this->assertEquals($contextid, $templateupdatedevent->contextid);
$this->assertDebuggingNotCalled();
}

Expand Down Expand Up @@ -328,10 +446,12 @@ public function test_deleting_an_element(): void {
// Check that the event data is valid.
$this->assertInstanceOf('\mod_customcert\event\element_deleted', $elementdeletedevent);
$this->assertEquals($elementdeletedevent->objectid, $element->id);
$this->assertEquals($elementdeletedevent->contextid, \context_system::instance()->id);
$this->assertDebuggingNotCalled();

$this->assertInstanceOf('\mod_customcert\event\template_updated', $templateupdatedevent);
$this->assertEquals($templateupdatedevent->objectid, $template->get_id());
$this->assertEquals($templateupdatedevent->contextid, \context_system::instance()->id);
$this->assertDebuggingNotCalled();
}
}
Loading