| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Specials; |
| 4 |
|
| 5 |
use DOMElement; |
| 6 |
use MediaWiki\Context\RequestContext; |
| 7 |
use MediaWiki\MainConfigNames; |
| 8 |
use MediaWiki\Request\FauxRequest; |
| 9 |
use MediaWiki\Revision\RevisionRecord; |
| 10 |
use MediaWiki\Tests\User\TempUser\TempUserTestTrait; |
| 11 |
use MediaWiki\Title\Title; |
| 12 |
use MediaWiki\User\UserFactory; |
| 13 |
use MediaWiki\User\UserIdentity; |
| 14 |
use SpecialPageTestBase; |
| 15 |
use Wikimedia\Parsoid\DOM\Document; |
| 16 |
use Wikimedia\Parsoid\DOM\Element; |
| 17 |
use Wikimedia\Parsoid\Utils\DOMCompat; |
| 18 |
use Wikimedia\Parsoid\Utils\DOMUtils; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
class SpecialNewPagesTest extends SpecialPageTestBase { |
| 26 |
|
| 27 |
use TempUserTestTrait; |
| 28 |
|
| 29 |
private static UserIdentity $testUser1; |
| 30 |
|
| 31 |
|
| 32 |
private static array $testUser1Pages; |
| 33 |
|
| 34 |
|
| 35 |
private static array $allPages; |
| 36 |
|
| 37 |
private static int $editRevId; |
| 38 |
|
| 39 |
protected function newSpecialPage() { |
| 40 |
return $this->getServiceContainer()->getSpecialPageFactory()->getPage( 'Newpages' ); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
private function verifyFormFieldsArePresent( |
| 50 |
string $html, bool $canAnonUsersCreatePages |
| 51 |
) { |
| 52 |
|
| 53 |
|
| 54 |
$this->assertStringContainsString( '(namespace)', $html, 'Namespace filter not added to form' ); |
| 55 |
$this->assertStringContainsString( '(newpages-username)', $html, 'Username filter not added to form' ); |
| 56 |
$this->assertStringContainsString( |
| 57 |
'(namespace_association)', $html, 'Associated namespace filter not added to form' |
| 58 |
); |
| 59 |
$this->assertStringContainsString( '(tag-filter)', $html, 'Tag filter not added to form' ); |
| 60 |
$this->assertStringContainsString( '(invert)', $html, 'Invert checkbox not added to form' ); |
| 61 |
$this->assertStringContainsString( '(minimum-size)', $html, 'Size filter not added to form' ); |
| 62 |
|
| 63 |
if ( $canAnonUsersCreatePages ) { |
| 64 |
$this->assertStringContainsString( |
| 65 |
'(newpages-showhide-registered', $html, 'Registered filter should be present' |
| 66 |
); |
| 67 |
} else { |
| 68 |
$this->assertStringNotContainsString( |
| 69 |
'(newpages-showhide-registered', $html, 'Registered filter should not be present' |
| 70 |
); |
| 71 |
} |
| 72 |
$this->assertStringContainsString( '(newpages-showhide-bots', $html, 'Missing bots filter' ); |
| 73 |
$this->assertStringContainsString( '(newpages-showhide-redirect', $html, 'Missing redirect filter' ); |
| 74 |
|
| 75 |
$this->assertStringContainsString( '(newpages-submit)', $html, 'Submit button text not as expected' ); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
private function setGroupHasRightsToCreatePages( string $group, bool $state ) { |
| 83 |
|
| 84 |
$groupPermissionsValue = $this->getServiceContainer()->getMainConfig() |
| 85 |
->get( MainConfigNames::GroupPermissions ); |
| 86 |
if ( $state ) { |
| 87 |
$groupPermissionsValue[$group]['createtalk'] = true; |
| 88 |
$groupPermissionsValue[$group]['createpage'] = true; |
| 89 |
} else { |
| 90 |
unset( $groupPermissionsValue[$group]['createtalk'] ); |
| 91 |
unset( $groupPermissionsValue[$group]['createpage'] ); |
| 92 |
} |
| 93 |
$this->overrideConfigValue( MainConfigNames::GroupPermissions, $groupPermissionsValue ); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
private function getAndExpectSingleMatchingElement( $document, string $selector ) { |
| 104 |
$matchingClass = DOMCompat::querySelectorAll( $document, $selector ); |
| 105 |
$this->assertCount( 1, $matchingClass, "One element was expected to match $selector" ); |
| 106 |
return $matchingClass[0]; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
private function verifyLineHasExpectedElements( $line, RevisionRecord $firstRevision ) { |
| 116 |
|
| 117 |
$this->getAndExpectSingleMatchingElement( $line, ".mw-newpages-time" ); |
| 118 |
|
| 119 |
$pageNameElement = $this->getAndExpectSingleMatchingElement( |
| 120 |
$line, ".mw-newpages-pagename" |
| 121 |
); |
| 122 |
$this->assertSame( |
| 123 |
$this->getServiceContainer()->getTitleFormatter()->getPrefixedText( $firstRevision->getPage() ), |
| 124 |
$pageNameElement->textContent |
| 125 |
); |
| 126 |
|
| 127 |
$editLinkElement = $this->getAndExpectSingleMatchingElement( $line, ".mw-newpages-edit" ); |
| 128 |
$this->assertSame( '(editlink)', $editLinkElement->textContent ); |
| 129 |
$pageHistoryLinkElement = $this->getAndExpectSingleMatchingElement( |
| 130 |
$line, ".mw-newpages-history" |
| 131 |
); |
| 132 |
$this->assertSame( '(hist)', $pageHistoryLinkElement->textContent ); |
| 133 |
|
| 134 |
|
| 135 |
$authority = RequestContext::getMain()->getAuthority(); |
| 136 |
$userNameElement = $this->getAndExpectSingleMatchingElement( $line, ".mw-userlink" ); |
| 137 |
if ( $firstRevision->userCan( RevisionRecord::DELETED_USER, $authority ) ) { |
| 138 |
$expectedUserText = $firstRevision->getUser( RevisionRecord::RAW )->getName(); |
| 139 |
} else { |
| 140 |
$expectedUserText = '(rev-deleted-user)'; |
| 141 |
} |
| 142 |
$this->assertSame( $expectedUserText, $userNameElement->textContent ); |
| 143 |
|
| 144 |
$commentElement = $this->getAndExpectSingleMatchingElement( $line, ".comment" ); |
| 145 |
if ( $firstRevision->userCan( RevisionRecord::DELETED_COMMENT, $authority ) ) { |
| 146 |
$this->assertStringContainsString( $firstRevision->getComment()->text, $commentElement->textContent ); |
| 147 |
} else { |
| 148 |
$this->assertStringContainsString( '(rev-deleted-comment)', $commentElement->textContent ); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
private function testLoadPage( |
| 164 |
array $expectedPages, array $expectedPagesNotShown, ?FauxRequest $fauxRequest = null, |
| 165 |
bool $canAnonUsersCreatePages = false, ?bool $canTempUsersCreatePages = false |
| 166 |
): string { |
| 167 |
$this->setGroupHasRightsToCreatePages( '*', $canAnonUsersCreatePages ); |
| 168 |
if ( $canTempUsersCreatePages !== null ) { |
| 169 |
|
| 170 |
|
| 171 |
$this->enableAutoCreateTempUser(); |
| 172 |
$this->setGroupHasRightsToCreatePages( 'temp', $canTempUsersCreatePages ); |
| 173 |
} |
| 174 |
$this->overrideConfigValues( [ |
| 175 |
MainConfigNames::UseNPPatrol => true, |
| 176 |
MainConfigNames::UseRCPatrol => true, |
| 177 |
] ); |
| 178 |
|
| 179 |
|
| 180 |
$this->setUserLang( 'qqx' ); |
| 181 |
|
| 182 |
[ $html ] = $this->executeSpecialPage( '', $fauxRequest ); |
| 183 |
$this->verifyFormFieldsArePresent( $html, $canAnonUsersCreatePages ); |
| 184 |
|
| 185 |
$contributionsList = $this->getAndExpectSingleMatchingElement( |
| 186 |
DOMUtils::parseHTML( $html ), '.mw-contributions-list' |
| 187 |
); |
| 188 |
foreach ( $expectedPages as $page ) { |
| 189 |
|
| 190 |
$firstRevision = $this->getServiceContainer()->getRevisionStore()->getFirstRevision( $page ); |
| 191 |
$matchingLine = $this->getAndExpectSingleMatchingElement( |
| 192 |
$contributionsList, "li[data-mw-revid=\"{$firstRevision->getId()}\"]" |
| 193 |
); |
| 194 |
|
| 195 |
$this->verifyLineHasExpectedElements( $matchingLine, $firstRevision ); |
| 196 |
} |
| 197 |
|
| 198 |
foreach ( $expectedPagesNotShown as $page ) { |
| 199 |
$firstRevId = $this->getServiceContainer()->getRevisionStore()->getFirstRevision( $page )->getId(); |
| 200 |
$matchingLines = DOMCompat::querySelectorAll( $contributionsList, "[data-mw-revid=\"$firstRevId\"]" ); |
| 201 |
$this->assertCount( |
| 202 |
0, $matchingLines, "New page entry for revision $firstRevId was not expected" |
| 203 |
); |
| 204 |
} |
| 205 |
|
| 206 |
$matchingLines = DOMCompat::querySelectorAll( |
| 207 |
$contributionsList, '[data-mw-revid="' . self::$editRevId . '"]' |
| 208 |
); |
| 209 |
$this->assertCount( |
| 210 |
0, $matchingLines, |
| 211 |
'A revision ID which is not associated with a new page creation is present in Special:NewPages.' |
| 212 |
); |
| 213 |
|
| 214 |
return $html; |
| 215 |
} |
| 216 |
|
| 217 |
public function testLoadWithNoOptionsSpecified() { |
| 218 |
|
| 219 |
$expectedPages = []; |
| 220 |
$expectedPagesNotShown = []; |
| 221 |
foreach ( self::$allPages as $page ) { |
| 222 |
if ( $page->getNamespace() === NS_MAIN ) { |
| 223 |
$expectedPages[] = $page; |
| 224 |
} else { |
| 225 |
$expectedPagesNotShown[] = $page; |
| 226 |
} |
| 227 |
} |
| 228 |
$this->testLoadPage( $expectedPages, $expectedPagesNotShown ); |
| 229 |
} |
| 230 |
|
| 231 |
public function testWhenFilteredToJustTestUser1Pages() { |
| 232 |
|
| 233 |
$this->testLoadPage( |
| 234 |
self::$testUser1Pages, array_diff( self::$allPages, self::$testUser1Pages ), |
| 235 |
new FauxRequest( [ 'username' => self::$testUser1->getName(), 'namespace' => 'all' ] ) |
| 236 |
); |
| 237 |
} |
| 238 |
|
| 239 |
public function testWhenFilteredToJustAnonCreations() { |
| 240 |
|
| 241 |
$fauxRequest = new FauxRequest( [ 'hideliu' => true, 'namespace' => '' ] ); |
| 242 |
$this->testLoadPage( |
| 243 |
array_diff( self::$allPages, self::$testUser1Pages ), self::$testUser1Pages, $fauxRequest, |
| 244 |
true, true |
| 245 |
); |
| 246 |
} |
| 247 |
|
| 248 |
public function testWhenFilteredToJustAnonCreationsWhenTemporaryAccountsAreDisabled() { |
| 249 |
|
| 250 |
$fauxRequest = new FauxRequest( [ 'hideliu' => true, 'namespace' => '' ] ); |
| 251 |
|
| 252 |
$expectedPages = array_filter( self::$allPages, function ( $page ) { |
| 253 |
$firstRev = $this->getServiceContainer()->getRevisionStore()->getFirstRevision( $page ); |
| 254 |
return !$firstRev->getUser()->isRegistered(); |
| 255 |
} ); |
| 256 |
$this->disableAutoCreateTempUser(); |
| 257 |
$this->testLoadPage( |
| 258 |
$expectedPages, array_diff( self::$allPages, $expectedPages ), $fauxRequest, |
| 259 |
true, null |
| 260 |
); |
| 261 |
} |
| 262 |
|
| 263 |
public function addDBDataOnce() { |
| 264 |
|
| 265 |
$testUser1 = $this->getMutableTestUser()->getUser(); |
| 266 |
|
| 267 |
$firstPage = $this->insertPage( 'SpecialNewPagesTest1', 'test', NS_MAIN, $testUser1 ); |
| 268 |
$secondPage = $this->insertPage( 'SpecialNewPagesTest1', 'talk', NS_TALK, $testUser1 ); |
| 269 |
|
| 270 |
$thirdPage = $this->insertPage( $testUser1->getName(), 'userpage', NS_USER, $testUser1 ); |
| 271 |
|
| 272 |
$this->disableAutoCreateTempUser(); |
| 273 |
$fourthPage = $this->insertPage( |
| 274 |
'SpecialNewPagesTest2', 'test', NS_TEMPLATE, |
| 275 |
$this->getServiceContainer()->getUserFactory()->newFromName( '127.0.0.1', UserFactory::RIGOR_NONE ) |
| 276 |
); |
| 277 |
|
| 278 |
$this->enableAutoCreateTempUser(); |
| 279 |
$testTempUser = $this->getServiceContainer()->getTempUserCreator() |
| 280 |
->create( null, RequestContext::getMain()->getRequest() ); |
| 281 |
$this->assertStatusGood( $testTempUser ); |
| 282 |
$fifthPage = $this->insertPage( |
| 283 |
'SpecialNewPagesTest3', 'test', NS_PROJECT, $testTempUser->getUser() |
| 284 |
); |
| 285 |
|
| 286 |
$editStatus = $this->editPage( $firstPage['title'], 'testing1234', 'test edit' ); |
| 287 |
$this->assertStatusGood( $editStatus ); |
| 288 |
self::$testUser1 = $testUser1; |
| 289 |
self::$testUser1Pages = [ $firstPage['title'], $secondPage['title'], $thirdPage['title'], ]; |
| 290 |
self::$allPages = array_merge( self::$testUser1Pages, [ $fourthPage['title'], $fifthPage['title'] ] ); |
| 291 |
self::$editRevId = $editStatus->getNewRevision()->getId(); |
| 292 |
} |
| 293 |
} |