diff --git a/includes/menu/AuthMenuEntry.php b/includes/menu/AuthMenuEntry.php index 7c31944..2ae80e6 100644 --- a/includes/menu/AuthMenuEntry.php +++ b/includes/menu/AuthMenuEntry.php @@ -148,6 +148,7 @@ class AuthMenuEntry implements IMenuEntry { } return $returnToQuery; } + /** * @param array $returnToQuery * @param array $authLinksQuery @@ -191,7 +192,7 @@ class AuthMenuEntry implements IMenuEntry { private function buildComponentsForAnon( array $returnToQuery, $authLinksQuery ): array { // unset campaign on login link so as not to interfere with A/B tests unset( $returnToQuery['campaign'] ); - if ( !empty( $returntoquery ) ) { + if ( !empty( $returnToQuery ) ) { $authLinksQuery['returntoquery'] = wfArrayToCgi( $returnToQuery ); } diff --git a/includes/menu/Group.php b/includes/menu/Group.php index a3e4e58..96a09e6 100644 --- a/includes/menu/Group.php +++ b/includes/menu/Group.php @@ -69,9 +69,12 @@ class Group { * @throws DomainException When the entry already exists */ private function throwIfNotUnique( $name ) { - if ( $this->search( $name ) !== -1 ) { - throw new DomainException( "The \"${name}\" entry already exists." ); + try { + $this->search( $name ); + } catch ( DomainException $exception ) { + return; } + throw new DomainException( "The \"${name}\" entry already exists." ); } /** @@ -104,6 +107,7 @@ class Group { * * @param string $name * @return integer If the menu entry exists, then the 0-based index of the entry; otherwise, -1 + * @throws DomainException */ private function search( $name ) { $count = count( $this->entries ); @@ -113,8 +117,7 @@ class Group { return $i; } } - - return -1; + throw new DomainException( "The \"{$name}\" entry doesn't exist." ); } /** @@ -131,15 +134,21 @@ class Group { $this->throwIfNotUnique( $name ); $index = $this->search( $targetName ); - if ( $index === -1 ) { - throw new DomainException( "The \"{$targetName}\" entry doesn't exist." ); - } - $entry = new MenuEntry( $name, $isJSOnly ); array_splice( $this->entries, $index + 1, 0, [ $entry ] ); return $entry; } + + /** + * @param string $targetName + * @return IMenuEntry + * @throws DomainException + */ + public function getEntryByName( $targetName ): IMenuEntry { + $index = $this->search( $targetName ); + return $this->entries[$index]; + } } /** diff --git a/includes/menu/MenuEntry.php b/includes/menu/MenuEntry.php index c384bc7..9a8729f 100644 --- a/includes/menu/MenuEntry.php +++ b/includes/menu/MenuEntry.php @@ -77,11 +77,11 @@ class MenuEntry implements IMenuEntry { */ public function addComponent( $label, $url, $className = '', $attrs = [] ) { $this->components[] = [ - 'text' => $label, - 'href' => $url, - 'class' => $className, - ] + $attrs; - + 'text' => $label, + 'href' => $url, + 'class' => $className + ] + $attrs; return $this; } + } diff --git a/tests/phpunit/menu/GroupTest.php b/tests/phpunit/menu/GroupTest.php index 3983c3f..c225137 100644 --- a/tests/phpunit/menu/GroupTest.php +++ b/tests/phpunit/menu/GroupTest.php @@ -3,6 +3,7 @@ namespace Tests\MediaWiki\Minerva\Menu; use MediaWiki\Minerva\Menu\Group; +use MediaWiki\Minerva\Menu\IMenuEntry; /** * @group MinervaNeue @@ -236,4 +237,28 @@ class GroupTest extends \MediaWikiTestCase { $this->assertEquals( $expectedEntries, $menu->getEntries() ); } + /** + * @covers ::getEntryByName + * @covers ::search + */ + public function testGetEntryByName() { + $menu = new Group(); + $menu->insert( 'home' ) + ->addComponent( + $this->homeComponent['text'], + $this->homeComponent['href'] + ); + $this->assertInstanceOf( IMenuEntry::class, $menu->getEntryByName( 'home' ) ); + } + + /** + * @covers ::getEntryByName + * @covers ::search + * @expectedException \DomainException + */ + public function testGetEntryByNameException() { + $menu = new Group(); + $menu->getEntryByName( 'home' ); + } + }