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',
|
2023-08-08 23:33:28 +00:00
|
|
|
'class' => '',
|
2022-06-21 16:20:53 +00:00
|
|
|
'data-event-name' => 'menu.home',
|
2023-08-01 10:57:48 +00:00
|
|
|
'icon' => 'home'
|
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',
|
2023-08-08 23:33:28 +00:00
|
|
|
'class' => '',
|
2023-08-01 10:57:48 +00:00
|
|
|
'icon' => 'nearby'
|
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
|
|
|
|
2022-11-24 21:51:26 +00:00
|
|
|
$this->assertSame( [], $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' => [
|
|
|
|
[
|
2023-08-08 23:33:28 +00:00
|
|
|
'tag-name' => 'a',
|
|
|
|
'label' => $this->homeComponent['text'],
|
|
|
|
'array-attributes' => [
|
|
|
|
[
|
|
|
|
'key' => 'href',
|
|
|
|
'value' => $this->homeComponent['href'],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'key' => 'data-event-name',
|
|
|
|
'value' => 'menu.home'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'key' => 'data-mw',
|
|
|
|
'value' => 'interface'
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'classes' => 'menu__item--home',
|
2023-08-01 10:57:48 +00:00
|
|
|
'data-icon' => [
|
2023-09-05 17:07:13 +00:00
|
|
|
'icon' => 'home',
|
2023-08-01 10:57:48 +00:00
|
|
|
],
|
2022-06-21 16:20:53 +00:00
|
|
|
]
|
|
|
|
],
|
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
|
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 ::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' => [
|
|
|
|
[
|
2023-08-08 23:33:28 +00:00
|
|
|
'tag-name' => 'a',
|
|
|
|
'label' => $this->nearbyComponent['text'],
|
|
|
|
'array-attributes' => [
|
|
|
|
[
|
|
|
|
'key' => 'href',
|
|
|
|
'value' => $this->nearbyComponent['href'],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'key' => 'data-mw',
|
|
|
|
'value' => 'interface'
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'classes' => 'menu__item--nearby',
|
2023-08-01 10:57:48 +00:00
|
|
|
'data-icon' => [
|
2023-09-05 17:07:13 +00:00
|
|
|
'icon' => 'nearby',
|
2023-08-01 10:57:48 +00:00
|
|
|
]
|
2022-06-21 16:20:53 +00:00
|
|
|
]
|
|
|
|
],
|
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
|
|
|
}
|