Skip to content

Commit

Permalink
Minor adjustments (#570)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed Jan 30, 2024
1 parent 62b2c2f commit e975367
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 29 deletions.
2 changes: 1 addition & 1 deletion classes/event/element_created.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static function get_name() {
* @param \mod_customcert\element $element
* @return element_created
*/
public static function create_from_element(\mod_customcert\element $element) : element_created {
public static function create_from_element(\mod_customcert\element $element): element_created {
global $DB;

$page = $DB->get_record('customcert_pages', ['id' => $element->get_pageid()]);
Expand Down
2 changes: 1 addition & 1 deletion classes/event/element_deleted.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static function get_name() {
* @param \mod_customcert\element $element
* @return element_deleted
*/
public static function create_from_element(\mod_customcert\element $element) : element_deleted {
public static function create_from_element(\mod_customcert\element $element): element_deleted {
global $DB;

$page = $DB->get_record('customcert_pages', ['id' => $element->get_pageid()]);
Expand Down
2 changes: 1 addition & 1 deletion classes/event/element_updated.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static function get_name() {
* @param \mod_customcert\element $element
* @return element_updated
*/
public static function create_from_element(\mod_customcert\element $element) : element_updated {
public static function create_from_element(\mod_customcert\element $element): element_updated {
global $DB;

$page = $DB->get_record('customcert_pages', ['id' => $element->get_pageid()]);
Expand Down
2 changes: 1 addition & 1 deletion classes/event/page_created.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static function get_name() {
* @param template $template
* @return page_created
*/
public static function create_from_page(\stdClass $page, template $template) : page_created {
public static function create_from_page(\stdClass $page, template $template): page_created {
$data = array(
'context' => $template->get_context(),
'objectid' => $page->id,
Expand Down
2 changes: 1 addition & 1 deletion classes/event/page_deleted.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static function get_name() {
* @param template $template
* @return page_deleted
*/
public static function create_from_page(\stdClass $page, template $template) : page_deleted {
public static function create_from_page(\stdClass $page, template $template): page_deleted {
$data = array(
'context' => $template->get_context(),
'objectid' => $page->id,
Expand Down
2 changes: 1 addition & 1 deletion classes/event/page_updated.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static function get_name() {
* @param template $template
* @return page_updated
*/
public static function create_from_page(\stdClass $page, template $template) : page_updated {
public static function create_from_page(\stdClass $page, template $template): page_updated {
$data = array(
'context' => $template->get_context(),
'objectid' => $page->id,
Expand Down
2 changes: 1 addition & 1 deletion classes/event/template_created.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static function get_name() {
* @param template $template
* @return template_created
*/
public static function create_from_template(template $template) : template_created {
public static function create_from_template(template $template): template_created {
$data = array(
'context' => $template->get_context(),
'objectid' => $template->get_id(),
Expand Down
2 changes: 1 addition & 1 deletion classes/event/template_deleted.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static function get_name() {
* @param template $template
* @return template_deleted
*/
public static function create_from_template(template $template) : template_deleted {
public static function create_from_template(template $template): template_deleted {
$data = array(
'context' => $template->get_context(),
'objectid' => $template->get_id(),
Expand Down
2 changes: 1 addition & 1 deletion classes/event/template_updated.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static function get_name() {
* @param template $template
* @return template_updated
*/
public static function create_from_template(template $template) : template_updated {
public static function create_from_template(template $template): template_updated {
$data = array(
'context' => $template->get_context(),
'objectid' => $template->get_id(),
Expand Down
50 changes: 30 additions & 20 deletions tests/event/events_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public function test_creating_a_template(): void {
$sink = $this->redirectEvents();
$template = \mod_customcert\template::create('Test name', \context_system::instance()->id);
$events = $sink->get_events();
$this->assertCount(1, $events);

$event = reset($events);

// Check that the event data is valid.
Expand All @@ -66,7 +68,6 @@ public function test_creating_a_page(): void {
$sink = $this->redirectEvents();
$page = $template->add_page();
$events = $sink->get_events();

$this->assertCount(2, $events);

$pagecreatedevent = array_shift($events);
Expand Down Expand Up @@ -97,6 +98,7 @@ public function test_moving_item(): void {
$sink = $this->redirectEvents();
$template->move_item('page', $page1id, 'down');
$events = $sink->get_events();
$this->assertCount(1, $events);

$event = reset($events);
$this->assertInstanceOf('\mod_customcert\event\template_updated', $event);
Expand All @@ -112,13 +114,18 @@ public function test_moving_item(): void {
*/
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();

// Date we are updating to.
$data = new \stdClass();
$data->id = $template->get_id();
$data->name = 'Test name 2';

// Trigger and capture the event.
$sink = $this->redirectEvents();
$template->save($data);
$events = $sink->get_events();
$this->assertCount(1, $events);

$event = reset($events);

// Check that the event data is valid.
Expand All @@ -135,11 +142,13 @@ public function test_updating_a_template(): void {
*/
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();

// Trigger and capture the event.
$sink = $this->redirectEvents();
$template->save($data);
$events = $sink->get_events();

Expand All @@ -156,9 +165,11 @@ 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
Expand Down Expand Up @@ -205,19 +216,20 @@ public function test_deleting_a_page(): void {
$sink = $this->redirectEvents();
$template->delete_page($page1id);
$events = $sink->get_events();
$this->assertCount(2, $events);

$pagedeletedevent = array_shift($events);
$templatedeletedevent = array_shift($events);
$templateupdatedevent = array_shift($events);

// 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->assertInstanceOf('\mod_customcert\event\template_updated', $templateupdatedevent);
$this->assertEquals($template->get_id(), $templateupdatedevent->objectid);
$this->assertEquals(\context_system::instance()->id, $templateupdatedevent->contextid);
$this->assertDebuggingNotCalled();
}

Expand Down Expand Up @@ -245,9 +257,9 @@ public function test_updating_a_page() {
$sink = $this->redirectEvents();
$template->save_page($p);
$events = $sink->get_events();
$this->assertCount(1, $events);

$pageupdatedevent = array_shift($events);
$templateupdatedevent = array_shift($events);

// Check that the event data is valid.
$this->assertInstanceOf('\mod_customcert\event\page_updated', $pageupdatedevent);
Expand All @@ -266,23 +278,17 @@ public function test_save_form_elements_insert() {
$page1id = $template->add_page();

$data = new \stdClass();
$data->pageid = $page1id;
$data->name = 'A name';
$data->element = 'text';
$data->text = 'Some text';
$data->pageid = $page1id;
$data->data = '';
$data->font = $data->font ?? null;
$data->fontsize = $data->fontsize ?? null;
$data->colour = $data->colour ?? null;
$data->width = $data->width ?? null;
$data->refpoint = $data->refpoint ?? null;
$data->alignment = $data->alignment ?? \mod_customcert\element::ALIGN_LEFT;
$data->timemodified = time();

$sink = $this->redirectEvents();
$e = \mod_customcert\element_factory::get_element_instance($data);
$e->save_form_elements($data);
$events = $sink->get_events();
$this->assertCount(1, $events);

$event = reset($events);

// Check that the event data is valid.
Expand Down Expand Up @@ -321,6 +327,8 @@ public function test_save_form_elements_update() {
$sink = $this->redirectEvents();
$element->save_form_elements($data);
$events = $sink->get_events();
$this->assertCount(1, $events);

$event = reset($events);

// Check that the event data is valid.
Expand Down Expand Up @@ -388,7 +396,7 @@ public function test_load_template(): void {
$element->name = 'image';
$element->element = 'image';
$element->data = '';
$DB->insert_record('customcert_elements', $element);
$element->id = $DB->insert_record('customcert_elements', $element);

$course = $this->getDataGenerator()->create_course();
$activity = $this->getDataGenerator()->create_module('customcert', ['course' => $course->id]);
Expand Down Expand Up @@ -434,11 +442,13 @@ public function test_deleting_an_element(): void {
$element->pageid = $page1id;
$element->name = 'image';
$element->element = 'image';
$element->data = '';
$element->id = $DB->insert_record('customcert_elements', $element);

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

$elementdeletedevent = array_shift($events);
$templateupdatedevent = array_shift($events);
Expand Down

0 comments on commit e975367

Please sign in to comment.