9 class ApiQueryTest
extends ApiTestCase
{
10 protected function setUp() {
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' ];
27 public function testTitlesGetNormalized() {
28 global $wgMetaNamespace;
30 $this->setMwGlobals( [
31 'wgCapitalLinks' => true,
34 $data = $this->doApiRequest( [
36 'titles' => 'Project:articleA|article_B' ] );
38 $this->assertArrayHasKey( 'query', $data[0] );
39 $this->assertArrayHasKey( 'normalized', $data[0]['query'] );
41 // Forge a normalized title
42 $to = Title
::newFromText( $wgMetaNamespace . ':ArticleA' );
46 'fromencoded' => false,
47 'from' => 'Project:articleA',
48 'to' => $to->getPrefixedText(),
50 $data[0]['query']['normalized'][0]
55 'fromencoded' => false,
56 'from' => 'article_B',
59 $data[0]['query']['normalized'][1]
63 public function testTitlesAreRejectedIfInvalid() {
65 while ( !$title || Title
::newFromText( $title )->exists() ) {
66 $title = md5( mt_rand( 0, 100000 ) );
69 $data = $this->doApiRequest( [
71 'titles' => $title . '|Talk:' ] );
73 $this->assertArrayHasKey( 'query', $data[0] );
74 $this->assertArrayHasKey( 'pages', $data[0]['query'] );
75 $this->assertEquals( 2, count( $data[0]['query']['pages'] ) );
77 $this->assertArrayHasKey( -2, $data[0]['query']['pages'] );
78 $this->assertArrayHasKey( -1, $data[0]['query']['pages'] );
80 $this->assertArrayHasKey( 'missing', $data[0]['query']['pages'][-2] );
81 $this->assertArrayHasKey( 'invalid', $data[0]['query']['pages'][-1] );
84 public function testTitlesWithWhitespaces() {
85 $data = $this->doApiRequest( [
90 $this->assertArrayHasKey( 'query', $data[0] );
91 $this->assertArrayHasKey( 'pages', $data[0]['query'] );
92 $this->assertEquals( 1, count( $data[0]['query']['pages'] ) );
93 $this->assertArrayHasKey( -1, $data[0]['query']['pages'] );
94 $this->assertArrayHasKey( 'invalid', $data[0]['query']['pages'][-1] );
98 * Test the ApiBase::titlePartToKey function
100 * @param string $titlePart
101 * @param int $namespace
102 * @param string $expected
103 * @param string $expectException
104 * @dataProvider provideTestTitlePartToKey
106 function testTitlePartToKey( $titlePart, $namespace, $expected, $expectException ) {
107 $this->setMwGlobals( [
108 'wgCapitalLinks' => true,
111 $api = new MockApiQueryBase();
112 $exceptionCaught = false;
114 $this->assertEquals( $expected, $api->titlePartToKey( $titlePart, $namespace ) );
115 } catch ( ApiUsageException
$e ) {
116 $exceptionCaught = true;
118 $this->assertEquals( $expectException, $exceptionCaught,
119 'ApiUsageException thrown by titlePartToKey' );
122 function provideTestTitlePartToKey() {
124 [ 'a b c', NS_MAIN
, 'A_b_c', false ],
125 [ 'x', NS_MAIN
, 'X', false ],
126 [ 'y ', NS_MAIN
, 'Y_', false ],
127 [ 'template:foo', NS_CATEGORY
, 'Template:foo', false ],
128 [ 'apiquerytestiw:foo', NS_CATEGORY
, 'Apiquerytestiw:foo', false ],
129 [ "\xF7", NS_MAIN
, null, true ],
130 [ 'template:foo', NS_MAIN
, null, true ],
131 [ 'apiquerytestiw:foo', NS_MAIN
, null, true ],
136 * Test if all classes in the query module manager exists
138 public function testClassNamesInModuleManager() {
140 new FauxRequest( [ 'action' => 'query', 'meta' => 'siteinfo' ] )
142 $queryApi = new ApiQuery( $api, 'query' );
143 $modules = $queryApi->getModuleManager()->getNamesWithClasses();
145 foreach ( $modules as $name => $class ) {
147 class_exists( $class ),
148 'Class ' . $class . ' for api module ' . $name . ' does not exist (with exact case)'