Merge "Add SPARQL client to core"
[lhc/web/wiklou.git] / tests / phpunit / includes / api / query / ApiQueryBasicTest.php
1 <?php
2 /**
3 * Copyright © 2013 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * These tests validate basic functionality of the api query module
25 *
26 * @group API
27 * @group Database
28 * @group medium
29 * @covers ApiQuery
30 */
31 class ApiQueryBasicTest extends ApiQueryTestBase {
32 protected $exceptionFromAddDBData;
33
34 /**
35 * Create a set of pages. These must not change, otherwise the tests might give wrong results.
36 *
37 *@see MediaWikiTestCase::addDBDataOnce()
38 */
39 function addDBDataOnce() {
40 try {
41 if ( Title::newFromText( 'AQBT-All' )->exists() ) {
42 return;
43 }
44
45 // Ordering is important, as it will be returned in the same order as stored in the index
46 $this->editPage( 'AQBT-All', '[[Category:AQBT-Cat]] [[AQBT-Links]] {{AQBT-T}}' );
47 $this->editPage( 'AQBT-Categories', '[[Category:AQBT-Cat]]' );
48 $this->editPage( 'AQBT-Links', '[[AQBT-All]] [[AQBT-Categories]] [[AQBT-Templates]]' );
49 $this->editPage( 'AQBT-Templates', '{{AQBT-T}}' );
50 $this->editPage( 'AQBT-T', 'Content', '', NS_TEMPLATE );
51
52 // Refresh due to the bug with listing transclusions as links if they don't exist
53 $this->editPage( 'AQBT-All', '[[Category:AQBT-Cat]] [[AQBT-Links]] {{AQBT-T}}' );
54 $this->editPage( 'AQBT-Templates', '{{AQBT-T}}' );
55 } catch ( Exception $e ) {
56 $this->exceptionFromAddDBData = $e;
57 }
58 }
59
60 private static $links = [
61 [ 'prop' => 'links', 'titles' => 'AQBT-All' ],
62 [ 'pages' => [
63 '1' => [
64 'pageid' => 1,
65 'ns' => 0,
66 'title' => 'AQBT-All',
67 'links' => [
68 [ 'ns' => 0, 'title' => 'AQBT-Links' ],
69 ]
70 ]
71 ] ]
72 ];
73
74 private static $templates = [
75 [ 'prop' => 'templates', 'titles' => 'AQBT-All' ],
76 [ 'pages' => [
77 '1' => [
78 'pageid' => 1,
79 'ns' => 0,
80 'title' => 'AQBT-All',
81 'templates' => [
82 [ 'ns' => 10, 'title' => 'Template:AQBT-T' ],
83 ]
84 ]
85 ] ]
86 ];
87
88 private static $categories = [
89 [ 'prop' => 'categories', 'titles' => 'AQBT-All' ],
90 [ 'pages' => [
91 '1' => [
92 'pageid' => 1,
93 'ns' => 0,
94 'title' => 'AQBT-All',
95 'categories' => [
96 [ 'ns' => 14, 'title' => 'Category:AQBT-Cat' ],
97 ]
98 ]
99 ] ]
100 ];
101
102 private static $allpages = [
103 [ 'list' => 'allpages', 'apprefix' => 'AQBT-' ],
104 [ 'allpages' => [
105 [ 'pageid' => 1, 'ns' => 0, 'title' => 'AQBT-All' ],
106 [ 'pageid' => 2, 'ns' => 0, 'title' => 'AQBT-Categories' ],
107 [ 'pageid' => 3, 'ns' => 0, 'title' => 'AQBT-Links' ],
108 [ 'pageid' => 4, 'ns' => 0, 'title' => 'AQBT-Templates' ],
109 ] ]
110 ];
111
112 private static $alllinks = [
113 [ 'list' => 'alllinks', 'alprefix' => 'AQBT-' ],
114 [ 'alllinks' => [
115 [ 'ns' => 0, 'title' => 'AQBT-All' ],
116 [ 'ns' => 0, 'title' => 'AQBT-Categories' ],
117 [ 'ns' => 0, 'title' => 'AQBT-Links' ],
118 [ 'ns' => 0, 'title' => 'AQBT-Templates' ],
119 ] ]
120 ];
121
122 private static $alltransclusions = [
123 [ 'list' => 'alltransclusions', 'atprefix' => 'AQBT-' ],
124 [ 'alltransclusions' => [
125 [ 'ns' => 10, 'title' => 'Template:AQBT-T' ],
126 [ 'ns' => 10, 'title' => 'Template:AQBT-T' ],
127 ] ]
128 ];
129
130 // Although this appears to have no use it is used by testLists()
131 private static $allcategories = [
132 [ 'list' => 'allcategories', 'acprefix' => 'AQBT-' ],
133 [ 'allcategories' => [
134 [ '*' => 'AQBT-Cat' ],
135 ] ]
136 ];
137
138 private static $backlinks = [
139 [ 'list' => 'backlinks', 'bltitle' => 'AQBT-Links' ],
140 [ 'backlinks' => [
141 [ 'pageid' => 1, 'ns' => 0, 'title' => 'AQBT-All' ],
142 ] ]
143 ];
144
145 private static $embeddedin = [
146 [ 'list' => 'embeddedin', 'eititle' => 'Template:AQBT-T' ],
147 [ 'embeddedin' => [
148 [ 'pageid' => 1, 'ns' => 0, 'title' => 'AQBT-All' ],
149 [ 'pageid' => 4, 'ns' => 0, 'title' => 'AQBT-Templates' ],
150 ] ]
151 ];
152
153 private static $categorymembers = [
154 [ 'list' => 'categorymembers', 'cmtitle' => 'Category:AQBT-Cat' ],
155 [ 'categorymembers' => [
156 [ 'pageid' => 1, 'ns' => 0, 'title' => 'AQBT-All' ],
157 [ 'pageid' => 2, 'ns' => 0, 'title' => 'AQBT-Categories' ],
158 ] ]
159 ];
160
161 private static $generatorAllpages = [
162 [ 'generator' => 'allpages', 'gapprefix' => 'AQBT-' ],
163 [ 'pages' => [
164 '1' => [
165 'pageid' => 1,
166 'ns' => 0,
167 'title' => 'AQBT-All' ],
168 '2' => [
169 'pageid' => 2,
170 'ns' => 0,
171 'title' => 'AQBT-Categories' ],
172 '3' => [
173 'pageid' => 3,
174 'ns' => 0,
175 'title' => 'AQBT-Links' ],
176 '4' => [
177 'pageid' => 4,
178 'ns' => 0,
179 'title' => 'AQBT-Templates' ],
180 ] ]
181 ];
182
183 private static $generatorLinks = [
184 [ 'generator' => 'links', 'titles' => 'AQBT-Links' ],
185 [ 'pages' => [
186 '1' => [
187 'pageid' => 1,
188 'ns' => 0,
189 'title' => 'AQBT-All' ],
190 '2' => [
191 'pageid' => 2,
192 'ns' => 0,
193 'title' => 'AQBT-Categories' ],
194 '4' => [
195 'pageid' => 4,
196 'ns' => 0,
197 'title' => 'AQBT-Templates' ],
198 ] ]
199 ];
200
201 private static $generatorLinksPropLinks = [
202 [ 'prop' => 'links' ],
203 [ 'pages' => [
204 '1' => [ 'links' => [
205 [ 'ns' => 0, 'title' => 'AQBT-Links' ],
206 ] ]
207 ] ]
208 ];
209
210 private static $generatorLinksPropTemplates = [
211 [ 'prop' => 'templates' ],
212 [ 'pages' => [
213 '1' => [ 'templates' => [
214 [ 'ns' => 10, 'title' => 'Template:AQBT-T' ] ] ],
215 '4' => [ 'templates' => [
216 [ 'ns' => 10, 'title' => 'Template:AQBT-T' ] ] ],
217 ] ]
218 ];
219
220 /**
221 * Test basic props
222 */
223 public function testProps() {
224 $this->check( self::$links );
225 $this->check( self::$templates );
226 $this->check( self::$categories );
227 }
228
229 /**
230 * Test basic lists
231 */
232 public function testLists() {
233 $this->check( self::$allpages );
234 $this->check( self::$alllinks );
235 $this->check( self::$alltransclusions );
236 // This test is temporarily disabled until a sqlite bug is fixed
237 // Confirmed still broken 15-nov-2013
238 // $this->check( self::$allcategories );
239 $this->check( self::$backlinks );
240 $this->check( self::$embeddedin );
241 $this->check( self::$categorymembers );
242 }
243
244 /**
245 * Test basic lists
246 */
247 public function testAllTogether() {
248 // All props together
249 $this->check( $this->merge(
250 self::$links,
251 self::$templates,
252 self::$categories
253 ) );
254
255 // All lists together
256 $this->check( $this->merge(
257 self::$allpages,
258 self::$alllinks,
259 self::$alltransclusions,
260 // This test is temporarily disabled until a sqlite bug is fixed
261 // self::$allcategories,
262 self::$backlinks,
263 self::$embeddedin,
264 self::$categorymembers
265 ) );
266
267 // All props+lists together
268 $this->check( $this->merge(
269 self::$links,
270 self::$templates,
271 self::$categories,
272 self::$allpages,
273 self::$alllinks,
274 self::$alltransclusions,
275 // This test is temporarily disabled until a sqlite bug is fixed
276 // self::$allcategories,
277 self::$backlinks,
278 self::$embeddedin,
279 self::$categorymembers
280 ) );
281 }
282
283 /**
284 * Test basic lists
285 */
286 public function testGenerator() {
287 // generator=allpages
288 $this->check( self::$generatorAllpages );
289 // generator=allpages & list=allpages
290 $this->check( $this->merge(
291 self::$generatorAllpages,
292 self::$allpages ) );
293 // generator=links
294 $this->check( self::$generatorLinks );
295 // generator=links & prop=links
296 $this->check( $this->merge(
297 self::$generatorLinks,
298 self::$generatorLinksPropLinks ) );
299 // generator=links & prop=templates
300 $this->check( $this->merge(
301 self::$generatorLinks,
302 self::$generatorLinksPropTemplates ) );
303 // generator=links & prop=links|templates
304 $this->check( $this->merge(
305 self::$generatorLinks,
306 self::$generatorLinksPropLinks,
307 self::$generatorLinksPropTemplates ) );
308 // generator=links & prop=links|templates & list=allpages|...
309 $this->check( $this->merge(
310 self::$generatorLinks,
311 self::$generatorLinksPropLinks,
312 self::$generatorLinksPropTemplates,
313 self::$allpages,
314 self::$alllinks,
315 self::$alltransclusions,
316 // This test is temporarily disabled until a sqlite bug is fixed
317 // self::$allcategories,
318 self::$backlinks,
319 self::$embeddedin,
320 self::$categorymembers ) );
321 }
322
323 /**
324 * Test T53821
325 */
326 public function testGeneratorRedirects() {
327 $this->editPage( 'AQBT-Target', 'test' );
328 $this->editPage( 'AQBT-Redir', '#REDIRECT [[AQBT-Target]]' );
329 $this->check( [
330 [ 'generator' => 'backlinks', 'gbltitle' => 'AQBT-Target', 'redirects' => '1' ],
331 [
332 'redirects' => [
333 [
334 'from' => 'AQBT-Redir',
335 'to' => 'AQBT-Target',
336 ]
337 ],
338 'pages' => [
339 '6' => [
340 'pageid' => 6,
341 'ns' => 0,
342 'title' => 'AQBT-Target',
343 ]
344 ],
345 ]
346 ] );
347 }
348 }