Merge "Add method for getting menu entry by name"

This commit is contained in:
jenkins-bot 2019-06-06 21:46:26 +00:00 committed by Gerrit Code Review
commit c99188376f
4 changed files with 49 additions and 14 deletions

View File

@ -148,6 +148,7 @@ class AuthMenuEntry implements IMenuEntry {
} }
return $returnToQuery; return $returnToQuery;
} }
/** /**
* @param array $returnToQuery * @param array $returnToQuery
* @param array $authLinksQuery * @param array $authLinksQuery
@ -191,7 +192,7 @@ class AuthMenuEntry implements IMenuEntry {
private function buildComponentsForAnon( array $returnToQuery, $authLinksQuery ): array { private function buildComponentsForAnon( array $returnToQuery, $authLinksQuery ): array {
// unset campaign on login link so as not to interfere with A/B tests // unset campaign on login link so as not to interfere with A/B tests
unset( $returnToQuery['campaign'] ); unset( $returnToQuery['campaign'] );
if ( !empty( $returntoquery ) ) { if ( !empty( $returnToQuery ) ) {
$authLinksQuery['returntoquery'] = wfArrayToCgi( $returnToQuery ); $authLinksQuery['returntoquery'] = wfArrayToCgi( $returnToQuery );
} }

View File

@ -69,9 +69,12 @@ class Group {
* @throws DomainException When the entry already exists * @throws DomainException When the entry already exists
*/ */
private function throwIfNotUnique( $name ) { private function throwIfNotUnique( $name ) {
if ( $this->search( $name ) !== -1 ) { try {
throw new DomainException( "The \"${name}\" entry already exists." ); $this->search( $name );
} catch ( DomainException $exception ) {
return;
} }
throw new DomainException( "The \"${name}\" entry already exists." );
} }
/** /**
@ -104,6 +107,7 @@ class Group {
* *
* @param string $name * @param string $name
* @return integer If the menu entry exists, then the 0-based index of the entry; otherwise, -1 * @return integer If the menu entry exists, then the 0-based index of the entry; otherwise, -1
* @throws DomainException
*/ */
private function search( $name ) { private function search( $name ) {
$count = count( $this->entries ); $count = count( $this->entries );
@ -113,8 +117,7 @@ class Group {
return $i; return $i;
} }
} }
throw new DomainException( "The \"{$name}\" entry doesn't exist." );
return -1;
} }
/** /**
@ -131,15 +134,21 @@ class Group {
$this->throwIfNotUnique( $name ); $this->throwIfNotUnique( $name );
$index = $this->search( $targetName ); $index = $this->search( $targetName );
if ( $index === -1 ) {
throw new DomainException( "The \"{$targetName}\" entry doesn't exist." );
}
$entry = new MenuEntry( $name, $isJSOnly ); $entry = new MenuEntry( $name, $isJSOnly );
array_splice( $this->entries, $index + 1, 0, [ $entry ] ); array_splice( $this->entries, $index + 1, 0, [ $entry ] );
return $entry; return $entry;
} }
/**
* @param string $targetName
* @return IMenuEntry
* @throws DomainException
*/
public function getEntryByName( $targetName ): IMenuEntry {
$index = $this->search( $targetName );
return $this->entries[$index];
}
} }
/** /**

View File

@ -77,11 +77,11 @@ class MenuEntry implements IMenuEntry {
*/ */
public function addComponent( $label, $url, $className = '', $attrs = [] ) { public function addComponent( $label, $url, $className = '', $attrs = [] ) {
$this->components[] = [ $this->components[] = [
'text' => $label, 'text' => $label,
'href' => $url, 'href' => $url,
'class' => $className, 'class' => $className
] + $attrs; ] + $attrs;
return $this; return $this;
} }
} }

View File

@ -3,6 +3,7 @@
namespace Tests\MediaWiki\Minerva\Menu; namespace Tests\MediaWiki\Minerva\Menu;
use MediaWiki\Minerva\Menu\Group; use MediaWiki\Minerva\Menu\Group;
use MediaWiki\Minerva\Menu\IMenuEntry;
/** /**
* @group MinervaNeue * @group MinervaNeue
@ -236,4 +237,28 @@ class GroupTest extends \MediaWikiTestCase {
$this->assertEquals( $expectedEntries, $menu->getEntries() ); $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' );
}
} }