2017-07-12 15:12:40 +00:00
|
|
|
<?php
|
|
|
|
|
2019-04-08 17:08:57 +00:00
|
|
|
namespace Tests\MediaWiki\Minerva\Menu;
|
2017-07-12 15:12:40 +00:00
|
|
|
|
2019-10-20 02:26:08 +00:00
|
|
|
use DomainException;
|
2019-07-01 18:06:01 +00:00
|
|
|
use MediaWiki\Minerva\Menu\Entries\IMenuEntry;
|
2019-04-08 17:08:57 +00:00
|
|
|
use MediaWiki\Minerva\Menu\Group;
|
2017-07-12 15:12:40 +00:00
|
|
|
|
|
|
|
/**
|
2017-07-14 23:56:16 +00:00
|
|
|
* @group MinervaNeue
|
2019-04-08 17:08:57 +00:00
|
|
|
* @coversDefaultClass \MediaWiki\Minerva\Menu\Group
|
2017-07-12 15:12:40 +00:00
|
|
|
*/
|
2021-10-11 22:53:10 +00:00
|
|
|
class GroupTest extends \MediaWikiIntegrationTestCase {
|
2021-01-15 20:57:04 +00:00
|
|
|
/** @var string[] */
|
2017-07-12 15:12:40 +00:00
|
|
|
private $homeComponent = [
|
|
|
|
'text' => 'Home',
|
|
|
|
'href' => '/Main_page',
|
|
|
|
'class' => 'mw-ui-icon mw-ui-icon-before mw-ui-icon-home',
|
|
|
|
'data-event-name' => 'home',
|
2021-09-09 15:56:42 +00:00
|
|
|
'icon' => null,
|
2017-07-12 15:12:40 +00:00
|
|
|
];
|
|
|
|
|
2021-01-15 20:57:04 +00:00
|
|
|
/** @var string[] */
|
2017-07-12 15:12:40 +00:00
|
|
|
private $nearbyComponent = [
|
|
|
|
'text' => 'Nearby',
|
|
|
|
'href' => '/wiki/Special:Nearby',
|
|
|
|
'class' => 'mw-ui-icon mw-ui-icon-before mw-ui-icon-nearby',
|
2021-09-09 15:56:42 +00:00
|
|
|
'icon' => null
|
2017-07-12 15:12:40 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
2019-04-08 17:08:57 +00:00
|
|
|
* @covers ::getEntries
|
2017-07-12 15:12:40 +00:00
|
|
|
*/
|
|
|
|
public function testItShouldntHaveEntriesByDefault() {
|
2019-10-23 18:30:55 +00:00
|
|
|
$menu = new Group( 'p-test' );
|
2017-07-12 15:12:40 +00:00
|
|
|
|
|
|
|
$this->assertEmpty( $menu->getEntries() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-08 17:08:57 +00:00
|
|
|
* @covers ::insert
|
|
|
|
* @covers ::search
|
|
|
|
* @covers ::getEntries
|
2019-07-01 18:06:01 +00:00
|
|
|
* @covers \MediaWiki\Minerva\Menu\Entries\MenuEntry::addComponent
|
2017-07-12 15:12:40 +00:00
|
|
|
*/
|
|
|
|
public function testInsertingAnEntry() {
|
2019-10-23 18:30:55 +00:00
|
|
|
$menu = new Group( 'p-test' );
|
2017-07-12 15:12:40 +00:00
|
|
|
$menu->insert( 'home' )
|
|
|
|
->addComponent(
|
|
|
|
$this->homeComponent['text'],
|
|
|
|
$this->homeComponent['href'],
|
|
|
|
$this->homeComponent['class'],
|
|
|
|
[
|
|
|
|
'data-event-name' => $this->homeComponent['data-event-name']
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$expectedEntries = [
|
|
|
|
[
|
|
|
|
'name' => 'home',
|
|
|
|
'components' => [ $this->homeComponent ],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertEquals( $expectedEntries, $menu->getEntries() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-08 17:08:57 +00:00
|
|
|
* @covers ::insert
|
|
|
|
* @covers ::search
|
|
|
|
* @covers ::getEntries
|
2019-07-01 18:06:01 +00:00
|
|
|
* @covers \MediaWiki\Minerva\Menu\Entries\MenuEntry::addComponent
|
2017-07-12 15:12:40 +00:00
|
|
|
*/
|
|
|
|
public function testInsertingAnEntryAfterAnother() {
|
2019-10-23 18:30:55 +00:00
|
|
|
$menu = new Group( 'p-test' );
|
2017-07-12 15:12:40 +00:00
|
|
|
$menu->insert( 'home' )
|
|
|
|
->addComponent(
|
|
|
|
$this->homeComponent['text'],
|
|
|
|
$this->homeComponent['href'],
|
|
|
|
$this->homeComponent['class'],
|
|
|
|
[
|
|
|
|
'data-event-name' => $this->homeComponent['data-event-name']
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$menu->insert( 'another_home' )
|
|
|
|
->addComponent(
|
|
|
|
$this->homeComponent['text'],
|
|
|
|
$this->homeComponent['href'],
|
|
|
|
$this->homeComponent['class'],
|
|
|
|
[
|
|
|
|
'data-event-name' => $this->homeComponent['data-event-name']
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$menu->insertAfter( 'home', 'nearby' )
|
|
|
|
->addComponent(
|
|
|
|
$this->nearbyComponent['text'],
|
|
|
|
$this->nearbyComponent['href'],
|
|
|
|
$this->nearbyComponent['class']
|
|
|
|
);
|
|
|
|
|
|
|
|
$expectedEntries = [
|
|
|
|
[
|
|
|
|
'name' => 'home',
|
|
|
|
'components' => [ $this->homeComponent ],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'name' => 'nearby',
|
|
|
|
'components' => [ $this->nearbyComponent ],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'name' => 'another_home',
|
|
|
|
'components' => [ $this->homeComponent ],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertEquals( $expectedEntries, $menu->getEntries() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-08 17:08:57 +00:00
|
|
|
* @covers ::insertAfter
|
|
|
|
* @covers ::search
|
2019-07-01 18:06:01 +00:00
|
|
|
* @covers \MediaWiki\Minerva\Menu\Entries\MenuEntry::addComponent
|
2017-07-12 15:12:40 +00:00
|
|
|
*/
|
|
|
|
public function testInsertAfterWhenTargetEntryDoesntExist() {
|
2019-10-23 18:30:55 +00:00
|
|
|
$menu = new Group( 'p-test' );
|
2019-10-20 02:26:08 +00:00
|
|
|
$this->expectException( DomainException::class );
|
|
|
|
$this->expectExceptionMessage( 'The "home" entry doesn\'t exist.' );
|
2017-07-12 15:12:40 +00:00
|
|
|
$menu->insertAfter( 'home', 'nearby' )
|
|
|
|
->addComponent(
|
|
|
|
$this->nearbyComponent['text'],
|
|
|
|
$this->nearbyComponent['href'],
|
|
|
|
$this->nearbyComponent['class']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-08 17:08:57 +00:00
|
|
|
* @covers ::insertAfter
|
2017-07-12 15:12:40 +00:00
|
|
|
*/
|
|
|
|
public function testInsertAfterWithAnEntryWithAnExistingName() {
|
2019-10-23 18:30:55 +00:00
|
|
|
$menu = new Group( 'p-test' );
|
2017-07-12 15:12:40 +00:00
|
|
|
$menu->insert( 'home' );
|
|
|
|
$menu->insert( 'car' );
|
2019-10-20 02:26:08 +00:00
|
|
|
$this->expectException( DomainException::class );
|
|
|
|
$this->expectExceptionMessage( 'The "car" entry already exists.' );
|
2017-07-12 15:12:40 +00:00
|
|
|
$menu->insertAfter( 'home', 'car' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-08 17:08:57 +00:00
|
|
|
* @covers ::insert
|
2017-07-12 15:12:40 +00:00
|
|
|
*/
|
|
|
|
public function testInsertingAnEntryWithAnExistingName() {
|
2019-10-23 18:30:55 +00:00
|
|
|
$menu = new Group( 'p-test' );
|
2017-07-12 15:12:40 +00:00
|
|
|
$menu->insert( 'home' );
|
2019-10-20 02:26:08 +00:00
|
|
|
$this->expectException( DomainException::class );
|
|
|
|
$this->expectExceptionMessage( 'The "home" entry already exists.' );
|
2017-07-12 15:12:40 +00:00
|
|
|
$menu->insert( 'home' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-08 17:08:57 +00:00
|
|
|
* @covers ::insert
|
|
|
|
* @covers ::insertAfter
|
2017-07-12 15:12:40 +00:00
|
|
|
*/
|
|
|
|
public function testInsertingAnEntryAfterAnotherOne() {
|
2019-10-23 18:30:55 +00:00
|
|
|
$menu = new Group( 'p-test' );
|
2017-07-12 15:12:40 +00:00
|
|
|
$menu->insert( 'first' );
|
|
|
|
$menu->insert( 'last' );
|
|
|
|
$menu->insertAfter( 'first', 'middle' );
|
|
|
|
$items = $menu->getEntries();
|
|
|
|
$this->assertCount( 3, $items );
|
|
|
|
$this->assertSame( 'first', $items[0]['name'] );
|
|
|
|
$this->assertSame( 'middle', $items[1]['name'] );
|
|
|
|
$this->assertSame( 'last', $items[2]['name'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-08 17:08:57 +00:00
|
|
|
* @covers ::insert
|
|
|
|
* @covers ::getEntries
|
2019-07-01 18:06:01 +00:00
|
|
|
* @covers \MediaWiki\Minerva\Menu\Entries\MenuEntry::addComponent
|
2017-07-12 15:12:40 +00:00
|
|
|
*/
|
|
|
|
public function testinsertingAnEntryWithMultipleComponents() {
|
|
|
|
$authLoginComponent = [
|
|
|
|
'text' => 'Phuedx (WMF)',
|
|
|
|
'href' => '/wiki/User:Phuedx_(WMF)',
|
|
|
|
'class' =>
|
|
|
|
'mw-ui-icon mw-ui-icon-before mw-ui-icon-profile truncated-text primary-action',
|
2021-09-09 15:56:42 +00:00
|
|
|
'icon' => null,
|
2017-07-12 15:12:40 +00:00
|
|
|
];
|
|
|
|
$authLogoutComponent = [
|
|
|
|
'text' => 'Logout',
|
|
|
|
'href' => '/wiki/Special:UserLogout',
|
|
|
|
'class' =>
|
|
|
|
'mw-ui-icon mw-ui-icon-element secondary-logout secondary-action truncated-text',
|
2021-09-09 15:56:42 +00:00
|
|
|
'icon' => null,
|
2017-07-12 15:12:40 +00:00
|
|
|
];
|
|
|
|
|
2019-10-23 18:30:55 +00:00
|
|
|
$menu = new Group( 'p-test' );
|
2017-07-12 15:12:40 +00:00
|
|
|
$menu->insert( 'auth' )
|
|
|
|
->addComponent(
|
|
|
|
$authLoginComponent['text'],
|
|
|
|
$authLoginComponent['href'],
|
|
|
|
$authLoginComponent['class']
|
|
|
|
)
|
|
|
|
->addComponent(
|
|
|
|
$authLogoutComponent['text'],
|
|
|
|
$authLogoutComponent['href'],
|
|
|
|
$authLogoutComponent['class']
|
|
|
|
);
|
|
|
|
|
|
|
|
$expectedEntries = [
|
|
|
|
[
|
|
|
|
'name' => 'auth',
|
|
|
|
'components' => [
|
|
|
|
$authLoginComponent,
|
|
|
|
$authLogoutComponent
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertEquals( $expectedEntries, $menu->getEntries() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-08 17:08:57 +00:00
|
|
|
* @covers ::insert
|
|
|
|
* @covers ::getEntries
|
2019-07-01 18:06:01 +00:00
|
|
|
* @covers \MediaWiki\Minerva\Menu\Entries\MenuEntry::addComponent
|
2017-07-12 15:12:40 +00:00
|
|
|
*/
|
|
|
|
public function testInsertingAJavascriptOnlyEntry() {
|
2019-10-23 18:30:55 +00:00
|
|
|
$menu = new Group( 'p-test' );
|
2017-07-12 15:12:40 +00:00
|
|
|
$menu->insert( 'nearby', $isJSOnly = true )
|
|
|
|
->addComponent(
|
|
|
|
$this->nearbyComponent['text'],
|
|
|
|
$this->nearbyComponent['href'],
|
|
|
|
$this->nearbyComponent['class']
|
|
|
|
);
|
|
|
|
|
|
|
|
$expectedEntries = [
|
|
|
|
[
|
|
|
|
'name' => 'nearby',
|
|
|
|
'components' => [ $this->nearbyComponent ],
|
|
|
|
'class' => 'jsonly'
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertEquals( $expectedEntries, $menu->getEntries() );
|
|
|
|
}
|
|
|
|
|
2019-05-29 17:00:22 +00:00
|
|
|
/**
|
|
|
|
* @covers ::getEntryByName
|
|
|
|
* @covers ::search
|
|
|
|
*/
|
|
|
|
public function testGetEntryByName() {
|
2019-10-23 18:30:55 +00:00
|
|
|
$menu = new Group( 'p-test' );
|
2019-05-29 17:00:22 +00:00
|
|
|
$menu->insert( 'home' )
|
|
|
|
->addComponent(
|
|
|
|
$this->homeComponent['text'],
|
|
|
|
$this->homeComponent['href']
|
|
|
|
);
|
|
|
|
$this->assertInstanceOf( IMenuEntry::class, $menu->getEntryByName( 'home' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::getEntryByName
|
|
|
|
* @covers ::search
|
|
|
|
*/
|
|
|
|
public function testGetEntryByNameException() {
|
2019-10-23 18:30:55 +00:00
|
|
|
$menu = new Group( 'p-test' );
|
2019-10-20 02:26:08 +00:00
|
|
|
$this->expectException( DomainException::class );
|
2019-05-29 17:00:22 +00:00
|
|
|
$menu->getEntryByName( 'home' );
|
|
|
|
}
|
|
|
|
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|