Fixed spacing
[lhc/web/wiklou.git] / tests / phpunit / includes / api / query / ApiQueryTest.php
1 <?php
2
3 /**
4 * @group API
5 * @group Database
6 * @group medium
7 * @covers ApiQuery
8 */
9 class ApiQueryTest extends ApiTestCase {
10 protected function setUp() {
11 parent::setUp();
12 $this->doLogin();
13
14 // Setup apiquerytestiw: as interwiki prefix
15 $this->setMwGlobals( 'wgHooks', array(
16 'InterwikiLoadPrefix' => array(
17 function ( $prefix, &$data ) {
18 if ( $prefix == 'apiquerytestiw' ) {
19 $data = array( 'iw_url' => 'wikipedia' );
20 }
21 return false;
22 }
23 )
24 ) );
25 }
26
27 public function testTitlesGetNormalized() {
28 global $wgMetaNamespace;
29
30 $this->setMwGlobals( array(
31 'wgCapitalLinks' => true,
32 ) );
33
34 $data = $this->doApiRequest( array(
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 array(
46 'from' => 'Project:articleA',
47 'to' => $to->getPrefixedText(),
48 ),
49 $data[0]['query']['normalized'][0]
50 );
51
52 $this->assertEquals(
53 array(
54 'from' => 'article_B',
55 'to' => 'Article B'
56 ),
57 $data[0]['query']['normalized'][1]
58 );
59 }
60
61 public function testTitlesAreRejectedIfInvalid() {
62 $title = false;
63 while ( !$title || Title::newFromText( $title )->exists() ) {
64 $title = md5( mt_rand( 0, 10000 ) + rand( 0, 999000 ) );
65 }
66
67 $data = $this->doApiRequest( array(
68 'action' => 'query',
69 'titles' => $title . '|Talk:' ) );
70
71 $this->assertArrayHasKey( 'query', $data[0] );
72 $this->assertArrayHasKey( 'pages', $data[0]['query'] );
73 $this->assertEquals( 2, count( $data[0]['query']['pages'] ) );
74
75 $this->assertArrayHasKey( -2, $data[0]['query']['pages'] );
76 $this->assertArrayHasKey( -1, $data[0]['query']['pages'] );
77
78 $this->assertArrayHasKey( 'missing', $data[0]['query']['pages'][-2] );
79 $this->assertArrayHasKey( 'invalid', $data[0]['query']['pages'][-1] );
80 }
81
82 /**
83 * Test the ApiBase::titlePartToKey function
84 *
85 * @param string $titlePart
86 * @param int $namespace
87 * @param string $expected
88 * @param string $expectException
89 * @dataProvider provideTestTitlePartToKey
90 */
91 function testTitlePartToKey( $titlePart, $namespace, $expected, $expectException ) {
92 $this->setMwGlobals( array(
93 'wgCapitalLinks' => true,
94 ) );
95
96 $api = new MockApiQueryBase();
97 $exceptionCaught = false;
98 try {
99 $this->assertEquals( $expected, $api->titlePartToKey( $titlePart, $namespace ) );
100 } catch ( UsageException $e ) {
101 $exceptionCaught = true;
102 }
103 $this->assertEquals( $expectException, $exceptionCaught,
104 'UsageException thrown by titlePartToKey' );
105 }
106
107 function provideTestTitlePartToKey() {
108 return array(
109 array( 'a b c', NS_MAIN, 'A_b_c', false ),
110 array( 'x', NS_MAIN, 'X', false ),
111 array( 'y ', NS_MAIN, 'Y_', false ),
112 array( 'template:foo', NS_CATEGORY, 'Template:foo', false ),
113 array( 'apiquerytestiw:foo', NS_CATEGORY, 'Apiquerytestiw:foo', false ),
114 array( "\xF7", NS_MAIN, null, true ),
115 array( 'template:foo', NS_MAIN, null, true ),
116 array( 'apiquerytestiw:foo', NS_MAIN, null, true ),
117 );
118 }
119
120 /**
121 * Test if all classes in the query module manager exists
122 */
123 public function testClassNamesInModuleManager() {
124 global $wgAutoloadLocalClasses, $wgAutoloadClasses;
125
126 // wgAutoloadLocalClasses has precedence, just like in includes/AutoLoader.php
127 $classes = $wgAutoloadLocalClasses + $wgAutoloadClasses;
128
129 $api = new ApiMain(
130 new FauxRequest( array( 'action' => 'query', 'meta' => 'siteinfo' ) )
131 );
132 $queryApi = new ApiQuery( $api, 'query' );
133 $modules = $queryApi->getModuleManager()->getNamesWithClasses();
134 foreach ( $modules as $name => $class ) {
135 $this->assertArrayHasKey(
136 $class,
137 $classes,
138 'Class ' . $class . ' for api module ' . $name . ' not in autoloader (with exact case)'
139 );
140 }
141 }
142 }