Merge "Use camel case for variable names in Article.php"
[lhc/web/wiklou.git] / tests / phpunit / includes / api / query / ApiQueryTest.php
1 <?php
2
3 /**
4 * @group API
5 * @group Database
6 * @group medium
7 * @covers ApiQuery
8 */
9 class ApiQueryTest extends ApiTestCase {
10 /**
11 * @var array Storage for $wgHooks
12 */
13 protected $hooks;
14
15 protected function setUp() {
16 global $wgHooks;
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() {
32 global $wgHooks;
33 $wgHooks = $this->hooks;
34
35 parent::tearDown();
36 }
37
38 public function testTitlesGetNormalized() {
39 global $wgMetaNamespace;
40
41 $this->setMwGlobals( array(
42 'wgCapitalLinks' => true,
43 ) );
44
45 $data = $this->doApiRequest( array(
46 'action' => 'query',
47 'titles' => 'Project:articleA|article_B' ) );
48
49 $this->assertArrayHasKey( 'query', $data[0] );
50 $this->assertArrayHasKey( 'normalized', $data[0]['query'] );
51
52 // Forge a normalized title
53 $to = Title::newFromText( $wgMetaNamespace . ':ArticleA' );
54
55 $this->assertEquals(
56 array(
57 'from' => 'Project:articleA',
58 'to' => $to->getPrefixedText(),
59 ),
60 $data[0]['query']['normalized'][0]
61 );
62
63 $this->assertEquals(
64 array(
65 'from' => 'article_B',
66 'to' => 'Article B'
67 ),
68 $data[0]['query']['normalized'][1]
69 );
70 }
71
72 public function testTitlesAreRejectedIfInvalid() {
73 $title = false;
74 while ( !$title || Title::newFromText( $title )->exists() ) {
75 $title = md5( mt_rand( 0, 10000 ) + rand( 0, 999000 ) );
76 }
77
78 $data = $this->doApiRequest( array(
79 'action' => 'query',
80 'titles' => $title . '|Talk:' ) );
81
82 $this->assertArrayHasKey( 'query', $data[0] );
83 $this->assertArrayHasKey( 'pages', $data[0]['query'] );
84 $this->assertEquals( 2, count( $data[0]['query']['pages'] ) );
85
86 $this->assertArrayHasKey( -2, $data[0]['query']['pages'] );
87 $this->assertArrayHasKey( -1, $data[0]['query']['pages'] );
88
89 $this->assertArrayHasKey( 'missing', $data[0]['query']['pages'][-2] );
90 $this->assertArrayHasKey( 'invalid', $data[0]['query']['pages'][-1] );
91 }
92
93 /**
94 * Test the ApiBase::titlePartToKey function
95 *
96 * @param string $titlePart
97 * @param int $namespace
98 * @param string $expected
99 * @param string $expectException
100 * @dataProvider provideTestTitlePartToKey
101 */
102 function testTitlePartToKey( $titlePart, $namespace, $expected, $expectException ) {
103 $this->setMwGlobals( array(
104 'wgCapitalLinks' => true,
105 ) );
106
107 $api = new MockApiQueryBase();
108 $exceptionCaught = false;
109 try {
110 $this->assertEquals( $expected, $api->titlePartToKey( $titlePart, $namespace ) );
111 } catch ( UsageException $e ) {
112 $exceptionCaught = true;
113 }
114 $this->assertEquals( $expectException, $exceptionCaught,
115 'UsageException thrown by titlePartToKey' );
116 }
117
118 function provideTestTitlePartToKey() {
119 return array(
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 ),
128 );
129 }
130 }