2011-01-04 03:22:40 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
2012-04-02 13:33:43 +00:00
|
|
|
* @group API
|
2011-01-04 03:22:40 +00:00
|
|
|
* @group Database
|
2013-01-18 19:02:28 +00:00
|
|
|
* @group medium
|
2013-10-24 10:04:00 +00:00
|
|
|
* @covers ApiQuery
|
2011-01-04 03:22:40 +00:00
|
|
|
*/
|
2011-07-01 16:34:02 +00:00
|
|
|
class ApiQueryTest extends ApiTestCase {
|
2012-10-08 10:56:20 +00:00
|
|
|
protected function setUp() {
|
2011-01-04 03:22:40 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
$this->doLogin();
|
2013-11-17 17:52:54 +00:00
|
|
|
|
2014-10-15 15:50:36 +00:00
|
|
|
// Setup apiquerytestiw: as interwiki prefix
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( 'wgHooks', [
|
|
|
|
|
'InterwikiLoadPrefix' => [
|
2014-10-15 15:50:36 +00:00
|
|
|
function ( $prefix, &$data ) {
|
|
|
|
|
if ( $prefix == 'apiquerytestiw' ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$data = [ 'iw_url' => 'wikipedia' ];
|
2014-10-15 15:50:36 +00:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
]
|
|
|
|
|
] );
|
2011-01-04 03:22:40 +00:00
|
|
|
}
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testTitlesGetNormalized() {
|
2011-04-07 14:54:38 +00:00
|
|
|
global $wgMetaNamespace;
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( [
|
2014-07-07 23:21:09 +00:00
|
|
|
'wgCapitalLinks' => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2014-07-07 23:21:09 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$data = $this->doApiRequest( [
|
2011-01-04 03:22:40 +00:00
|
|
|
'action' => 'query',
|
2016-02-17 09:09:32 +00:00
|
|
|
'titles' => 'Project:articleA|article_B' ] );
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2011-01-04 03:22:40 +00:00
|
|
|
$this->assertArrayHasKey( 'query', $data[0] );
|
|
|
|
|
$this->assertArrayHasKey( 'normalized', $data[0]['query'] );
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2012-01-30 15:54:46 +00:00
|
|
|
// Forge a normalized title
|
2013-02-14 13:10:38 +00:00
|
|
|
$to = Title::newFromText( $wgMetaNamespace . ':ArticleA' );
|
2012-01-30 15:54:46 +00:00
|
|
|
|
2011-02-12 04:06:22 +00:00
|
|
|
$this->assertEquals(
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2016-08-24 18:07:43 +00:00
|
|
|
'fromencoded' => false,
|
2011-01-04 03:22:40 +00:00
|
|
|
'from' => 'Project:articleA',
|
2012-01-30 15:54:46 +00:00
|
|
|
'to' => $to->getPrefixedText(),
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2011-01-04 03:22:40 +00:00
|
|
|
$data[0]['query']['normalized'][0]
|
|
|
|
|
);
|
2011-02-12 04:06:22 +00:00
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2016-08-24 18:07:43 +00:00
|
|
|
'fromencoded' => false,
|
2011-01-04 03:22:40 +00:00
|
|
|
'from' => 'article_B',
|
|
|
|
|
'to' => 'Article B'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2011-01-04 03:22:40 +00:00
|
|
|
$data[0]['query']['normalized'][1]
|
|
|
|
|
);
|
|
|
|
|
}
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testTitlesAreRejectedIfInvalid() {
|
2011-01-04 03:22:40 +00:00
|
|
|
$title = false;
|
2013-02-14 13:10:38 +00:00
|
|
|
while ( !$title || Title::newFromText( $title )->exists() ) {
|
2015-12-03 13:32:04 +00:00
|
|
|
$title = md5( mt_rand( 0, 100000 ) );
|
2011-01-04 03:22:40 +00:00
|
|
|
}
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$data = $this->doApiRequest( [
|
2011-01-04 03:22:40 +00:00
|
|
|
'action' => 'query',
|
2016-02-17 09:09:32 +00:00
|
|
|
'titles' => $title . '|Talk:' ] );
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2011-01-04 03:22:40 +00:00
|
|
|
$this->assertArrayHasKey( 'query', $data[0] );
|
|
|
|
|
$this->assertArrayHasKey( 'pages', $data[0]['query'] );
|
|
|
|
|
$this->assertEquals( 2, count( $data[0]['query']['pages'] ) );
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2011-01-04 03:22:40 +00:00
|
|
|
$this->assertArrayHasKey( -2, $data[0]['query']['pages'] );
|
|
|
|
|
$this->assertArrayHasKey( -1, $data[0]['query']['pages'] );
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2011-01-04 03:22:40 +00:00
|
|
|
$this->assertArrayHasKey( 'missing', $data[0]['query']['pages'][-2] );
|
|
|
|
|
$this->assertArrayHasKey( 'invalid', $data[0]['query']['pages'][-1] );
|
|
|
|
|
}
|
2013-11-17 17:52:54 +00:00
|
|
|
|
2018-01-21 20:58:59 +00:00
|
|
|
public function testTitlesWithWhitespaces() {
|
|
|
|
|
$data = $this->doApiRequest( [
|
|
|
|
|
'action' => 'query',
|
|
|
|
|
'titles' => ' '
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( 'query', $data[0] );
|
|
|
|
|
$this->assertArrayHasKey( 'pages', $data[0]['query'] );
|
|
|
|
|
$this->assertEquals( 1, count( $data[0]['query']['pages'] ) );
|
|
|
|
|
$this->assertArrayHasKey( -1, $data[0]['query']['pages'] );
|
|
|
|
|
$this->assertArrayHasKey( 'invalid', $data[0]['query']['pages'][-1] );
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-17 17:52:54 +00:00
|
|
|
/**
|
|
|
|
|
* Test the ApiBase::titlePartToKey function
|
|
|
|
|
*
|
|
|
|
|
* @param string $titlePart
|
|
|
|
|
* @param int $namespace
|
|
|
|
|
* @param string $expected
|
2014-08-13 19:41:39 +00:00
|
|
|
* @param string $expectException
|
2013-11-17 17:52:54 +00:00
|
|
|
* @dataProvider provideTestTitlePartToKey
|
|
|
|
|
*/
|
|
|
|
|
function testTitlePartToKey( $titlePart, $namespace, $expected, $expectException ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( [
|
2014-07-07 23:21:09 +00:00
|
|
|
'wgCapitalLinks' => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2014-07-07 23:21:09 +00:00
|
|
|
|
2013-11-17 17:52:54 +00:00
|
|
|
$api = new MockApiQueryBase();
|
|
|
|
|
$exceptionCaught = false;
|
|
|
|
|
try {
|
|
|
|
|
$this->assertEquals( $expected, $api->titlePartToKey( $titlePart, $namespace ) );
|
2016-10-19 16:54:25 +00:00
|
|
|
} catch ( ApiUsageException $e ) {
|
2013-11-17 17:52:54 +00:00
|
|
|
$exceptionCaught = true;
|
|
|
|
|
}
|
|
|
|
|
$this->assertEquals( $expectException, $exceptionCaught,
|
2016-10-19 16:54:25 +00:00
|
|
|
'ApiUsageException thrown by titlePartToKey' );
|
2013-11-17 17:52:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function provideTestTitlePartToKey() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ 'a b c', NS_MAIN, 'A_b_c', false ],
|
|
|
|
|
[ 'x', NS_MAIN, 'X', false ],
|
|
|
|
|
[ 'y ', NS_MAIN, 'Y_', false ],
|
|
|
|
|
[ 'template:foo', NS_CATEGORY, 'Template:foo', false ],
|
|
|
|
|
[ 'apiquerytestiw:foo', NS_CATEGORY, 'Apiquerytestiw:foo', false ],
|
|
|
|
|
[ "\xF7", NS_MAIN, null, true ],
|
|
|
|
|
[ 'template:foo', NS_MAIN, null, true ],
|
|
|
|
|
[ 'apiquerytestiw:foo', NS_MAIN, null, true ],
|
|
|
|
|
];
|
2013-11-17 17:52:54 +00:00
|
|
|
}
|
2014-11-17 19:08:10 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test if all classes in the query module manager exists
|
|
|
|
|
*/
|
|
|
|
|
public function testClassNamesInModuleManager() {
|
|
|
|
|
$api = new ApiMain(
|
2016-02-17 09:09:32 +00:00
|
|
|
new FauxRequest( [ 'action' => 'query', 'meta' => 'siteinfo' ] )
|
2014-11-17 19:08:10 +00:00
|
|
|
);
|
|
|
|
|
$queryApi = new ApiQuery( $api, 'query' );
|
|
|
|
|
$modules = $queryApi->getModuleManager()->getNamesWithClasses();
|
2016-12-21 15:06:57 +00:00
|
|
|
|
2015-06-17 20:01:00 +00:00
|
|
|
foreach ( $modules as $name => $class ) {
|
2016-12-21 15:06:57 +00:00
|
|
|
$this->assertTrue(
|
|
|
|
|
class_exists( $class ),
|
|
|
|
|
'Class ' . $class . ' for api module ' . $name . ' does not exist (with exact case)'
|
2014-11-17 19:08:10 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-01-04 03:22:40 +00:00
|
|
|
}
|