MediaWiki  1.28.0
ApiFormatPhpTest.php
Go to the documentation of this file.
1 <?php
2 
8 
9  protected $printerName = 'php';
10 
11  private static function addFormatVersion( $format, $arr ) {
12  foreach ( $arr as &$p ) {
13  if ( !isset( $p[2] ) ) {
14  $p[2] = [ 'formatversion' => $format ];
15  } else {
16  $p[2]['formatversion'] = $format;
17  }
18  }
19  return $arr;
20  }
21 
22  public static function provideGeneralEncoding() {
23  // @codingStandardsIgnoreStart Generic.Files.LineLength
24  return array_merge(
25  self::addFormatVersion( 1, [
26  // Basic types
27  [ [ null ], 'a:1:{i:0;N;}' ],
28  [ [ true ], 'a:1:{i:0;s:0:"";}' ],
29  [ [ false ], 'a:0:{}' ],
30  [ [ true, ApiResult::META_BC_BOOLS => [ 0 ] ],
31  'a:1:{i:0;b:1;}' ],
32  [ [ false, ApiResult::META_BC_BOOLS => [ 0 ] ],
33  'a:1:{i:0;b:0;}' ],
34  [ [ 42 ], 'a:1:{i:0;i:42;}' ],
35  [ [ 42.5 ], 'a:1:{i:0;d:42.5;}' ],
36  [ [ 1e42 ], 'a:1:{i:0;d:1.0E+42;}' ],
37  [ [ 'foo' ], 'a:1:{i:0;s:3:"foo";}' ],
38  [ [ 'fóo' ], 'a:1:{i:0;s:4:"fóo";}' ],
39 
40  // Arrays and objects
41  [ [ [] ], 'a:1:{i:0;a:0:{}}' ],
42  [ [ [ 1 ] ], 'a:1:{i:0;a:1:{i:0;i:1;}}' ],
43  [ [ [ 'x' => 1 ] ], 'a:1:{i:0;a:1:{s:1:"x";i:1;}}' ],
44  [ [ [ 2 => 1 ] ], 'a:1:{i:0;a:1:{i:2;i:1;}}' ],
45  [ [ (object)[] ], 'a:1:{i:0;a:0:{}}' ],
46  [ [ [ 1, ApiResult::META_TYPE => 'assoc' ] ], 'a:1:{i:0;a:1:{i:0;i:1;}}' ],
47  [ [ [ 'x' => 1, ApiResult::META_TYPE => 'array' ] ], 'a:1:{i:0;a:1:{i:0;i:1;}}' ],
48  [ [ [ 'x' => 1, ApiResult::META_TYPE => 'kvp' ] ], 'a:1:{i:0;a:1:{s:1:"x";i:1;}}' ],
49  [ [ [ 'x' => 1, ApiResult::META_TYPE => 'BCkvp', ApiResult::META_KVP_KEY_NAME => 'key' ] ],
50  'a:1:{i:0;a:1:{i:0;a:2:{s:3:"key";s:1:"x";s:1:"*";i:1;}}}' ],
51  [ [ [ 'x' => 1, ApiResult::META_TYPE => 'BCarray' ] ], 'a:1:{i:0;a:1:{s:1:"x";i:1;}}' ],
52  [ [ [ 'a', 'b', ApiResult::META_TYPE => 'BCassoc' ] ], 'a:1:{i:0;a:2:{i:0;s:1:"a";i:1;s:1:"b";}}' ],
53 
54  // Content
55  [ [ 'content' => 'foo', ApiResult::META_CONTENT => 'content' ],
56  'a:1:{s:1:"*";s:3:"foo";}' ],
57 
58  // BC Subelements
59  [ [ 'foo' => 'foo', ApiResult::META_BC_SUBELEMENTS => [ 'foo' ] ],
60  'a:1:{s:3:"foo";a:1:{s:1:"*";s:3:"foo";}}' ],
61  ] ),
62  self::addFormatVersion( 2, [
63  // Basic types
64  [ [ null ], 'a:1:{i:0;N;}' ],
65  [ [ true ], 'a:1:{i:0;b:1;}' ],
66  [ [ false ], 'a:1:{i:0;b:0;}' ],
67  [ [ true, ApiResult::META_BC_BOOLS => [ 0 ] ],
68  'a:1:{i:0;b:1;}' ],
69  [ [ false, ApiResult::META_BC_BOOLS => [ 0 ] ],
70  'a:1:{i:0;b:0;}' ],
71  [ [ 42 ], 'a:1:{i:0;i:42;}' ],
72  [ [ 42.5 ], 'a:1:{i:0;d:42.5;}' ],
73  [ [ 1e42 ], 'a:1:{i:0;d:1.0E+42;}' ],
74  [ [ 'foo' ], 'a:1:{i:0;s:3:"foo";}' ],
75  [ [ 'fóo' ], 'a:1:{i:0;s:4:"fóo";}' ],
76 
77  // Arrays and objects
78  [ [ [] ], 'a:1:{i:0;a:0:{}}' ],
79  [ [ [ 1 ] ], 'a:1:{i:0;a:1:{i:0;i:1;}}' ],
80  [ [ [ 'x' => 1 ] ], 'a:1:{i:0;a:1:{s:1:"x";i:1;}}' ],
81  [ [ [ 2 => 1 ] ], 'a:1:{i:0;a:1:{i:2;i:1;}}' ],
82  [ [ (object)[] ], 'a:1:{i:0;a:0:{}}' ],
83  [ [ [ 1, ApiResult::META_TYPE => 'assoc' ] ], 'a:1:{i:0;a:1:{i:0;i:1;}}' ],
84  [ [ [ 'x' => 1, ApiResult::META_TYPE => 'array' ] ], 'a:1:{i:0;a:1:{i:0;i:1;}}' ],
85  [ [ [ 'x' => 1, ApiResult::META_TYPE => 'kvp' ] ], 'a:1:{i:0;a:1:{s:1:"x";i:1;}}' ],
86  [ [ [ 'x' => 1, ApiResult::META_TYPE => 'BCkvp', ApiResult::META_KVP_KEY_NAME => 'key' ] ],
87  'a:1:{i:0;a:1:{s:1:"x";i:1;}}' ],
88  [ [ [ 'x' => 1, ApiResult::META_TYPE => 'BCarray' ] ], 'a:1:{i:0;a:1:{i:0;i:1;}}' ],
89  [ [ [ 'a', 'b', ApiResult::META_TYPE => 'BCassoc' ] ], 'a:1:{i:0;a:2:{i:0;s:1:"a";i:1;s:1:"b";}}' ],
90 
91  // Content
92  [ [ 'content' => 'foo', ApiResult::META_CONTENT => 'content' ],
93  'a:1:{s:7:"content";s:3:"foo";}' ],
94 
95  // BC Subelements
96  [ [ 'foo' => 'foo', ApiResult::META_BC_SUBELEMENTS => [ 'foo' ] ],
97  'a:1:{s:3:"foo";s:3:"foo";}' ],
98  ] )
99  );
100  // @codingStandardsIgnoreEnd
101  }
102 
103  public function testCrossDomainMangling() {
104  $config = new HashConfig( [ 'MangleFlashPolicy' => false ] );
105  $context = new RequestContext;
106  $context->setConfig( new MultiConfig( [
107  $config,
108  $context->getConfig(),
109  ] ) );
110  $main = new ApiMain( $context );
111  $main->getResult()->addValue( null, null, '< Cross-Domain-Policy >' );
112 
113  if ( !function_exists( 'wfOutputHandler' ) ) {
114  function wfOutputHandler( $s ) {
115  return $s;
116  }
117  }
118 
119  $printer = $main->createPrinterByName( 'php' );
120  ob_start( 'wfOutputHandler' );
121  $printer->initPrinter();
122  $printer->execute();
123  $printer->closePrinter();
124  $ret = ob_get_clean();
125  $this->assertSame( 'a:1:{i:0;s:23:"< Cross-Domain-Policy >";}', $ret );
126 
127  $config->set( 'MangleFlashPolicy', true );
128  $printer = $main->createPrinterByName( 'php' );
129  ob_start( 'wfOutputHandler' );
130  try {
131  $printer->initPrinter();
132  $printer->execute();
133  $printer->closePrinter();
134  ob_end_clean();
135  $this->fail( 'Expected exception not thrown' );
136  } catch ( UsageException $ex ) {
137  ob_end_clean();
138  $this->assertSame(
139  'This response cannot be represented using format=php. ' .
140  'See https://phabricator.wikimedia.org/T68776',
141  $ex->getMessage(),
142  'Expected exception'
143  );
144  }
145  }
146 
147 }
Group all the pieces relevant to the context of a request into one instance.
$context
Definition: load.php:50
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 & $ret
Definition: hooks.txt:1936
const META_TYPE
Key for the 'type' metadata item.
Definition: ApiResult.php:108
wfOutputHandler($s)
Standard output handler for use with ob_start.
API ApiFormatPhp.
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:43
Provides a fallback sequence for Config objects.
Definition: MultiConfig.php:28
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
static provideGeneralEncoding()
static addFormatVersion($format, $arr)
const META_BC_BOOLS
Key for the 'BC bools' metadata item.
Definition: ApiResult.php:134
const META_CONTENT
Key for the 'content' metadata item.
Definition: ApiResult.php:88
const META_BC_SUBELEMENTS
Key for the 'BC subelements' metadata item.
Definition: ApiResult.php:141
A Config instance which stores all settings as a member variable.
Definition: HashConfig.php:28
This exception will be thrown when dieUsage is called to stop module execution.
Definition: ApiMain.php:1837
const META_KVP_KEY_NAME
Key for the metadata item whose value specifies the name used for the kvp key in the alternative outp...
Definition: ApiResult.php:117