From 9b7b10bbe9f6ce4de362aca8d17bdc2d7a59b58b Mon Sep 17 00:00:00 2001 From: Kosta Harlan Date: Wed, 26 Jun 2019 22:04:42 -0400 Subject: [PATCH] Allow overriding text and CSS class for home menu entry * Introduce a HomeMenuEntry class and use it for adding the home menu link * Provide override methods for text and CSS class Bug: T223210 Change-Id: I37160887478cba829a6e2f10a4d8f87d95167556 --- includes/menu/Definitions.php | 2 +- includes/menu/HomeMenuEntry.php | 101 ++++++++++++++++++ tests/phpunit/unit/menu/HomeMenuEntryTest.php | 57 ++++++++++ 3 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 includes/menu/HomeMenuEntry.php create mode 100644 tests/phpunit/unit/menu/HomeMenuEntryTest.php diff --git a/includes/menu/Definitions.php b/includes/menu/Definitions.php index 36575a767..235350294 100644 --- a/includes/menu/Definitions.php +++ b/includes/menu/Definitions.php @@ -126,7 +126,7 @@ final class Definitions { * @param Group $group */ public function insertHomeItem( Group $group ) { - $group->insertEntry( new SingleMenuEntry( + $group->insertEntry( new HomeMenuEntry( 'home', $this->context->msg( 'mobile-frontend-home-button' )->escaped(), Title::newMainPage()->getLocalURL() diff --git a/includes/menu/HomeMenuEntry.php b/includes/menu/HomeMenuEntry.php new file mode 100644 index 000000000..3a9af074b --- /dev/null +++ b/includes/menu/HomeMenuEntry.php @@ -0,0 +1,101 @@ +component['text'] = $text; + return $this; + } + + /** + * Override the CSS class used in the home menu entry. + * + * @param string $cssClass + * @return $this + */ + public function overrideCssClass( $cssClass ) { + $this->component['class'] = $cssClass; + return $this; + } + + /** + * Create a home menu element with one component + * + * @param string $name An unique menu element identifier + * @param string $text Text to show on menu element + * @param string $url URL menu element points to + * @param bool|string $trackClicks Should clicks be tracked. To override the tracking code + * pass the tracking code as string + */ + public function __construct( $name, $text, $url, $trackClicks = true ) { + $this->name = $name; + $this->component = [ + 'text' => $text, + 'href' => $url, + 'class' => trim( MinervaUI::iconClass( $name, 'before' ) ) + ]; + if ( $trackClicks !== false ) { + $this->component['data-event-name'] = $trackClicks === true ? $name : $trackClicks; + } + } + + /** + * @inheritDoc + */ + public function getName() { + return $this->name; + } + + /** + * @inheritDoc + */ + public function getCSSClasses(): array { + return []; + } + + /** + * @inheritDoc + */ + public function getComponents(): array { + return [ $this->component ]; + } + +} diff --git a/tests/phpunit/unit/menu/HomeMenuEntryTest.php b/tests/phpunit/unit/menu/HomeMenuEntryTest.php new file mode 100644 index 000000000..ee8891c0b --- /dev/null +++ b/tests/phpunit/unit/menu/HomeMenuEntryTest.php @@ -0,0 +1,57 @@ +assertSame( $name, $entry->getName() ); + $this->assertSame( [], $entry->getCSSClasses() ); + $this->assertSame( [ [ + 'text' => $text, + 'href' => $url, + 'class' => 'mw-ui-icon mw-ui-icon-before mw-ui-icon-minerva-foo', + 'data-event-name' => 'foo' + ] ], $entry->getComponents() ); + } + + /** + * @covers ::overrideCssClass + * @covers ::overrideText + * @covers ::getComponents + */ + public function testOverride() { + $entry = new HomeMenuEntry( 'foo', 'bar', 'http://baz' ); + $component = current( $entry->getComponents() ); + $this->assertSame( 'bar', $component['text'] ); + $this->assertSame( + 'mw-ui-icon mw-ui-icon-before mw-ui-icon-minerva-foo', + $component['class'] + ); + $entry->overrideText( 'blah' ) + ->overrideCssClass( 'classy' ); + $component = current( $entry->getComponents() ); + $this->assertSame( 'blah', $component['text'] ); + $this->assertSame( + 'classy', + $component['class'] + ); + } + +}