| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Title\Title; |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
class BacklinkCacheTest extends MediaWikiIntegrationTestCase { |
| 11 |
|
| 12 |
private static $backlinkCacheTest; |
| 13 |
|
| 14 |
public function addDBDataOnce() { |
| 15 |
$this->insertPage( 'Template:BacklinkCacheTestA', 'wooooooo' ); |
| 16 |
$this->insertPage( 'Template:BacklinkCacheTestB', '{{BacklinkCacheTestA}}' ); |
| 17 |
|
| 18 |
self::$backlinkCacheTest = $this->insertPage( 'BacklinkCacheTest_1', '{{BacklinkCacheTestB}}' ); |
| 19 |
$this->insertPage( 'BacklinkCacheTest_2', '[[BacklinkCacheTest_1]] [[Image:test.png]]' ); |
| 20 |
$this->insertPage( 'BacklinkCacheTest_3', '[[BacklinkCacheTest_1]]' ); |
| 21 |
$this->insertPage( 'BacklinkCacheTest_4', '[[BacklinkCacheTest_1]]' ); |
| 22 |
$this->insertPage( 'BacklinkCacheTest_5', '[[BacklinkCacheTest_1]]' ); |
| 23 |
|
| 24 |
$cascade = 1; |
| 25 |
$this->getServiceContainer()->getWikiPageFactory()->newFromTitle( self::$backlinkCacheTest['title'] )->doUpdateRestrictions( |
| 26 |
[ 'edit' => 'sysop' ], |
| 27 |
[], |
| 28 |
$cascade, |
| 29 |
'test', |
| 30 |
$this->getTestSysop()->getUser() |
| 31 |
); |
| 32 |
} |
| 33 |
|
| 34 |
public static function provideCasesForHasLink() { |
| 35 |
return [ |
| 36 |
[ true, 'BacklinkCacheTest_1', 'pagelinks' ], |
| 37 |
[ false, 'BacklinkCacheTest_2', 'pagelinks' ], |
| 38 |
[ true, 'Image:test.png', 'imagelinks' ] |
| 39 |
]; |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public function testHasLink( bool $expected, string $title, string $table, string $msg = '' ) { |
| 47 |
$blcFactory = $this->getServiceContainer()->getBacklinkCacheFactory(); |
| 48 |
$backlinkCache = $blcFactory->getBacklinkCache( Title::newFromText( $title ) ); |
| 49 |
$this->assertEquals( $expected, $backlinkCache->hasLinks( $table ), $msg ); |
| 50 |
} |
| 51 |
|
| 52 |
public static function provideCasesForGetNumLinks() { |
| 53 |
return [ |
| 54 |
[ 4, 'BacklinkCacheTest_1', 'pagelinks' ], |
| 55 |
[ 0, 'BacklinkCacheTest_2', 'pagelinks' ], |
| 56 |
[ 1, 'Image:test.png', 'imagelinks' ], |
| 57 |
]; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public function testGetNumLinks( int $numLinks, string $title, string $table ) { |
| 65 |
$blcFactory = $this->getServiceContainer()->getBacklinkCacheFactory(); |
| 66 |
$backlinkCache = $blcFactory->getBacklinkCache( Title::newFromText( $title ) ); |
| 67 |
$this->assertEquals( $numLinks, $backlinkCache->getNumLinks( $table ) ); |
| 68 |
} |
| 69 |
|
| 70 |
public static function provideCasesForGetLinks() { |
| 71 |
return [ |
| 72 |
[ |
| 73 |
[ 'BacklinkCacheTest_2', 'BacklinkCacheTest_3', 'BacklinkCacheTest_4', 'BacklinkCacheTest_5' ], |
| 74 |
'BacklinkCacheTest_1', |
| 75 |
'pagelinks' |
| 76 |
], |
| 77 |
[ |
| 78 |
[ 'BacklinkCacheTest_4', 'BacklinkCacheTest_5' ], |
| 79 |
'BacklinkCacheTest_1', |
| 80 |
'pagelinks', |
| 81 |
'BacklinkCacheTest_4' |
| 82 |
], |
| 83 |
[ |
| 84 |
[ 'BacklinkCacheTest_2', 'BacklinkCacheTest_3' ], |
| 85 |
'BacklinkCacheTest_1', |
| 86 |
'pagelinks', |
| 87 |
false, |
| 88 |
'BacklinkCacheTest_3' |
| 89 |
], |
| 90 |
[ |
| 91 |
[ 'BacklinkCacheTest_3', 'BacklinkCacheTest_4' ], |
| 92 |
'BacklinkCacheTest_1', |
| 93 |
'pagelinks', |
| 94 |
'BacklinkCacheTest_3', |
| 95 |
'BacklinkCacheTest_4' |
| 96 |
], |
| 97 |
[ [ 'BacklinkCacheTest_2' ], 'BacklinkCacheTest_1', 'pagelinks', false, false, 1 ], |
| 98 |
[ [], 'BacklinkCacheTest_2', 'pagelinks' ], |
| 99 |
[ [ 'BacklinkCacheTest_2' ], 'Image:test.png', 'imagelinks' ], |
| 100 |
]; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public function testGetLinkPages( |
| 108 |
array $expectedTitles, string $title, string $table, $startId = false, $endId = false, $max = INF |
| 109 |
) { |
| 110 |
$startId = $startId ? Title::newFromText( $startId )->getId() : false; |
| 111 |
$endId = $endId ? Title::newFromText( $endId )->getId() : false; |
| 112 |
$blcFactory = $this->getServiceContainer()->getBacklinkCacheFactory(); |
| 113 |
$backlinkCache = $blcFactory->getBacklinkCache( Title::newFromText( $title ) ); |
| 114 |
$titlesArray = iterator_to_array( $backlinkCache->getLinkPages( $table, $startId, $endId, $max ) ); |
| 115 |
$this->assertSameSize( $expectedTitles, $titlesArray ); |
| 116 |
$numOfTitles = count( $titlesArray ); |
| 117 |
for ( $i = 0; $i < $numOfTitles; $i++ ) { |
| 118 |
$this->assertEquals( $expectedTitles[$i], $titlesArray[$i]->getDbKey() ); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public function testPartition() { |
| 126 |
$targetId = $this->getServiceContainer()->getLinkTargetLookup()->acquireLinkTargetId( |
| 127 |
Title::makeTitle( NS_MAIN, 'BLCTest1234' ), |
| 128 |
$this->getDb() |
| 129 |
); |
| 130 |
$targetRow = [ |
| 131 |
'tl_target_id' => $targetId, |
| 132 |
]; |
| 133 |
$this->getDb()->newInsertQueryBuilder() |
| 134 |
->insertInto( 'templatelinks' ) |
| 135 |
->rows( [ |
| 136 |
[ 'tl_from' => 56890, 'tl_from_namespace' => 0 ] + $targetRow, |
| 137 |
[ 'tl_from' => 56891, 'tl_from_namespace' => 0 ] + $targetRow, |
| 138 |
[ 'tl_from' => 56892, 'tl_from_namespace' => 0 ] + $targetRow, |
| 139 |
[ 'tl_from' => 56893, 'tl_from_namespace' => 0 ] + $targetRow, |
| 140 |
[ 'tl_from' => 56894, 'tl_from_namespace' => 0 ] + $targetRow, |
| 141 |
] ) |
| 142 |
->caller( __METHOD__ ) |
| 143 |
->execute(); |
| 144 |
$blcFactory = $this->getServiceContainer()->getBacklinkCacheFactory(); |
| 145 |
$backlinkCache = $blcFactory->getBacklinkCache( Title::makeTitle( NS_MAIN, 'BLCTest1234' ) ); |
| 146 |
$partition = $backlinkCache->partition( 'templatelinks', 2 ); |
| 147 |
$this->assertArrayEquals( [ |
| 148 |
[ false, 56891 ], |
| 149 |
[ 56892, 56893 ], |
| 150 |
[ 56894, false ] |
| 151 |
], $partition ); |
| 152 |
} |
| 153 |
|
| 154 |
} |