mediawiki-skins-MinervaNeue/tests/phpunit/skins/SkinMinervaTest.php

376 lines
11 KiB
PHP
Raw Normal View History

<?php
namespace MediaWiki\Minerva;
use MediaWiki\Minerva\Skins\SkinMinerva;
use MediaWiki\Title\Title;
use MediaWikiIntegrationTestCase;
use OutputPage;
use RequestContext;
use Wikimedia\TestingAccessWrapper;
/**
* @coversDefaultClass \MediaWiki\Minerva\Skins\SkinMinerva
* @group MinervaNeue
*/
class SkinMinervaTest extends MediaWikiIntegrationTestCase {
Echo uses Button template Changes * Update FIXME in userMenu.less to merge selectors * We replace minerva-user-notifications class with minerva-notifications to short-circuit Echo's code so that it no longer replaces the Minerva notification badge with its own. * We update resources/skins.minerva.scripts/initMobile.js to introduce our own wire up code - this is responsible for opening Echo overlay and reseting the counter. The code in Echo will be removed in a follow up (see <I2f923e509d24524a2375ffbe6b3ef336487574bb>) * We update skinStyles/ext.echo.styles.badge.less with styles from Vector 2022 so that Minerva desktop remains consistent with desktop Vector 2022 experience. * We clearly mark technical debt relating to the special mobile version. Testing: * Pixel.js has a group echo that covers all the different variants. Make sure to update to latest main branch before running these. * Desktop should behave the same for Minerva as Echo. * On mobile only when a user has unseen notifications a red circle is shown. Otherwise a bell icon is shown, never with number. * On mobile a single button is visible that combines alert and count numbers. * With Echo disabled a bell shows that links to the user talk page Visual changes: * Previously the red circle became a transparent/gray circle on click. Now it will always be red. * Minor aligment changes to red circle and bell icon are expected as the change prevents MobileFrontend/Echo updating the icon to use Codex. Bug: T342907 Change-Id: I55c18cf723a32f80b93a01dd0687e005162c4e93
2023-08-01 00:12:59 +00:00
private const ATTRIBUTE_NOTIFICATION_HREF = [
'key' => 'href',
'value' => '/wiki/Special:Notifications',
];
private const ATTRIBUTE_NOTIFICATION_DATA_EVENT_NAME = [
'key' => 'data-event-name',
'value' => 'ui.notifications',
];
private const ATTRIBUTE_NOTIFICATION_DATA_COUNTER_TEXT = [
'key' => 'data-counter-text',
'value' => "13",
];
private const ATTRIBUTE_NOTIFICATION_DATA_COUNTER_NUM = [
'key' => 'data-counter-num',
'value' => 13,
];
private const ATTRIBUTE_NOTIFICATION_TITLE = [
'key' => 'title',
'value' => "Your alerts",
];
/**
* @param array $options
*/
private function overrideSkinOptions( $options ) {
$mockOptions = new SkinOptions();
$mockOptions->setMultiple( $options );
$this->setService( 'Minerva.SkinOptions', $mockOptions );
}
public static function provideHasPageActions() {
return [
[ NS_MAIN, 'test', 'view', true ],
[ NS_SPECIAL, 'test', 'view', false ],
[ NS_MAIN, 'Main Page', 'view', false ],
[ NS_MAIN, 'test', 'history', false ]
];
}
/**
* @dataProvider provideHasPageActions
* @covers ::hasPageActions
*/
public function testHasPageActions( int $namespace, string $title, string $action, bool $expected ) {
$context = new RequestContext();
$context->setTitle( Title::makeTitle( $namespace, $title ) );
$context->setActionName( $action );
$skin = new SkinMinerva();
$skin->setContext( $context );
$this->assertEquals( $expected, TestingAccessWrapper::newFromObject( $skin )->hasPageActions() );
}
public static function provideHasPageTabs() {
return [
[ [ SkinOptions::TABS_ON_SPECIALS => false ], NS_MAIN, 'test', 'view', true ],
[ [ SkinOptions::TABS_ON_SPECIALS => false ], NS_MAIN, 'Main Page', 'view', false ],
[ [ SkinOptions::TABS_ON_SPECIALS => false ], NS_TALK, 'Main Page', 'view', false ],
[ [ SkinOptions::TALK_AT_TOP => false ], NS_MAIN, 'test', 'view', false ],
[ [ SkinOptions::TALK_AT_TOP => false ], NS_SPECIAL, 'test', 'view', true ],
[ [ SkinOptions::TALK_AT_TOP => false ], NS_MAIN, 'test', 'history', true ],
];
}
/**
* @dataProvider provideHasPageTabs
* @covers ::hasPageTabs
*/
public function testHasPageTabs( array $options, int $namespace, string $title, string $action, bool $expected ) {
// both tabs on specials and talk at top default to true
$this->overrideSkinOptions( $options );
$context = new RequestContext();
$context->setTitle( Title::makeTitle( $namespace, $title ) );
$context->setActionName( $action );
// hasPageTabs gets the action directly from the request rather than the context so we set it here as well
$context->getRequest()->setVal( 'action', $action );
$skin = new SkinMinerva();
$skin->setContext( $context );
$this->assertEquals( $expected, TestingAccessWrapper::newFromObject( $skin )->hasPageTabs() );
}
/**
* @covers ::getTabsData
*/
public function testGetTabsData() {
$context = new RequestContext();
$context->setTitle( Title::makeTitle( NS_MAIN, 'test' ) );
$skin = new SkinMinerva();
$skin->setContext( $context );
$contentNavigationUrls = [ 'associated-pages' => [ 'testkey' => 'testvalue' ] ];
$associatedPages = [ 'id' => 'test' ];
$data = TestingAccessWrapper::newFromObject( $skin )->getTabsData( $contentNavigationUrls, $associatedPages );
$this->assertEquals( [ 'items' => [ 'testvalue' ], 'id' => 'test' ], $data );
}
/**
* @covers ::getTabsData when hasPageTabs is false
*/
public function testGetTabsDataNoPageTabs() {
$context = new RequestContext();
$context->setTitle( Title::makeTitle( NS_MAIN, 'Main Page' ) );
$skin = new SkinMinerva();
$skin->setContext( $context );
$contentNavigationUrls = [ 'associated-pages' => [ 'testkey' => 'testvalue' ] ];
$associatedPages = [ 'id' => 'test' ];
$data = TestingAccessWrapper::newFromObject( $skin )->getTabsData( $contentNavigationUrls, $associatedPages );
$this->assertEquals( [], $data );
}
/**
* @covers ::getTabsData when contentNavigationUrls is empty
*/
public function testGetTabsDataNoContentNavigationUrls() {
$context = new RequestContext();
$context->setTitle( Title::makeTitle( NS_MAIN, 'test' ) );
$skin = new SkinMinerva();
$skin->setContext( $context );
$contentNavigationUrls = [];
$associatedPages = [ 'id' => 'test' ];
$data = TestingAccessWrapper::newFromObject( $skin )->getTabsData( $contentNavigationUrls, $associatedPages );
$this->assertEquals( [], $data );
}
/**
* @covers ::getTabsData when associatedPages has no id
*/
public function testGetTabsDataNoId() {
$context = new RequestContext();
$context->setTitle( Title::makeTitle( NS_MAIN, 'test' ) );
$skin = new SkinMinerva();
$skin->setContext( $context );
$contentNavigationUrls = [ 'associated-pages' => [ 'testkey' => 'testvalue' ] ];
$associatedPages = [];
$data = TestingAccessWrapper::newFromObject( $skin )->getTabsData( $contentNavigationUrls, $associatedPages );
$this->assertEquals( [ 'items' => [ 'testvalue' ], 'id' => null ], $data );
}
/**
* @covers ::setContext
* @covers ::hasCategoryLinks
*/
public function testHasCategoryLinksWhenOptionIsOff() {
$outputPage = $this->getMockBuilder( OutputPage::class )
->disableOriginalConstructor()
->getMock();
$outputPage->expects( $this->never() )
->method( 'getCategoryLinks' );
$this->overrideSkinOptions( [ SkinOptions::CATEGORIES => false ] );
$context = new RequestContext();
$context->setTitle( Title::makeTitle( NS_MAIN, 'Test' ) );
$context->setOutput( $outputPage );
$skin = new SkinMinerva();
$skin->setContext( $context );
$skin = TestingAccessWrapper::newFromObject( $skin );
$this->assertFalse( $skin->hasCategoryLinks() );
}
/**
* @dataProvider provideHasCategoryLinks
* @param array $categoryLinks
* @param bool $expected
* @covers ::setContext
* @covers ::hasCategoryLinks
*/
public function testHasCategoryLinks( array $categoryLinks, $expected ) {
$outputPage = $this->getMockBuilder( OutputPage::class )
->disableOriginalConstructor()
->getMock();
$outputPage->expects( $this->once() )
->method( 'getCategoryLinks' )
->willReturn( $categoryLinks );
$this->overrideSkinOptions( [ SkinOptions::CATEGORIES => true ] );
$context = new RequestContext();
$context->setTitle( Title::makeTitle( NS_MAIN, 'Test' ) );
$context->setOutput( $outputPage );
$skin = new SkinMinerva();
$skin->setContext( $context );
$skin = TestingAccessWrapper::newFromObject( $skin );
$this->assertEquals( $expected, $skin->hasCategoryLinks() );
}
public static function provideHasCategoryLinks() {
return [
[ [], false ],
[
[
'normal' => '<ul><li><a href="/wiki/Category:1">1</a></li></ul>'
],
true
],
[
[
'hidden' => '<ul><li><a href="/wiki/Category:Hidden">Hidden</a></li></ul>'
],
true
],
[
[
'normal' => '<ul><li><a href="/wiki/Category:1">1</a></li></ul>',
'hidden' => '<ul><li><a href="/wiki/Category:Hidden">Hidden</a></li></ul>'
],
true
],
[
[
'unexpected' => '<ul><li><a href="/wiki/Category:1">1</a></li></ul>'
],
false
],
];
}
Echo uses Button template Changes * Update FIXME in userMenu.less to merge selectors * We replace minerva-user-notifications class with minerva-notifications to short-circuit Echo's code so that it no longer replaces the Minerva notification badge with its own. * We update resources/skins.minerva.scripts/initMobile.js to introduce our own wire up code - this is responsible for opening Echo overlay and reseting the counter. The code in Echo will be removed in a follow up (see <I2f923e509d24524a2375ffbe6b3ef336487574bb>) * We update skinStyles/ext.echo.styles.badge.less with styles from Vector 2022 so that Minerva desktop remains consistent with desktop Vector 2022 experience. * We clearly mark technical debt relating to the special mobile version. Testing: * Pixel.js has a group echo that covers all the different variants. Make sure to update to latest main branch before running these. * Desktop should behave the same for Minerva as Echo. * On mobile only when a user has unseen notifications a red circle is shown. Otherwise a bell icon is shown, never with number. * On mobile a single button is visible that combines alert and count numbers. * With Echo disabled a bell shows that links to the user talk page Visual changes: * Previously the red circle became a transparent/gray circle on click. Now it will always be red. * Minor aligment changes to red circle and bell icon are expected as the change prevents MobileFrontend/Echo updating the icon to use Codex. Bug: T342907 Change-Id: I55c18cf723a32f80b93a01dd0687e005162c4e93
2023-08-01 00:12:59 +00:00
public static function provideGetNotificationButtons() {
return [
[
[],
[]
],
//
// CIRCLE
//
[
[
'tag-name' => 'a',
'classes' => 'mw-echo-notifications-badge mw-echo-notification-badge-nojs '
. ' mw-echo-unseen-notifications',
'array-attributes' => [
self::ATTRIBUTE_NOTIFICATION_HREF,
self::ATTRIBUTE_NOTIFICATION_DATA_COUNTER_TEXT,
self::ATTRIBUTE_NOTIFICATION_DATA_COUNTER_NUM,
self::ATTRIBUTE_NOTIFICATION_TITLE,
[
'key' => 'id',
'value' => 'pt-notifications-alert',
],
],
'data-icon' => [
'icon' => 'circle'
],
'label' => 'Alerts (13)',
],
[
[
'name' => 'notifications-alert',
'id' => 'pt-notifications-alert',
'class' => 'notification-count notification-unseen mw-echo-unseen-notifications mw-list-item',
'array-links' => [
[
'icon' => 'circle',
'array-attributes' => [
self::ATTRIBUTE_NOTIFICATION_HREF,
self::ATTRIBUTE_NOTIFICATION_DATA_COUNTER_TEXT,
self::ATTRIBUTE_NOTIFICATION_DATA_COUNTER_NUM,
self::ATTRIBUTE_NOTIFICATION_TITLE,
[
'key' => 'class',
'value' => 'mw-echo-notifications-badge '
. 'mw-echo-notification-badge-nojs oo-ui-icon-bellOutline '
. 'mw-echo-unseen-notifications',
],
],
'text' => 'Alerts (13)'
]
]
]
]
],
//
// BELL
//
[
[
'tag-name' => 'a',
'classes' => 'mw-echo-notifications-badge mw-echo-notification-badge-nojs',
'array-attributes' => [
self::ATTRIBUTE_NOTIFICATION_HREF,
self::ATTRIBUTE_NOTIFICATION_DATA_COUNTER_TEXT,
self::ATTRIBUTE_NOTIFICATION_DATA_COUNTER_NUM,
self::ATTRIBUTE_NOTIFICATION_TITLE,
[
'key' => 'id',
'value' => 'pt-notifications-alert',
],
],
'data-icon' => [
'icon' => 'bellOutline-base20'
Echo uses Button template Changes * Update FIXME in userMenu.less to merge selectors * We replace minerva-user-notifications class with minerva-notifications to short-circuit Echo's code so that it no longer replaces the Minerva notification badge with its own. * We update resources/skins.minerva.scripts/initMobile.js to introduce our own wire up code - this is responsible for opening Echo overlay and reseting the counter. The code in Echo will be removed in a follow up (see <I2f923e509d24524a2375ffbe6b3ef336487574bb>) * We update skinStyles/ext.echo.styles.badge.less with styles from Vector 2022 so that Minerva desktop remains consistent with desktop Vector 2022 experience. * We clearly mark technical debt relating to the special mobile version. Testing: * Pixel.js has a group echo that covers all the different variants. Make sure to update to latest main branch before running these. * Desktop should behave the same for Minerva as Echo. * On mobile only when a user has unseen notifications a red circle is shown. Otherwise a bell icon is shown, never with number. * On mobile a single button is visible that combines alert and count numbers. * With Echo disabled a bell shows that links to the user talk page Visual changes: * Previously the red circle became a transparent/gray circle on click. Now it will always be red. * Minor aligment changes to red circle and bell icon are expected as the change prevents MobileFrontend/Echo updating the icon to use Codex. Bug: T342907 Change-Id: I55c18cf723a32f80b93a01dd0687e005162c4e93
2023-08-01 00:12:59 +00:00
],
'label' => 'Alerts (13)',
],
[
[
'html-item' => 'n/a',
'name' => 'notifications-alert',
'html' => 'HTML',
'id' => 'pt-notifications-alert',
'class' => 'mw-list-item',
'array-links' => [
[
'icon' => 'bellOutline-base20',
Echo uses Button template Changes * Update FIXME in userMenu.less to merge selectors * We replace minerva-user-notifications class with minerva-notifications to short-circuit Echo's code so that it no longer replaces the Minerva notification badge with its own. * We update resources/skins.minerva.scripts/initMobile.js to introduce our own wire up code - this is responsible for opening Echo overlay and reseting the counter. The code in Echo will be removed in a follow up (see <I2f923e509d24524a2375ffbe6b3ef336487574bb>) * We update skinStyles/ext.echo.styles.badge.less with styles from Vector 2022 so that Minerva desktop remains consistent with desktop Vector 2022 experience. * We clearly mark technical debt relating to the special mobile version. Testing: * Pixel.js has a group echo that covers all the different variants. Make sure to update to latest main branch before running these. * Desktop should behave the same for Minerva as Echo. * On mobile only when a user has unseen notifications a red circle is shown. Otherwise a bell icon is shown, never with number. * On mobile a single button is visible that combines alert and count numbers. * With Echo disabled a bell shows that links to the user talk page Visual changes: * Previously the red circle became a transparent/gray circle on click. Now it will always be red. * Minor aligment changes to red circle and bell icon are expected as the change prevents MobileFrontend/Echo updating the icon to use Codex. Bug: T342907 Change-Id: I55c18cf723a32f80b93a01dd0687e005162c4e93
2023-08-01 00:12:59 +00:00
'array-attributes' => [
self::ATTRIBUTE_NOTIFICATION_HREF,
self::ATTRIBUTE_NOTIFICATION_DATA_COUNTER_TEXT,
self::ATTRIBUTE_NOTIFICATION_DATA_COUNTER_NUM,
self::ATTRIBUTE_NOTIFICATION_TITLE,
[
'key' => 'class',
'value' => 'mw-echo-notifications-badge mw-echo-notification-badge-nojs',
],
],
'text' => 'Alerts (13)'
]
]
]
]
]
];
}
/**
* @dataProvider provideGetNotificationButtons
* @param array $expected
* @param array $from
* @covers ::getNotificationButtons
*/
public function testGetNotificationButtons( $expected, $from ) {
$btns = SkinMinerva::getNotificationButtons( $from );
$this->assertEquals( $expected['classes'] ?? '', $btns[0]['classes'] ?? '' );
$this->assertEquals( $expected['data-attributes'] ?? [], $btns[0]['data-attributes'] ?? [] );
$this->assertEquals( $expected['data-icon'] ?? [], $btns[0]['data-icon'] ?? [] );
$this->assertEquals( $expected['data-label'] ?? '', $btns[0]['data-label'] ?? '' );
}
}