Merge "AutoLoader: Use require_once rather than require"
[mediawiki.git] / tests / phpunit / includes / cache / BacklinkCacheTest.php
blob5b960aaedb3ca918f0499265d5a461ec90a4b6d9
1 <?php
3 use MediaWiki\Title\Title;
5 /**
6 * @group Database
7 * @group Cache
8 * @covers \MediaWiki\Cache\BacklinkCache
9 */
10 class BacklinkCacheTest extends MediaWikiIntegrationTestCase {
11 /** @var array */
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]]' );
24 $cascade = 1;
25 $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( self::$backlinkCacheTest['title'] )->doUpdateRestrictions(
26 [ 'edit' => 'sysop' ],
27 [],
28 $cascade,
29 'test',
30 $this->getTestSysop()->getUser()
34 public static function provideCasesForHasLink() {
35 return [
36 [ true, 'BacklinkCacheTest_1', 'pagelinks' ],
37 [ false, 'BacklinkCacheTest_2', 'pagelinks' ],
38 [ true, 'Image:test.png', 'imagelinks' ]
42 /**
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() {
53 return [
54 [ 4, 'BacklinkCacheTest_1', 'pagelinks' ],
55 [ 0, 'BacklinkCacheTest_2', 'pagelinks' ],
56 [ 1, 'Image:test.png', 'imagelinks' ],
60 /**
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() {
71 return [
73 [ 'BacklinkCacheTest_2', 'BacklinkCacheTest_3', 'BacklinkCacheTest_4', 'BacklinkCacheTest_5' ],
74 'BacklinkCacheTest_1',
75 'pagelinks'
78 [ 'BacklinkCacheTest_4', 'BacklinkCacheTest_5' ],
79 'BacklinkCacheTest_1',
80 'pagelinks',
81 'BacklinkCacheTest_4'
84 [ 'BacklinkCacheTest_2', 'BacklinkCacheTest_3' ],
85 'BacklinkCacheTest_1',
86 'pagelinks',
87 false,
88 'BacklinkCacheTest_3'
91 [ 'BacklinkCacheTest_3', 'BacklinkCacheTest_4' ],
92 'BacklinkCacheTest_1',
93 'pagelinks',
94 'BacklinkCacheTest_3',
95 'BacklinkCacheTest_4'
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' ),
128 $this->getDb()
130 $targetRow = [
131 'tl_target_id' => $targetId,
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,
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 );