| 1 |
<?php |
| 2 |
namespace MediaWiki\Tests\Page; |
| 3 |
|
| 4 |
use Exception; |
| 5 |
use InvalidArgumentException; |
| 6 |
use LinkCacheTestTrait; |
| 7 |
use MediaWiki\Config\ServiceOptions; |
| 8 |
use MediaWiki\DAO\WikiAwareEntity; |
| 9 |
use MediaWiki\MainConfigNames; |
| 10 |
use MediaWiki\Page\PageIdentity; |
| 11 |
use MediaWiki\Page\PageIdentityValue; |
| 12 |
use MediaWiki\Page\PageRecord; |
| 13 |
use MediaWiki\Page\PageStore; |
| 14 |
use MediaWiki\Title\Title; |
| 15 |
use MediaWiki\Title\TitleValue; |
| 16 |
use MediaWikiIntegrationTestCase; |
| 17 |
use MockTitleTrait; |
| 18 |
use Wikimedia\Assert\PreconditionException; |
| 19 |
use Wikimedia\Rdbms\IDatabase; |
| 20 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 21 |
use Wikimedia\Rdbms\LoadBalancer; |
| 22 |
use Wikimedia\Stats\Metrics\MetricInterface; |
| 23 |
use Wikimedia\Stats\StatsFactory; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
class PageStoreTest extends MediaWikiIntegrationTestCase { |
| 29 |
|
| 30 |
use MockTitleTrait; |
| 31 |
use LinkCacheTestTrait; |
| 32 |
|
| 33 |
private StatsFactory $statsFactory; |
| 34 |
private MetricInterface $linkCacheAccesses; |
| 35 |
|
| 36 |
protected function setUp(): void { |
| 37 |
parent::setUp(); |
| 38 |
$this->statsFactory = StatsFactory::newNull(); |
| 39 |
|
| 40 |
$this->linkCacheAccesses = $this->statsFactory->getCounter( 'pagestore_linkcache_accesses_total' ); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
private function getPageStore( $options = [], $params = [] ) { |
| 51 |
$services = $this->getServiceContainer(); |
| 52 |
|
| 53 |
$serviceOptions = new ServiceOptions( |
| 54 |
PageStore::CONSTRUCTOR_OPTIONS, |
| 55 |
$options + [ |
| 56 |
MainConfigNames::LanguageCode => $services->getContentLanguage()->getCode(), |
| 57 |
MainConfigNames::PageLanguageUseDB => true, |
| 58 |
] |
| 59 |
); |
| 60 |
|
| 61 |
return new PageStore( |
| 62 |
$serviceOptions, |
| 63 |
$params['dbLoadBalancer'] ?? $services->getDBLoadBalancer(), |
| 64 |
$services->getNamespaceInfo(), |
| 65 |
$services->getTitleParser(), |
| 66 |
array_key_exists( 'linkCache', $params ) |
| 67 |
? $params['linkCache'] |
| 68 |
: $services->getLinkCache(), |
| 69 |
$this->statsFactory, |
| 70 |
$params['wikiId'] ?? WikiAwareEntity::LOCAL |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
private function assertSamePage( PageIdentity $expected, PageIdentity $actual ) { |
| 79 |
|
| 80 |
|
| 81 |
$expWiki = $expected->getWikiId(); |
| 82 |
$actWiki = $actual->getWikiId(); |
| 83 |
|
| 84 |
$this->assertSame( $expected->getId( $expWiki ), $actual->getId( $actWiki ) ); |
| 85 |
$this->assertSame( $expected->getNamespace(), $actual->getNamespace() ); |
| 86 |
$this->assertSame( $expected->getDBkey(), $actual->getDBkey() ); |
| 87 |
|
| 88 |
if ( $expected instanceof PageRecord ) { |
| 89 |
$this->assertInstanceOf( PageRecord::class, $actual ); |
| 90 |
|
| 91 |
|
| 92 |
$this->assertSame( $expected->getLatest( $expWiki ), $actual->getLatest( $actWiki ) ); |
| 93 |
$this->assertSame( $expected->getLanguage(), $actual->getLanguage() ); |
| 94 |
$this->assertSame( $expected->getTouched(), $actual->getTouched() ); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public function testGetPageForLink_nonexisting() { |
| 103 |
$nonexistingPage = $this->getNonexistingTestPage(); |
| 104 |
$pageStore = $this->getPageStore(); |
| 105 |
|
| 106 |
$page = $pageStore->getPageForLink( $nonexistingPage->getTitle() ); |
| 107 |
|
| 108 |
$this->assertTrue( $page->canExist() ); |
| 109 |
$this->assertFalse( $page->exists() ); |
| 110 |
|
| 111 |
$this->assertSamePage( $nonexistingPage->getTitle(), $page ); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public function testGetPageForLink_existing() { |
| 119 |
$existingPage = $this->getExistingTestPage(); |
| 120 |
$pageStore = $this->getPageStore(); |
| 121 |
|
| 122 |
$page = $pageStore->getPageForLink( $existingPage->getTitle() ); |
| 123 |
|
| 124 |
$this->assertTrue( $page->exists() ); |
| 125 |
$this->assertInstanceOf( PageRecord::class, $page ); |
| 126 |
$this->assertSamePage( $existingPage->toPageRecord(), $page ); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public function testGetPageForLink_crossWiki() { |
| 134 |
$wikiId = $this->getDb()->getDomainID(); |
| 135 |
|
| 136 |
$nonexistingPage = $this->getNonexistingTestPage(); |
| 137 |
$pageStore = $this->getPageStore( [], [ 'wikiId' => $wikiId, 'linkCache' => null ] ); |
| 138 |
|
| 139 |
$page = $pageStore->getPageForLink( $nonexistingPage->getTitle() ); |
| 140 |
|
| 141 |
$this->assertSame( $wikiId, $page->getWikiId() ); |
| 142 |
$this->assertSamePage( $nonexistingPage->getTitle(), $page ); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public function testGetPageForLink_media() { |
| 150 |
$link = new TitleValue( NS_MEDIA, 'Test913847659234.jpg' ); |
| 151 |
$pageStore = $this->getPageStore(); |
| 152 |
|
| 153 |
$page = $pageStore->getPageForLink( $link ); |
| 154 |
|
| 155 |
$this->assertTrue( $page->canExist() ); |
| 156 |
$this->assertSame( NS_FILE, $page->getNamespace() ); |
| 157 |
$this->assertSame( $link->getDBkey(), $page->getDBkey() ); |
| 158 |
} |
| 159 |
|
| 160 |
public static function provideInvalidLinks() { |
| 161 |
yield 'section link' => [ new TitleValue( NS_MAIN, '', '#References' ) ]; |
| 162 |
yield 'special page' => [ new TitleValue( NS_SPECIAL, 'Test' ) ]; |
| 163 |
yield 'interwiki link' => [ new TitleValue( NS_MAIN, 'Test', '', 'acme' ) ]; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public function testGetPageForLink_invalid( $link ) { |
| 174 |
$pageStore = $this->getPageStore(); |
| 175 |
|
| 176 |
$this->expectException( InvalidArgumentException::class ); |
| 177 |
$pageStore->getPageForLink( $link ); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public function testGetPageByName_existing() { |
| 185 |
$existingPage = $this->getExistingTestPage(); |
| 186 |
$ns = $existingPage->getNamespace(); |
| 187 |
$dbkey = $existingPage->getDBkey(); |
| 188 |
|
| 189 |
$pageStore = $this->getPageStore(); |
| 190 |
$page = $pageStore->getPageByName( $ns, $dbkey ); |
| 191 |
|
| 192 |
$this->assertTrue( $page->exists() ); |
| 193 |
$this->assertSamePage( $existingPage, $page ); |
| 194 |
|
| 195 |
$linkCache = $this->getServiceContainer()->getLinkCache(); |
| 196 |
$this->assertSame( $page->getId(), $linkCache->getGoodLinkID( $page ) ); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public function testGetPageByName_existing_noLinkCache() { |
| 205 |
$existingPage = $this->getExistingTestPage(); |
| 206 |
$ns = $existingPage->getNamespace(); |
| 207 |
$dbkey = $existingPage->getDBkey(); |
| 208 |
|
| 209 |
$pageStore = $this->getPageStore( [], [ 'linkCache' => null ] ); |
| 210 |
$page = $pageStore->getPageByName( $ns, $dbkey ); |
| 211 |
|
| 212 |
$this->assertTrue( $page->exists() ); |
| 213 |
$this->assertSamePage( $existingPage, $page ); |
| 214 |
|
| 215 |
$linkCache = $this->getServiceContainer()->getLinkCache(); |
| 216 |
$this->assertSame( $page->getId(), $linkCache->getGoodLinkID( $page ) ); |
| 217 |
$this->assertSame( 0, $this->linkCacheAccesses->getSampleCount() ); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
public function testGetPageByName_nonexisting() { |
| 225 |
$nonexistingPage = $this->getNonexistingTestPage(); |
| 226 |
$ns = $nonexistingPage->getNamespace(); |
| 227 |
$dbkey = $nonexistingPage->getDBkey(); |
| 228 |
|
| 229 |
$pageStore = $this->getPageStore(); |
| 230 |
$page = $pageStore->getPageByName( $ns, $dbkey ); |
| 231 |
|
| 232 |
$this->assertNull( $page ); |
| 233 |
|
| 234 |
$linkCache = $this->getServiceContainer()->getLinkCache(); |
| 235 |
$this->assertTrue( $linkCache->isBadLink( $nonexistingPage ) ); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public function testGetPageByName_nonexisting_cached() { |
| 244 |
$nonexistingPage = $this->getNonexistingTestPage(); |
| 245 |
$ns = $nonexistingPage->getNamespace(); |
| 246 |
$dbkey = $nonexistingPage->getDBkey(); |
| 247 |
|
| 248 |
$linkCache = $this->getServiceContainer()->getLinkCache(); |
| 249 |
$linkCache->addBadLinkObj( $nonexistingPage ); |
| 250 |
|
| 251 |
$mockLB = $this->createNoOpMock( LoadBalancer::class ); |
| 252 |
$pageStore = $this->getPageStore( [], [ 'doLoadBalancer' => $mockLB ] ); |
| 253 |
$page = $pageStore->getPageByName( $ns, $dbkey ); |
| 254 |
|
| 255 |
$this->assertNull( $page ); |
| 256 |
$this->assertTrue( $linkCache->isBadLink( $nonexistingPage ) ); |
| 257 |
$this->assertSame( 1, $this->linkCacheAccesses->getSampleCount() ); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
public function testGetPageByName_cachedFullRow() { |
| 265 |
$nonexistingPage = $this->getNonexistingTestPage(); |
| 266 |
$ns = $nonexistingPage->getNamespace(); |
| 267 |
$dbkey = $nonexistingPage->getDBkey(); |
| 268 |
|
| 269 |
$row = (object)[ |
| 270 |
'page_id' => 8, |
| 271 |
'page_namespace' => $ns, |
| 272 |
'page_title' => $dbkey, |
| 273 |
'page_is_redirect' => 0, |
| 274 |
'page_is_new' => 1, |
| 275 |
'page_touched' => '12345', |
| 276 |
'page_links_updated' => '12345', |
| 277 |
'page_latest' => 118, |
| 278 |
'page_len' => 155, |
| 279 |
'page_content_model' => CONTENT_FORMAT_TEXT, |
| 280 |
'page_lang' => 'xyz', |
| 281 |
]; |
| 282 |
|
| 283 |
$linkCache = $this->getServiceContainer()->getLinkCache(); |
| 284 |
$linkCache->addGoodLinkObjFromRow( $nonexistingPage, $row ); |
| 285 |
|
| 286 |
$mockLB = $this->createNoOpMock( LoadBalancer::class ); |
| 287 |
|
| 288 |
$pageStore = $this->getPageStore( [], [ 'doLoadBalancer' => $mockLB ] ); |
| 289 |
$page = $pageStore->getPageByName( $ns, $dbkey ); |
| 290 |
|
| 291 |
$this->assertSame( $row->page_id, $page->getId() ); |
| 292 |
$this->assertSame( $row->page_namespace, $page->getNamespace() ); |
| 293 |
$this->assertSame( $row->page_title, $page->getDBkey() ); |
| 294 |
$this->assertSame( $row->page_latest, $page->getLatest() ); |
| 295 |
$this->assertSame( 1, $this->linkCacheAccesses->getSampleCount() ); |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
public function testGetPageByName_cachedFakeRow() { |
| 303 |
$nonexistingPage = $this->getNonexistingTestPage(); |
| 304 |
$ns = $nonexistingPage->getNamespace(); |
| 305 |
$dbkey = $nonexistingPage->getDBkey(); |
| 306 |
|
| 307 |
$linkCache = $this->getServiceContainer()->getLinkCache(); |
| 308 |
$linkCache->clearLink( $nonexistingPage ); |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
$this->addGoodLinkObject( 8, $nonexistingPage ); |
| 313 |
|
| 314 |
$pageStore = $this->getPageStore(); |
| 315 |
$page = $pageStore->getPageByName( $ns, $dbkey ); |
| 316 |
|
| 317 |
$this->assertSame( 8, $page->getId() ); |
| 318 |
$this->assertSame( $nonexistingPage->getNamespace(), $page->getNamespace() ); |
| 319 |
$this->assertSame( $nonexistingPage->getDBkey(), $page->getDBkey() ); |
| 320 |
$this->assertSame( 1, $this->linkCacheAccesses->getSampleCount() ); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
public function testGetPageByText_existing() { |
| 327 |
$existingPage = $this->getExistingTestPage(); |
| 328 |
|
| 329 |
$pageStore = $this->getPageStore(); |
| 330 |
$page = $pageStore->getPageByText( $existingPage->getTitle()->getPrefixedText() ); |
| 331 |
|
| 332 |
$this->assertTrue( $page->exists() ); |
| 333 |
$this->assertSamePage( $existingPage, $page ); |
| 334 |
|
| 335 |
$page = $pageStore->getExistingPageByText( $existingPage->getTitle()->getPrefixedText() ); |
| 336 |
|
| 337 |
$this->assertTrue( $page->exists() ); |
| 338 |
$this->assertSamePage( $existingPage, $page ); |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
public function testGetPageByText_nonexisting() { |
| 345 |
$nonexistingPage = $this->getNonexistingTestPage(); |
| 346 |
$pageStore = $this->getPageStore(); |
| 347 |
$page = $pageStore->getPageByText( $nonexistingPage->getTitle()->getPrefixedText() ); |
| 348 |
$this->assertFalse( $page->exists() ); |
| 349 |
$this->assertTrue( $nonexistingPage->isSamePageAs( $page ) ); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
public function testGetExistingPageByText_existing() { |
| 356 |
$existingPage = $this->getExistingTestPage(); |
| 357 |
|
| 358 |
$pageStore = $this->getPageStore(); |
| 359 |
$page = $pageStore->getExistingPageByText( $existingPage->getTitle()->getPrefixedText() ); |
| 360 |
|
| 361 |
$this->assertTrue( $page->exists() ); |
| 362 |
$this->assertSamePage( $existingPage, $page ); |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
public function testGetExistingPageByText_nonexisting() { |
| 369 |
$nonexistingPage = $this->getNonexistingTestPage(); |
| 370 |
$pageStore = $this->getPageStore(); |
| 371 |
$page = $pageStore->getExistingPageByText( $nonexistingPage->getTitle()->getPrefixedText() ); |
| 372 |
$this->assertNull( $page ); |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
private function setDomainAlias( $wikiId ) { |
| 381 |
$dbLoadBalancer = $this->getServiceContainer()->getDBLoadBalancer(); |
| 382 |
$dbLoadBalancer->setDomainAliases( [ $wikiId => $dbLoadBalancer->getLocalDomainID() ] ); |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
public function testGetPageByName_crossWiki() { |
| 390 |
$wikiId = 'acme'; |
| 391 |
$this->setDomainAlias( $wikiId ); |
| 392 |
|
| 393 |
$existingPage = $this->getExistingTestPage(); |
| 394 |
$ns = $existingPage->getNamespace(); |
| 395 |
$dbkey = $existingPage->getDBkey(); |
| 396 |
|
| 397 |
$pageStore = $this->getPageStore( [], [ 'wikiId' => $wikiId, 'linkCache' => null ] ); |
| 398 |
$page = $pageStore->getPageByName( $ns, $dbkey ); |
| 399 |
|
| 400 |
$this->assertSame( $wikiId, $page->getWikiId() ); |
| 401 |
$this->assertSamePage( $existingPage, $page ); |
| 402 |
} |
| 403 |
|
| 404 |
public static function provideGetPageByName_invalid() { |
| 405 |
yield 'empty title' => [ NS_MAIN, '' ]; |
| 406 |
yield 'spaces in title' => [ NS_MAIN, 'Foo Bar' ]; |
| 407 |
yield 'special page' => [ NS_SPECIAL, 'Test' ]; |
| 408 |
yield 'media link' => [ NS_MEDIA, 'Test' ]; |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
public function testGetPageByName_invalid( $ns, $dbkey ) { |
| 419 |
$pageStore = $this->getPageStore(); |
| 420 |
|
| 421 |
$this->expectException( InvalidArgumentException::class ); |
| 422 |
$pageStore->getPageByName( $ns, $dbkey ); |
| 423 |
} |
| 424 |
|
| 425 |
public static function provideInvalidTitleText() { |
| 426 |
yield 'empty' => [ '' ]; |
| 427 |
yield 'section' => [ '#foo' ]; |
| 428 |
yield 'autoblock' => [ 'User:#12345' ]; |
| 429 |
yield 'special' => [ 'Special:RecentChanges' ]; |
| 430 |
yield 'invalid' => [ 'foo|bar' ]; |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
public function testGetPageByText_invalid( $text ) { |
| 438 |
$pageStore = $this->getPageStore(); |
| 439 |
$page = $pageStore->getPageByText( $text ); |
| 440 |
$this->assertNull( $page ); |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
public function testGetExistingPageByText_invalid( $text ) { |
| 448 |
$pageStore = $this->getPageStore(); |
| 449 |
$page = $pageStore->getExistingPageByText( $text ); |
| 450 |
$this->assertNull( $page ); |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
public function testGetPageById_existing() { |
| 458 |
$existingPage = $this->getExistingTestPage(); |
| 459 |
|
| 460 |
$pageStore = $this->getPageStore(); |
| 461 |
$page = $pageStore->getPageById( $existingPage->getId() ); |
| 462 |
|
| 463 |
$this->assertTrue( $page->exists() ); |
| 464 |
$this->assertSamePage( $existingPage, $page ); |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
public function testGetPageById_nonexisting() { |
| 472 |
$pageStore = $this->getPageStore(); |
| 473 |
$page = $pageStore->getPageById( 813451092 ); |
| 474 |
|
| 475 |
$this->assertNull( $page ); |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
public function testGetPageById_crossWiki() { |
| 483 |
$wikiId = 'acme'; |
| 484 |
$this->setDomainAlias( $wikiId ); |
| 485 |
|
| 486 |
$existingPage = $this->getExistingTestPage(); |
| 487 |
|
| 488 |
$pageStore = $this->getPageStore( [], [ 'wikiId' => $wikiId, 'linkCache' => null ] ); |
| 489 |
$page = $pageStore->getPageById( $existingPage->getId() ); |
| 490 |
|
| 491 |
$this->assertSame( $wikiId, $page->getWikiId() ); |
| 492 |
$this->assertSamePage( $existingPage, $page ); |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
public function testGetPageById_noLanguage() { |
| 500 |
$existingPage = $this->getExistingTestPage(); |
| 501 |
|
| 502 |
$pageStore = $this->getPageStore(); |
| 503 |
$page = $pageStore->getPageById( $existingPage->getId() ); |
| 504 |
|
| 505 |
$this->assertNull( $page->getLanguage() ); |
| 506 |
} |
| 507 |
|
| 508 |
public static function provideGetPageById_invalid() { |
| 509 |
yield 'zero' => [ 0 ]; |
| 510 |
yield 'negative' => [ -1 ]; |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
public function testGetPageById_invalid( $id ) { |
| 520 |
$pageStore = $this->getPageStore(); |
| 521 |
|
| 522 |
$this->expectException( InvalidArgumentException::class ); |
| 523 |
$pageStore->getPageById( $id ); |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
public function testGetPageByIdentity_existing() { |
| 532 |
$existingPage = $this->getExistingTestPage(); |
| 533 |
$identity = $existingPage->getTitle()->toPageIdentity(); |
| 534 |
|
| 535 |
$pageStore = $this->getPageStore(); |
| 536 |
$page = $pageStore->getPageByReference( $identity ); |
| 537 |
|
| 538 |
$this->assertTrue( $page->exists() ); |
| 539 |
$this->assertSamePage( $existingPage, $page ); |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
public function testGetPageByIdentity_cached() { |
| 549 |
$title = $this->makeMockTitle( __METHOD__, [ 'id' => 23 ] ); |
| 550 |
$this->addGoodLinkObject( 23, $title ); |
| 551 |
|
| 552 |
$pageStore = $this->getPageStore(); |
| 553 |
$page = $pageStore->getPageByReference( $title ); |
| 554 |
|
| 555 |
$this->assertNotNull( $page ); |
| 556 |
$this->assertSame( 23, $page->getId() ); |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
public function testGetPageByIdentity_knowNonexisting() { |
| 565 |
$nonexistingPage = new PageIdentityValue( 0, NS_MAIN, 'Test', PageIdentity::LOCAL ); |
| 566 |
|
| 567 |
$pageStore = $this->getPageStore(); |
| 568 |
$page = $pageStore->getPageByReference( $nonexistingPage ); |
| 569 |
|
| 570 |
$this->assertNull( $page ); |
| 571 |
} |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
public function testGetPageByIdentity_notFound() { |
| 579 |
$nonexistingPage = new PageIdentityValue( 523478562, NS_MAIN, 'Test', PageIdentity::LOCAL ); |
| 580 |
|
| 581 |
$pageStore = $this->getPageStore(); |
| 582 |
$page = $pageStore->getPageByReference( $nonexistingPage ); |
| 583 |
|
| 584 |
$this->assertNull( $page ); |
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
public function testGetPageByIdentity_PageRecord() { |
| 593 |
$existingPage = $this->getExistingTestPage(); |
| 594 |
$rec = $existingPage->toPageRecord(); |
| 595 |
|
| 596 |
$pageStore = $this->getPageStore(); |
| 597 |
$page = $pageStore->getPageByReference( $rec ); |
| 598 |
|
| 599 |
$this->assertSame( $rec, $page ); |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
public function testGetPageByIdentity_crossWiki() { |
| 608 |
$wikiId = 'acme'; |
| 609 |
$this->setDomainAlias( $wikiId ); |
| 610 |
|
| 611 |
$existingPage = $this->getExistingTestPage(); |
| 612 |
|
| 613 |
$identity = new PageIdentityValue( |
| 614 |
$existingPage->getId(), |
| 615 |
$existingPage->getNamespace(), |
| 616 |
$existingPage->getDBkey(), |
| 617 |
$wikiId |
| 618 |
); |
| 619 |
|
| 620 |
$pageStore = $this->getPageStore( [], [ 'wikiId' => $wikiId, 'linkCache' => null ] ); |
| 621 |
$page = $pageStore->getPageByReference( $identity ); |
| 622 |
|
| 623 |
$this->assertSame( $wikiId, $page->getWikiId() ); |
| 624 |
$this->assertSamePage( $existingPage, $page ); |
| 625 |
} |
| 626 |
|
| 627 |
public function provideGetPageByIdentity_invalid() { |
| 628 |
yield 'section' => [ |
| 629 |
$this->makeMockTitle( '', [ 'fragment' => 'See also' ] ), |
| 630 |
InvalidArgumentException::class |
| 631 |
]; |
| 632 |
yield 'special' => [ |
| 633 |
$this->makeMockTitle( 'Blankpage', [ 'namespace' => NS_SPECIAL ] ), |
| 634 |
InvalidArgumentException::class |
| 635 |
]; |
| 636 |
yield 'interwiki' => [ |
| 637 |
$this->makeMockTitle( 'Foo', [ 'interwiki' => 'acme' ] ), |
| 638 |
InvalidArgumentException::class |
| 639 |
]; |
| 640 |
|
| 641 |
$identity = new PageIdentityValue( 7, NS_MAIN, 'Test', 'acme' ); |
| 642 |
yield 'cross-wiki' => [ $identity, PreconditionException::class ]; |
| 643 |
} |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
public function testGetPageByIdentity_invalid( $identity, $exception ) { |
| 652 |
$pageStore = $this->getPageStore(); |
| 653 |
|
| 654 |
$this->expectException( $exception ); |
| 655 |
$pageStore->getPageByReference( $identity ); |
| 656 |
} |
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
public function testNewPageRecordFromRow() { |
| 663 |
$existingPage = $this->getExistingTestPage(); |
| 664 |
$pageStore = $this->getPageStore(); |
| 665 |
|
| 666 |
$row = $this->getDb()->newSelectQueryBuilder() |
| 667 |
->select( $pageStore->getSelectFields() ) |
| 668 |
->from( 'page' ) |
| 669 |
->where( [ 'page_id' => $existingPage->getId() ] ) |
| 670 |
->fetchRow(); |
| 671 |
|
| 672 |
$rec = $pageStore->newPageRecordFromRow( $row ); |
| 673 |
$this->assertSamePage( $existingPage, $rec ); |
| 674 |
} |
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
public function testNewSelectQueryBuilder() { |
| 680 |
$existingPage = $this->getExistingTestPage(); |
| 681 |
|
| 682 |
$wikiId = 'acme'; |
| 683 |
$this->setDomainAlias( $wikiId ); |
| 684 |
|
| 685 |
$pageStore = $this->getPageStore( [], [ 'wikiId' => $wikiId, 'linkCache' => null ] ); |
| 686 |
|
| 687 |
$rec = $pageStore->newSelectQueryBuilder() |
| 688 |
->wherePageIds( $existingPage->getId() ) |
| 689 |
->fetchPageRecord(); |
| 690 |
|
| 691 |
$this->assertSame( $wikiId, $rec->getWikiId() ); |
| 692 |
$this->assertSamePage( $existingPage, $rec ); |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
public function testNewSelectQueryBuilder_passDatabase() { |
| 699 |
$pageStore = $this->getPageStore(); |
| 700 |
|
| 701 |
|
| 702 |
$db = $this->createMock( IDatabase::class ); |
| 703 |
$db->expects( $this->atLeastOnce() )->method( 'selectRow' )->willReturn( false ); |
| 704 |
|
| 705 |
$pageStore->newSelectQueryBuilder( $db ) |
| 706 |
->fetchPageRecord(); |
| 707 |
} |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
public function testNewSelectQueryBuilder_passFlags() { |
| 713 |
|
| 714 |
$db = $this->createMock( IDatabase::class ); |
| 715 |
$db->expects( $this->atLeastOnce() )->method( 'selectRow' )->willReturn( false ); |
| 716 |
|
| 717 |
|
| 718 |
$lb = $this->createMock( LoadBalancer::class ); |
| 719 |
$lb->expects( $this->atLeastOnce() ) |
| 720 |
->method( 'getConnection' ) |
| 721 |
->with( DB_PRIMARY ) |
| 722 |
->willReturn( $db ); |
| 723 |
|
| 724 |
$pageStore = $this->getPageStore( |
| 725 |
[ |
| 726 |
MainConfigNames::LanguageCode => 'qxx', |
| 727 |
MainConfigNames::PageLanguageUseDB => true, |
| 728 |
], |
| 729 |
[ 'dbLoadBalancer' => $lb ] |
| 730 |
); |
| 731 |
|
| 732 |
$pageStore->newSelectQueryBuilder( IDBAccessObject::READ_LATEST ) |
| 733 |
->fetchPageRecord(); |
| 734 |
} |
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
public function testGetSubpages() { |
| 740 |
$existingPage = $this->getExistingTestPage(); |
| 741 |
$title = $existingPage->getTitle(); |
| 742 |
|
| 743 |
$this->overrideConfigValue( |
| 744 |
MainConfigNames::NamespacesWithSubpages, |
| 745 |
[ $title->getNamespace() => true ] |
| 746 |
); |
| 747 |
|
| 748 |
$existingSubpageA = $this->getExistingTestPage( $title->getSubpage( 'A' ) ); |
| 749 |
$existingSubpageB = $this->getExistingTestPage( $title->getSubpage( 'B' ) ); |
| 750 |
|
| 751 |
$notQuiteSubpageTitle = $title->getPrefixedDBkey() . 'X'; |
| 752 |
$this->getExistingTestPage( $notQuiteSubpageTitle ); |
| 753 |
|
| 754 |
$pageStore = $this->getPageStore(); |
| 755 |
|
| 756 |
$subpages = iterator_to_array( $pageStore->getSubpages( $title, 100 ) ); |
| 757 |
|
| 758 |
$this->assertCount( 2, $subpages ); |
| 759 |
$this->assertTrue( $existingSubpageA->isSamePageAs( $subpages[0] ) ); |
| 760 |
$this->assertTrue( $existingSubpageB->isSamePageAs( $subpages[1] ) ); |
| 761 |
|
| 762 |
|
| 763 |
$this->assertCount( 1, iterator_to_array( $pageStore->getSubpages( $title, 1 ) ) ); |
| 764 |
} |
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
public function testGetSubpages_disabled() { |
| 770 |
$this->overrideConfigValue( MainConfigNames::NamespacesWithSubpages, [] ); |
| 771 |
|
| 772 |
$existingPage = $this->getExistingTestPage(); |
| 773 |
$title = $existingPage->getTitle(); |
| 774 |
|
| 775 |
$this->getExistingTestPage( $title->getSubpage( 'A' ) ); |
| 776 |
$this->getExistingTestPage( $title->getSubpage( 'B' ) ); |
| 777 |
|
| 778 |
$pageStore = $this->getPageStore(); |
| 779 |
$this->assertCount( 0, $pageStore->getSubpages( $title, 100 ) ); |
| 780 |
} |
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
public function testGetPageByReferenceTitleExistsHook() { |
| 788 |
$this->setTemporaryHook( 'TitleExists', static function ( $title, &$exists ) { |
| 789 |
$exists = true; |
| 790 |
} ); |
| 791 |
$this->assertNull( |
| 792 |
$this->getPageStore()->getPageByReference( |
| 793 |
Title::newFromText( __METHOD__ ) |
| 794 |
) |
| 795 |
); |
| 796 |
} |
| 797 |
|
| 798 |
} |