MDL-72757 reportbuilder: improve model event tests.
Remove some boilerplate/duplication in the current test methods using @depends annotation to pass persistents between them.
This commit is contained in:
committed by
David Matamoros
parent
39b90208e1
commit
ee566b88a8
@@ -19,6 +19,7 @@ declare(strict_types=1);
|
||||
namespace core_reportbuilder\local\models;
|
||||
|
||||
use advanced_testcase;
|
||||
use core\persistent;
|
||||
use core_reportbuilder\event\audience_created;
|
||||
use core_reportbuilder\event\audience_deleted;
|
||||
use core_reportbuilder\event\audience_updated;
|
||||
@@ -29,6 +30,7 @@ use core_user\reportbuilder\datasource\users;
|
||||
* Unit tests for the audience model
|
||||
*
|
||||
* @package core_reportbuilder
|
||||
* @covers \core_reportbuilder\local\models\audience
|
||||
* @copyright 2021 David Matamoros <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
@@ -37,9 +39,11 @@ class audience_test extends advanced_testcase {
|
||||
/**
|
||||
* Tests for audience_created event
|
||||
*
|
||||
* @return persistent[]
|
||||
*
|
||||
* @covers \core_reportbuilder\event\audience_created
|
||||
*/
|
||||
public function test_audience_created_event(): void {
|
||||
public function test_audience_created_event(): array {
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
|
||||
@@ -54,97 +58,98 @@ class audience_test extends advanced_testcase {
|
||||
|
||||
// Catch the events.
|
||||
$sink = $this->redirectEvents();
|
||||
$audience = $generator->create_audience([
|
||||
'reportid' => $report->get('id'),
|
||||
'configdata' => [],
|
||||
]);
|
||||
$audience = $generator->create_audience(['reportid' => $report->get('id'), 'configdata' => []])
|
||||
->get_persistent();
|
||||
$events = $sink->get_events();
|
||||
$sink->close();
|
||||
|
||||
// Validate the event.
|
||||
$this->assertCount(1, $events);
|
||||
|
||||
$event = reset($events);
|
||||
$this->assertInstanceOf(audience_created::class, $event);
|
||||
$this->assertEquals(audience::TABLE, $event->objecttable);
|
||||
$this->assertEquals($audience->get_persistent()->get('id'), $event->objectid);
|
||||
$this->assertEquals($audience->get('id'), $event->objectid);
|
||||
$this->assertEquals($report->get('id'), $event->other['reportid']);
|
||||
}
|
||||
$this->assertEquals($report->get_context()->id, $event->contextid);
|
||||
|
||||
/**
|
||||
* Tests for audience_deleted event
|
||||
*
|
||||
* @covers \core_reportbuilder\event\audience_deleted
|
||||
*/
|
||||
public function test_audience_deleted_event(): void {
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
|
||||
/** @var core_reportbuilder_generator $generator */
|
||||
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
||||
|
||||
$report = $generator->create_report([
|
||||
'name' => 'My report',
|
||||
'source' => users::class,
|
||||
'default' => false,
|
||||
]);
|
||||
|
||||
$audience = $generator->create_audience([
|
||||
'reportid' => $report->get('id'),
|
||||
'configdata' => [],
|
||||
]);
|
||||
$audienceid = $audience->get_persistent()->get('id');
|
||||
|
||||
// Catch the events.
|
||||
$sink = $this->redirectEvents();
|
||||
$audience->get_persistent()->delete();
|
||||
$events = $sink->get_events();
|
||||
$sink->close();
|
||||
|
||||
// Validate the event.
|
||||
$this->assertCount(1, $events);
|
||||
$event = reset($events);
|
||||
$this->assertInstanceOf(audience_deleted::class, $event);
|
||||
$this->assertEquals(audience::TABLE, $event->objecttable);
|
||||
$this->assertEquals($audienceid, $event->objectid);
|
||||
$this->assertEquals($report->get('id'), $event->other['reportid']);
|
||||
return [$report, $audience];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests for audience_updated event
|
||||
*
|
||||
* @param persistent[] $persistents
|
||||
* @return persistent[]
|
||||
*
|
||||
* @depends test_audience_created_event
|
||||
* @covers \core_reportbuilder\event\audience_updated
|
||||
*/
|
||||
public function test_audience_updated_event(): void {
|
||||
public function test_audience_updated_event(array $persistents): array {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
|
||||
/** @var core_reportbuilder_generator $generator */
|
||||
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
||||
|
||||
$report = $generator->create_report([
|
||||
'name' => 'My report',
|
||||
'source' => users::class,
|
||||
'default' => false,
|
||||
]);
|
||||
|
||||
$audience = $generator->create_audience([
|
||||
'reportid' => $report->get('id'),
|
||||
'configdata' => [],
|
||||
]);
|
||||
// Re-create the persistents.
|
||||
[$report, $audience] = $persistents;
|
||||
$report = new report($DB->insert_record(report::TABLE, $report->to_record()));
|
||||
$audience = new audience($DB->insert_record(audience::TABLE, $audience->to_record()));
|
||||
|
||||
// Catch the events.
|
||||
$sink = $this->redirectEvents();
|
||||
$audience->get_persistent()->set('heading', 'Hello');
|
||||
$audience->get_persistent()->update();
|
||||
$audience->set('heading', 'Hello')->update();
|
||||
$events = $sink->get_events();
|
||||
$sink->close();
|
||||
|
||||
// Validate the event.
|
||||
$this->assertCount(1, $events);
|
||||
|
||||
$event = reset($events);
|
||||
$this->assertInstanceOf(audience_updated::class, $event);
|
||||
$this->assertEquals(audience::TABLE, $event->objecttable);
|
||||
$this->assertEquals($audience->get_persistent()->get('id'), $event->objectid);
|
||||
$this->assertEquals($audience->get('id'), $event->objectid);
|
||||
$this->assertEquals($report->get('id'), $event->other['reportid']);
|
||||
$this->assertEquals($report->get_context()->id, $event->contextid);
|
||||
|
||||
return [$report, $audience];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests for audience_deleted event
|
||||
*
|
||||
* @param persistent[] $persistents
|
||||
*
|
||||
* @depends test_audience_updated_event
|
||||
* @covers \core_reportbuilder\event\audience_deleted
|
||||
*/
|
||||
public function test_audience_deleted_event(array $persistents): void {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
|
||||
// Re-create the persistents (remembering audience ID which is removed from persistent upon deletion).
|
||||
[$report, $audience] = $persistents;
|
||||
$report = new report($DB->insert_record(report::TABLE, $report->to_record()));
|
||||
|
||||
$audienceid = $DB->insert_record(audience::TABLE, $audience->to_record());
|
||||
$audience = new audience($audienceid);
|
||||
|
||||
// Catch the events.
|
||||
$sink = $this->redirectEvents();
|
||||
$audience->delete();
|
||||
$events = $sink->get_events();
|
||||
$sink->close();
|
||||
|
||||
// Validate the event.
|
||||
$this->assertCount(1, $events);
|
||||
|
||||
$event = reset($events);
|
||||
$this->assertInstanceOf(audience_deleted::class, $event);
|
||||
$this->assertEquals(audience::TABLE, $event->objecttable);
|
||||
$this->assertEquals($audienceid, $event->objectid);
|
||||
$this->assertEquals($report->get('id'), $event->other['reportid']);
|
||||
$this->assertEquals($report->get_context()->id, $event->contextid);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ use core_user\reportbuilder\datasource\users;
|
||||
* Unit tests for the report model
|
||||
*
|
||||
* @package core_reportbuilder
|
||||
* @covers \core_reportbuilder\local\models\report
|
||||
* @copyright 2021 David Matamoros <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
@@ -37,9 +38,11 @@ class report_test extends advanced_testcase {
|
||||
/**
|
||||
* Tests for report_created event
|
||||
*
|
||||
* @return report
|
||||
*
|
||||
* @covers \core_reportbuilder\event\report_created
|
||||
*/
|
||||
public function test_report_created_event(): void {
|
||||
public function test_report_created_event(): report {
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
|
||||
@@ -58,33 +61,73 @@ class report_test extends advanced_testcase {
|
||||
|
||||
// Validate the event.
|
||||
$this->assertCount(1, $events);
|
||||
|
||||
$event = reset($events);
|
||||
$this->assertInstanceOf(report_created::class, $event);
|
||||
$this->assertEquals(report::TABLE, $event->objecttable);
|
||||
$this->assertEquals($report->get('id'), $event->objectid);
|
||||
$this->assertEquals('My report', $event->other['name']);
|
||||
$this->assertEquals(users::class, $event->other['source']);
|
||||
$this->assertEquals($report->get('name'), $event->other['name']);
|
||||
$this->assertEquals($report->get('source'), $event->other['source']);
|
||||
$this->assertEquals($report->get_context()->id, $event->contextid);
|
||||
|
||||
return $report;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests for report_updated event
|
||||
*
|
||||
* @param report $report
|
||||
* @return report
|
||||
*
|
||||
* @depends test_report_created_event
|
||||
* @covers \core_reportbuilder\event\report_updated
|
||||
*/
|
||||
public function test_report_updated_event(report $report): report {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
|
||||
// Re-create the persistent.
|
||||
$report = new report($DB->insert_record(report::TABLE, $report->to_record()));
|
||||
|
||||
// Catch the events.
|
||||
$sink = $this->redirectEvents();
|
||||
$report->set('name', 'New report name')->update();
|
||||
$events = $sink->get_events();
|
||||
$sink->close();
|
||||
|
||||
// Validate the event.
|
||||
$this->assertCount(1, $events);
|
||||
|
||||
$event = reset($events);
|
||||
$this->assertInstanceOf(report_updated::class, $event);
|
||||
$this->assertEquals(report::TABLE, $event->objecttable);
|
||||
$this->assertEquals($report->get('id'), $event->objectid);
|
||||
$this->assertEquals('New report name', $event->other['name']);
|
||||
$this->assertEquals($report->get('source'), $event->other['source']);
|
||||
$this->assertEquals($report->get_context()->id, $event->contextid);
|
||||
|
||||
return $report;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests for report_deleted event
|
||||
*
|
||||
* @param report $report
|
||||
*
|
||||
* @depends test_report_updated_event
|
||||
* @covers \core_reportbuilder\event\report_deleted
|
||||
*/
|
||||
public function test_report_deleted_event(): void {
|
||||
public function test_report_deleted_event(report $report): void {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
|
||||
/** @var core_reportbuilder_generator $generator */
|
||||
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
||||
|
||||
$report = $generator->create_report([
|
||||
'name' => 'My report',
|
||||
'source' => users::class,
|
||||
'default' => false,
|
||||
]);
|
||||
|
||||
$reportid = $report->get('id');
|
||||
// Re-create the persistent (remembering report ID which is removed from persistent upon deletion).
|
||||
$reportid = $DB->insert_record(report::TABLE, $report->to_record());
|
||||
$report = new report($reportid);
|
||||
|
||||
// Catch the events.
|
||||
$sink = $this->redirectEvents();
|
||||
@@ -94,46 +137,13 @@ class report_test extends advanced_testcase {
|
||||
|
||||
// Validate the event.
|
||||
$this->assertCount(1, $events);
|
||||
|
||||
$event = reset($events);
|
||||
$this->assertInstanceOf(report_deleted::class, $event);
|
||||
$this->assertEquals(report::TABLE, $event->objecttable);
|
||||
$this->assertEquals($reportid, $event->objectid);
|
||||
$this->assertEquals('My report', $event->other['name']);
|
||||
$this->assertEquals(users::class, $event->other['source']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests for report_updated event
|
||||
*
|
||||
* @covers \core_reportbuilder\event\report_updated
|
||||
*/
|
||||
public function test_report_updated_event(): void {
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
|
||||
/** @var core_reportbuilder_generator $generator */
|
||||
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
||||
|
||||
$report = $generator->create_report([
|
||||
'name' => 'My report',
|
||||
'source' => users::class,
|
||||
'default' => false,
|
||||
]);
|
||||
|
||||
// Catch the events.
|
||||
$sink = $this->redirectEvents();
|
||||
$report->set('name', 'New report name');
|
||||
$report->update();
|
||||
$events = $sink->get_events();
|
||||
$sink->close();
|
||||
|
||||
// Validate the event.
|
||||
$this->assertCount(1, $events);
|
||||
$event = reset($events);
|
||||
$this->assertInstanceOf(report_updated::class, $event);
|
||||
$this->assertEquals(report::TABLE, $event->objecttable);
|
||||
$this->assertEquals($report->get('id'), $event->objectid);
|
||||
$this->assertEquals('New report name', $event->other['name']);
|
||||
$this->assertEquals(users::class, $event->other['source']);
|
||||
$this->assertEquals($report->get('name'), $event->other['name']);
|
||||
$this->assertEquals($report->get('source'), $event->other['source']);
|
||||
$this->assertEquals($report->get_context()->id, $event->contextid);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ declare(strict_types=1);
|
||||
namespace core_reportbuilder\local\models;
|
||||
|
||||
use advanced_testcase;
|
||||
use core\persistent;
|
||||
use core_reportbuilder\event\schedule_created;
|
||||
use core_reportbuilder\event\schedule_deleted;
|
||||
use core_reportbuilder\event\schedule_updated;
|
||||
@@ -29,6 +30,7 @@ use core_user\reportbuilder\datasource\users;
|
||||
* Unit tests for the schedule model
|
||||
*
|
||||
* @package core_reportbuilder
|
||||
* @covers \core_reportbuilder\local\models\schedule
|
||||
* @copyright 2021 David Matamoros <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
@@ -37,9 +39,11 @@ class schedule_test extends advanced_testcase {
|
||||
/**
|
||||
* Tests for schedule_created event
|
||||
*
|
||||
* @return persistent[]
|
||||
*
|
||||
* @covers \core_reportbuilder\event\schedule_created
|
||||
*/
|
||||
public function test_schedule_created_event(): void {
|
||||
public function test_schedule_created_event(): array {
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
|
||||
@@ -60,33 +64,76 @@ class schedule_test extends advanced_testcase {
|
||||
|
||||
// Validate the event.
|
||||
$this->assertCount(1, $events);
|
||||
|
||||
$event = reset($events);
|
||||
$this->assertInstanceOf(schedule_created::class, $event);
|
||||
$this->assertEquals(schedule::TABLE, $event->objecttable);
|
||||
$this->assertEquals($schedule->get('id'), $event->objectid);
|
||||
$this->assertEquals($report->get('id'), $event->other['reportid']);
|
||||
$this->assertEquals($report->get_context()->id, $event->contextid);
|
||||
|
||||
return [$report, $schedule];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests for schedule_updated event
|
||||
*
|
||||
* @param persistent[] $persistents
|
||||
* @return persistent[]
|
||||
*
|
||||
* @depends test_schedule_created_event
|
||||
* @covers \core_reportbuilder\event\schedule_updated
|
||||
*/
|
||||
public function test_schedule_updated_event(array $persistents): array {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
|
||||
// Re-create the persistents.
|
||||
[$report, $schedule] = $persistents;
|
||||
$report = new report($DB->insert_record(report::TABLE, $report->to_record()));
|
||||
$schedule = new schedule($DB->insert_record(schedule::TABLE, $schedule->to_record()));
|
||||
|
||||
// Catch the events.
|
||||
$sink = $this->redirectEvents();
|
||||
$schedule->set('name', 'My new schedule')->update();
|
||||
$events = $sink->get_events();
|
||||
$sink->close();
|
||||
|
||||
// Validate the event.
|
||||
$this->assertCount(1, $events);
|
||||
|
||||
$event = reset($events);
|
||||
$this->assertInstanceOf(schedule_updated::class, $event);
|
||||
$this->assertEquals(schedule::TABLE, $event->objecttable);
|
||||
$this->assertEquals($schedule->get('id'), $event->objectid);
|
||||
$this->assertEquals($report->get('id'), $event->other['reportid']);
|
||||
$this->assertEquals($report->get_context()->id, $event->contextid);
|
||||
|
||||
return [$report, $schedule];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests for schedule_deleted event
|
||||
*
|
||||
* @param persistent[] $persistents
|
||||
*
|
||||
* @depends test_schedule_updated_event
|
||||
* @covers \core_reportbuilder\event\schedule_deleted
|
||||
*/
|
||||
public function test_schedule_deleted_event(): void {
|
||||
public function test_schedule_deleted_event(array $persistents): void {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
|
||||
/** @var core_reportbuilder_generator $generator */
|
||||
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
||||
// Re-create the persistents (remembering schedule ID which is removed from persistent upon deletion).
|
||||
[$report, $schedule] = $persistents;
|
||||
$report = new report($DB->insert_record(report::TABLE, $report->to_record()));
|
||||
|
||||
$report = $generator->create_report([
|
||||
'name' => 'My report',
|
||||
'source' => users::class,
|
||||
'default' => false,
|
||||
]);
|
||||
|
||||
$schedule = $generator->create_schedule(['reportid' => $report->get('id'), 'name' => 'My schedule']);
|
||||
$scheduleid = $schedule->get('id');
|
||||
$scheduleid = $DB->insert_record(schedule::TABLE, $schedule->to_record());
|
||||
$schedule = new schedule($scheduleid);
|
||||
|
||||
// Catch the events.
|
||||
$sink = $this->redirectEvents();
|
||||
@@ -96,46 +143,12 @@ class schedule_test extends advanced_testcase {
|
||||
|
||||
// Validate the event.
|
||||
$this->assertCount(1, $events);
|
||||
|
||||
$event = reset($events);
|
||||
$this->assertInstanceOf(schedule_deleted::class, $event);
|
||||
$this->assertEquals(schedule::TABLE, $event->objecttable);
|
||||
$this->assertEquals($scheduleid, $event->objectid);
|
||||
$this->assertEquals($report->get('id'), $event->other['reportid']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests for schedule_updated event
|
||||
*
|
||||
* @covers \core_reportbuilder\event\schedule_updated
|
||||
*/
|
||||
public function test_schedule_updated_event(): void {
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
|
||||
/** @var core_reportbuilder_generator $generator */
|
||||
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
||||
|
||||
$report = $generator->create_report([
|
||||
'name' => 'My report',
|
||||
'source' => users::class,
|
||||
'default' => false,
|
||||
]);
|
||||
|
||||
$schedule = $generator->create_schedule(['reportid' => $report->get('id'), 'name' => 'My schedule']);
|
||||
|
||||
// Catch the events.
|
||||
$sink = $this->redirectEvents();
|
||||
$schedule->set('name', 'My new schedule');
|
||||
$schedule->update();
|
||||
$events = $sink->get_events();
|
||||
$sink->close();
|
||||
|
||||
// Validate the event.
|
||||
$this->assertCount(1, $events);
|
||||
$event = reset($events);
|
||||
$this->assertInstanceOf(schedule_updated::class, $event);
|
||||
$this->assertEquals(schedule::TABLE, $event->objecttable);
|
||||
$this->assertEquals($schedule->get('id'), $event->objectid);
|
||||
$this->assertEquals($report->get('id'), $event->other['reportid']);
|
||||
$this->assertEquals($report->get_context()->id, $event->contextid);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user