2020-01-16 21:11:54 +00:00
|
|
|
<?php
|
|
|
|
namespace MediaWiki\Skins\Vector\Tests\Integration;
|
|
|
|
|
2024-06-10 20:28:24 +00:00
|
|
|
use MediaWiki\Cache\LinkCache;
|
|
|
|
use MediaWiki\Context\RequestContext;
|
2022-05-23 07:32:40 +00:00
|
|
|
use MediaWiki\Skins\Vector\SkinVectorLegacy;
|
2023-08-01 18:13:25 +00:00
|
|
|
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
|
2023-08-19 18:19:12 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-08-01 18:13:25 +00:00
|
|
|
use MediaWiki\User\TalkPageNotificationManager;
|
Add opt-out link to Sidebar for Vector/Logged-in Users Without Abstractions
This commit is singularly focused on adding a link to the sidebar for
Vector, logged-in users. It does the bare minimum to fulfill the
requirements of T243281.
Additionally, it will help to answer the question "Do we need to use
abstractions (other than maybe different templates) to separate Legacy
Vector from Vector" by intentionally leaving out any abstractions in
order to make it easier to compare with a follow-up patch
(Ib2ef15180df73360cc1de25b893e49d415d23e1a) which does use abstractions.
It is a good thing to question whether or not we need addtional
abstractions in VectorTemplate and if they will help us as unnecessary
abstractions can have the opposite effect and just lead to further
frustrations down the road.
Therefore, I urge you, the reviewer, to let me know your thoughts! If
abstractions are viewed as not making our lives any easier, the
follow-up patches may be completely discarded and that's totally okay
with me. :) I think it's a good think to talk about now though.
Important changes:
* The VectorTemplate constructor was changed to allow injecting the
config, templateParser, and isLegacy boolean (only the config was
allowed before this commit). According to MediaWiki's Stable Interface
Policy, "Constructor signatures are generally considered unstable unless
explicitly declared stable for calling" [3]. Given that VecorTemplate's
constructor is not marked as stable, it is justified to do this without
warning according to the policy.
* Due to the above, the 'setTemplate' method is no longer needed and was
marked as deprecated.
* VectorTemplateTest was made to adapt to the new VectorTemplate
constructor. Additionally, it now extends from
MediaWikiIntegrationTestCase which my intelliphense server can pick up.
I *think* MediaWikiTestCase is just an alias to
MediaWikiIntegrationTestCase [1] and MediaWikiTestCase file was renamed
to MediaWikiIntegrationTestCase in [2], but I'm willing to change it
back if there is pushback to this.
Open questions:
* What are VectorTemplate's responsibilities? To me, it acts right now
as a controller (because it echos the full HTML string from the
template), a model (because SkinTemplate::prepareQuickTemplate sets data
on it which it later retrieves through `$this->get()`), a presenter
(because it adds data tailored for a web-centric view), and a view
(because it renders HTML strings instead of letting the view/template be
solely responsible for that). Arguably, some business logic might be
mixed in there as well (because it checks to see if a User is logged
in/has necessary permissions to show x which my changes here add to).
This might not be a problem if we keep VectorTemplate relatively small,
but will it remain this way as we progress further in Desktop
Improvements?
* How do we write tests for VectorTemplate without exposing unnecessary
public methods? For example, if I want to test the `getSkinData()`
method to see what state will be sent to the template, how should I do
this? One option might be to use `TestingAccessWrapper` to expose these
private methods which is what
`VectorTemplateTest::testbuildViewsProps()` does. Another option is to
accept this method as public. Is there a better way? Keep in mind that
even with access to this method, there might be many things to mock.
[1] https://github.com/wikimedia/mediawiki/blob/0030cb525be6cabc1d63de80586b2017d4bbe354/tests/common/TestsAutoLoader.php#L64
[2] Ie717b0ecf4fcfd089d46248f14853c80b7ef4a76
[3] https://www.mediawiki.org/wiki/Stable_interface_policy
Bug: T243281
Change-Id: I0571b041bcd7f19bec9f103fa7bccdd093f6394d
2020-03-17 20:21:33 +00:00
|
|
|
use MediaWikiIntegrationTestCase;
|
2020-01-16 21:11:54 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class VectorTemplateTest
|
|
|
|
* @group Vector
|
|
|
|
* @group Skins
|
|
|
|
*/
|
2020-07-09 16:42:33 +00:00
|
|
|
class SkinVectorTest extends MediaWikiIntegrationTestCase {
|
2023-08-01 18:13:25 +00:00
|
|
|
use MockAuthorityTrait;
|
|
|
|
|
|
|
|
protected function setUp(): void {
|
|
|
|
parent::setUp();
|
|
|
|
// Mock TalkPageNotificationManager to avoid DB queries
|
|
|
|
$this->setService( 'TalkPageNotificationManager', $this->createMock( TalkPageNotificationManager::class ) );
|
|
|
|
$this->clearHooks();
|
|
|
|
}
|
2020-01-16 21:11:54 +00:00
|
|
|
|
|
|
|
/**
|
2022-01-20 22:14:19 +00:00
|
|
|
* @return SkinVectorLegacy
|
2020-01-16 21:11:54 +00:00
|
|
|
*/
|
2024-02-27 19:14:24 +00:00
|
|
|
private function provideVectorTemplateObject() {
|
|
|
|
$skinFactory = $this->getServiceContainer()->getSkinFactory();
|
2022-01-20 22:14:19 +00:00
|
|
|
$template = $skinFactory->makeSkin( 'vector' );
|
|
|
|
return $template;
|
2020-01-16 21:11:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-07 00:51:14 +00:00
|
|
|
* @covers \MediaWiki\Skins\Vector\SkinVectorLegacy::getTemplateData
|
2020-01-16 21:11:54 +00:00
|
|
|
*/
|
2020-09-04 19:18:57 +00:00
|
|
|
public function testGetTemplateData() {
|
2023-08-01 18:13:25 +00:00
|
|
|
$this->setService( 'LinkCache', $this->createMock( LinkCache::class ) );
|
|
|
|
$title = Title::makeTitle( NS_MAIN, 'SkinVector' );
|
|
|
|
$title->resetArticleID( 0 );
|
|
|
|
$context = new RequestContext();
|
|
|
|
$context->setTitle( $title );
|
|
|
|
$context->setAuthority( $this->mockAnonNullAuthority() );
|
2020-05-18 18:55:52 +00:00
|
|
|
$context->setLanguage( 'fr' );
|
2023-08-01 18:13:25 +00:00
|
|
|
$context->setActionName( 'view' );
|
Add opt-out link to Sidebar for Vector/Logged-in Users Without Abstractions
This commit is singularly focused on adding a link to the sidebar for
Vector, logged-in users. It does the bare minimum to fulfill the
requirements of T243281.
Additionally, it will help to answer the question "Do we need to use
abstractions (other than maybe different templates) to separate Legacy
Vector from Vector" by intentionally leaving out any abstractions in
order to make it easier to compare with a follow-up patch
(Ib2ef15180df73360cc1de25b893e49d415d23e1a) which does use abstractions.
It is a good thing to question whether or not we need addtional
abstractions in VectorTemplate and if they will help us as unnecessary
abstractions can have the opposite effect and just lead to further
frustrations down the road.
Therefore, I urge you, the reviewer, to let me know your thoughts! If
abstractions are viewed as not making our lives any easier, the
follow-up patches may be completely discarded and that's totally okay
with me. :) I think it's a good think to talk about now though.
Important changes:
* The VectorTemplate constructor was changed to allow injecting the
config, templateParser, and isLegacy boolean (only the config was
allowed before this commit). According to MediaWiki's Stable Interface
Policy, "Constructor signatures are generally considered unstable unless
explicitly declared stable for calling" [3]. Given that VecorTemplate's
constructor is not marked as stable, it is justified to do this without
warning according to the policy.
* Due to the above, the 'setTemplate' method is no longer needed and was
marked as deprecated.
* VectorTemplateTest was made to adapt to the new VectorTemplate
constructor. Additionally, it now extends from
MediaWikiIntegrationTestCase which my intelliphense server can pick up.
I *think* MediaWikiTestCase is just an alias to
MediaWikiIntegrationTestCase [1] and MediaWikiTestCase file was renamed
to MediaWikiIntegrationTestCase in [2], but I'm willing to change it
back if there is pushback to this.
Open questions:
* What are VectorTemplate's responsibilities? To me, it acts right now
as a controller (because it echos the full HTML string from the
template), a model (because SkinTemplate::prepareQuickTemplate sets data
on it which it later retrieves through `$this->get()`), a presenter
(because it adds data tailored for a web-centric view), and a view
(because it renders HTML strings instead of letting the view/template be
solely responsible for that). Arguably, some business logic might be
mixed in there as well (because it checks to see if a User is logged
in/has necessary permissions to show x which my changes here add to).
This might not be a problem if we keep VectorTemplate relatively small,
but will it remain this way as we progress further in Desktop
Improvements?
* How do we write tests for VectorTemplate without exposing unnecessary
public methods? For example, if I want to test the `getSkinData()`
method to see what state will be sent to the template, how should I do
this? One option might be to use `TestingAccessWrapper` to expose these
private methods which is what
`VectorTemplateTest::testbuildViewsProps()` does. Another option is to
accept this method as public. Is there a better way? Keep in mind that
even with access to this method, there might be many things to mock.
[1] https://github.com/wikimedia/mediawiki/blob/0030cb525be6cabc1d63de80586b2017d4bbe354/tests/common/TestsAutoLoader.php#L64
[2] Ie717b0ecf4fcfd089d46248f14853c80b7ef4a76
[3] https://www.mediawiki.org/wiki/Stable_interface_policy
Bug: T243281
Change-Id: I0571b041bcd7f19bec9f103fa7bccdd093f6394d
2020-03-17 20:21:33 +00:00
|
|
|
$vectorTemplate = $this->provideVectorTemplateObject();
|
2022-07-29 03:55:30 +00:00
|
|
|
$vectorTemplate->setContext( $context );
|
2023-06-14 19:55:23 +00:00
|
|
|
$this->setTemporaryHook( 'SkinTemplateNavigation::Universal',
|
2021-05-03 14:19:12 +00:00
|
|
|
static function ( &$skinTemplate, &$content_navigation ) {
|
2021-01-10 18:22:21 +00:00
|
|
|
$content_navigation['actions'] = [
|
|
|
|
'action-1' => []
|
2020-09-16 23:07:25 +00:00
|
|
|
];
|
2021-01-10 18:22:21 +00:00
|
|
|
$content_navigation['namespaces'] = [
|
|
|
|
'ns-1' => []
|
|
|
|
];
|
|
|
|
$content_navigation['variants'] = [
|
2021-07-28 18:07:42 +00:00
|
|
|
[
|
|
|
|
'class' => 'selected',
|
|
|
|
'text' => 'Language variant',
|
|
|
|
'href' => '/url/to/variant',
|
|
|
|
'lang' => 'zh-hant',
|
|
|
|
'hreflang' => 'zh-hant',
|
|
|
|
]
|
2021-01-10 18:22:21 +00:00
|
|
|
];
|
|
|
|
$content_navigation['views'] = [];
|
2022-09-06 21:48:27 +00:00
|
|
|
$content_navigation['user-menu'] = [
|
|
|
|
'pt-1' => [ 'text' => 'pt1' ],
|
|
|
|
];
|
2020-09-16 23:07:25 +00:00
|
|
|
}
|
2023-06-14 19:55:23 +00:00
|
|
|
);
|
2020-01-16 21:11:54 +00:00
|
|
|
$openVectorTemplate = TestingAccessWrapper::newFromObject( $vectorTemplate );
|
|
|
|
|
2020-09-04 19:18:57 +00:00
|
|
|
$props = $openVectorTemplate->getTemplateData()['data-portlets'];
|
|
|
|
$views = $props['data-views'];
|
|
|
|
$namespaces = $props['data-namespaces'];
|
2020-04-07 22:55:08 +00:00
|
|
|
|
2022-01-28 23:59:19 +00:00
|
|
|
// The mediawiki core specification might change at any time
|
|
|
|
// so let's limit the values we test to those we are aware of.
|
|
|
|
$keysToTest = [
|
|
|
|
'id', 'class', 'html-tooltip', 'html-items',
|
|
|
|
'html-after-portal', 'html-before-portal',
|
|
|
|
'label', 'heading-class', 'is-dropdown'
|
|
|
|
];
|
|
|
|
foreach ( $views as $key => $value ) {
|
|
|
|
if ( !in_array( $key, $keysToTest ) ) {
|
|
|
|
unset( $views[ $key] );
|
|
|
|
}
|
|
|
|
}
|
2020-09-11 15:06:26 +00:00
|
|
|
$this->assertSame(
|
|
|
|
[
|
2020-09-23 22:29:30 +00:00
|
|
|
// Provided by core
|
2020-09-11 15:06:26 +00:00
|
|
|
'id' => 'p-views',
|
2022-06-08 18:29:22 +00:00
|
|
|
'class' => 'mw-portlet mw-portlet-views emptyPortlet ' .
|
2022-05-04 22:20:23 +00:00
|
|
|
'vector-menu-tabs vector-menu-tabs-legacy',
|
2020-09-23 22:29:30 +00:00
|
|
|
'html-tooltip' => '',
|
|
|
|
'html-items' => '',
|
|
|
|
'html-after-portal' => '',
|
2021-06-14 18:47:42 +00:00
|
|
|
'html-before-portal' => '',
|
2020-09-11 15:06:26 +00:00
|
|
|
'label' => $context->msg( 'views' )->text(),
|
2022-03-24 08:05:11 +00:00
|
|
|
'heading-class' => '',
|
2020-09-11 15:06:26 +00:00
|
|
|
'is-dropdown' => false,
|
|
|
|
],
|
2021-06-16 00:23:50 +00:00
|
|
|
$views
|
2020-09-11 15:06:26 +00:00
|
|
|
);
|
2020-04-21 21:35:11 +00:00
|
|
|
|
2020-04-07 22:55:08 +00:00
|
|
|
$variants = $props['data-variants'];
|
2020-09-04 19:18:57 +00:00
|
|
|
$actions = $props['data-actions'];
|
2020-09-11 15:06:26 +00:00
|
|
|
$this->assertSame(
|
2022-05-04 22:20:23 +00:00
|
|
|
'mw-portlet mw-portlet-namespaces vector-menu-tabs vector-menu-tabs-legacy',
|
2020-09-16 23:07:25 +00:00
|
|
|
$namespaces['class']
|
2020-09-11 15:06:26 +00:00
|
|
|
);
|
|
|
|
$this->assertSame(
|
2023-01-10 18:41:49 +00:00
|
|
|
'mw-portlet mw-portlet-variants vector-menu-dropdown',
|
2020-09-11 15:06:26 +00:00
|
|
|
$variants['class']
|
|
|
|
);
|
|
|
|
$this->assertSame(
|
2023-01-10 18:41:49 +00:00
|
|
|
'mw-portlet mw-portlet-cactions vector-menu-dropdown',
|
2020-09-11 15:06:26 +00:00
|
|
|
$actions['class']
|
|
|
|
);
|
|
|
|
$this->assertSame(
|
2022-05-04 22:20:23 +00:00
|
|
|
'mw-portlet mw-portlet-personal vector-user-menu-legacy',
|
2020-09-04 19:18:57 +00:00
|
|
|
$props['data-personal']['class']
|
2020-09-11 15:06:26 +00:00
|
|
|
);
|
2020-01-16 21:11:54 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 22:44:41 +00:00
|
|
|
/**
|
|
|
|
* Standard config for Language Alert in Sidebar
|
|
|
|
* @return array
|
|
|
|
*/
|
2023-05-20 09:01:16 +00:00
|
|
|
private static function enableLanguageInHeaderFeatureConfig(): array {
|
2022-02-18 22:44:41 +00:00
|
|
|
return [
|
|
|
|
'VectorLanguageInHeader' => [
|
|
|
|
'logged_in' => true,
|
|
|
|
'logged_out' => true
|
|
|
|
],
|
|
|
|
'VectorLanguageInMainPageHeader' => [
|
|
|
|
'logged_in' => false,
|
|
|
|
'logged_out' => false
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-05-20 09:01:16 +00:00
|
|
|
public static function providerLanguageAlertRequirements() {
|
2022-02-18 22:44:41 +00:00
|
|
|
$testTitle = Title::makeTitle( NS_MAIN, 'Test' );
|
|
|
|
$testTitleMainPage = Title::makeTitle( NS_MAIN, 'MAIN_PAGE' );
|
|
|
|
return [
|
|
|
|
'When none of the requirements are present, do not show alert' => [
|
|
|
|
// Configuration requirements for language in header and alert in sidebar
|
|
|
|
[],
|
|
|
|
// Title instance
|
|
|
|
$testTitle,
|
|
|
|
// Cached languages
|
|
|
|
[],
|
|
|
|
// Is the language selector at the top of the content?
|
|
|
|
false,
|
|
|
|
// Should the language button be hidden?
|
|
|
|
false,
|
|
|
|
// Expected
|
|
|
|
false
|
|
|
|
],
|
|
|
|
'When the feature is enabled and languages should be hidden, do not show alert' => [
|
2023-05-20 09:01:16 +00:00
|
|
|
self::enableLanguageInHeaderFeatureConfig(),
|
2022-02-18 22:44:41 +00:00
|
|
|
$testTitle,
|
|
|
|
[], true, true, false
|
|
|
|
],
|
|
|
|
'When the language in header feature is disabled, do not show alert' => [
|
|
|
|
[
|
|
|
|
'VectorLanguageInHeader' => [
|
|
|
|
'logged_in' => false,
|
|
|
|
'logged_out' => false
|
|
|
|
],
|
|
|
|
],
|
|
|
|
$testTitle,
|
|
|
|
[ 'fr', 'en', 'ko' ], true, false, false
|
|
|
|
],
|
|
|
|
'When it is a main page, feature is enabled, and there are no languages, do not show alert' => [
|
2023-05-20 09:01:16 +00:00
|
|
|
self::enableLanguageInHeaderFeatureConfig(),
|
2022-02-18 22:44:41 +00:00
|
|
|
$testTitleMainPage,
|
|
|
|
[], true, true, false
|
|
|
|
],
|
|
|
|
'When it is a non-main page, feature is enabled, and there are no languages, do not show alert' => [
|
2023-05-20 09:01:16 +00:00
|
|
|
self::enableLanguageInHeaderFeatureConfig(),
|
2022-02-18 22:44:41 +00:00
|
|
|
$testTitle,
|
|
|
|
[], true, true, false
|
|
|
|
],
|
|
|
|
'When it is a main page, header feature is disabled, and there are languages, do not show alert' => [
|
|
|
|
[
|
|
|
|
'VectorLanguageInHeader' => [
|
|
|
|
'logged_in' => false,
|
|
|
|
'logged_out' => false
|
|
|
|
],
|
|
|
|
],
|
|
|
|
$testTitleMainPage,
|
|
|
|
[ 'fr', 'en', 'ko' ], true, true, false
|
|
|
|
],
|
|
|
|
'When most requirements are present but languages are not at the top, do not show alert' => [
|
2023-05-20 09:01:16 +00:00
|
|
|
self::enableLanguageInHeaderFeatureConfig(),
|
2022-02-18 22:44:41 +00:00
|
|
|
$testTitle,
|
|
|
|
[ 'fr', 'en', 'ko' ], false, false, false
|
|
|
|
],
|
|
|
|
'When most requirements are present but languages should be hidden, do not show alert' => [
|
2023-05-20 09:01:16 +00:00
|
|
|
self::enableLanguageInHeaderFeatureConfig(),
|
2022-02-18 22:44:41 +00:00
|
|
|
$testTitle,
|
|
|
|
[ 'fr', 'en', 'ko' ], true, true, false
|
|
|
|
],
|
|
|
|
'When it is a main page, features are enabled, and there are languages, show alert' => [
|
2023-05-20 09:01:16 +00:00
|
|
|
self::enableLanguageInHeaderFeatureConfig(),
|
2022-02-18 22:44:41 +00:00
|
|
|
$testTitleMainPage,
|
|
|
|
[ 'fr', 'en', 'ko' ], true, false, true
|
|
|
|
],
|
|
|
|
'When all the requirements are present on a non-main page, show alert' => [
|
2023-05-20 09:01:16 +00:00
|
|
|
self::enableLanguageInHeaderFeatureConfig(),
|
2022-02-18 22:44:41 +00:00
|
|
|
$testTitle,
|
|
|
|
[ 'fr', 'en', 'ko' ], true, false, true
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
2020-01-16 21:11:54 +00:00
|
|
|
}
|