MediaWiki REL1_31
ApiQueryTest.php
Go to the documentation of this file.
1<?php
2
9class ApiQueryTest extends ApiTestCase {
10 protected function setUp() {
11 parent::setUp();
12
13 // Setup apiquerytestiw: as interwiki prefix
14 $this->setMwGlobals( 'wgHooks', [
15 'InterwikiLoadPrefix' => [
16 function ( $prefix, &$data ) {
17 if ( $prefix == 'apiquerytestiw' ) {
18 $data = [ 'iw_url' => 'wikipedia' ];
19 }
20 return false;
21 }
22 ]
23 ] );
24 }
25
26 public function testTitlesGetNormalized() {
27 global $wgMetaNamespace;
28
29 $this->setMwGlobals( [
30 'wgCapitalLinks' => true,
31 ] );
32
33 $data = $this->doApiRequest( [
34 'action' => 'query',
35 'titles' => 'Project:articleA|article_B' ] );
36
37 $this->assertArrayHasKey( 'query', $data[0] );
38 $this->assertArrayHasKey( 'normalized', $data[0]['query'] );
39
40 // Forge a normalized title
41 $to = Title::newFromText( $wgMetaNamespace . ':ArticleA' );
42
43 $this->assertEquals(
44 [
45 'fromencoded' => false,
46 'from' => 'Project:articleA',
47 'to' => $to->getPrefixedText(),
48 ],
49 $data[0]['query']['normalized'][0]
50 );
51
52 $this->assertEquals(
53 [
54 'fromencoded' => false,
55 'from' => 'article_B',
56 'to' => 'Article B'
57 ],
58 $data[0]['query']['normalized'][1]
59 );
60 }
61
63 $title = false;
64 while ( !$title || Title::newFromText( $title )->exists() ) {
65 $title = md5( mt_rand( 0, 100000 ) );
66 }
67
68 $data = $this->doApiRequest( [
69 'action' => 'query',
70 'titles' => $title . '|Talk:' ] );
71
72 $this->assertArrayHasKey( 'query', $data[0] );
73 $this->assertArrayHasKey( 'pages', $data[0]['query'] );
74 $this->assertEquals( 2, count( $data[0]['query']['pages'] ) );
75
76 $this->assertArrayHasKey( -2, $data[0]['query']['pages'] );
77 $this->assertArrayHasKey( -1, $data[0]['query']['pages'] );
78
79 $this->assertArrayHasKey( 'missing', $data[0]['query']['pages'][-2] );
80 $this->assertArrayHasKey( 'invalid', $data[0]['query']['pages'][-1] );
81 }
82
83 public function testTitlesWithWhitespaces() {
84 $data = $this->doApiRequest( [
85 'action' => 'query',
86 'titles' => ' '
87 ] );
88
89 $this->assertArrayHasKey( 'query', $data[0] );
90 $this->assertArrayHasKey( 'pages', $data[0]['query'] );
91 $this->assertEquals( 1, count( $data[0]['query']['pages'] ) );
92 $this->assertArrayHasKey( -1, $data[0]['query']['pages'] );
93 $this->assertArrayHasKey( 'invalid', $data[0]['query']['pages'][-1] );
94 }
95
105 function testTitlePartToKey( $titlePart, $namespace, $expected, $expectException ) {
106 $this->setMwGlobals( [
107 'wgCapitalLinks' => true,
108 ] );
109
110 $api = new MockApiQueryBase();
111 $exceptionCaught = false;
112 try {
113 $this->assertEquals( $expected, $api->titlePartToKey( $titlePart, $namespace ) );
114 } catch ( ApiUsageException $e ) {
115 $exceptionCaught = true;
116 }
117 $this->assertEquals( $expectException, $exceptionCaught,
118 'ApiUsageException thrown by titlePartToKey' );
119 }
120
122 return [
123 [ 'a b c', NS_MAIN, 'A_b_c', false ],
124 [ 'x', NS_MAIN, 'X', false ],
125 [ 'y ', NS_MAIN, 'Y_', false ],
126 [ 'template:foo', NS_CATEGORY, 'Template:foo', false ],
127 [ 'apiquerytestiw:foo', NS_CATEGORY, 'Apiquerytestiw:foo', false ],
128 [ "\xF7", NS_MAIN, null, true ],
129 [ 'template:foo', NS_MAIN, null, true ],
130 [ 'apiquerytestiw:foo', NS_MAIN, null, true ],
131 ];
132 }
133
138 $api = new ApiMain(
139 new FauxRequest( [ 'action' => 'query', 'meta' => 'siteinfo' ] )
140 );
141 $queryApi = new ApiQuery( $api, 'query' );
142 $modules = $queryApi->getModuleManager()->getNamesWithClasses();
143
144 foreach ( $modules as $name => $class ) {
145 $this->assertTrue(
146 class_exists( $class ),
147 'Class ' . $class . ' for api module ' . $name . ' does not exist (with exact case)'
148 );
149 }
150 }
151}
$wgMetaNamespace
Name of the project namespace.
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:43
API Database medium ApiQuery.
testTitlesAreRejectedIfInvalid()
testClassNamesInModuleManager()
Test if all classes in the query module manager exists.
testTitlesWithWhitespaces()
testTitlePartToKey( $titlePart, $namespace, $expected, $expectException)
Test the ApiBase::titlePartToKey function.
This is the main query class.
Definition ApiQuery.php:36
doApiRequest(array $params, array $session=null, $appendModule=false, User $user=null, $tokenType=null)
Does the API request and returns the result.
Exception used to abort API execution with an error.
WebRequest clone which takes values from a provided array.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition hooks.txt:2006
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
returning false will NOT prevent logging $e
Definition hooks.txt:2176
const NS_MAIN
Definition Defines.php:74
const NS_CATEGORY
Definition Defines.php:88