1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
<?php
use MediaWiki\Cache\CacheKeyHelper;
use MediaWiki\Cache\GenderCache;
use MediaWiki\Cache\LinkBatch;
use MediaWiki\Cache\LinkCache;
use MediaWiki\Language\Language;
use MediaWiki\Linker\LinksMigration;
use MediaWiki\Linker\LinkTarget;
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\Page\PageReference;
use MediaWiki\Page\PageReferenceValue;
use MediaWiki\Title\Title;
use MediaWiki\Title\TitleFormatter;
use MediaWiki\Title\TitleValue;
use Wikimedia\Rdbms\IConnectionProvider;
/**
* @group Database
* @group Cache
* @covers \MediaWiki\Cache\LinkBatch
*/
class LinkBatchTest extends MediaWikiIntegrationTestCase {
/**
* @covers \MediaWiki\Cache\LinkBatch::__construct()
* @covers \MediaWiki\Cache\LinkBatch::getSize()
* @covers \MediaWiki\Cache\LinkBatch::isEmpty()
*/
public function testConstructEmptyWithServices() {
$batch = new LinkBatch(
[],
$this->createMock( LinkCache::class ),
$this->createMock( TitleFormatter::class ),
$this->createMock( Language::class ),
$this->createMock( GenderCache::class ),
$this->createMock( IConnectionProvider::class ),
$this->createMock( LinksMigration::class ),
LoggerFactory::getInstance( 'LinkBatch' )
);
$this->assertTrue( $batch->isEmpty() );
$this->assertSame( 0, $batch->getSize() );
}
/**
* @covers \MediaWiki\Cache\LinkBatch::__construct()
* @covers \MediaWiki\Cache\LinkBatch::getSize()
* @covers \MediaWiki\Cache\LinkBatch::isEmpty()
*/
public function testConstructWithServices() {
$batch = new LinkBatch(
[
new TitleValue( NS_MAIN, 'Foo' ),
new TitleValue( NS_TALK, 'Bar' ),
],
$this->createMock( LinkCache::class ),
$this->createMock( TitleFormatter::class ),
$this->createMock( Language::class ),
$this->createMock( GenderCache::class ),
$this->createMock( IConnectionProvider::class ),
$this->createMock( LinksMigration::class ),
LoggerFactory::getInstance( 'LinkBatch' )
);
$this->assertFalse( $batch->isEmpty() );
$this->assertSame( 2, $batch->getSize() );
}
/**
* @param iterable<LinkTarget>|iterable<PageReference> $objects
*
* @return LinkBatch
* @throws Exception
*/
private function newLinkBatch( $objects = [] ) {
return new LinkBatch(
$objects,
$this->createMock( LinkCache::class ),
$this->createMock( TitleFormatter::class ),
$this->createMock( Language::class ),
$this->createMock( GenderCache::class ),
$this->getServiceContainer()->getConnectionProvider(),
$this->getServiceContainer()->getLinksMigration(),
LoggerFactory::getInstance( 'LinkBatch' )
);
}
/**
* @covers \MediaWiki\Cache\LinkBatch::addObj()
* @covers \MediaWiki\Cache\LinkBatch::getSize()
*/
public function testAddObj() {
$batch = $this->newLinkBatch(
[
new TitleValue( NS_MAIN, 'Foo' ),
new PageReferenceValue( NS_USER, 'Foo', PageReference::LOCAL ),
]
);
$batch->addObj( new PageReferenceValue( NS_TALK, 'Bar', PageReference::LOCAL ) );
$batch->addObj( new TitleValue( NS_MAIN, 'Foo' ) );
$this->assertSame( 3, $batch->getSize() );
$this->assertCount( 3, $batch->getPageIdentities() );
}
/**
* @covers \MediaWiki\Cache\LinkBatch::add()
* @covers \MediaWiki\Cache\LinkBatch::getSize()
*/
public function testAdd() {
$batch = $this->newLinkBatch(
[
new TitleValue( NS_MAIN, 'Foo' )
]
);
$batch->add( NS_TALK, 'Bar' );
$batch->add( NS_MAIN, 'Foo' );
$this->assertSame( 2, $batch->getSize() );
$this->assertCount( 2, $batch->getPageIdentities() );
}
public function testExecute() {
$existing1 = $this->getExistingTestPage( __METHOD__ . '1' )->getTitle();
$existing2 = $this->getExistingTestPage( __METHOD__ . '2' )->getTitle();
$nonexisting1 = $this->getNonexistingTestPage( __METHOD__ . 'x' )->getTitle();
$nonexisting2 = $this->getNonexistingTestPage( __METHOD__ . 'y' )->getTitle();
$cache = $this->createMock( LinkCache::class );
$good = [];
$bad = [];
$cache->expects( $this->exactly( 2 ) )
->method( 'addGoodLinkObjFromRow' )
->willReturnCallback( static function ( TitleValue $title, $row ) use ( &$good ) {
$good["$title"] = $title;
} );
$cache->expects( $this->exactly( 2 ) )
->method( 'addBadLinkObj' )
->willReturnCallback( static function ( TitleValue $title ) use ( &$bad ) {
$bad["$title"] = $title;
} );
$services = $this->getServiceContainer();
$batch = new LinkBatch(
[],
$cache,
// TODO: This would be even better with mocked dependencies
$services->getTitleFormatter(),
$services->getContentLanguage(),
$services->getGenderCache(),
$services->getConnectionProvider(),
$services->getLinksMigration(),
LoggerFactory::getInstance( 'LinkBatch' )
);
$batch->addObj( $existing1 );
$batch->addObj( $existing2 );
$batch->addObj( $nonexisting1 );
$batch->addObj( $nonexisting2 );
// Bad stuff, should be skipped!
$batch->add( NS_MAIN, '_X' );
$batch->add( NS_MAIN, 'X_' );
$batch->add( NS_MAIN, '' );
@$batch->execute();
$this->assertArrayHasKey( $existing1->getTitleValue()->__toString(), $good );
$this->assertArrayHasKey( $existing2->getTitleValue()->__toString(), $good );
$this->assertArrayHasKey( $nonexisting1->getTitleValue()->__toString(), $bad );
$this->assertArrayHasKey( $nonexisting2->getTitleValue()->__toString(), $bad );
$expected = array_map(
[ CacheKeyHelper::class, 'getKeyForPage' ],
[ $existing1, $existing2, $nonexisting1, $nonexisting2 ]
);
$actual = array_map(
[ CacheKeyHelper::class, 'getKeyForPage' ],
$batch->getPageIdentities()
);
sort( $expected );
sort( $actual );
$this->assertEquals( $expected, $actual );
}
public function testDoGenderQueryWithEmptyLinkBatch() {
$batch = new LinkBatch(
[],
$this->createMock( LinkCache::class ),
$this->createMock( TitleFormatter::class ),
$this->createNoOpMock( Language::class ),
$this->createNoOpMock( GenderCache::class ),
$this->createMock( IConnectionProvider::class ),
$this->createMock( LinksMigration::class ),
LoggerFactory::getInstance( 'LinkBatch' )
);
$this->assertFalse( $batch->doGenderQuery() );
}
public function testDoGenderQueryWithLanguageWithoutGenderDistinction() {
$language = $this->createMock( Language::class );
$language->method( 'needsGenderDistinction' )->willReturn( false );
$batch = new LinkBatch(
[],
$this->createMock( LinkCache::class ),
$this->createMock( TitleFormatter::class ),
$language,
$this->createNoOpMock( GenderCache::class ),
$this->createMock( IConnectionProvider::class ),
$this->createMock( LinksMigration::class ),
LoggerFactory::getInstance( 'LinkBatch' )
);
$batch->addObj(
new TitleValue( NS_MAIN, 'Foo' )
);
$this->assertFalse( $batch->doGenderQuery() );
}
public function testDoGenderQueryWithLanguageWithGenderDistinction() {
$language = $this->createMock( Language::class );
$language->method( 'needsGenderDistinction' )->willReturn( true );
$genderCache = $this->createMock( GenderCache::class );
$genderCache->expects( $this->once() )->method( 'doLinkBatch' );
$batch = new LinkBatch(
[],
$this->createMock( LinkCache::class ),
$this->createMock( TitleFormatter::class ),
$language,
$genderCache,
$this->createMock( IConnectionProvider::class ),
$this->createMock( LinksMigration::class ),
LoggerFactory::getInstance( 'LinkBatch' )
);
$batch->addObj(
new TitleValue( NS_MAIN, 'Foo' )
);
$this->assertTrue( $batch->doGenderQuery() );
}
public static function provideBadObjects() {
yield 'null' => [ null ];
yield 'empty' => [ Title::makeTitle( NS_MAIN, '' ) ];
yield 'bad user' => [ Title::makeTitle( NS_USER, '#12345' ) ];
yield 'section' => [ new TitleValue( NS_MAIN, '', '#See_also' ) ];
yield 'special' => [ new TitleValue( NS_SPECIAL, 'RecentChanges' ) ];
}
/**
* @dataProvider provideBadObjects
*/
public function testAddBadObj( $obj ) {
$linkBatch = $this->newLinkBatch();
$linkBatch->addObj( $obj );
$linkBatch->execute();
$this->addToAssertionCount( 1 );
}
public static function provideBadDBKeys() {
yield 'empty' => [ '' ];
yield 'section' => [ '#See_also' ];
yield 'pipe' => [ 'foo|bar' ];
}
/**
* @dataProvider provideBadDBKeys
*/
public function testAddBadDBKeys( $key ) {
$linkBatch = $this->newLinkBatch();
$linkBatch->add( NS_MAIN, $key );
$linkBatch->execute();
$this->addToAssertionCount( 1 );
}
}
|