From dd0c545ec60e8e4736a14e45516eb6591a957c9f Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Wed, 5 Oct 2022 16:37:18 +0100 Subject: [PATCH] MDL-75077 output: prevent duplicate ID errors in custom menus. Following on from 8e4a7c6b, we should apply a similar change to custom menus to account for being collapsed into the "More" menu. --- lib/outputcomponents.php | 1 + lib/tests/navigation/output/primary_test.php | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/outputcomponents.php b/lib/outputcomponents.php index 925f2f49b0b..d3235a76ada 100644 --- a/lib/outputcomponents.php +++ b/lib/outputcomponents.php @@ -3657,6 +3657,7 @@ class custom_menu_item implements renderable, templatable { $syscontext = context_system::instance(); $context = new stdClass(); + $context->moremenuid = uniqid(); $context->text = external_format_string($this->text, $syscontext->id); $context->url = $this->url ? $this->url->out() : null; // No need for the title if it's the same with text. diff --git a/lib/tests/navigation/output/primary_test.php b/lib/tests/navigation/output/primary_test.php index d25e18f513f..5fbe0847f13 100644 --- a/lib/tests/navigation/output/primary_test.php +++ b/lib/tests/navigation/output/primary_test.php @@ -159,7 +159,20 @@ class primary_test extends \advanced_testcase { $method = new ReflectionMethod('core\navigation\output\primary', 'get_custom_menu'); $method->setAccessible(true); $renderer = $PAGE->get_renderer('core'); - $this->assertEquals($expected, $method->invoke($output, $renderer)); + + // We can't assert the value of each menuitem "moremenuid" property (because it's random). + $custommenufilter = static function(array $custommenu) use (&$custommenufilter): void { + foreach ($custommenu as $menuitem) { + unset($menuitem->moremenuid); + // Recursively move through child items. + $custommenufilter($menuitem->children); + } + }; + + $actual = $method->invoke($output, $renderer); + $custommenufilter($actual); + + $this->assertEquals($expected, $actual); } /**