Remove messages.inc, rebuildLanguage.php, writeMessagesArray.inc
[mediawiki.git] / tests / phpunit / includes / api / query / ApiQueryTest.php
blobea0b3235ab532b0b7c7c96613fb662d1c74cf183
1 <?php
3 /**
4 * @group API
5 * @group Database
6 * @group medium
7 * @covers ApiQuery
8 */
9 class ApiQueryTest extends ApiTestCase {
10 /**
11 * @var array Storage for $wgHooks
13 protected $hooks;
15 protected function setUp() {
16 global $wgHooks;
18 parent::setUp();
19 $this->doLogin();
21 // Setup en: as interwiki prefix
22 $this->hooks = $wgHooks;
23 $wgHooks['InterwikiLoadPrefix'][] = function ( $prefix, &$data ) {
24 if ( $prefix == 'apiquerytestiw' ) {
25 $data = array( 'iw_url' => 'wikipedia' );
27 return false;
31 protected function tearDown() {
32 global $wgHooks;
33 $wgHooks = $this->hooks;
35 parent::tearDown();
38 public function testTitlesGetNormalized() {
40 global $wgMetaNamespace;
42 $data = $this->doApiRequest( array(
43 'action' => 'query',
44 'titles' => 'Project:articleA|article_B' ) );
46 $this->assertArrayHasKey( 'query', $data[0] );
47 $this->assertArrayHasKey( 'normalized', $data[0]['query'] );
49 // Forge a normalized title
50 $to = Title::newFromText( $wgMetaNamespace . ':ArticleA' );
52 $this->assertEquals(
53 array(
54 'from' => 'Project:articleA',
55 'to' => $to->getPrefixedText(),
57 $data[0]['query']['normalized'][0]
60 $this->assertEquals(
61 array(
62 'from' => 'article_B',
63 'to' => 'Article B'
65 $data[0]['query']['normalized'][1]
69 public function testTitlesAreRejectedIfInvalid() {
70 $title = false;
71 while ( !$title || Title::newFromText( $title )->exists() ) {
72 $title = md5( mt_rand( 0, 10000 ) + rand( 0, 999000 ) );
75 $data = $this->doApiRequest( array(
76 'action' => 'query',
77 'titles' => $title . '|Talk:' ) );
79 $this->assertArrayHasKey( 'query', $data[0] );
80 $this->assertArrayHasKey( 'pages', $data[0]['query'] );
81 $this->assertEquals( 2, count( $data[0]['query']['pages'] ) );
83 $this->assertArrayHasKey( -2, $data[0]['query']['pages'] );
84 $this->assertArrayHasKey( -1, $data[0]['query']['pages'] );
86 $this->assertArrayHasKey( 'missing', $data[0]['query']['pages'][-2] );
87 $this->assertArrayHasKey( 'invalid', $data[0]['query']['pages'][-1] );
90 /**
91 * Test the ApiBase::titlePartToKey function
93 * @param string $titlePart
94 * @param int $namespace
95 * @param string $expected
96 * @param string $description
97 * @dataProvider provideTestTitlePartToKey
99 function testTitlePartToKey( $titlePart, $namespace, $expected, $expectException ) {
100 $api = new MockApiQueryBase();
101 $exceptionCaught = false;
102 try {
103 $this->assertEquals( $expected, $api->titlePartToKey( $titlePart, $namespace ) );
104 } catch ( UsageException $e ) {
105 $exceptionCaught = true;
107 $this->assertEquals( $expectException, $exceptionCaught,
108 'UsageException thrown by titlePartToKey' );
111 function provideTestTitlePartToKey() {
112 return array(
113 array( 'a b c', NS_MAIN, 'A_b_c', false ),
114 array( 'x', NS_MAIN, 'X', false ),
115 array( 'y ', NS_MAIN, 'Y_', false ),
116 array( 'template:foo', NS_CATEGORY, 'Template:foo', false ),
117 array( 'apiquerytestiw:foo', NS_CATEGORY, 'Apiquerytestiw:foo', false ),
118 array( "\xF7", NS_MAIN, null, true ),
119 array( 'template:foo', NS_MAIN, null, true ),
120 array( 'apiquerytestiw:foo', NS_MAIN, null, true ),