| 1 |
<?php |
| 2 |
namespace MediaWiki\Skins\Vector\Tests\Integration; |
| 3 |
|
| 4 |
use MediaWiki\Cache\LinkCache; |
| 5 |
use MediaWiki\Context\RequestContext; |
| 6 |
use MediaWiki\Skins\Vector\SkinVectorLegacy; |
| 7 |
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait; |
| 8 |
use MediaWiki\Title\Title; |
| 9 |
use MediaWiki\User\TalkPageNotificationManager; |
| 10 |
use MediaWikiIntegrationTestCase; |
| 11 |
use Wikimedia\TestingAccessWrapper; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class SkinVectorTest extends MediaWikiIntegrationTestCase { |
| 19 |
use MockAuthorityTrait; |
| 20 |
|
| 21 |
protected function setUp(): void { |
| 22 |
parent::setUp(); |
| 23 |
|
| 24 |
$this->setService( 'TalkPageNotificationManager', $this->createMock( TalkPageNotificationManager::class ) ); |
| 25 |
$this->clearHooks(); |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
private function provideVectorTemplateObject() { |
| 32 |
$skinFactory = $this->getServiceContainer()->getSkinFactory(); |
| 33 |
$template = $skinFactory->makeSkin( 'vector' ); |
| 34 |
return $template; |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
private function expectNodeAttribute( $nodeString, $tag, $attribute, $search ) { |
| 45 |
$node = new \DOMDocument(); |
| 46 |
$node->loadHTML( $nodeString ); |
| 47 |
$element = $node->getElementsByTagName( $tag )->item( 0 ); |
| 48 |
if ( !$element ) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
$values = explode( ' ', $element->getAttribute( $attribute ) ); |
| 53 |
return in_array( $search, $values ); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public function testGetTemplateData() { |
| 60 |
$this->setService( 'LinkCache', $this->createMock( LinkCache::class ) ); |
| 61 |
$title = Title::makeTitle( NS_MAIN, 'SkinVector' ); |
| 62 |
$title->resetArticleID( 0 ); |
| 63 |
$context = new RequestContext(); |
| 64 |
$context->setTitle( $title ); |
| 65 |
$context->setAuthority( $this->mockAnonNullAuthority() ); |
| 66 |
$context->setLanguage( 'fr' ); |
| 67 |
$context->setActionName( 'view' ); |
| 68 |
$vectorTemplate = $this->provideVectorTemplateObject(); |
| 69 |
$vectorTemplate->setContext( $context ); |
| 70 |
$this->setTemporaryHook( 'SkinTemplateNavigation::Universal', |
| 71 |
static function ( &$skinTemplate, &$content_navigation ) { |
| 72 |
$content_navigation['actions'] = [ |
| 73 |
'action-1' => [] |
| 74 |
]; |
| 75 |
$content_navigation['namespaces'] = [ |
| 76 |
'ns-1' => [] |
| 77 |
]; |
| 78 |
$content_navigation['variants'] = [ |
| 79 |
[ |
| 80 |
'class' => 'selected', |
| 81 |
'text' => 'Language variant', |
| 82 |
'href' => '/url/to/variant', |
| 83 |
'lang' => 'zh-hant', |
| 84 |
'hreflang' => 'zh-hant', |
| 85 |
] |
| 86 |
]; |
| 87 |
$content_navigation['views'] = []; |
| 88 |
$content_navigation['user-menu'] = [ |
| 89 |
'pt-1' => [ 'text' => 'pt1' ], |
| 90 |
]; |
| 91 |
} |
| 92 |
); |
| 93 |
$openVectorTemplate = TestingAccessWrapper::newFromObject( $vectorTemplate ); |
| 94 |
|
| 95 |
$props = $openVectorTemplate->getTemplateData()['data-portlets']; |
| 96 |
$views = $props['data-views']; |
| 97 |
$namespaces = $props['data-namespaces']; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
$keysToTest = [ |
| 102 |
'id', 'class', 'html-tooltip', 'html-items', |
| 103 |
'html-after-portal', 'html-before-portal', |
| 104 |
'label', 'heading-class', 'is-dropdown' |
| 105 |
]; |
| 106 |
foreach ( $views as $key => $value ) { |
| 107 |
if ( !in_array( $key, $keysToTest ) ) { |
| 108 |
unset( $views[ $key] ); |
| 109 |
} |
| 110 |
} |
| 111 |
$this->assertSame( |
| 112 |
[ |
| 113 |
|
| 114 |
'id' => 'p-views', |
| 115 |
'class' => 'mw-portlet mw-portlet-views emptyPortlet ' . |
| 116 |
'vector-menu-tabs vector-menu-tabs-legacy', |
| 117 |
'html-tooltip' => '', |
| 118 |
'html-items' => '', |
| 119 |
'html-after-portal' => '', |
| 120 |
'html-before-portal' => '', |
| 121 |
'label' => $context->msg( 'views' )->text(), |
| 122 |
'heading-class' => '', |
| 123 |
'is-dropdown' => false, |
| 124 |
], |
| 125 |
$views |
| 126 |
); |
| 127 |
|
| 128 |
$variants = $props['data-variants']; |
| 129 |
$actions = $props['data-actions']; |
| 130 |
$this->assertSame( |
| 131 |
'mw-portlet mw-portlet-namespaces vector-menu-tabs vector-menu-tabs-legacy', |
| 132 |
$namespaces['class'] |
| 133 |
); |
| 134 |
$this->assertSame( |
| 135 |
'mw-portlet mw-portlet-variants vector-menu-dropdown', |
| 136 |
$variants['class'] |
| 137 |
); |
| 138 |
$this->assertSame( |
| 139 |
'mw-portlet mw-portlet-cactions vector-menu-dropdown', |
| 140 |
$actions['class'] |
| 141 |
); |
| 142 |
$this->assertSame( |
| 143 |
'mw-portlet mw-portlet-personal vector-user-menu-legacy', |
| 144 |
$props['data-personal']['class'] |
| 145 |
); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
private static function enableLanguageInHeaderFeatureConfig(): array { |
| 153 |
return [ |
| 154 |
'VectorLanguageInHeader' => [ |
| 155 |
'logged_in' => true, |
| 156 |
'logged_out' => true |
| 157 |
], |
| 158 |
'VectorLanguageInMainPageHeader' => [ |
| 159 |
'logged_in' => false, |
| 160 |
'logged_out' => false |
| 161 |
], |
| 162 |
]; |
| 163 |
} |
| 164 |
|
| 165 |
public static function providerLanguageAlertRequirements() { |
| 166 |
$testTitle = Title::makeTitle( NS_MAIN, 'Test' ); |
| 167 |
$testTitleMainPage = Title::makeTitle( NS_MAIN, 'MAIN_PAGE' ); |
| 168 |
return [ |
| 169 |
'When none of the requirements are present, do not show alert' => [ |
| 170 |
|
| 171 |
[], |
| 172 |
|
| 173 |
$testTitle, |
| 174 |
|
| 175 |
[], |
| 176 |
|
| 177 |
false, |
| 178 |
|
| 179 |
false, |
| 180 |
|
| 181 |
false |
| 182 |
], |
| 183 |
'When the feature is enabled and languages should be hidden, do not show alert' => [ |
| 184 |
self::enableLanguageInHeaderFeatureConfig(), |
| 185 |
$testTitle, |
| 186 |
[], true, true, false |
| 187 |
], |
| 188 |
'When the language in header feature is disabled, do not show alert' => [ |
| 189 |
[ |
| 190 |
'VectorLanguageInHeader' => [ |
| 191 |
'logged_in' => false, |
| 192 |
'logged_out' => false |
| 193 |
], |
| 194 |
], |
| 195 |
$testTitle, |
| 196 |
[ 'fr', 'en', 'ko' ], true, false, false |
| 197 |
], |
| 198 |
'When it is a main page, feature is enabled, and there are no languages, do not show alert' => [ |
| 199 |
self::enableLanguageInHeaderFeatureConfig(), |
| 200 |
$testTitleMainPage, |
| 201 |
[], true, true, false |
| 202 |
], |
| 203 |
'When it is a non-main page, feature is enabled, and there are no languages, do not show alert' => [ |
| 204 |
self::enableLanguageInHeaderFeatureConfig(), |
| 205 |
$testTitle, |
| 206 |
[], true, true, false |
| 207 |
], |
| 208 |
'When it is a main page, header feature is disabled, and there are languages, do not show alert' => [ |
| 209 |
[ |
| 210 |
'VectorLanguageInHeader' => [ |
| 211 |
'logged_in' => false, |
| 212 |
'logged_out' => false |
| 213 |
], |
| 214 |
], |
| 215 |
$testTitleMainPage, |
| 216 |
[ 'fr', 'en', 'ko' ], true, true, false |
| 217 |
], |
| 218 |
'When most requirements are present but languages are not at the top, do not show alert' => [ |
| 219 |
self::enableLanguageInHeaderFeatureConfig(), |
| 220 |
$testTitle, |
| 221 |
[ 'fr', 'en', 'ko' ], false, false, false |
| 222 |
], |
| 223 |
'When most requirements are present but languages should be hidden, do not show alert' => [ |
| 224 |
self::enableLanguageInHeaderFeatureConfig(), |
| 225 |
$testTitle, |
| 226 |
[ 'fr', 'en', 'ko' ], true, true, false |
| 227 |
], |
| 228 |
'When it is a main page, features are enabled, and there are languages, show alert' => [ |
| 229 |
self::enableLanguageInHeaderFeatureConfig(), |
| 230 |
$testTitleMainPage, |
| 231 |
[ 'fr', 'en', 'ko' ], true, false, true |
| 232 |
], |
| 233 |
'When all the requirements are present on a non-main page, show alert' => [ |
| 234 |
self::enableLanguageInHeaderFeatureConfig(), |
| 235 |
$testTitle, |
| 236 |
[ 'fr', 'en', 'ko' ], true, false, true |
| 237 |
], |
| 238 |
]; |
| 239 |
} |
| 240 |
} |