2017-07-12 15:12:40 +00:00
|
|
|
<?php
|
|
|
|
|
2022-05-03 18:11:54 +00:00
|
|
|
namespace 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;
|
2022-06-21 16:20:53 +00:00
|
|
|
use MediaWiki\Minerva\Menu\Entries\SingleMenuEntry;
|
2022-05-03 18:11:54 +00:00
|
|
|
use MediaWikiIntegrationTestCase;
|
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
|
|
|
*/
|
2022-05-03 18:11:54 +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',
|
2022-06-21 16:20:53 +00:00
|
|
|
'data-event-name' => 'menu.home',
|
|
|
|
'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
|
2022-06-21 16:20:53 +00:00
|
|
|
* @covers ::hasEntries
|
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() );
|
2022-06-21 16:20:53 +00:00
|
|
|
$this->assertFalse( $menu->hasEntries() );
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-06-21 16:20:53 +00:00
|
|
|
* @covers ::insertEntry
|
2019-04-08 17:08:57 +00:00
|
|
|
* @covers ::search
|
|
|
|
* @covers ::getEntries
|
2022-06-21 16:20:53 +00:00
|
|
|
* @covers ::hasEntries
|
2017-07-12 15:12:40 +00:00
|
|
|
*/
|
|
|
|
public function testInsertingAnEntry() {
|
2019-10-23 18:30:55 +00:00
|
|
|
$menu = new Group( 'p-test' );
|
2022-06-21 16:20:53 +00:00
|
|
|
$entry = SingleMenuEntry::create(
|
|
|
|
'home',
|
|
|
|
$this->homeComponent['text'],
|
|
|
|
$this->homeComponent['href'],
|
|
|
|
$this->homeComponent['class'],
|
|
|
|
$this->homeComponent['icon'],
|
|
|
|
true
|
|
|
|
);
|
|
|
|
$menu->insertEntry( $entry );
|
2017-07-12 15:12:40 +00:00
|
|
|
|
|
|
|
$expectedEntries = [
|
|
|
|
[
|
|
|
|
'name' => 'home',
|
2022-06-21 16:20:53 +00:00
|
|
|
'components' => [
|
|
|
|
[
|
|
|
|
'text' => $this->homeComponent['text'],
|
|
|
|
'href' => $this->homeComponent['href'],
|
|
|
|
'class' => 'mw-ui-icon mw-ui-icon-before mw-ui-icon-home menu__item--home',
|
|
|
|
'icon' => 'minerva-home',
|
|
|
|
'data-event-name' => 'menu.home'
|
|
|
|
]
|
|
|
|
],
|
2017-07-12 15:12:40 +00:00
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertEquals( $expectedEntries, $menu->getEntries() );
|
2022-06-21 16:20:53 +00:00
|
|
|
$this->assertTrue( $menu->hasEntries() );
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-06-21 16:20:53 +00:00
|
|
|
* @covers ::insertEntry
|
2019-04-08 17:08:57 +00:00
|
|
|
* @covers ::search
|
|
|
|
* @covers ::getEntries
|
2022-06-21 16:20:53 +00:00
|
|
|
* @covers ::insertAfter
|
2017-07-12 15:12:40 +00:00
|
|
|
*/
|
|
|
|
public function testInsertingAnEntryAfterAnother() {
|
2019-10-23 18:30:55 +00:00
|
|
|
$menu = new Group( 'p-test' );
|
2022-06-21 16:20:53 +00:00
|
|
|
$entryHome = SingleMenuEntry::create(
|
|
|
|
'home',
|
|
|
|
$this->homeComponent['text'],
|
|
|
|
$this->homeComponent['href'],
|
|
|
|
$this->homeComponent['class'],
|
|
|
|
$this->homeComponent['icon'],
|
|
|
|
true
|
|
|
|
);
|
|
|
|
$menu->insertEntry( $entryHome );
|
|
|
|
|
|
|
|
$entryOtherHome = SingleMenuEntry::create(
|
|
|
|
'another_home',
|
|
|
|
$this->homeComponent['text'],
|
|
|
|
$this->homeComponent['href'],
|
|
|
|
$this->homeComponent['class'],
|
|
|
|
$this->homeComponent['icon'],
|
|
|
|
true
|
|
|
|
);
|
|
|
|
$menu->insertEntry( $entryOtherHome );
|
|
|
|
$menu->insertAfter(
|
|
|
|
'home',
|
|
|
|
'nearby',
|
|
|
|
$this->nearbyComponent['text'],
|
|
|
|
$this->nearbyComponent['href'],
|
|
|
|
$this->nearbyComponent['class'],
|
|
|
|
$this->nearbyComponent['icon']
|
|
|
|
);
|
2017-07-12 15:12:40 +00:00
|
|
|
|
|
|
|
$expectedEntries = [
|
|
|
|
[
|
|
|
|
'name' => 'home',
|
2022-06-21 16:20:53 +00:00
|
|
|
'components' => [
|
|
|
|
[
|
|
|
|
'text' => $this->homeComponent['text'],
|
|
|
|
'href' => $this->homeComponent['href'],
|
|
|
|
'class' => 'mw-ui-icon mw-ui-icon-before mw-ui-icon-home menu__item--home',
|
|
|
|
'icon' => 'minerva-home',
|
|
|
|
'data-event-name' => 'menu.home'
|
|
|
|
]
|
|
|
|
],
|
2017-07-12 15:12:40 +00:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'name' => 'nearby',
|
2022-06-21 16:20:53 +00:00
|
|
|
'components' => [
|
|
|
|
[
|
|
|
|
'text' => $this->nearbyComponent['text'],
|
|
|
|
'href' => $this->nearbyComponent['href'],
|
|
|
|
'class' => 'mw-ui-icon mw-ui-icon-before mw-ui-icon-nearby menu__item--nearby',
|
|
|
|
'icon' => 'minerva-nearby'
|
|
|
|
]
|
|
|
|
],
|
2017-07-12 15:12:40 +00:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'name' => 'another_home',
|
2022-06-21 16:20:53 +00:00
|
|
|
'components' => [
|
|
|
|
[
|
|
|
|
'text' => $this->homeComponent['text'],
|
|
|
|
'href' => $this->homeComponent['href'],
|
|
|
|
'class' => 'mw-ui-icon mw-ui-icon-before mw-ui-icon-home menu__item--another_home',
|
|
|
|
'icon' => 'minerva-another_home',
|
|
|
|
'data-event-name' => 'menu.another_home'
|
|
|
|
]
|
|
|
|
],
|
2017-07-12 15:12:40 +00:00
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertEquals( $expectedEntries, $menu->getEntries() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-08 17:08:57 +00:00
|
|
|
* @covers ::insertAfter
|
|
|
|
* @covers ::search
|
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.' );
|
2022-06-21 16:20:53 +00:00
|
|
|
$menu->insertAfter(
|
|
|
|
'home',
|
|
|
|
'nearby',
|
2017-07-12 15:12:40 +00:00
|
|
|
$this->nearbyComponent['text'],
|
|
|
|
$this->nearbyComponent['href'],
|
2022-06-21 16:20:53 +00:00
|
|
|
$this->nearbyComponent['class'],
|
|
|
|
$this->nearbyComponent['icon']
|
2017-07-12 15:12:40 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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' );
|
2022-06-21 16:20:53 +00:00
|
|
|
$entryHome = SingleMenuEntry::create(
|
|
|
|
'home',
|
|
|
|
$this->homeComponent['text'],
|
|
|
|
$this->homeComponent['href'],
|
|
|
|
$this->homeComponent['class']
|
|
|
|
);
|
|
|
|
$menu->insertEntry( $entryHome );
|
|
|
|
$entryCar = SingleMenuEntry::create(
|
|
|
|
'car',
|
|
|
|
$this->homeComponent['text'],
|
|
|
|
$this->homeComponent['href'],
|
|
|
|
$this->homeComponent['class']
|
|
|
|
);
|
|
|
|
$menu->insertEntry( $entryCar );
|
2019-10-20 02:26:08 +00:00
|
|
|
$this->expectException( DomainException::class );
|
|
|
|
$this->expectExceptionMessage( 'The "car" entry already exists.' );
|
2022-06-21 16:20:53 +00:00
|
|
|
$menu->insertAfter(
|
|
|
|
'home',
|
|
|
|
'car',
|
|
|
|
$this->homeComponent['text'],
|
|
|
|
$this->homeComponent['href'],
|
|
|
|
$this->homeComponent['class']
|
|
|
|
);
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-06-21 16:20:53 +00:00
|
|
|
* @covers ::insertEntry
|
2017-07-12 15:12:40 +00:00
|
|
|
*/
|
|
|
|
public function testInsertingAnEntryWithAnExistingName() {
|
2019-10-23 18:30:55 +00:00
|
|
|
$menu = new Group( 'p-test' );
|
2022-06-21 16:20:53 +00:00
|
|
|
$entryHome = SingleMenuEntry::create(
|
|
|
|
'home',
|
|
|
|
$this->homeComponent['text'],
|
|
|
|
$this->homeComponent['href'],
|
|
|
|
$this->homeComponent['class']
|
|
|
|
);
|
|
|
|
$menu->insertEntry( $entryHome );
|
2019-10-20 02:26:08 +00:00
|
|
|
$this->expectException( DomainException::class );
|
|
|
|
$this->expectExceptionMessage( 'The "home" entry already exists.' );
|
2022-06-21 16:20:53 +00:00
|
|
|
$menu->insertEntry( $entryHome );
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-06-21 16:20:53 +00:00
|
|
|
* @covers ::insertEntry
|
2019-04-08 17:08:57 +00:00
|
|
|
* @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' );
|
2022-06-21 16:20:53 +00:00
|
|
|
$entryFirst = SingleMenuEntry::create(
|
|
|
|
'first',
|
|
|
|
$this->homeComponent['text'],
|
|
|
|
$this->homeComponent['href'],
|
|
|
|
$this->homeComponent['class']
|
|
|
|
);
|
|
|
|
$menu->insertEntry( $entryFirst );
|
|
|
|
|
|
|
|
$entryLast = SingleMenuEntry::create(
|
|
|
|
'last',
|
|
|
|
$this->homeComponent['text'],
|
|
|
|
$this->homeComponent['href'],
|
|
|
|
$this->homeComponent['class']
|
|
|
|
);
|
|
|
|
$menu->insertEntry( $entryLast );
|
|
|
|
$menu->insertAfter(
|
|
|
|
'first',
|
|
|
|
'middle',
|
|
|
|
$this->nearbyComponent['text'],
|
|
|
|
$this->nearbyComponent['href'],
|
|
|
|
$this->nearbyComponent['class'],
|
|
|
|
$this->nearbyComponent['icon']
|
|
|
|
);
|
2017-07-12 15:12:40 +00:00
|
|
|
$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'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-06-21 16:20:53 +00:00
|
|
|
* @covers ::insertEntry
|
2019-04-08 17:08:57 +00:00
|
|
|
* @covers ::getEntries
|
2017-07-12 15:12:40 +00:00
|
|
|
*/
|
|
|
|
public function testInsertingAJavascriptOnlyEntry() {
|
2019-10-23 18:30:55 +00:00
|
|
|
$menu = new Group( 'p-test' );
|
2022-06-21 16:20:53 +00:00
|
|
|
$entryHome = SingleMenuEntry::create(
|
|
|
|
'nearby',
|
|
|
|
$this->nearbyComponent['text'],
|
|
|
|
$this->nearbyComponent['href'],
|
|
|
|
$this->nearbyComponent['class']
|
|
|
|
);
|
|
|
|
$entryHome->setJSOnly();
|
|
|
|
$menu->insertEntry( $entryHome );
|
2017-07-12 15:12:40 +00:00
|
|
|
|
|
|
|
$expectedEntries = [
|
|
|
|
[
|
|
|
|
'name' => 'nearby',
|
2022-06-21 16:20:53 +00:00
|
|
|
'components' => [
|
|
|
|
[
|
|
|
|
'text' => $this->nearbyComponent['text'],
|
|
|
|
'href' => $this->nearbyComponent['href'],
|
|
|
|
'class' => 'mw-ui-icon mw-ui-icon-before mw-ui-icon-nearby menu__item--nearby',
|
|
|
|
'icon' => 'minerva-nearby'
|
|
|
|
]
|
|
|
|
],
|
2017-07-12 15:12:40 +00:00
|
|
|
'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' );
|
2022-06-21 16:20:53 +00:00
|
|
|
$entryHome = SingleMenuEntry::create(
|
|
|
|
'home',
|
|
|
|
$this->homeComponent['text'],
|
|
|
|
$this->homeComponent['href'],
|
|
|
|
$this->homeComponent['class']
|
|
|
|
);
|
|
|
|
$menu->insertEntry( $entryHome );
|
2019-05-29 17:00:22 +00:00
|
|
|
$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
|
|
|
}
|