MediaWiki  REL1_23
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 
00040         global $wgMetaNamespace;
00041 
00042         $data = $this->doApiRequest( array(
00043             'action' => 'query',
00044             'titles' => 'Project:articleA|article_B' ) );
00045 
00046         $this->assertArrayHasKey( 'query', $data[0] );
00047         $this->assertArrayHasKey( 'normalized', $data[0]['query'] );
00048 
00049         // Forge a normalized title
00050         $to = Title::newFromText( $wgMetaNamespace . ':ArticleA' );
00051 
00052         $this->assertEquals(
00053             array(
00054                 'from' => 'Project:articleA',
00055                 'to' => $to->getPrefixedText(),
00056             ),
00057             $data[0]['query']['normalized'][0]
00058         );
00059 
00060         $this->assertEquals(
00061             array(
00062                 'from' => 'article_B',
00063                 'to' => 'Article B'
00064             ),
00065             $data[0]['query']['normalized'][1]
00066         );
00067     }
00068 
00069     public function testTitlesAreRejectedIfInvalid() {
00070         $title = false;
00071         while ( !$title || Title::newFromText( $title )->exists() ) {
00072             $title = md5( mt_rand( 0, 10000 ) + rand( 0, 999000 ) );
00073         }
00074 
00075         $data = $this->doApiRequest( array(
00076             'action' => 'query',
00077             'titles' => $title . '|Talk:' ) );
00078 
00079         $this->assertArrayHasKey( 'query', $data[0] );
00080         $this->assertArrayHasKey( 'pages', $data[0]['query'] );
00081         $this->assertEquals( 2, count( $data[0]['query']['pages'] ) );
00082 
00083         $this->assertArrayHasKey( -2, $data[0]['query']['pages'] );
00084         $this->assertArrayHasKey( -1, $data[0]['query']['pages'] );
00085 
00086         $this->assertArrayHasKey( 'missing', $data[0]['query']['pages'][-2] );
00087         $this->assertArrayHasKey( 'invalid', $data[0]['query']['pages'][-1] );
00088     }
00089 
00099     function testTitlePartToKey( $titlePart, $namespace, $expected, $expectException ) {
00100         $api = new MockApiQueryBase();
00101         $exceptionCaught = false;
00102         try {
00103             $this->assertEquals( $expected, $api->titlePartToKey( $titlePart, $namespace ) );
00104         } catch ( UsageException $e ) {
00105             $exceptionCaught = true;
00106         }
00107         $this->assertEquals( $expectException, $exceptionCaught,
00108             'UsageException thrown by titlePartToKey' );
00109     }
00110 
00111     function provideTestTitlePartToKey() {
00112         return array(
00113             array( 'a  b  c', NS_MAIN, 'A_b_c', false ),
00114             array( 'x', NS_MAIN, 'X', false ),
00115             array( 'y ', NS_MAIN, 'Y_', false ),
00116             array( 'template:foo', NS_CATEGORY, 'Template:foo', false ),
00117             array( 'apiquerytestiw:foo', NS_CATEGORY, 'Apiquerytestiw:foo', false ),
00118             array( "\xF7", NS_MAIN, null, true ),
00119             array( 'template:foo', NS_MAIN, null, true ),
00120             array( 'apiquerytestiw:foo', NS_MAIN, null, true ),
00121         );
00122     }
00123 }