MediaWiki REL1_30
ApiQueryTest.php
Go to the documentation of this file.
1<?php
2
9class ApiQueryTest extends ApiTestCase {
10 protected function setUp() {
11 parent::setUp();
12 $this->doLogin();
13
14 // Setup apiquerytestiw: as interwiki prefix
15 $this->setMwGlobals( 'wgHooks', [
16 'InterwikiLoadPrefix' => [
17 function ( $prefix, &$data ) {
18 if ( $prefix == 'apiquerytestiw' ) {
19 $data = [ 'iw_url' => 'wikipedia' ];
20 }
21 return false;
22 }
23 ]
24 ] );
25 }
26
27 public function testTitlesGetNormalized() {
28 global $wgMetaNamespace;
29
30 $this->setMwGlobals( [
31 'wgCapitalLinks' => true,
32 ] );
33
34 $data = $this->doApiRequest( [
35 'action' => 'query',
36 'titles' => 'Project:articleA|article_B' ] );
37
38 $this->assertArrayHasKey( 'query', $data[0] );
39 $this->assertArrayHasKey( 'normalized', $data[0]['query'] );
40
41 // Forge a normalized title
42 $to = Title::newFromText( $wgMetaNamespace . ':ArticleA' );
43
44 $this->assertEquals(
45 [
46 'fromencoded' => false,
47 'from' => 'Project:articleA',
48 'to' => $to->getPrefixedText(),
49 ],
50 $data[0]['query']['normalized'][0]
51 );
52
53 $this->assertEquals(
54 [
55 'fromencoded' => false,
56 'from' => 'article_B',
57 'to' => 'Article B'
58 ],
59 $data[0]['query']['normalized'][1]
60 );
61 }
62
64 $title = false;
65 while ( !$title || Title::newFromText( $title )->exists() ) {
66 $title = md5( mt_rand( 0, 100000 ) );
67 }
68
69 $data = $this->doApiRequest( [
70 'action' => 'query',
71 'titles' => $title . '|Talk:' ] );
72
73 $this->assertArrayHasKey( 'query', $data[0] );
74 $this->assertArrayHasKey( 'pages', $data[0]['query'] );
75 $this->assertEquals( 2, count( $data[0]['query']['pages'] ) );
76
77 $this->assertArrayHasKey( -2, $data[0]['query']['pages'] );
78 $this->assertArrayHasKey( -1, $data[0]['query']['pages'] );
79
80 $this->assertArrayHasKey( 'missing', $data[0]['query']['pages'][-2] );
81 $this->assertArrayHasKey( 'invalid', $data[0]['query']['pages'][-1] );
82 }
83
93 function testTitlePartToKey( $titlePart, $namespace, $expected, $expectException ) {
94 $this->setMwGlobals( [
95 'wgCapitalLinks' => true,
96 ] );
97
98 $api = new MockApiQueryBase();
99 $exceptionCaught = false;
100 try {
101 $this->assertEquals( $expected, $api->titlePartToKey( $titlePart, $namespace ) );
102 } catch ( ApiUsageException $e ) {
103 $exceptionCaught = true;
104 }
105 $this->assertEquals( $expectException, $exceptionCaught,
106 'ApiUsageException thrown by titlePartToKey' );
107 }
108
110 return [
111 [ 'a b c', NS_MAIN, 'A_b_c', false ],
112 [ 'x', NS_MAIN, 'X', false ],
113 [ 'y ', NS_MAIN, 'Y_', false ],
114 [ 'template:foo', NS_CATEGORY, 'Template:foo', false ],
115 [ 'apiquerytestiw:foo', NS_CATEGORY, 'Apiquerytestiw:foo', false ],
116 [ "\xF7", NS_MAIN, null, true ],
117 [ 'template:foo', NS_MAIN, null, true ],
118 [ 'apiquerytestiw:foo', NS_MAIN, null, true ],
119 ];
120 }
121
126 $api = new ApiMain(
127 new FauxRequest( [ 'action' => 'query', 'meta' => 'siteinfo' ] )
128 );
129 $queryApi = new ApiQuery( $api, 'query' );
130 $modules = $queryApi->getModuleManager()->getNamesWithClasses();
131
132 foreach ( $modules as $name => $class ) {
133 $this->assertTrue(
134 class_exists( $class ),
135 'Class ' . $class . ' for api module ' . $name . ' does not exist (with exact case)'
136 );
137 }
138 }
139}
$wgMetaNamespace
Name of the project namespace.
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:45
API Database medium ApiQuery.
testTitlesAreRejectedIfInvalid()
testClassNamesInModuleManager()
Test if all classes in the query module manager exists.
testTitlePartToKey( $titlePart, $namespace, $expected, $expectException)
Test the ApiBase::titlePartToKey function.
This is the main query class.
Definition ApiQuery.php:40
doLogin( $testUser='sysop')
doApiRequest(array $params, array $session=null, $appendModule=false, User $user=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:1976
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:2146
const NS_MAIN
Definition Defines.php:65
const NS_CATEGORY
Definition Defines.php:79