MediaWiki  1.28.3
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() {
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 ( UsageException $e ) {
103  $exceptionCaught = true;
104  }
105  $this->assertEquals( $expectException, $exceptionCaught,
106  'UsageException 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() {
127 
128  // wgAutoloadLocalClasses has precedence, just like in includes/AutoLoader.php
129  $classes = $wgAutoloadLocalClasses + $wgAutoloadClasses;
130 
131  $api = new ApiMain(
132  new FauxRequest( [ 'action' => 'query', 'meta' => 'siteinfo' ] )
133  );
134  $queryApi = new ApiQuery( $api, 'query' );
135  $modules = $queryApi->getModuleManager()->getNamesWithClasses();
136  foreach ( $modules as $name => $class ) {
137  $this->assertArrayHasKey(
138  $class,
139  $classes,
140  'Class ' . $class . ' for api module ' . $name . ' not in autoloader (with exact case)'
141  );
142  }
143  }
144 }
const NS_MAIN
Definition: Defines.php:56
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException'returning false will NOT prevent logging $e
Definition: hooks.txt:2106
API Database medium ApiQuery.
Definition: ApiQueryTest.php:9
$wgMetaNamespace
Name of the project namespace.
static newFromText($text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:262
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
testTitlePartToKey($titlePart, $namespace, $expected, $expectException)
Test the ApiBase::titlePartToKey function.
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:1940
provideTestTitlePartToKey()
$modules
testTitlesAreRejectedIfInvalid()
doLogin($testUser= 'sysop')
testClassNamesInModuleManager()
Test if all classes in the query module manager exists.
const NS_CATEGORY
Definition: Defines.php:70
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:43
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:957
global $wgAutoloadClasses
This is the main query class.
Definition: ApiQuery.php:38
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
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
doApiRequest(array $params, array $session=null, $appendModule=false, User $user=null)
Does the API request and returns the result.
Definition: ApiTestCase.php:71
global $wgAutoloadLocalClasses
Definition: autoload.php:4
testTitlesGetNormalized()
setMwGlobals($pairs, $value=null)
This exception will be thrown when dieUsage is called to stop module execution.
Definition: ApiMain.php:1860
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:304