9 class ApiQueryTest
extends ApiTestCase
{
11 * @var array Storage for $wgHooks
15 protected function setUp() {
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' );
31 protected function tearDown() {
33 $wgHooks = $this->hooks
;
38 public function testTitlesGetNormalized() {
39 global $wgMetaNamespace;
41 $this->setMwGlobals( array(
42 'wgCapitalLinks' => true,
45 $data = $this->doApiRequest( array(
47 'titles' => 'Project:articleA|article_B' ) );
49 $this->assertArrayHasKey( 'query', $data[0] );
50 $this->assertArrayHasKey( 'normalized', $data[0]['query'] );
52 // Forge a normalized title
53 $to = Title
::newFromText( $wgMetaNamespace . ':ArticleA' );
57 'from' => 'Project:articleA',
58 'to' => $to->getPrefixedText(),
60 $data[0]['query']['normalized'][0]
65 'from' => 'article_B',
68 $data[0]['query']['normalized'][1]
72 public function testTitlesAreRejectedIfInvalid() {
74 while ( !$title || Title
::newFromText( $title )->exists() ) {
75 $title = md5( mt_rand( 0, 10000 ) +
rand( 0, 999000 ) );
78 $data = $this->doApiRequest( array(
80 'titles' => $title . '|Talk:' ) );
82 $this->assertArrayHasKey( 'query', $data[0] );
83 $this->assertArrayHasKey( 'pages', $data[0]['query'] );
84 $this->assertEquals( 2, count( $data[0]['query']['pages'] ) );
86 $this->assertArrayHasKey( -2, $data[0]['query']['pages'] );
87 $this->assertArrayHasKey( -1, $data[0]['query']['pages'] );
89 $this->assertArrayHasKey( 'missing', $data[0]['query']['pages'][-2] );
90 $this->assertArrayHasKey( 'invalid', $data[0]['query']['pages'][-1] );
94 * Test the ApiBase::titlePartToKey function
96 * @param string $titlePart
97 * @param int $namespace
98 * @param string $expected
99 * @param string $expectException
100 * @dataProvider provideTestTitlePartToKey
102 function testTitlePartToKey( $titlePart, $namespace, $expected, $expectException ) {
103 $this->setMwGlobals( array(
104 'wgCapitalLinks' => true,
107 $api = new MockApiQueryBase();
108 $exceptionCaught = false;
110 $this->assertEquals( $expected, $api->titlePartToKey( $titlePart, $namespace ) );
111 } catch ( UsageException
$e ) {
112 $exceptionCaught = true;
114 $this->assertEquals( $expectException, $exceptionCaught,
115 'UsageException thrown by titlePartToKey' );
118 function provideTestTitlePartToKey() {
120 array( 'a b c', NS_MAIN
, 'A_b_c', false ),
121 array( 'x', NS_MAIN
, 'X', false ),
122 array( 'y ', NS_MAIN
, 'Y_', false ),
123 array( 'template:foo', NS_CATEGORY
, 'Template:foo', false ),
124 array( 'apiquerytestiw:foo', NS_CATEGORY
, 'Apiquerytestiw:foo', false ),
125 array( "\xF7", NS_MAIN
, null, true ),
126 array( 'template:foo', NS_MAIN
, null, true ),
127 array( 'apiquerytestiw:foo', NS_MAIN
, null, true ),