MediaWiki  1.29.1
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  $this->doLogin();
13 
14  // Setup apiquerytestiw: as interwiki prefix
15  $this->setMwGlobals( 'wgHooks', [
16  'InterwikiLoadPrefix' => [
17  function ( $prefix, &$data ) {
18  if ( $prefix == 'apiquerytestiw' ) {
19  $data = [ 'iw_url' => 'wikipedia' ];
20  }
21  return false;
22  }
23  ]
24  ] );
25  }
26 
27  public function testTitlesGetNormalized() {
28  global $wgMetaNamespace;
29 
30  $this->setMwGlobals( [
31  'wgCapitalLinks' => true,
32  ] );
33 
34  $data = $this->doApiRequest( [
35  'action' => 'query',
36  'titles' => 'Project:articleA|article_B' ] );
37 
38  $this->assertArrayHasKey( 'query', $data[0] );
39  $this->assertArrayHasKey( 'normalized', $data[0]['query'] );
40 
41  // Forge a normalized title
42  $to = Title::newFromText( $wgMetaNamespace . ':ArticleA' );
43 
44  $this->assertEquals(
45  [
46  'fromencoded' => false,
47  'from' => 'Project:articleA',
48  'to' => $to->getPrefixedText(),
49  ],
50  $data[0]['query']['normalized'][0]
51  );
52 
53  $this->assertEquals(
54  [
55  'fromencoded' => false,
56  'from' => 'article_B',
57  'to' => 'Article B'
58  ],
59  $data[0]['query']['normalized'][1]
60  );
61  }
62 
63  public function testTitlesAreRejectedIfInvalid() {
64  $title = false;
65  while ( !$title || Title::newFromText( $title )->exists() ) {
66  $title = md5( mt_rand( 0, 100000 ) );
67  }
68 
69  $data = $this->doApiRequest( [
70  'action' => 'query',
71  'titles' => $title . '|Talk:' ] );
72 
73  $this->assertArrayHasKey( 'query', $data[0] );
74  $this->assertArrayHasKey( 'pages', $data[0]['query'] );
75  $this->assertEquals( 2, count( $data[0]['query']['pages'] ) );
76 
77  $this->assertArrayHasKey( -2, $data[0]['query']['pages'] );
78  $this->assertArrayHasKey( -1, $data[0]['query']['pages'] );
79 
80  $this->assertArrayHasKey( 'missing', $data[0]['query']['pages'][-2] );
81  $this->assertArrayHasKey( 'invalid', $data[0]['query']['pages'][-1] );
82  }
83 
93  function testTitlePartToKey( $titlePart, $namespace, $expected, $expectException ) {
94  $this->setMwGlobals( [
95  'wgCapitalLinks' => true,
96  ] );
97 
98  $api = new MockApiQueryBase();
99  $exceptionCaught = false;
100  try {
101  $this->assertEquals( $expected, $api->titlePartToKey( $titlePart, $namespace ) );
102  } catch ( ApiUsageException $e ) {
103  $exceptionCaught = true;
104  }
105  $this->assertEquals( $expectException, $exceptionCaught,
106  'ApiUsageException thrown by titlePartToKey' );
107  }
108 
110  return [
111  [ 'a b c', NS_MAIN, 'A_b_c', false ],
112  [ 'x', NS_MAIN, 'X', false ],
113  [ 'y ', NS_MAIN, 'Y_', false ],
114  [ 'template:foo', NS_CATEGORY, 'Template:foo', false ],
115  [ 'apiquerytestiw:foo', NS_CATEGORY, 'Apiquerytestiw:foo', false ],
116  [ "\xF7", NS_MAIN, null, true ],
117  [ 'template:foo', NS_MAIN, null, true ],
118  [ 'apiquerytestiw:foo', NS_MAIN, null, true ],
119  ];
120  }
121 
125  public function testClassNamesInModuleManager() {
126  $api = new ApiMain(
127  new FauxRequest( [ 'action' => 'query', 'meta' => 'siteinfo' ] )
128  );
129  $queryApi = new ApiQuery( $api, 'query' );
130  $modules = $queryApi->getModuleManager()->getNamesWithClasses();
131 
132  foreach ( $modules as $name => $class ) {
133  $this->assertTrue(
134  class_exists( $class ),
135  'Class ' . $class . ' for api module ' . $name . ' does not exist (with exact case)'
136  );
137  }
138  }
139 }
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:45
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:265
ApiUsageException
Exception used to abort API execution with an error.
Definition: ApiUsageException.php:98
ApiQuery
This is the main query class.
Definition: ApiQuery.php:40
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
captcha-old.count
count
Definition: captcha-old.py:225
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:304
ApiQueryTest
API Database medium ApiQuery.
Definition: ApiQueryTest.php:9
ApiQueryTest\testTitlesGetNormalized
testTitlesGetNormalized()
Definition: ApiQueryTest.php:27
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:62
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:934
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Definition: MediaWikiTestCase.php:658
ApiQueryTest\testTitlePartToKey
testTitlePartToKey( $titlePart, $namespace, $expected, $expectException)
Test the ApiBase::titlePartToKey function.
Definition: ApiQueryTest.php:93
$modules
$modules
Definition: HTMLFormElement.php:12
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:76
ApiQueryTest\setUp
setUp()
Definition: ApiQueryTest.php:10
ApiTestCase\doLogin
doLogin( $testUser='sysop')
Definition: ApiTestCase.php:152
ApiQueryTest\provideTestTitlePartToKey
provideTestTitlePartToKey()
Definition: ApiQueryTest.php:109
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2122
ApiTestCase
Definition: ApiTestCase.php:3
ApiQueryTest\testClassNamesInModuleManager
testClassNamesInModuleManager()
Test if all classes in the query module manager exists.
Definition: ApiQueryTest.php:125
ApiTestCase\doApiRequest
doApiRequest(array $params, array $session=null, $appendModule=false, User $user=null)
Does the API request and returns the result.
Definition: ApiTestCase.php:73
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:1956
ApiQueryTest\testTitlesAreRejectedIfInvalid
testTitlesAreRejectedIfInvalid()
Definition: ApiQueryTest.php:63
MockApiQueryBase
Definition: MockApiQueryBase.php:2