MediaWiki  REL1_24
ApiQueryTest.php
Go to the documentation of this file.
00001 <?php
00002 
00009 class ApiQueryTest extends ApiTestCase {
00013     protected $hooks;
00014 
00015     protected function setUp() {
00016         global $wgHooks;
00017 
00018         parent::setUp();
00019         $this->doLogin();
00020 
00021         // Setup en: as interwiki prefix
00022         $this->hooks = $wgHooks;
00023         $wgHooks['InterwikiLoadPrefix'][] = function ( $prefix, &$data ) {
00024             if ( $prefix == 'apiquerytestiw' ) {
00025                 $data = array( 'iw_url' => 'wikipedia' );
00026             }
00027             return false;
00028         };
00029     }
00030 
00031     protected function tearDown() {
00032         global $wgHooks;
00033         $wgHooks = $this->hooks;
00034 
00035         parent::tearDown();
00036     }
00037 
00038     public function testTitlesGetNormalized() {
00039         global $wgMetaNamespace;
00040 
00041         $this->setMwGlobals( array(
00042             'wgCapitalLinks' => true,
00043         ) );
00044 
00045         $data = $this->doApiRequest( array(
00046             'action' => 'query',
00047             'titles' => 'Project:articleA|article_B' ) );
00048 
00049         $this->assertArrayHasKey( 'query', $data[0] );
00050         $this->assertArrayHasKey( 'normalized', $data[0]['query'] );
00051 
00052         // Forge a normalized title
00053         $to = Title::newFromText( $wgMetaNamespace . ':ArticleA' );
00054 
00055         $this->assertEquals(
00056             array(
00057                 'from' => 'Project:articleA',
00058                 'to' => $to->getPrefixedText(),
00059             ),
00060             $data[0]['query']['normalized'][0]
00061         );
00062 
00063         $this->assertEquals(
00064             array(
00065                 'from' => 'article_B',
00066                 'to' => 'Article B'
00067             ),
00068             $data[0]['query']['normalized'][1]
00069         );
00070     }
00071 
00072     public function testTitlesAreRejectedIfInvalid() {
00073         $title = false;
00074         while ( !$title || Title::newFromText( $title )->exists() ) {
00075             $title = md5( mt_rand( 0, 10000 ) + rand( 0, 999000 ) );
00076         }
00077 
00078         $data = $this->doApiRequest( array(
00079             'action' => 'query',
00080             'titles' => $title . '|Talk:' ) );
00081 
00082         $this->assertArrayHasKey( 'query', $data[0] );
00083         $this->assertArrayHasKey( 'pages', $data[0]['query'] );
00084         $this->assertEquals( 2, count( $data[0]['query']['pages'] ) );
00085 
00086         $this->assertArrayHasKey( -2, $data[0]['query']['pages'] );
00087         $this->assertArrayHasKey( -1, $data[0]['query']['pages'] );
00088 
00089         $this->assertArrayHasKey( 'missing', $data[0]['query']['pages'][-2] );
00090         $this->assertArrayHasKey( 'invalid', $data[0]['query']['pages'][-1] );
00091     }
00092 
00102     function testTitlePartToKey( $titlePart, $namespace, $expected, $expectException ) {
00103         $this->setMwGlobals( array(
00104             'wgCapitalLinks' => true,
00105         ) );
00106 
00107         $api = new MockApiQueryBase();
00108         $exceptionCaught = false;
00109         try {
00110             $this->assertEquals( $expected, $api->titlePartToKey( $titlePart, $namespace ) );
00111         } catch ( UsageException $e ) {
00112             $exceptionCaught = true;
00113         }
00114         $this->assertEquals( $expectException, $exceptionCaught,
00115             'UsageException thrown by titlePartToKey' );
00116     }
00117 
00118     function provideTestTitlePartToKey() {
00119         return array(
00120             array( 'a  b  c', NS_MAIN, 'A_b_c', false ),
00121             array( 'x', NS_MAIN, 'X', false ),
00122             array( 'y ', NS_MAIN, 'Y_', false ),
00123             array( 'template:foo', NS_CATEGORY, 'Template:foo', false ),
00124             array( 'apiquerytestiw:foo', NS_CATEGORY, 'Apiquerytestiw:foo', false ),
00125             array( "\xF7", NS_MAIN, null, true ),
00126             array( 'template:foo', NS_MAIN, null, true ),
00127             array( 'apiquerytestiw:foo', NS_MAIN, null, true ),
00128         );
00129     }
00130 }