Move ResultWrapper subclasses to Rdbms
[mediawiki.git] / tests / phpunit / includes / api / query / ApiQueryTest.php
blob8026e5444eae94a81c8999343bd5ecdc1e42571f
1 <?php
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();
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' ];
21 return false;
24 ] );
27 public function testTitlesGetNormalized() {
28 global $wgMetaNamespace;
30 $this->setMwGlobals( [
31 'wgCapitalLinks' => true,
32 ] );
34 $data = $this->doApiRequest( [
35 'action' => 'query',
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' );
44 $this->assertEquals(
46 'fromencoded' => false,
47 'from' => 'Project:articleA',
48 'to' => $to->getPrefixedText(),
50 $data[0]['query']['normalized'][0]
53 $this->assertEquals(
55 'fromencoded' => false,
56 'from' => 'article_B',
57 'to' => 'Article B'
59 $data[0]['query']['normalized'][1]
63 public function testTitlesAreRejectedIfInvalid() {
64 $title = false;
65 while ( !$title || Title::newFromText( $title )->exists() ) {
66 $title = md5( mt_rand( 0, 100000 ) );
69 $data = $this->doApiRequest( [
70 'action' => 'query',
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 /**
85 * Test the ApiBase::titlePartToKey function
87 * @param string $titlePart
88 * @param int $namespace
89 * @param string $expected
90 * @param string $expectException
91 * @dataProvider provideTestTitlePartToKey
93 function testTitlePartToKey( $titlePart, $namespace, $expected, $expectException ) {
94 $this->setMwGlobals( [
95 'wgCapitalLinks' => true,
96 ] );
98 $api = new MockApiQueryBase();
99 $exceptionCaught = false;
100 try {
101 $this->assertEquals( $expected, $api->titlePartToKey( $titlePart, $namespace ) );
102 } catch ( ApiUsageException $e ) {
103 $exceptionCaught = true;
105 $this->assertEquals( $expectException, $exceptionCaught,
106 'ApiUsageException thrown by titlePartToKey' );
109 function provideTestTitlePartToKey() {
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 ],
123 * Test if all classes in the query module manager exists
125 public function testClassNamesInModuleManager() {
126 $api = new ApiMain(
127 new FauxRequest( [ 'action' => 'query', 'meta' => 'siteinfo' ] )
129 $queryApi = new ApiQuery( $api, 'query' );
130 $modules = $queryApi->getModuleManager()->getNamesWithClasses();
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)'