Merge ".mailmap: Correct two contributor names"
[mediawiki.git] / tests / phpunit / includes / page / PageSelectQueryBuilderTest.php
blob701cb254f03caea2d63a8e6ad8a0bcb794bff6ff
1 <?php
2 namespace MediaWiki\Tests\Page;
4 use Exception;
5 use MediaWiki\Config\ServiceOptions;
6 use MediaWiki\MainConfigNames;
7 use MediaWiki\Page\PageSelectQueryBuilder;
8 use MediaWiki\Page\PageStore;
9 use MediaWikiIntegrationTestCase;
11 /**
12 * @group Database
14 class PageSelectQueryBuilderTest extends MediaWikiIntegrationTestCase {
16 public function addDBDataOnce() {
17 $this->getExistingTestPage( 'AA' );
18 $this->getExistingTestPage( 'AB' );
19 $this->getExistingTestPage( 'BB' );
21 $this->getExistingTestPage( 'Talk:AA' );
22 $this->getExistingTestPage( 'User:AB' );
25 /**
26 * @return PageStore
27 * @throws Exception
29 private function getPageStore() {
30 $services = $this->getServiceContainer();
32 $serviceOptions = new ServiceOptions(
33 PageStore::CONSTRUCTOR_OPTIONS,
35 MainConfigNames::LanguageCode => $services->getContentLanguageCode()->toString(),
36 MainConfigNames::PageLanguageUseDB => true,
40 return new PageStore(
41 $serviceOptions,
42 $services->getDBLoadBalancer(),
43 $services->getNamespaceInfo(),
44 $services->getTitleParser(),
45 $services->getLinkCache(),
46 $services->getStatsFactory()
50 /**
51 * @covers \MediaWiki\Page\PageSelectQueryBuilder::wherePageIds
52 * @covers \MediaWiki\Page\PageSelectQueryBuilder::fetchPageRecordArray
54 public function testFetchBatchOfPagesById() {
55 $pageStore = $this->getPageStore();
57 $recAA = $pageStore->getPageByName( NS_MAIN, 'AA' );
58 $recAB = $pageStore->getPageByName( NS_MAIN, 'AB' );
60 $recs = $pageStore->newSelectQueryBuilder()
61 ->wherePageIds( [] )
62 ->fetchPageRecordArray();
64 $this->assertCount( 0, $recs );
66 $recs = $pageStore->newSelectQueryBuilder()
67 ->wherePageIds( [ $recAA->getId(), $recAB->getId() ] )
68 ->fetchPageRecordArray();
70 $this->assertCount( 2, $recs );
71 $this->assertSame( 'AA', $recs[ $recAA->getId() ]->getDBkey() );
72 $this->assertSame( 'AB', $recs[ $recAB->getId() ]->getDBkey() );
75 /**
76 * @covers \MediaWiki\Page\PageSelectQueryBuilder::wherePageIds
77 * @covers \MediaWiki\Page\PageSelectQueryBuilder::fetchPageRecord
79 public function testFetchSinglePageById() {
80 $pageStore = $this->getPageStore();
82 $recAB = $pageStore->getPageByName( NS_MAIN, 'AB' );
84 $rec = $pageStore->newSelectQueryBuilder()
85 ->wherePageIds( $recAB->getId() )
86 ->fetchPageRecord();
88 $this->assertTrue( $recAB->isSamePageAs( $rec ) );
90 $rec = $pageStore->newSelectQueryBuilder()
91 ->wherePageIds( 348529043 )
92 ->fetchPageRecord();
94 $this->assertNull( $rec );
97 /**
98 * @covers \MediaWiki\Page\PageSelectQueryBuilder::whereTitles
99 * @covers \MediaWiki\Page\PageSelectQueryBuilder::fetchPageRecordArray
100 * @covers \MediaWiki\Page\PageSelectQueryBuilder::orderByPageId
102 public function testFindBatchOfPageIdsByTitle() {
103 $pageStore = $this->getPageStore();
105 $recAA = $pageStore->getPageByName( NS_MAIN, 'AA' );
106 $recAB = $pageStore->getPageByName( NS_MAIN, 'AB' );
107 $recAC = $pageStore->getPageByName( NS_MAIN, 'BB' );
109 $recs = $pageStore->newSelectQueryBuilder()
110 ->whereTitles( NS_FILE, [ 'AA', 'AB', 'BB' ] )
111 ->fetchPageIds();
113 $this->assertCount( 0, $recs );
115 $recs = $pageStore->newSelectQueryBuilder()
116 ->whereTitles( NS_MAIN, [ 'AA', 'AB', 'BB' ] )
117 ->orderByPageId( PageSelectQueryBuilder::SORT_DESC )
118 ->fetchPageIds();
120 $expectedIds = [ $recAA->getId(), $recAB->getId(), $recAC->getId() ];
121 sort( $expectedIds );
122 $expectedIds = array_reverse( $expectedIds );
124 $this->assertSame( $expectedIds, $recs );
128 * @covers \MediaWiki\Page\PageSelectQueryBuilder::whereTitles
129 * @covers \MediaWiki\Page\PageSelectQueryBuilder::fetchPageRecord
131 public function testFetchSinglePageByTitle() {
132 $pageStore = $this->getPageStore();
134 $recAB = $pageStore->getPageByName( NS_MAIN, 'AB' );
136 $rec = $pageStore->newSelectQueryBuilder()
137 ->whereTitles( NS_MAIN, 'AB' )
138 ->fetchPageRecord();
140 $this->assertTrue( $recAB->isSamePageAs( $rec ) );
142 // The page should have ended up in the LinkCache
143 $linkCache = $this->getServiceContainer()->getLinkCache();
144 $this->assertSame( $rec->getId(), $linkCache->getGoodLinkID( $rec ) );
146 $rec = $pageStore->newSelectQueryBuilder()
147 ->whereTitles( NS_TALK, 'AB' )
148 ->fetchPageRecord();
150 $this->assertNull( $rec );
154 * @covers \MediaWiki\Page\PageSelectQueryBuilder::whereNamespace
155 * @covers \MediaWiki\Page\PageSelectQueryBuilder::fetchPageRecordArray
157 public function testFilterByNamespace() {
158 $pageStore = $this->getPageStore();
160 $recAA = $pageStore->getPageByName( NS_TALK, 'AA' );
162 $recs = $pageStore->newSelectQueryBuilder()
163 ->whereNamespace( NS_TALK )
164 ->fetchPageRecordArray();
166 $this->assertCount( 1, $recs );
167 $this->assertSame( 'AA', $recs[ $recAA->getId() ]->getDBkey() );
171 * @covers \MediaWiki\Page\PageSelectQueryBuilder::whereTitlePrefix
172 * @covers \MediaWiki\Page\PageSelectQueryBuilder::fetchPageRecords
173 * @covers \MediaWiki\Page\PageSelectQueryBuilder::orderByTitle
175 public function testListPagesByPrefix() {
176 $pageStore = $this->getPageStore();
178 $recs = $pageStore->newSelectQueryBuilder()
179 ->whereTitlePrefix( NS_MAIN, 'A' )
180 ->orderByTitle( PageSelectQueryBuilder::SORT_DESC )
181 ->fetchPageRecords();
183 $recs = iterator_to_array( $recs );
185 $this->assertCount( 2, $recs );
187 // descending order
188 $this->assertSame( 'AB', $recs[0]->getDBkey() );
189 $this->assertSame( 'AA', $recs[1]->getDBkey() );
191 $recs = $pageStore->newSelectQueryBuilder()
192 ->whereTitlePrefix( NS_TALK, 'A' )
193 ->fetchPageRecords();
195 $this->assertCount( 1, iterator_to_array( $recs ) );
197 $recs = $pageStore->newSelectQueryBuilder()
198 ->whereTitlePrefix( NS_MAIN, 'XX' )
199 ->fetchPageRecords();
201 $this->assertCount( 0, iterator_to_array( $recs ) );