Add method for getting menu entry by name
* Also fix typo with $returnToQuery * More usage of DomainException Bug: T222834 Change-Id: I5623741779eebe4c5694bdbd7a1aa1bcdd7f04c6
This commit is contained in:
parent
b64585dfad
commit
b035d76d0f
|
@ -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 );
|
||||
}
|
||||
|
||||
|
|
|
@ -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];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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' );
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue