| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Revision; |
| 4 |
|
| 5 |
use MediaWiki\Content\WikitextContent; |
| 6 |
use MediaWiki\Content\WikitextContentHandler; |
| 7 |
use MediaWiki\Page\PageIdentityValue; |
| 8 |
use MediaWiki\Revision\IncompleteRevisionException; |
| 9 |
use MediaWiki\Revision\RevisionAccessException; |
| 10 |
use MediaWiki\Revision\RevisionStore; |
| 11 |
use MediaWiki\Revision\SlotRecord; |
| 12 |
use MediaWiki\Utils\MWTimestamp; |
| 13 |
use MediaWikiIntegrationTestCase; |
| 14 |
use MWException; |
| 15 |
use PHPUnit\Framework\MockObject\MockObject; |
| 16 |
use Wikimedia\Rdbms\IDatabase; |
| 17 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 18 |
use Wikimedia\Rdbms\ILoadBalancer; |
| 19 |
use Wikimedia\Rdbms\LBFactory; |
| 20 |
use Wikimedia\TestingAccessWrapper; |
| 21 |
use Wikimedia\Timestamp\ConvertibleTimestamp; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
class RevisionStoreTest extends MediaWikiIntegrationTestCase { |
| 27 |
|
| 28 |
private function getRevisionStore(): RevisionStore { |
| 29 |
return $this->getServiceContainer()->getRevisionStore(); |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
private function installMockLoadBalancer( IDatabase $db ) { |
| 37 |
$lb = $this->createNoOpMock( |
| 38 |
ILoadBalancer::class, |
| 39 |
[ 'getConnection', 'getLocalDomainID' ] |
| 40 |
); |
| 41 |
|
| 42 |
$lb->method( 'getConnection' )->willReturn( $db ); |
| 43 |
$lb->method( 'getLocalDomainID' )->willReturn( 'fake' ); |
| 44 |
|
| 45 |
$lbf = $this->createNoOpMock( LBFactory::class, [ 'getMainLB', 'getLocalDomainID' ] ); |
| 46 |
$lbf->method( 'getMainLB' )->willReturn( $lb ); |
| 47 |
$lbf->method( 'getLocalDomainID' )->willReturn( 'fake' ); |
| 48 |
|
| 49 |
$this->setService( 'DBLoadBalancerFactory', $lbf ); |
| 50 |
return $lb; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
private function installMockDatabase() { |
| 57 |
$db = $this->getMockBuilder( IDatabase::class ) |
| 58 |
->disableAutoReturnValueGeneration() |
| 59 |
->disableOriginalConstructor()->getMock(); |
| 60 |
|
| 61 |
$db->method( 'getDomainId' )->willReturn( 'fake' ); |
| 62 |
|
| 63 |
$this->installMockLoadBalancer( $db ); |
| 64 |
return $db; |
| 65 |
} |
| 66 |
|
| 67 |
private function getDummyPageRow( $extra = [] ) { |
| 68 |
return (object)( $extra + [ |
| 69 |
'page_id' => 1337, |
| 70 |
'page_namespace' => 0, |
| 71 |
'page_title' => 'Test', |
| 72 |
'page_is_redirect' => 0, |
| 73 |
'page_is_new' => 0, |
| 74 |
'page_touched' => MWTimestamp::now(), |
| 75 |
'page_links_updated' => MWTimestamp::now(), |
| 76 |
'page_latest' => 23948576, |
| 77 |
'page_len' => 2323, |
| 78 |
'page_content_model' => CONTENT_MODEL_WIKITEXT, |
| 79 |
'page_lang' => null, |
| 80 |
] ); |
| 81 |
} |
| 82 |
|
| 83 |
public function testGetTitle_successFromPageId() { |
| 84 |
$db = $this->installMockDatabase(); |
| 85 |
|
| 86 |
|
| 87 |
$db |
| 88 |
->method( 'selectRow' ) |
| 89 |
->with( |
| 90 |
[ 'page' ], |
| 91 |
$this->anything(), |
| 92 |
[ 'page_id' => 1 ] |
| 93 |
) |
| 94 |
->willReturn( $this->getDummyPageRow( [ |
| 95 |
'page_id' => '1', |
| 96 |
'page_namespace' => '3', |
| 97 |
'page_title' => 'Food', |
| 98 |
] ) ); |
| 99 |
|
| 100 |
$store = $this->getRevisionStore(); |
| 101 |
$title = $store->getTitle( 1, 2, IDBAccessObject::READ_NORMAL ); |
| 102 |
|
| 103 |
$this->assertSame( 3, $title->getNamespace() ); |
| 104 |
$this->assertSame( 'Food', $title->getDBkey() ); |
| 105 |
} |
| 106 |
|
| 107 |
public function testGetTitle_successFromPageIdOnFallback() { |
| 108 |
$db = $this->installMockDatabase(); |
| 109 |
|
| 110 |
$selectRowArgs = [ |
| 111 |
[ |
| 112 |
|
| 113 |
[ 'page' ], |
| 114 |
[ 'page_id' => 1 ], |
| 115 |
false, |
| 116 |
], |
| 117 |
[ |
| 118 |
|
| 119 |
[ 0 => 'page', 'revision' => 'revision' ], |
| 120 |
[ 'rev_id' => 2 ], |
| 121 |
false, |
| 122 |
], |
| 123 |
[ |
| 124 |
|
| 125 |
[ 'page' ], |
| 126 |
[ 'page_id' => 1 ], |
| 127 |
$this->getDummyPageRow( [ |
| 128 |
'page_namespace' => '2', |
| 129 |
'page_title' => 'Foodey', |
| 130 |
] ) |
| 131 |
] |
| 132 |
]; |
| 133 |
$db->expects( $this->exactly( 3 ) ) |
| 134 |
->method( 'selectRow' ) |
| 135 |
->willReturnCallback( function ( $table, $vars, $conds ) use ( &$selectRowArgs ) { |
| 136 |
[ $nextTable, $nextConds, $returnValue ] = array_shift( $selectRowArgs ); |
| 137 |
$this->assertSame( $nextTable, $table ); |
| 138 |
$this->assertSame( $nextConds, $conds ); |
| 139 |
return $returnValue; |
| 140 |
} ); |
| 141 |
|
| 142 |
$store = $this->getRevisionStore(); |
| 143 |
$title = $store->getTitle( 1, 2, IDBAccessObject::READ_NORMAL ); |
| 144 |
|
| 145 |
$this->assertSame( 2, $title->getNamespace() ); |
| 146 |
$this->assertSame( 'Foodey', $title->getDBkey() ); |
| 147 |
} |
| 148 |
|
| 149 |
public function testGetTitle_successFromRevId() { |
| 150 |
$db = $this->installMockDatabase(); |
| 151 |
|
| 152 |
$selectRowArgs = [ |
| 153 |
[ |
| 154 |
[ 'page' ], |
| 155 |
[ 'page_id' => 1 ], |
| 156 |
false, |
| 157 |
], |
| 158 |
[ |
| 159 |
[ 0 => 'page', 'revision' => 'revision' ], |
| 160 |
[ 'rev_id' => 2 ], |
| 161 |
$this->getDummyPageRow( [ |
| 162 |
'page_namespace' => '1', |
| 163 |
'page_title' => 'Food2', |
| 164 |
] ) |
| 165 |
] |
| 166 |
]; |
| 167 |
|
| 168 |
|
| 169 |
$db->expects( $this->exactly( 2 ) ) |
| 170 |
->method( 'selectRow' ) |
| 171 |
->willReturnCallback( function ( $table, $vars, $conds ) use ( &$selectRowArgs ) { |
| 172 |
[ $nextTable, $nextConds, $returnValue ] = array_shift( $selectRowArgs ); |
| 173 |
$this->assertSame( $nextTable, $table ); |
| 174 |
$this->assertSame( $nextConds, $conds ); |
| 175 |
return $returnValue; |
| 176 |
} ); |
| 177 |
|
| 178 |
$store = $this->getRevisionStore(); |
| 179 |
$title = $store->getTitle( 1, 2, IDBAccessObject::READ_NORMAL ); |
| 180 |
|
| 181 |
$this->assertSame( 1, $title->getNamespace() ); |
| 182 |
$this->assertSame( 'Food2', $title->getDBkey() ); |
| 183 |
} |
| 184 |
|
| 185 |
public function testGetTitle_successFromRevIdOnFallback() { |
| 186 |
$db = $this->installMockDatabase(); |
| 187 |
|
| 188 |
$selectRowArgs = [ |
| 189 |
[ |
| 190 |
|
| 191 |
[ 'page' ], |
| 192 |
[ 'page_id' => 1 ], |
| 193 |
false, |
| 194 |
], |
| 195 |
[ |
| 196 |
|
| 197 |
[ 0 => 'page', 'revision' => 'revision' ], |
| 198 |
[ 'rev_id' => 2 ], |
| 199 |
false, |
| 200 |
], |
| 201 |
[ |
| 202 |
|
| 203 |
[ 'page' ], |
| 204 |
[ 'page_id' => 1 ], |
| 205 |
false, |
| 206 |
], |
| 207 |
[ |
| 208 |
|
| 209 |
[ 0 => 'page', 'revision' => 'revision' ], |
| 210 |
[ 'rev_id' => 2 ], |
| 211 |
$this->getDummyPageRow( [ |
| 212 |
'page_namespace' => '2', |
| 213 |
'page_title' => 'Foodey', |
| 214 |
] ) |
| 215 |
] |
| 216 |
]; |
| 217 |
$db->expects( $this->exactly( 4 ) ) |
| 218 |
->method( 'selectRow' ) |
| 219 |
->willReturnCallback( function ( $table, $vars, $conds ) use ( &$selectRowArgs ) { |
| 220 |
[ $nextTable, $nextConds, $returnValue ] = array_shift( $selectRowArgs ); |
| 221 |
$this->assertSame( $nextTable, $table ); |
| 222 |
$this->assertSame( $nextConds, $conds ); |
| 223 |
return $returnValue; |
| 224 |
} ); |
| 225 |
|
| 226 |
$store = $this->getRevisionStore(); |
| 227 |
$title = $store->getTitle( 1, 2, IDBAccessObject::READ_NORMAL ); |
| 228 |
|
| 229 |
$this->assertSame( 2, $title->getNamespace() ); |
| 230 |
$this->assertSame( 'Foodey', $title->getDBkey() ); |
| 231 |
} |
| 232 |
|
| 233 |
public function testGetTitle_correctFallbackAndthrowsExceptionAfterFallbacks() { |
| 234 |
$db = $this->createMock( IDatabase::class ); |
| 235 |
$mockLoadBalancer = $this->installMockLoadBalancer( $db ); |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
$mockLoadBalancer->expects( $this->exactly( 4 ) ) |
| 241 |
->method( 'getConnection' ) |
| 242 |
->willReturnCallback( function ( $masterOrReplica ) use ( $db ) { |
| 243 |
static $callCounter = 0; |
| 244 |
$callCounter++; |
| 245 |
|
| 246 |
if ( $callCounter < 3 ) { |
| 247 |
$this->assertSame( DB_REPLICA, $masterOrReplica ); |
| 248 |
} else { |
| 249 |
$this->assertSame( DB_PRIMARY, $masterOrReplica ); |
| 250 |
} |
| 251 |
return $db; |
| 252 |
} ); |
| 253 |
|
| 254 |
|
| 255 |
$selectRowArgs = [ |
| 256 |
[ |
| 257 |
[ 'page' ], |
| 258 |
[ 'page_id' => 1 ] |
| 259 |
], |
| 260 |
[ |
| 261 |
[ 0 => 'page', 'revision' => 'revision' ], |
| 262 |
[ 'rev_id' => 2 ] |
| 263 |
], |
| 264 |
[ |
| 265 |
[ 'page' ], |
| 266 |
[ 'page_id' => 1 ] |
| 267 |
], |
| 268 |
[ |
| 269 |
[ 0 => 'page', 'revision' => 'revision' ], |
| 270 |
[ 'rev_id' => 2 ] |
| 271 |
] |
| 272 |
]; |
| 273 |
$db->expects( $this->exactly( 4 ) ) |
| 274 |
->method( 'selectRow' ) |
| 275 |
->willReturnCallback( function ( $table, $vars, $conds ) use ( &$selectRowArgs ) { |
| 276 |
[ $nextTable, $nextConds ] = array_shift( $selectRowArgs ); |
| 277 |
$this->assertSame( $nextTable, $table ); |
| 278 |
$this->assertSame( $nextConds, $conds ); |
| 279 |
return false; |
| 280 |
} ); |
| 281 |
|
| 282 |
$store = $this->getRevisionStore( $mockLoadBalancer ); |
| 283 |
|
| 284 |
$this->expectException( RevisionAccessException::class ); |
| 285 |
$store->getTitle( 1, 2, IDBAccessObject::READ_NORMAL ); |
| 286 |
} |
| 287 |
|
| 288 |
public static function provideIsRevisionRow() { |
| 289 |
yield 'invalid row type' => [ |
| 290 |
'row' => new class() { |
| 291 |
}, |
| 292 |
'expect' => false, |
| 293 |
]; |
| 294 |
yield 'invalid row' => [ |
| 295 |
'row' => (object)[ 'blabla' => 'bla' ], |
| 296 |
'expect' => false, |
| 297 |
]; |
| 298 |
yield 'valid row' => [ |
| 299 |
'row' => (object)[ |
| 300 |
'rev_id' => 321, |
| 301 |
'rev_page' => 123, |
| 302 |
'rev_timestamp' => ConvertibleTimestamp::now(), |
| 303 |
'rev_minor_edit' => 0, |
| 304 |
'rev_deleted' => 0, |
| 305 |
'rev_len' => 10, |
| 306 |
'rev_parent_id' => 123, |
| 307 |
'rev_sha1' => 'abc', |
| 308 |
'rev_comment_text' => 'blabla', |
| 309 |
'rev_comment_data' => 'blablabla', |
| 310 |
'rev_comment_cid' => 1, |
| 311 |
'rev_actor' => 1, |
| 312 |
'rev_user' => 1, |
| 313 |
'rev_user_text' => 'alala', |
| 314 |
], |
| 315 |
'expect' => true, |
| 316 |
]; |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
public function testIsRevisionRow( $row, bool $expect ) { |
| 323 |
$this->assertSame( $expect, $this->getRevisionStore()->isRevisionRow( $row ) ); |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
public function testFailOnNull() { |
| 329 |
$revStore = TestingAccessWrapper::newFromObject( $this->getRevisionStore() ); |
| 330 |
|
| 331 |
$this->assertSame( 123, $revStore->failOnNull( 123, 'value' ) ); |
| 332 |
|
| 333 |
|
| 334 |
$this->expectException( IncompleteRevisionException::class ); |
| 335 |
$revStore->failOnNull( null, 'value' ); |
| 336 |
} |
| 337 |
|
| 338 |
public static function provideFailOnEmpty() { |
| 339 |
yield 'null' => [ null ]; |
| 340 |
yield 'zero' => [ 0 ]; |
| 341 |
yield 'empty string' => [ '' ]; |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
public function testFailOnEmpty( $emptyValue ) { |
| 348 |
$revStore = TestingAccessWrapper::newFromObject( $this->getRevisionStore() ); |
| 349 |
$this->expectException( IncompleteRevisionException::class ); |
| 350 |
$revStore->failOnEmpty( $emptyValue, 'value' ); |
| 351 |
} |
| 352 |
|
| 353 |
public function testFailOnEmpty_pass() { |
| 354 |
$revStore = TestingAccessWrapper::newFromObject( $this->getRevisionStore() ); |
| 355 |
$this->assertSame( 123, $revStore->failOnEmpty( 123, 'value' ) ); |
| 356 |
} |
| 357 |
|
| 358 |
public static function provideCheckContent() { |
| 359 |
yield 'unsupported format' => [ |
| 360 |
false, |
| 361 |
false, |
| 362 |
'Can\'t use format text/x-wiki with content model wikitext on [0:Example] role main' |
| 363 |
]; |
| 364 |
yield 'invalid content' => [ |
| 365 |
true, |
| 366 |
false, |
| 367 |
'New content for [0:Example] role main is not valid! Content model is wikitext' |
| 368 |
]; |
| 369 |
yield 'valid content' => [ true, true, null ]; |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
public function testCheckContent( bool $isSupported, bool $isValid, ?string $error ) { |
| 376 |
$revStore = TestingAccessWrapper::newFromObject( $this->getRevisionStore() ); |
| 377 |
$contentHandler = $this->createMock( WikitextContentHandler::class ); |
| 378 |
$contentHandler->method( 'isSupportedFormat' )->willReturn( $isSupported ); |
| 379 |
$content = $this->createMock( WikitextContent::class ); |
| 380 |
$content->method( 'getModel' )->willReturn( CONTENT_MODEL_WIKITEXT ); |
| 381 |
$content->method( 'getDefaultFormat' )->willReturn( CONTENT_FORMAT_WIKITEXT ); |
| 382 |
$content->method( 'getContentHandler' )->willReturn( $contentHandler ); |
| 383 |
$content->method( 'isValid' )->willReturn( $isValid ); |
| 384 |
|
| 385 |
if ( $error !== null ) { |
| 386 |
$this->expectException( MWException::class ); |
| 387 |
$this->expectExceptionMessage( $error ); |
| 388 |
} |
| 389 |
$revStore->checkContent( |
| 390 |
$content, |
| 391 |
new PageIdentityValue( 0, NS_MAIN, 'Example', PageIdentityValue::LOCAL ), |
| 392 |
SlotRecord::MAIN |
| 393 |
); |
| 394 |
|
| 395 |
$this->addToAssertionCount( 1 ); |
| 396 |
} |
| 397 |
} |