| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Context\RequestContext; |
| 4 |
use MediaWiki\Title\Title; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class OldChangesListTest extends MediaWikiLangTestCase { |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
private $testRecentChangesHelper; |
| 21 |
|
| 22 |
protected function setUp(): void { |
| 23 |
parent::setUp(); |
| 24 |
|
| 25 |
$this->setUserLang( 'qqx' ); |
| 26 |
$this->testRecentChangesHelper = new TestRecentChangesHelper(); |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public function testRecentChangesLine_CssForLineNumber( $expected, $linenumber, $message ) { |
| 33 |
$oldChangesList = $this->getOldChangesList(); |
| 34 |
$recentChange = $this->getEditChange(); |
| 35 |
|
| 36 |
$line = $oldChangesList->recentChangesLine( $recentChange, false, $linenumber ); |
| 37 |
|
| 38 |
$this->assertMatchesRegularExpression( $expected, $line, $message ); |
| 39 |
} |
| 40 |
|
| 41 |
public static function recentChangesLine_CssForLineNumberProvider() { |
| 42 |
return [ |
| 43 |
[ '/mw-line-odd/', 1, 'odd line number' ], |
| 44 |
[ '/mw-line-even/', 2, 'even line number' ] |
| 45 |
]; |
| 46 |
} |
| 47 |
|
| 48 |
public function testRecentChangesLine_NotWatchedCssClass() { |
| 49 |
$oldChangesList = $this->getOldChangesList(); |
| 50 |
$recentChange = $this->getEditChange(); |
| 51 |
|
| 52 |
$line = $oldChangesList->recentChangesLine( $recentChange, false, 1 ); |
| 53 |
|
| 54 |
$this->assertMatchesRegularExpression( '/mw-changeslist-line-not-watched/', $line ); |
| 55 |
} |
| 56 |
|
| 57 |
public function testRecentChangesLine_WatchedCssClass() { |
| 58 |
$oldChangesList = $this->getOldChangesList(); |
| 59 |
$recentChange = $this->getEditChange(); |
| 60 |
|
| 61 |
$line = $oldChangesList->recentChangesLine( $recentChange, true, 1 ); |
| 62 |
|
| 63 |
$this->assertMatchesRegularExpression( '/mw-changeslist-line-watched/', $line ); |
| 64 |
} |
| 65 |
|
| 66 |
public function testRecentChangesLine_LogTitle() { |
| 67 |
$oldChangesList = $this->getOldChangesList(); |
| 68 |
$recentChange = $this->getLogChange( 'delete', 'delete' ); |
| 69 |
|
| 70 |
$line = $oldChangesList->recentChangesLine( $recentChange, false, 1 ); |
| 71 |
|
| 72 |
$this->assertMatchesRegularExpression( '/href="\/wiki\/Special:Log\/delete/', $line, 'link has href attribute' ); |
| 73 |
$this->assertMatchesRegularExpression( '/title="Special:Log\/delete/', $line, 'link has title attribute' ); |
| 74 |
$this->assertMatchesRegularExpression( "/dellogpage/", $line, 'link text' ); |
| 75 |
} |
| 76 |
|
| 77 |
public function testRecentChangesLine_DiffHistLinks() { |
| 78 |
$oldChangesList = $this->getOldChangesList(); |
| 79 |
$recentChange = $this->getEditChange(); |
| 80 |
|
| 81 |
$line = $oldChangesList->recentChangesLine( $recentChange, false, 1 ); |
| 82 |
|
| 83 |
$this->assertMatchesRegularExpression( |
| 84 |
'/title=Cat&curid=20131103212153&diff=5&oldid=191/', |
| 85 |
$line, |
| 86 |
'assert diff link' |
| 87 |
); |
| 88 |
|
| 89 |
$this->assertMatchesRegularExpression( |
| 90 |
'/title=Cat&curid=20131103212153&action=history"/', |
| 91 |
$line, |
| 92 |
'assert history link' |
| 93 |
); |
| 94 |
} |
| 95 |
|
| 96 |
public function testRecentChangesLine_Flags() { |
| 97 |
$oldChangesList = $this->getOldChangesList(); |
| 98 |
$recentChange = $this->getNewBotEditChange(); |
| 99 |
|
| 100 |
$line = $oldChangesList->recentChangesLine( $recentChange, false, 1 ); |
| 101 |
|
| 102 |
$this->assertStringContainsString( |
| 103 |
'<abbr class="newpage" title="(recentchanges-label-newpage)">(newpageletter)</abbr>', |
| 104 |
$line, |
| 105 |
'new page flag' |
| 106 |
); |
| 107 |
|
| 108 |
$this->assertStringContainsString( |
| 109 |
'<abbr class="botedit" title="(recentchanges-label-bot)">(boteditletter)</abbr>', |
| 110 |
$line, |
| 111 |
'bot flag' |
| 112 |
); |
| 113 |
} |
| 114 |
|
| 115 |
public function testRecentChangesLine_Attribs() { |
| 116 |
$recentChange = $this->getEditChange(); |
| 117 |
$recentChange->mAttribs['ts_tags'] = 'vandalism,newbie'; |
| 118 |
|
| 119 |
$this->setTemporaryHook( 'OldChangesListRecentChangesLine', static function ( |
| 120 |
$oldChangesList, &$html, $rc, $classes, $attribs |
| 121 |
) { |
| 122 |
$html = $html . '/<div>Additional change line </div>/'; |
| 123 |
} ); |
| 124 |
|
| 125 |
$oldChangesList = $this->getOldChangesList(); |
| 126 |
$line = $oldChangesList->recentChangesLine( $recentChange, false, 1 ); |
| 127 |
|
| 128 |
$this->assertStringContainsString( |
| 129 |
'/<div>Additional change line </div>/', |
| 130 |
$line |
| 131 |
); |
| 132 |
$this->assertMatchesRegularExpression( |
| 133 |
'/<li data-mw-revid="\d+" data-mw-ts="\d+" class="[\w\s-]*mw-tag-vandalism[\w\s-]*">/', |
| 134 |
$line |
| 135 |
); |
| 136 |
$this->assertMatchesRegularExpression( |
| 137 |
'/<li data-mw-revid="\d+" data-mw-ts="\d+" class="[\w\s-]*mw-tag-newbie[\w\s-]*">/', |
| 138 |
$line |
| 139 |
); |
| 140 |
} |
| 141 |
|
| 142 |
public function testRecentChangesLine_numberOfWatchingUsers() { |
| 143 |
$oldChangesList = $this->getOldChangesList(); |
| 144 |
|
| 145 |
$recentChange = $this->getEditChange(); |
| 146 |
$recentChange->numberofWatchingusers = 100; |
| 147 |
|
| 148 |
$line = $oldChangesList->recentChangesLine( $recentChange, false, 1 ); |
| 149 |
$this->assertMatchesRegularExpression( "/(number-of-watching-users-for-recent-changes: 100)/", $line ); |
| 150 |
} |
| 151 |
|
| 152 |
public function testRecentChangesLine_watchlistCssClass() { |
| 153 |
$oldChangesList = $this->getOldChangesList(); |
| 154 |
$oldChangesList->setWatchlistDivs( true ); |
| 155 |
|
| 156 |
$recentChange = $this->getEditChange(); |
| 157 |
$line = $oldChangesList->recentChangesLine( $recentChange, false, 1 ); |
| 158 |
$this->assertMatchesRegularExpression( "/watchlist-0-Cat/", $line ); |
| 159 |
} |
| 160 |
|
| 161 |
public function testRecentChangesLine_dataAttribute() { |
| 162 |
$oldChangesList = $this->getOldChangesList(); |
| 163 |
$oldChangesList->setWatchlistDivs( true ); |
| 164 |
|
| 165 |
$recentChange = $this->getEditChange(); |
| 166 |
$line = $oldChangesList->recentChangesLine( $recentChange, false, 1 ); |
| 167 |
$this->assertMatchesRegularExpression( '/data-target-page=\"Cat\"/', $line ); |
| 168 |
|
| 169 |
$recentChange = $this->getLogChange( 'delete', 'delete' ); |
| 170 |
$line = $oldChangesList->recentChangesLine( $recentChange, false, 1 ); |
| 171 |
$this->assertMatchesRegularExpression( '/data-target-page="Abc"/', $line ); |
| 172 |
} |
| 173 |
|
| 174 |
public function testRecentChangesLine_prefix() { |
| 175 |
$mockContext = $this->getMockBuilder( RequestContext::class ) |
| 176 |
->onlyMethods( [ 'getTitle' ] ) |
| 177 |
->getMock(); |
| 178 |
$mockContext->method( 'getTitle' ) |
| 179 |
->willReturn( Title::makeTitle( NS_MAIN, 'Expected Context Title' ) ); |
| 180 |
|
| 181 |
$oldChangesList = $this->getOldChangesList(); |
| 182 |
$oldChangesList->setContext( $mockContext ); |
| 183 |
$recentChange = $this->getEditChange(); |
| 184 |
|
| 185 |
$oldChangesList->setChangeLinePrefixer( function ( $rc, $changesList ) { |
| 186 |
|
| 187 |
$this->assertEquals( 'Expected Context Title', $changesList->getContext()->getTitle() ); |
| 188 |
$this->assertEquals( 'Cat', $rc->getTitle() ); |
| 189 |
return 'I am a prefix'; |
| 190 |
} ); |
| 191 |
$line = $oldChangesList->recentChangesLine( $recentChange ); |
| 192 |
$this->assertMatchesRegularExpression( "/I am a prefix/", $line ); |
| 193 |
} |
| 194 |
|
| 195 |
private function getNewBotEditChange() { |
| 196 |
$user = $this->getMutableTestUser()->getUser(); |
| 197 |
|
| 198 |
$recentChange = $this->testRecentChangesHelper->makeNewBotEditRecentChange( |
| 199 |
$user, 'Abc', '20131103212153', 5, 191, 190, 0, 0 |
| 200 |
); |
| 201 |
|
| 202 |
return $recentChange; |
| 203 |
} |
| 204 |
|
| 205 |
private function getLogChange( $logType, $logAction ) { |
| 206 |
$user = $this->getMutableTestUser()->getUser(); |
| 207 |
|
| 208 |
$recentChange = $this->testRecentChangesHelper->makeLogRecentChange( |
| 209 |
$logType, $logAction, $user, 'Abc', '20131103212153', 0, 0 |
| 210 |
); |
| 211 |
|
| 212 |
return $recentChange; |
| 213 |
} |
| 214 |
|
| 215 |
private function getEditChange() { |
| 216 |
$user = $this->getMutableTestUser()->getUser(); |
| 217 |
$recentChange = $this->testRecentChangesHelper->makeEditRecentChange( |
| 218 |
$user, 'Cat', '20131103212153', 5, 191, 190, 0, 0 |
| 219 |
); |
| 220 |
|
| 221 |
return $recentChange; |
| 222 |
} |
| 223 |
|
| 224 |
private function getOldChangesList() { |
| 225 |
$context = $this->getContext(); |
| 226 |
return new OldChangesList( $context ); |
| 227 |
} |
| 228 |
|
| 229 |
private function getContext() { |
| 230 |
$user = $this->getMutableTestUser()->getUser(); |
| 231 |
$context = $this->testRecentChangesHelper->getTestContext( $user ); |
| 232 |
$context->setLanguage( 'qqx' ); |
| 233 |
|
| 234 |
return $context; |
| 235 |
} |
| 236 |
|
| 237 |
} |