| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Context\DerivativeContext; |
| 4 |
use MediaWiki\HookContainer\HookContainer; |
| 5 |
use MediaWiki\MainConfigNames; |
| 6 |
use MediaWiki\Request\FauxRequest; |
| 7 |
use MediaWiki\Specials\SpecialWatchlist; |
| 8 |
use Wikimedia\TestingAccessWrapper; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class SpecialWatchlistTest extends SpecialPageTestBase { |
| 18 |
protected function setUp(): void { |
| 19 |
parent::setUp(); |
| 20 |
|
| 21 |
$this->setTemporaryHook( |
| 22 |
'ChangesListSpecialPageQuery', |
| 23 |
HookContainer::NOOP |
| 24 |
); |
| 25 |
|
| 26 |
$this->overrideConfigValues( [ |
| 27 |
MainConfigNames::DefaultUserOptions => |
| 28 |
[ |
| 29 |
'extendwatchlist' => 1, |
| 30 |
'watchlistdays' => 3.0, |
| 31 |
'watchlisthideanons' => 0, |
| 32 |
'watchlisthidebots' => 0, |
| 33 |
'watchlisthideliu' => 0, |
| 34 |
'watchlisthideminor' => 0, |
| 35 |
'watchlisthideown' => 0, |
| 36 |
'watchlisthidepatrolled' => 1, |
| 37 |
'watchlisthidecategorization' => 0, |
| 38 |
'watchlistreloadautomatically' => 0, |
| 39 |
'watchlistunwatchlinks' => 0, |
| 40 |
'timecorrection' => '0' |
| 41 |
], |
| 42 |
MainConfigNames::WatchlistExpiry => true |
| 43 |
] ); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
protected function newSpecialPage() { |
| 52 |
$services = $this->getServiceContainer(); |
| 53 |
return new SpecialWatchlist( |
| 54 |
$services->getWatchedItemStore(), |
| 55 |
$services->getWatchlistManager(), |
| 56 |
$services->getUserOptionsLookup(), |
| 57 |
$services->getChangeTagsStore(), |
| 58 |
$services->getUserIdentityUtils(), |
| 59 |
$services->getTempUserConfig() |
| 60 |
); |
| 61 |
} |
| 62 |
|
| 63 |
public function testNotLoggedIn_throwsException() { |
| 64 |
$this->expectException( UserNotLoggedIn::class ); |
| 65 |
$this->executeSpecialPage(); |
| 66 |
} |
| 67 |
|
| 68 |
public function testUserWithNoWatchedItems_displaysNoWatchlistMessage() { |
| 69 |
$user = new TestUser( __METHOD__ ); |
| 70 |
[ $html, ] = $this->executeSpecialPage( '', null, 'qqx', $user->getUser() ); |
| 71 |
$this->assertStringContainsString( '(nowatchlist)', $html ); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public function testFetchOptionsFromRequest( |
| 78 |
$expectedValuesDefaults, $expectedValues, $preferences, $inputParams |
| 79 |
) { |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
$page = TestingAccessWrapper::newFromObject( |
| 85 |
$this->newSpecialPage() |
| 86 |
); |
| 87 |
|
| 88 |
$page->registerFilters(); |
| 89 |
|
| 90 |
|
| 91 |
$wikiDefaults = $page->getDefaultOptions()->getAllValues(); |
| 92 |
|
| 93 |
switch ( $expectedValuesDefaults ) { |
| 94 |
case 'allFalse': |
| 95 |
$allFalse = $wikiDefaults; |
| 96 |
|
| 97 |
foreach ( $allFalse as $key => $value ) { |
| 98 |
if ( $value === true ) { |
| 99 |
$allFalse[$key] = false; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
$allFalse['extended'] = true; |
| 106 |
|
| 107 |
$expectedValues += $allFalse; |
| 108 |
break; |
| 109 |
case 'wikiDefaults': |
| 110 |
$expectedValues += $wikiDefaults; |
| 111 |
break; |
| 112 |
default: |
| 113 |
$this->fail( "Unknown \$expectedValuesDefaults: $expectedValuesDefaults" ); |
| 114 |
} |
| 115 |
|
| 116 |
$page = TestingAccessWrapper::newFromObject( |
| 117 |
$this->newSpecialPage() |
| 118 |
); |
| 119 |
|
| 120 |
$context = new DerivativeContext( $page->getContext() ); |
| 121 |
|
| 122 |
$fauxRequest = new FauxRequest( $inputParams, false ); |
| 123 |
$user = $this->getTestUser()->getUser(); |
| 124 |
|
| 125 |
$userOptionsManager = $this->getServiceContainer()->getUserOptionsManager(); |
| 126 |
foreach ( $preferences as $key => $value ) { |
| 127 |
$userOptionsManager->setOption( $user, $key, $value ); |
| 128 |
} |
| 129 |
|
| 130 |
$context->setRequest( $fauxRequest ); |
| 131 |
$context->setUser( $user ); |
| 132 |
$page->setContext( $context ); |
| 133 |
|
| 134 |
$page->registerFilters(); |
| 135 |
$formOptions = $page->getDefaultOptions(); |
| 136 |
$page->fetchOptionsFromRequest( $formOptions ); |
| 137 |
|
| 138 |
$this->assertArrayEquals( |
| 139 |
$expectedValues, |
| 140 |
$formOptions->getAllValues(), |
| 141 |
false, |
| 142 |
true |
| 143 |
); |
| 144 |
} |
| 145 |
|
| 146 |
public static function provideFetchOptionsFromRequest() { |
| 147 |
return [ |
| 148 |
'ignores casing' => [ |
| 149 |
'expectedValuesDefaults' => 'wikiDefaults', |
| 150 |
'expectedValues' => [ |
| 151 |
'hideminor' => true, |
| 152 |
], |
| 153 |
'preferences' => [], |
| 154 |
'inputParams' => [ |
| 155 |
'hideMinor' => 1, |
| 156 |
], |
| 157 |
], |
| 158 |
|
| 159 |
'first two same as prefs, second two overridden' => [ |
| 160 |
'expectedValuesDefaults' => 'wikiDefaults', |
| 161 |
'expectedValues' => [ |
| 162 |
|
| 163 |
'hideminor' => true, |
| 164 |
'hidebots' => false, |
| 165 |
|
| 166 |
|
| 167 |
'hideanons' => false, |
| 168 |
'hideliu' => true, |
| 169 |
'userExpLevel' => 'registered' |
| 170 |
], |
| 171 |
'preferences' => [ |
| 172 |
'watchlisthideminor' => 1, |
| 173 |
'watchlisthidebots' => 0, |
| 174 |
|
| 175 |
'watchlisthideanons' => 1, |
| 176 |
'watchlisthideliu' => 0, |
| 177 |
], |
| 178 |
'inputParams' => [ |
| 179 |
'hideanons' => 0, |
| 180 |
'hideliu' => 1, |
| 181 |
], |
| 182 |
], |
| 183 |
|
| 184 |
'Defaults/preferences for form elements are entirely ignored for ' |
| 185 |
. 'action=submit and omitted elements become false' => [ |
| 186 |
'expectedValuesDefaults' => 'allFalse', |
| 187 |
'expectedValues' => [ |
| 188 |
'hideminor' => false, |
| 189 |
'hidebots' => true, |
| 190 |
'hideanons' => false, |
| 191 |
'hideliu' => true, |
| 192 |
'userExpLevel' => 'unregistered' |
| 193 |
], |
| 194 |
'preferences' => [ |
| 195 |
'watchlisthideminor' => 0, |
| 196 |
'watchlisthidebots' => 1, |
| 197 |
|
| 198 |
'watchlisthideanons' => 0, |
| 199 |
'watchlisthideliu' => 1, |
| 200 |
], |
| 201 |
'inputParams' => [ |
| 202 |
'hidebots' => 1, |
| 203 |
'hideliu' => 1, |
| 204 |
'action' => 'submit', |
| 205 |
], |
| 206 |
], |
| 207 |
]; |
| 208 |
} |
| 209 |
} |