MediaWiki  1.33.0
ApiQueryTest.php
Go to the documentation of this file.
1 <?php
2 
9 class ApiQueryTest extends ApiTestCase {
10  protected function setUp() {
11  parent::setUp();
12 
13  // Setup apiquerytestiw: as interwiki prefix
14  $this->setMwGlobals( 'wgHooks', [
15  'InterwikiLoadPrefix' => [
16  function ( $prefix, &$data ) {
17  if ( $prefix == 'apiquerytestiw' ) {
18  $data = [ 'iw_url' => 'wikipedia' ];
19  }
20  return false;
21  }
22  ]
23  ] );
24  }
25 
26  public function testTitlesGetNormalized() {
27  global $wgMetaNamespace;
28 
29  $this->setMwGlobals( [
30  'wgCapitalLinks' => true,
31  ] );
32 
33  $data = $this->doApiRequest( [
34  'action' => 'query',
35  'titles' => 'Project:articleA|article_B' ] );
36 
37  $this->assertArrayHasKey( 'query', $data[0] );
38  $this->assertArrayHasKey( 'normalized', $data[0]['query'] );
39 
40  // Forge a normalized title
41  $to = Title::newFromText( $wgMetaNamespace . ':ArticleA' );
42 
43  $this->assertEquals(
44  [
45  'fromencoded' => false,
46  'from' => 'Project:articleA',
47  'to' => $to->getPrefixedText(),
48  ],
49  $data[0]['query']['normalized'][0]
50  );
51 
52  $this->assertEquals(
53  [
54  'fromencoded' => false,
55  'from' => 'article_B',
56  'to' => 'Article B'
57  ],
58  $data[0]['query']['normalized'][1]
59  );
60  }
61 
62  public function testTitlesAreRejectedIfInvalid() {
63  $title = false;
64  while ( !$title || Title::newFromText( $title )->exists() ) {
65  $title = md5( mt_rand( 0, 100000 ) );
66  }
67 
68  $data = $this->doApiRequest( [
69  'action' => 'query',
70  'titles' => $title . '|Talk:' ] );
71 
72  $this->assertArrayHasKey( 'query', $data[0] );
73  $this->assertArrayHasKey( 'pages', $data[0]['query'] );
74  $this->assertEquals( 2, count( $data[0]['query']['pages'] ) );
75 
76  $this->assertArrayHasKey( -2, $data[0]['query']['pages'] );
77  $this->assertArrayHasKey( -1, $data[0]['query']['pages'] );
78 
79  $this->assertArrayHasKey( 'missing', $data[0]['query']['pages'][-2] );
80  $this->assertArrayHasKey( 'invalid', $data[0]['query']['pages'][-1] );
81  }
82 
83  public function testTitlesWithWhitespaces() {
84  $data = $this->doApiRequest( [
85  'action' => 'query',
86  'titles' => ' '
87  ] );
88 
89  $this->assertArrayHasKey( 'query', $data[0] );
90  $this->assertArrayHasKey( 'pages', $data[0]['query'] );
91  $this->assertEquals( 1, count( $data[0]['query']['pages'] ) );
92  $this->assertArrayHasKey( -1, $data[0]['query']['pages'] );
93  $this->assertArrayHasKey( 'invalid', $data[0]['query']['pages'][-1] );
94  }
95 
105  function testTitlePartToKey( $titlePart, $namespace, $expected, $expectException ) {
106  $this->setMwGlobals( [
107  'wgCapitalLinks' => true,
108  ] );
109 
110  $api = new MockApiQueryBase();
111  $exceptionCaught = false;
112  try {
113  $this->assertEquals( $expected, $api->titlePartToKey( $titlePart, $namespace ) );
114  } catch ( ApiUsageException $e ) {
115  $exceptionCaught = true;
116  }
117  $this->assertEquals( $expectException, $exceptionCaught,
118  'ApiUsageException thrown by titlePartToKey' );
119  }
120 
122  return [
123  [ 'a b c', NS_MAIN, 'A_b_c', false ],
124  [ 'x', NS_MAIN, 'X', false ],
125  [ 'y ', NS_MAIN, 'Y_', false ],
126  [ 'template:foo', NS_CATEGORY, 'Template:foo', false ],
127  [ 'apiquerytestiw:foo', NS_CATEGORY, 'Apiquerytestiw:foo', false ],
128  [ "\xF7", NS_MAIN, null, true ],
129  [ 'template:foo', NS_MAIN, null, true ],
130  [ 'apiquerytestiw:foo', NS_MAIN, null, true ],
131  ];
132  }
133 
137  public function testClassNamesInModuleManager() {
138  $api = new ApiMain(
139  new FauxRequest( [ 'action' => 'query', 'meta' => 'siteinfo' ] )
140  );
141  $queryApi = new ApiQuery( $api, 'query' );
142  $modules = $queryApi->getModuleManager()->getNamesWithClasses();
143 
144  foreach ( $modules as $name => $class ) {
145  $this->assertTrue(
146  class_exists( $class ),
147  'Class ' . $class . ' for api module ' . $name . ' does not exist (with exact case)'
148  );
149  }
150  }
151 }
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:41
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:33
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:306
ApiUsageException
Exception used to abort API execution with an error.
Definition: ApiUsageException.php:28
ApiQuery
This is the main query class.
Definition: ApiQuery.php:36
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
captcha-old.count
count
Definition: captcha-old.py:249
ApiQueryTest
API Database medium ApiQuery.
Definition: ApiQueryTest.php:9
ApiQueryTest\testTitlesGetNormalized
testTitlesGetNormalized()
Definition: ApiQueryTest.php:26
$wgMetaNamespace
$wgMetaNamespace
Name of the project namespace.
Definition: DefaultSettings.php:3813
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
NS_MAIN
const NS_MAIN
Definition: Defines.php:64
ApiTestCase\doApiRequest
doApiRequest(array $params, array $session=null, $appendModule=false, User $user=null, $tokenType=null)
Does the API request and returns the result.
Definition: ApiTestCase.php:62
$data
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
Definition: generatePhpCharToUpperMappings.php:13
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:709
ApiQueryTest\testTitlePartToKey
testTitlePartToKey( $titlePart, $namespace, $expected, $expectException)
Test the ApiBase::titlePartToKey function.
Definition: ApiQueryTest.php:105
$modules
$modules
Definition: HTMLFormElement.php:12
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:78
ApiQueryTest\setUp
setUp()
Definition: ApiQueryTest.php:10
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
ApiQueryTest\provideTestTitlePartToKey
provideTestTitlePartToKey()
Definition: ApiQueryTest.php:121
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2162
ApiTestCase
Definition: ApiTestCase.php:5
ApiQueryTest\testClassNamesInModuleManager
testClassNamesInModuleManager()
Test if all classes in the query module manager exists.
Definition: ApiQueryTest.php:137
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
true
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1985
ApiQueryTest\testTitlesAreRejectedIfInvalid
testTitlesAreRejectedIfInvalid()
Definition: ApiQueryTest.php:62
MockApiQueryBase
Definition: MockApiQueryBase.php:2
ApiQueryTest\testTitlesWithWhitespaces
testTitlesWithWhitespaces()
Definition: ApiQueryTest.php:83