From 337e69545d46b6f92c5c4be06cac99cd0f49403a Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Mon, 24 Mar 2025 07:53:41 +0000 Subject: [PATCH] MDL-84985 reportbuilder: export report action attributes correctly. When included as part of external response data, extra validation was performed on the returned structure. --- .../external/report_action_exporter.php | 1 + .../external/report_action_exporter_test.php | 54 +++++++++++++++++++ .../tests/external/reports/get_test.php | 9 ++-- 3 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 reportbuilder/tests/external/report_action_exporter_test.php diff --git a/reportbuilder/classes/external/report_action_exporter.php b/reportbuilder/classes/external/report_action_exporter.php index b9e310f0ae3..a63d635c4e2 100644 --- a/reportbuilder/classes/external/report_action_exporter.php +++ b/reportbuilder/classes/external/report_action_exporter.php @@ -67,6 +67,7 @@ class report_action_exporter extends exporter { 'optional' => true, ], ], + 'multiple' => true, ], ]; } diff --git a/reportbuilder/tests/external/report_action_exporter_test.php b/reportbuilder/tests/external/report_action_exporter_test.php new file mode 100644 index 00000000000..21fe6d45327 --- /dev/null +++ b/reportbuilder/tests/external/report_action_exporter_test.php @@ -0,0 +1,54 @@ +. + +declare(strict_types=1); + +namespace core_reportbuilder\external; + +use advanced_testcase; +use core_reportbuilder\output\report_action; + +/** + * Unit tests for report action exporter + * + * @package core_reportbuilder + * @covers \core_reportbuilder\external\report_action_exporter + * @copyright 2025 Paul Holden + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +final class report_action_exporter_test extends advanced_testcase { + + /** + * Test exported data/structure + */ + public function test_export(): void { + global $PAGE; + + $reportaction = new report_action('Add', ['class' => 'btn', 'data-action' => 'action']); + + $exporter = new report_action_exporter(null, ['reportaction' => $reportaction]); + $export = $exporter->export($PAGE->get_renderer('core_reportbuilder')); + + $this->assertEquals((object) [ + 'tag' => 'button', + 'title' => 'Add', + 'attributes' => [ + ['name' => 'class', 'value' => 'btn'], + ['name' => 'data-action', 'value' => 'action'], + ], + ], $export); + } +} diff --git a/reportbuilder/tests/external/reports/get_test.php b/reportbuilder/tests/external/reports/get_test.php index d1c403a1565..70a930b1060 100644 --- a/reportbuilder/tests/external/reports/get_test.php +++ b/reportbuilder/tests/external/reports/get_test.php @@ -54,7 +54,7 @@ final class get_test extends externallib_advanced_testcase { $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]); $instance = manager::get_report_from_persistent($report); - $instance->set_report_action(new report_action('Add', [])); + $instance->set_report_action(new report_action('Add', ['class' => 'btn', 'data-action' => 'action'])); $instance->set_report_info_container('Hello'); // Add two filters. @@ -100,7 +100,7 @@ final class get_test extends externallib_advanced_testcase { $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]); $instance = manager::get_report_from_persistent($report); - $instance->set_report_action(new report_action('Add', [])); + $instance->set_report_action(new report_action('Add', ['class' => 'btn', 'data-action' => 'action'])); $instance->set_report_info_container('Hello'); // Add two filters. @@ -118,7 +118,10 @@ final class get_test extends externallib_advanced_testcase { $this->assertEquals([ 'tag' => 'button', 'title' => 'Add', - 'attributes' => [], + 'attributes' => [ + ['name' => 'class', 'value' => 'btn'], + ['name' => 'data-action', 'value' => 'action'], + ], ], $result['button']); $this->assertEquals('Hello', $result['infocontainer']); $this->assertTrue($result['filterspresent']);