| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Context\RequestContext; |
| 4 |
use MediaWiki\Skins\Vector\Constants; |
| 5 |
use MediaWiki\Skins\Vector\FeatureManagement\FeatureManager; |
| 6 |
use MediaWiki\Title\Title; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
class FeatureManagerTest extends \MediaWikiIntegrationTestCase { |
| 12 |
private function newFeatureManager(): FeatureManager { |
| 13 |
return new FeatureManager( |
| 14 |
$this->getServiceContainer()->getUserOptionsLookup(), |
| 15 |
RequestContext::getMain() |
| 16 |
); |
| 17 |
} |
| 18 |
|
| 19 |
public static function provideGetFeatureBodyClass() { |
| 20 |
return [ |
| 21 |
[ CONSTANTS::FEATURE_LIMITED_WIDTH, false, 'vector-feature-limited-width-clientpref-0' ], |
| 22 |
[ CONSTANTS::FEATURE_LIMITED_WIDTH, true, 'vector-feature-limited-width-clientpref-1' ], |
| 23 |
[ CONSTANTS::FEATURE_TOC_PINNED, false, 'vector-feature-toc-pinned-clientpref-0' ], |
| 24 |
[ CONSTANTS::FEATURE_TOC_PINNED, true, 'vector-feature-toc-pinned-clientpref-1' ], |
| 25 |
[ CONSTANTS::FEATURE_NIGHT_MODE, false, 'vector-feature-night-mode-disabled' ], |
| 26 |
[ CONSTANTS::FEATURE_NIGHT_MODE, true, 'vector-feature-night-mode-enabled' ], |
| 27 |
]; |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public function testGetFeatureBodyClass( $feature, $enabled, $expected ) { |
| 35 |
$featureManager = $this->newFeatureManager(); |
| 36 |
$featureManager->registerSimpleRequirement( 'requirement', $enabled ); |
| 37 |
$featureManager->registerFeature( $feature, [ 'requirement' ] ); |
| 38 |
|
| 39 |
|
| 40 |
$context = RequestContext::getMain(); |
| 41 |
$context->setTitle( Title::makeTitle( NS_MAIN, 'test' ) ); |
| 42 |
$this->assertSame( [ $expected ], $featureManager->getFeatureBodyClass() ); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public function testGetFeatureBodyClassMultiple() { |
| 49 |
$featureManager = $this->newFeatureManager(); |
| 50 |
$featureManager->registerSimpleRequirement( 'requirement', true ); |
| 51 |
$featureManager->registerSimpleRequirement( 'disabled', false ); |
| 52 |
$featureManager->registerFeature( Constants::FEATURE_STICKY_HEADER, [ 'requirement' ] ); |
| 53 |
$featureManager->registerFeature( Constants::FEATURE_NIGHT_MODE, [ 'requirement' ] ); |
| 54 |
$featureManager->registerFeature( Constants::FEATURE_LIMITED_WIDTH_CONTENT, [ 'disabled' ] ); |
| 55 |
$this->assertEquals( |
| 56 |
[ |
| 57 |
'vector-feature-sticky-header-enabled', |
| 58 |
'vector-feature-night-mode-enabled', |
| 59 |
'vector-feature-limited-width-content-disabled' |
| 60 |
], |
| 61 |
$featureManager->getFeatureBodyClass() |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
public static function provideGetFeatureBodyClassNightModeQueryParam() { |
| 66 |
return [ |
| 67 |
[ 0, 'skin-theme-clientpref-day' ], |
| 68 |
[ 1, 'skin-theme-clientpref-night' ], |
| 69 |
[ 2, 'skin-theme-clientpref-os' ], |
| 70 |
[ 'day', 'skin-theme-clientpref-day' ], |
| 71 |
[ 'night', 'skin-theme-clientpref-night' ], |
| 72 |
[ 'os', 'skin-theme-clientpref-os' ], |
| 73 |
[ 'invalid', 'skin-theme-clientpref-day' ] |
| 74 |
]; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public function testGetFeatureBodyClassNightModeQueryParam( $value, $expected ) { |
| 82 |
$featureManager = $this->newFeatureManager(); |
| 83 |
$featureManager->registerFeature( CONSTANTS::PREF_NIGHT_MODE, [] ); |
| 84 |
|
| 85 |
$context = RequestContext::getMain(); |
| 86 |
$context->setTitle( Title::makeTitle( NS_MAIN, 'test' ) ); |
| 87 |
|
| 88 |
$request = $context->getRequest(); |
| 89 |
$request->setVal( 'vectornightmode', $value ); |
| 90 |
|
| 91 |
$this->assertEquals( [ $expected ], $featureManager->getFeatureBodyClass() ); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
public static function provideGetFeatureBodyClassNightModeDisabled() { |
| 96 |
return [ |
| 97 |
[ true ], [ false ] |
| 98 |
]; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public function testGetFeatureBodyClassNightModeDisabled( $disabled ) { |
| 106 |
$featureManager = $this->newFeatureManager(); |
| 107 |
$featureManager->registerFeature( CONSTANTS::PREF_NIGHT_MODE, [] ); |
| 108 |
|
| 109 |
$context = RequestContext::getMain(); |
| 110 |
$context->setTitle( Title::makeTitle( NS_MAIN, 'Main Page' ) ); |
| 111 |
|
| 112 |
$this->overrideConfigValues( [ 'VectorNightModeOptions' => [ 'exclude' => [ 'mainpage' => $disabled ] ] ] ); |
| 113 |
$this->assertEquals( |
| 114 |
in_array( 'skin-theme-clientpref--excluded', $featureManager->getFeatureBodyClass() ), |
| 115 |
$disabled |
| 116 |
); |
| 117 |
} |
| 118 |
} |