3 use MediaWiki\Title\Title
;
8 * @covers \MediaWiki\Cache\BacklinkCache
10 class BacklinkCacheTest
extends MediaWikiIntegrationTestCase
{
12 private static $backlinkCacheTest;
14 public function addDBDataOnce() {
15 $this->insertPage( 'Template:BacklinkCacheTestA', 'wooooooo' );
16 $this->insertPage( 'Template:BacklinkCacheTestB', '{{BacklinkCacheTestA}}' );
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]]' );
25 $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( self
::$backlinkCacheTest['title'] )->doUpdateRestrictions(
26 [ 'edit' => 'sysop' ],
30 $this->getTestSysop()->getUser()
34 public static function provideCasesForHasLink() {
36 [ true, 'BacklinkCacheTest_1', 'pagelinks' ],
37 [ false, 'BacklinkCacheTest_2', 'pagelinks' ],
38 [ true, 'Image:test.png', 'imagelinks' ]
43 * @dataProvider provideCasesForHasLink
44 * @covers \MediaWiki\Cache\BacklinkCache::hasLinks
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 );
52 public static function provideCasesForGetNumLinks() {
54 [ 4, 'BacklinkCacheTest_1', 'pagelinks' ],
55 [ 0, 'BacklinkCacheTest_2', 'pagelinks' ],
56 [ 1, 'Image:test.png', 'imagelinks' ],
61 * @dataProvider provideCasesForGetNumLinks
62 * @covers \MediaWiki\Cache\BacklinkCache::getNumLinks
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 ) );
70 public static function provideCasesForGetLinks() {
73 [ 'BacklinkCacheTest_2', 'BacklinkCacheTest_3', 'BacklinkCacheTest_4', 'BacklinkCacheTest_5' ],
74 'BacklinkCacheTest_1',
78 [ 'BacklinkCacheTest_4', 'BacklinkCacheTest_5' ],
79 'BacklinkCacheTest_1',
84 [ 'BacklinkCacheTest_2', 'BacklinkCacheTest_3' ],
85 'BacklinkCacheTest_1',
91 [ 'BacklinkCacheTest_3', 'BacklinkCacheTest_4' ],
92 'BacklinkCacheTest_1',
94 'BacklinkCacheTest_3',
97 [ [ 'BacklinkCacheTest_2' ], 'BacklinkCacheTest_1', 'pagelinks', false, false, 1 ],
98 [ [], 'BacklinkCacheTest_2', 'pagelinks' ],
99 [ [ 'BacklinkCacheTest_2' ], 'Image:test.png', 'imagelinks' ],
104 * @dataProvider provideCasesForGetLinks
105 * @covers \MediaWiki\Cache\BacklinkCache::getLinkPages
107 public function testGetLinkPages(
108 array $expectedTitles, string $title, string $table, $startId = false, $endId = false, $max = INF
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() );
123 * @covers \MediaWiki\Cache\BacklinkCache::partition
125 public function testPartition() {
126 $targetId = $this->getServiceContainer()->getLinkTargetLookup()->acquireLinkTargetId(
127 Title
::makeTitle( NS_MAIN
, 'BLCTest1234' ),
131 'tl_target_id' => $targetId,
133 $this->getDb()->newInsertQueryBuilder()
134 ->insertInto( 'templatelinks' )
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,
142 ->caller( __METHOD__
)
144 $blcFactory = $this->getServiceContainer()->getBacklinkCacheFactory();
145 $backlinkCache = $blcFactory->getBacklinkCache( Title
::makeTitle( NS_MAIN
, 'BLCTest1234' ) );
146 $partition = $backlinkCache->partition( 'templatelinks', 2 );
147 $this->assertArrayEquals( [