9 class ApiQueryTest
extends ApiTestCase
{
10 protected function setUp() {
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' ];
26 public function testTitlesGetNormalized() {
27 global $wgMetaNamespace;
29 $this->setMwGlobals( [
30 'wgCapitalLinks' => true,
33 $data = $this->doApiRequest( [
35 'titles' => 'Project:articleA|article_B' ] );
37 $this->assertArrayHasKey( 'query', $data[0] );
38 $this->assertArrayHasKey( 'normalized', $data[0]['query'] );
40 // Forge a normalized title
41 $to = Title
::newFromText( $wgMetaNamespace . ':ArticleA' );
45 'fromencoded' => false,
46 'from' => 'Project:articleA',
47 'to' => $to->getPrefixedText(),
49 $data[0]['query']['normalized'][0]
54 'fromencoded' => false,
55 'from' => 'article_B',
58 $data[0]['query']['normalized'][1]
62 public function testTitlesAreRejectedIfInvalid() {
64 while ( !$title || Title
::newFromText( $title )->exists() ) {
65 $title = md5( mt_rand( 0, 100000 ) );
68 $data = $this->doApiRequest( [
70 'titles' => $title . '|Talk:' ] );
72 $this->assertArrayHasKey( 'query', $data[0] );
73 $this->assertArrayHasKey( 'pages', $data[0]['query'] );
74 $this->assertEquals( 2, count( $data[0]['query']['pages'] ) );
76 $this->assertArrayHasKey( -2, $data[0]['query']['pages'] );
77 $this->assertArrayHasKey( -1, $data[0]['query']['pages'] );
79 $this->assertArrayHasKey( 'missing', $data[0]['query']['pages'][-2] );
80 $this->assertArrayHasKey( 'invalid', $data[0]['query']['pages'][-1] );
83 public function testTitlesWithWhitespaces() {
84 $data = $this->doApiRequest( [
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] );
97 * Test the ApiBase::titlePartToKey function
99 * @param string $titlePart
100 * @param int $namespace
101 * @param string $expected
102 * @param string $expectException
103 * @dataProvider provideTestTitlePartToKey
105 function testTitlePartToKey( $titlePart, $namespace, $expected, $expectException ) {
106 $this->setMwGlobals( [
107 'wgCapitalLinks' => true,
110 $api = new MockApiQueryBase();
111 $exceptionCaught = false;
113 $this->assertEquals( $expected, $api->titlePartToKey( $titlePart, $namespace ) );
114 } catch ( ApiUsageException
$e ) {
115 $exceptionCaught = true;
117 $this->assertEquals( $expectException, $exceptionCaught,
118 'ApiUsageException thrown by titlePartToKey' );
121 function provideTestTitlePartToKey() {
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 ],
135 * Test if all classes in the query module manager exists
137 public function testClassNamesInModuleManager() {
139 new FauxRequest( [ 'action' => 'query', 'meta' => 'siteinfo' ] )
141 $queryApi = new ApiQuery( $api, 'query' );
142 $modules = $queryApi->getModuleManager()->getNamesWithClasses();
144 foreach ( $modules as $name => $class ) {
146 class_exists( $class ),
147 'Class ' . $class . ' for api module ' . $name . ' does not exist (with exact case)'
152 public function testShouldNotExportPagesThatUserCanNotRead() {
153 $title = Title
::makeTitle( NS_MAIN
, 'Test article' );
154 $this->insertPage( $title );
156 $this->setTemporaryHook( 'getUserPermissionsErrors',
157 function ( Title
$page, &$user, $action, &$result ) use ( $title ) {
158 if ( $page->equals( $title ) && $action === 'read' ) {
164 $data = $this->doApiRequest( [
166 'titles' => $title->getPrefixedText(),
170 $this->assertArrayHasKey( 'query', $data[0] );
171 $this->assertArrayHasKey( 'export', $data[0]['query'] );
172 // This response field contains an XML document even if no pages were exported
173 $this->assertNotContains( $title->getPrefixedText(), $data[0]['query']['export'] );