Merge "maintenance: Document secondary purpose of --server"
[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
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
62 public function testTitlesAreRejectedIfInvalid() {
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
96 /**
97 * Test the ApiBase::titlePartToKey function
98 *
99 * @param string $titlePart
100 * @param int $namespace
101 * @param string $expected
102 * @param string $expectException
103 * @dataProvider provideTestTitlePartToKey
104 */
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
121 function provideTestTitlePartToKey() {
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
134 /**
135 * Test if all classes in the query module manager exists
136 */
137 public function testClassNamesInModuleManager() {
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 }