3 use MediaWiki\Context\RequestContext
;
4 use MediaWiki\MainConfigNames
;
5 use MediaWiki\Request\FauxRequest
;
6 use MediaWiki\Specials\SpecialRecentChanges
;
7 use MediaWiki\Tests\SpecialPage\AbstractChangesListSpecialPageTestCase
;
8 use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait
;
9 use MediaWiki\Tests\User\TempUser\TempUserTestTrait
;
10 use MediaWiki\Title\Title
;
11 use MediaWiki\Watchlist\WatchedItemStoreInterface
;
12 use Wikimedia\TestingAccessWrapper
;
15 * Test class for SpecialRecentchanges class
19 * @covers \MediaWiki\Specials\SpecialRecentChanges
20 * @covers \MediaWiki\SpecialPage\ChangesListSpecialPage
22 class SpecialRecentChangesTest
extends AbstractChangesListSpecialPageTestCase
{
23 use MockAuthorityTrait
;
24 use TempUserTestTrait
;
26 protected function getPage(): SpecialRecentChanges
{
27 return new SpecialRecentChanges(
28 $this->getServiceContainer()->getWatchedItemStore(),
29 $this->getServiceContainer()->getMessageCache(),
30 $this->getServiceContainer()->getUserOptionsLookup(),
31 $this->getServiceContainer()->getChangeTagsStore(),
32 $this->getServiceContainer()->getUserIdentityUtils(),
33 $this->getServiceContainer()->getTempUserConfig()
38 * @return TestingAccessWrapper
40 protected function getPageAccessWrapper() {
41 return TestingAccessWrapper
::newFromObject( $this->getPage() );
44 // Below providers should only be for features specific to
45 // RecentChanges. Otherwise, it should go in ChangesListSpecialPageTest
47 public function provideParseParameters() {
49 [ 'limit=123', [ 'limit' => '123' ] ],
51 [ '234', [ 'limit' => '234' ] ],
53 [ 'days=3', [ 'days' => '3' ] ],
55 [ 'days=0.25', [ 'days' => '0.25' ] ],
57 [ 'namespace=5', [ 'namespace' => '5' ] ],
59 [ 'namespace=5|3', [ 'namespace' => '5|3' ] ],
61 [ 'tagfilter=foo', [ 'tagfilter' => 'foo' ] ],
63 [ 'tagfilter=foo;bar', [ 'tagfilter' => 'foo;bar' ] ],
67 public function validateOptionsProvider() {
70 // hidebots=1 is default for Special:RecentChanges
71 [ 'hideanons' => 1, 'hideliu' => 1 ],
79 public function testAddWatchlistJoins() {
80 // Edit a test page so that it shows up in RC.
81 $testPage = $this->getExistingTestPage( 'Test page' );
82 $this->editPage( $testPage, 'Test content', '' );
85 $context = new RequestContext
;
86 $context->setTitle( Title
::newFromText( __METHOD__
) );
87 $context->setUser( $this->getTestUser()->getUser() );
88 $context->setRequest( new FauxRequest
);
90 // Confirm that the test page is in RC.
91 $rc1 = $this->getPage();
92 $rc1->setContext( $context );
93 $rc1->execute( null );
94 $this->assertStringContainsString( 'Test page', $rc1->getOutput()->getHTML() );
95 $this->assertStringContainsString( 'mw-changeslist-line-not-watched', $rc1->getOutput()->getHTML() );
97 // Watch the page, and check that it's now watched in RC.
98 $watchedItemStore = $this->getServiceContainer()->getWatchedItemStore();
99 $watchedItemStore->addWatch( $context->getUser(), $testPage );
100 $rc2 = $this->getPage();
101 $rc2->setContext( $context );
102 $rc2->execute( null );
103 $this->assertStringContainsString( 'Test page', $rc2->getOutput()->getHTML() );
104 $this->assertStringContainsString( 'mw-changeslist-line-watched', $rc2->getOutput()->getHTML() );
106 // Force a past expiry date on the watchlist item.
107 $db = $this->getDb();
108 $watchedItemId = $db->newSelectQueryBuilder()
110 ->from( 'watchlist' )
111 ->where( [ 'wl_namespace' => $testPage->getNamespace(), 'wl_title' => $testPage->getDBkey() ] )
112 ->caller( __METHOD__
)->fetchField();
113 $db->newUpdateQueryBuilder()
114 ->update( 'watchlist_expiry' )
115 ->set( [ 'we_expiry' => $db->timestamp( '20200101000000' ) ] )
116 ->where( [ 'we_item' => $watchedItemId ] )
117 ->caller( __METHOD__
)->execute();
119 // Check that the page is still in RC, but that it's no longer watched.
120 $rc3 = $this->getPage();
121 $rc3->setContext( $context );
122 $rc3->execute( null );
123 $this->assertStringContainsString( 'Test page', $rc3->getOutput()->getHTML() );
124 $this->assertStringContainsString( 'mw-changeslist-line-not-watched', $rc3->getOutput()->getHTML() );
127 public function testExperienceLevelFilter() {
128 $this->disableAutoCreateTempUser();
130 // Edit a test page so that it shows up in RC.
131 $testPage = $this->getExistingTestPage( 'Experience page' );
132 $this->editPage( $testPage, 'Registered content',
133 'registered summary', NS_MAIN
, $this->getTestUser()->getUser() );
134 $this->editPage( $testPage, 'Anon content',
135 'anon summary', NS_MAIN
, $this->mockAnonUltimateAuthority() );
138 $context = new RequestContext
;
139 $context->setTitle( Title
::newFromText( __METHOD__
) );
140 $context->setUser( $this->getTestUser()->getUser() );
141 $context->setRequest( new FauxRequest
);
143 // Confirm that the test page is in RC.
144 [ $html ] = ( new SpecialPageExecutor() )->executeSpecialPage(
149 $this->assertStringContainsString( 'Experience page', $html );
152 $req = new FauxRequest();
153 $req->setVal( 'userExpLevel', 'newcomer' );
154 [ $html ] = ( new SpecialPageExecutor() )->executeSpecialPage(
159 $this->assertStringContainsString( 'registered summary', $html );
162 $req = new FauxRequest();
163 $req->setVal( 'userExpLevel', 'unregistered' );
164 [ $html ] = ( new SpecialPageExecutor() )->executeSpecialPage(
169 $this->assertStringContainsString( 'anon summary', $html );
170 $this->assertStringNotContainsString( 'registered summary', $html );
173 $req = new FauxRequest();
174 $req->setVal( 'userExpLevel', 'registered' );
175 [ $html ] = ( new SpecialPageExecutor() )->executeSpecialPage(
180 $this->assertStringContainsString( 'registered summary', $html );
181 $this->assertStringNotContainsString( 'anon summary', $html );
184 public function testRegistrationFiltersDoShow() {
185 $this->disableAutoCreateTempUser();
187 $context = new RequestContext
;
188 $context->setTitle( Title
::newFromText( __METHOD__
) );
189 $context->setUser( $this->getTestUser()->getUser() );
190 $context->setRequest( new FauxRequest
);
192 [ $html ] = ( new SpecialPageExecutor() )->executeSpecialPage(
197 $this->assertStringContainsString( 'rcshowhideliu', $html );
198 $this->assertStringContainsString( 'rcshowhideanons', $html );
201 public function testRegistrationFiltersDoNotShowWhenRegistrationIsRequiredToEdit() {
202 $this->overrideConfigValue(
203 MainConfigNames
::GroupPermissions
,
204 [ '*' => [ 'edit' => false ] ]
206 $this->disableAutoCreateTempUser();
208 $context = new RequestContext
;
209 $context->setTitle( Title
::newFromText( __METHOD__
) );
210 $context->setUser( $this->getTestUser()->getUser() );
211 $context->setRequest( new FauxRequest
);
213 [ $html ] = ( new SpecialPageExecutor() )->executeSpecialPage(
218 $this->assertStringNotContainsString( 'rcshowhideliu', $html );
219 $this->assertStringNotContainsString( 'rcshowhideanons', $html );
223 * This integration test just tries to run the isDenseFilter() queries, to
224 * check for syntax errors etc. It doesn't verify the logic.
226 public function testIsDenseTagFilter() {
227 $this->getServiceContainer()->getChangeTagsStore()->defineTag( 'rc-test-tag' );
228 $req = new FauxRequest();
229 $req->setVal( 'tagfilter', 'rc-test-tag' );
230 $page = $this->getPage();
232 // Make sure thresholds are passed
233 $page->denseRcSizeThreshold
= 0;
234 $this->overrideConfigValue( MainConfigNames
::MiserMode
, true );
236 ( new SpecialPageExecutor() )->executeSpecialPage( $page, '', $req );
237 $this->assertTrue( true );
240 public static function provideDenseTagFilter() {
248 * This integration test injects the return value of isDenseFilter(),
249 * verifying the correctness of the resulting STRAIGHT_JOIN.
251 * @dataProvider provideDenseTagFilter
253 public function testDenseTagFilter( $dense ) {
254 $this->getServiceContainer()->getChangeTagsStore()->defineTag( 'rc-test-tag' );
255 $req = new FauxRequest();
256 $req->setVal( 'tagfilter', 'rc-test-tag' );
260 $this->getServiceContainer()->getWatchedItemStore(),
261 $this->getServiceContainer()->getMessageCache(),
262 $this->getServiceContainer()->getUserOptionsLookup()
263 ) extends SpecialRecentChanges
{
266 public function __construct(
268 ?WatchedItemStoreInterface
$watchedItemStore = null,
269 ?MessageCache
$messageCache = null,
270 ?\MediaWiki\User\Options\UserOptionsLookup
$userOptionsLookup = null
272 parent
::__construct( $watchedItemStore, $messageCache, $userOptionsLookup );
273 $this->dense
= $dense;
276 protected function isDenseTagFilter( $tagIds, $limit ) {
281 ( new SpecialPageExecutor() )->executeSpecialPage( $page, '', $req );
282 $this->assertTrue( true );