From f1c3aca7ea3618e21af35fb150ddd69152a81120 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Tue, 5 Jul 2022 16:43:14 +0100 Subject: [PATCH] MDL-75165 reportbuilder: entity method to override multiple aliases. --- reportbuilder/classes/local/entities/base.php | 18 ++++++++- reportbuilder/tests/local/entities/course_test.php | 44 +++++++++++++++++++--- reportbuilder/upgrade.txt | 1 + 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/reportbuilder/classes/local/entities/base.php b/reportbuilder/classes/local/entities/base.php index 51aebeea345..6b14162378a 100644 --- a/reportbuilder/classes/local/entities/base.php +++ b/reportbuilder/classes/local/entities/base.php @@ -139,7 +139,8 @@ abstract class base { } /** - * Override the default alias for given database table used in entity queries + * Override the default alias for given database table used in entity queries, to avoid table alias clashes that may occur + * if multiple entities of a report each define the same default alias for one of their tables * * @param string $tablename * @param string $alias @@ -156,6 +157,21 @@ abstract class base { } /** + * Override multiple default database table aliases used in entity queries as per {@see set_table_alias}, typically when + * you're adding an entity multiple times to a report you'd want to override the table aliases in the second instance to + * avoid clashes with the first + * + * @param array $aliases Array of tablename => alias values + * @return self + */ + final public function set_table_aliases(array $aliases): self { + foreach ($aliases as $tablename => $alias) { + $this->set_table_alias($tablename, $alias); + } + return $this; + } + + /** * Returns an alias used in the queries for a given table * * @param string $tablename diff --git a/reportbuilder/tests/local/entities/course_test.php b/reportbuilder/tests/local/entities/course_test.php index 6e769178e3b..17700ca90ca 100644 --- a/reportbuilder/tests/local/entities/course_test.php +++ b/reportbuilder/tests/local/entities/course_test.php @@ -340,13 +340,9 @@ class course_test extends advanced_testcase { /** * Test entity table alias */ - public function test_table_alias(): void { + public function test_get_table_alias(): void { $courseentity = new course(); - $this->assertEquals('c', $courseentity->get_table_alias('course')); - - $courseentity->set_table_alias('course', 'newalias'); - $this->assertEquals('newalias', $courseentity->get_table_alias('course')); } /** @@ -362,6 +358,16 @@ class course_test extends advanced_testcase { } /** + * Test setting table alias + */ + public function test_set_table_alias(): void { + $courseentity = new course(); + + $courseentity->set_table_alias('course', 'newalias'); + $this->assertEquals('newalias', $courseentity->get_table_alias('course')); + } + + /** * Test invalid entity set table alias */ public function test_set_table_alias_invalid(): void { @@ -373,6 +379,34 @@ class course_test extends advanced_testcase { } /** + * Test setting multiple table aliases + */ + public function test_set_table_aliases(): void { + $courseentity = new course(); + + $courseentity->set_table_aliases([ + 'course' => 'newalias', + 'context' => 'newalias2', + ]); + $this->assertEquals('newalias', $courseentity->get_table_alias('course')); + $this->assertEquals('newalias2', $courseentity->get_table_alias('context')); + } + + /** + * Test setting multiple table aliases, containing an invalid table + */ + public function test_set_table_aliases_invalid(): void { + $courseentity = new course(); + + $this->expectException(coding_exception::class); + $this->expectExceptionMessage('Coding error detected, it must be fixed by a programmer: Invalid table name (nonexistent)'); + $courseentity->set_table_aliases([ + 'course' => 'newalias', + 'nonexistent' => 'newalias2', + ]); + } + + /** * Test entity name */ public function test_name(): void { diff --git a/reportbuilder/upgrade.txt b/reportbuilder/upgrade.txt index 231f22fd7ec..95cfac859c6 100644 --- a/reportbuilder/upgrade.txt +++ b/reportbuilder/upgrade.txt @@ -28,6 +28,7 @@ Information provided here is intended especially for developers. * The base aggregation `format_value` method has a `$columntype` argument in order to preserve type during aggregation. When defining column callbacks, strict typing will now be preserved in your callback methods when the column is being aggregated * The method `get_joins()` in the base entity class is now public, allowing for easier joins within reports +* New method `set_table_aliases` in base entity class, for overriding multiple table aliases in a single call * The following local helper methods have been deprecated, their implementation moved to exporters: - `audience::get_all_audiences_menu_types` -> `custom_report_audience_cards_exporter` - `report::get_available_columns` -> `custom_report_column_cards_exporter` -- 2.11.4.GIT