MediaWiki  1.23.8
ApiQueryTest.php
Go to the documentation of this file.
1 <?php
2 
9 class ApiQueryTest extends ApiTestCase {
13  protected $hooks;
14 
15  protected function setUp() {
17 
18  parent::setUp();
19  $this->doLogin();
20 
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' );
26  }
27  return false;
28  };
29  }
30 
31  protected function tearDown() {
34 
35  parent::tearDown();
36  }
37 
38  public function testTitlesGetNormalized() {
39 
40  global $wgMetaNamespace;
41 
42  $data = $this->doApiRequest( array(
43  'action' => 'query',
44  'titles' => 'Project:articleA|article_B' ) );
45 
46  $this->assertArrayHasKey( 'query', $data[0] );
47  $this->assertArrayHasKey( 'normalized', $data[0]['query'] );
48 
49  // Forge a normalized title
50  $to = Title::newFromText( $wgMetaNamespace . ':ArticleA' );
51 
52  $this->assertEquals(
53  array(
54  'from' => 'Project:articleA',
55  'to' => $to->getPrefixedText(),
56  ),
57  $data[0]['query']['normalized'][0]
58  );
59 
60  $this->assertEquals(
61  array(
62  'from' => 'article_B',
63  'to' => 'Article B'
64  ),
65  $data[0]['query']['normalized'][1]
66  );
67  }
68 
69  public function testTitlesAreRejectedIfInvalid() {
70  $title = false;
71  while ( !$title || Title::newFromText( $title )->exists() ) {
72  $title = md5( mt_rand( 0, 10000 ) + rand( 0, 999000 ) );
73  }
74 
75  $data = $this->doApiRequest( array(
76  'action' => 'query',
77  'titles' => $title . '|Talk:' ) );
78 
79  $this->assertArrayHasKey( 'query', $data[0] );
80  $this->assertArrayHasKey( 'pages', $data[0]['query'] );
81  $this->assertEquals( 2, count( $data[0]['query']['pages'] ) );
82 
83  $this->assertArrayHasKey( -2, $data[0]['query']['pages'] );
84  $this->assertArrayHasKey( -1, $data[0]['query']['pages'] );
85 
86  $this->assertArrayHasKey( 'missing', $data[0]['query']['pages'][-2] );
87  $this->assertArrayHasKey( 'invalid', $data[0]['query']['pages'][-1] );
88  }
89 
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;
106  }
107  $this->assertEquals( $expectException, $exceptionCaught,
108  'UsageException thrown by titlePartToKey' );
109  }
110 
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 ),
121  );
122  }
123 }
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:189
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
ApiQueryTest\tearDown
tearDown()
Definition: ApiQueryTest.php:30
$wgHooks
$wgHooks['ArticleShow'][]
Definition: hooks.txt:110
ApiQueryTest
@group API @group Database @group medium @covers ApiQuery
Definition: ApiQueryTest.php:9
ApiQueryTest\testTitlesGetNormalized
testTitlesGetNormalized()
Definition: ApiQueryTest.php:37
NS_MAIN
const NS_MAIN
Definition: Defines.php:79
ApiQueryTest\$hooks
array $hooks
Storage for $wgHooks.
Definition: ApiQueryTest.php:12
hooks
Using a hook running we can avoid having all this option specific stuff in our mainline code Using hooks
Definition: hooks.txt:73
UsageException
This exception will be thrown when dieUsage is called to stop module execution.
Definition: ApiMain.php:1406
ApiQueryTest\testTitlePartToKey
testTitlePartToKey( $titlePart, $namespace, $expected, $expectException)
Test the ApiBase::titlePartToKey function.
Definition: ApiQueryTest.php:98
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:93
ApiQueryTest\setUp
setUp()
Definition: ApiQueryTest.php:14
ApiQueryTest\provideTestTitlePartToKey
provideTestTitlePartToKey()
Definition: ApiQueryTest.php:110
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
ApiTestCase
Definition: ApiTestCase.php:3
ApiTestCase\doLogin
doLogin( $user='sysop')
Definition: ApiTestCase.php:141
ApiTestCase\doApiRequest
doApiRequest(array $params, array $session=null, $appendModule=false, User $user=null)
Does the API request and returns the result.
Definition: ApiTestCase.php:74
$e
if( $useReadline) $e
Definition: eval.php:66
ApiQueryTest\testTitlesAreRejectedIfInvalid
testTitlesAreRejectedIfInvalid()
Definition: ApiQueryTest.php:68
MockApiQueryBase
Definition: MockApiQueryBase.php:2