7 class ApiFormatPhpTest
extends ApiFormatTestBase
{
9 protected $printerName = 'php';
11 private static function addFormatVersion( $format, $arr ) {
12 foreach ( $arr as &$p ) {
13 if ( !isset( $p[2] ) ) {
14 $p[2] = array( 'formatversion' => $format );
16 $p[2]['formatversion'] = $format;
22 public static function provideGeneralEncoding() {
23 // @codingStandardsIgnoreStart Generic.Files.LineLength
25 self
::addFormatVersion( 1, array(
27 array( array( null ), 'a:1:{i:0;N;}' ),
28 array( array( true ), 'a:1:{i:0;s:0:"";}' ),
29 array( array( false ), 'a:0:{}' ),
30 array( array( true, ApiResult
::META_BC_BOOLS
=> array( 0 ) ),
32 array( array( false, ApiResult
::META_BC_BOOLS
=> array( 0 ) ),
34 array( array( 42 ), 'a:1:{i:0;i:42;}' ),
35 array( array( 42.5 ), 'a:1:{i:0;d:42.5;}' ),
36 array( array( 1e42
), 'a:1:{i:0;d:1.0E+42;}' ),
37 array( array( 'foo' ), 'a:1:{i:0;s:3:"foo";}' ),
38 array( array( 'fóo' ), 'a:1:{i:0;s:4:"fóo";}' ),
41 array( array( array() ), 'a:1:{i:0;a:0:{}}' ),
42 array( array( array( 1 ) ), 'a:1:{i:0;a:1:{i:0;i:1;}}' ),
43 array( array( array( 'x' => 1 ) ), 'a:1:{i:0;a:1:{s:1:"x";i:1;}}' ),
44 array( array( array( 2 => 1 ) ), 'a:1:{i:0;a:1:{i:2;i:1;}}' ),
45 array( array( (object)array() ), 'a:1:{i:0;a:0:{}}' ),
46 array( array( array( 1, ApiResult
::META_TYPE
=> 'assoc' ) ), 'a:1:{i:0;a:1:{i:0;i:1;}}' ),
47 array( array( array( 'x' => 1, ApiResult
::META_TYPE
=> 'array' ) ), 'a:1:{i:0;a:1:{i:0;i:1;}}' ),
48 array( array( array( 'x' => 1, ApiResult
::META_TYPE
=> 'kvp' ) ), 'a:1:{i:0;a:1:{s:1:"x";i:1;}}' ),
49 array( array( array( '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 array( array( array( 'x' => 1, ApiResult
::META_TYPE
=> 'BCarray' ) ), 'a:1:{i:0;a:1:{s:1:"x";i:1;}}' ),
52 array( array( array( 'a', 'b', ApiResult
::META_TYPE
=> 'BCassoc' ) ), 'a:1:{i:0;a:2:{i:0;s:1:"a";i:1;s:1:"b";}}' ),
55 array( array( 'content' => 'foo', ApiResult
::META_CONTENT
=> 'content' ),
56 'a:1:{s:1:"*";s:3:"foo";}' ),
59 array( array( 'foo' => 'foo', ApiResult
::META_BC_SUBELEMENTS
=> array( 'foo' ) ),
60 'a:1:{s:3:"foo";a:1:{s:1:"*";s:3:"foo";}}' ),
62 self
::addFormatVersion( 2, array(
64 array( array( null ), 'a:1:{i:0;N;}' ),
65 array( array( true ), 'a:1:{i:0;b:1;}' ),
66 array( array( false ), 'a:1:{i:0;b:0;}' ),
67 array( array( true, ApiResult
::META_BC_BOOLS
=> array( 0 ) ),
69 array( array( false, ApiResult
::META_BC_BOOLS
=> array( 0 ) ),
71 array( array( 42 ), 'a:1:{i:0;i:42;}' ),
72 array( array( 42.5 ), 'a:1:{i:0;d:42.5;}' ),
73 array( array( 1e42
), 'a:1:{i:0;d:1.0E+42;}' ),
74 array( array( 'foo' ), 'a:1:{i:0;s:3:"foo";}' ),
75 array( array( 'fóo' ), 'a:1:{i:0;s:4:"fóo";}' ),
78 array( array( array() ), 'a:1:{i:0;a:0:{}}' ),
79 array( array( array( 1 ) ), 'a:1:{i:0;a:1:{i:0;i:1;}}' ),
80 array( array( array( 'x' => 1 ) ), 'a:1:{i:0;a:1:{s:1:"x";i:1;}}' ),
81 array( array( array( 2 => 1 ) ), 'a:1:{i:0;a:1:{i:2;i:1;}}' ),
82 array( array( (object)array() ), 'a:1:{i:0;a:0:{}}' ),
83 array( array( array( 1, ApiResult
::META_TYPE
=> 'assoc' ) ), 'a:1:{i:0;a:1:{i:0;i:1;}}' ),
84 array( array( array( 'x' => 1, ApiResult
::META_TYPE
=> 'array' ) ), 'a:1:{i:0;a:1:{i:0;i:1;}}' ),
85 array( array( array( 'x' => 1, ApiResult
::META_TYPE
=> 'kvp' ) ), 'a:1:{i:0;a:1:{s:1:"x";i:1;}}' ),
86 array( array( array( '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 array( array( array( 'x' => 1, ApiResult
::META_TYPE
=> 'BCarray' ) ), 'a:1:{i:0;a:1:{i:0;i:1;}}' ),
89 array( array( array( 'a', 'b', ApiResult
::META_TYPE
=> 'BCassoc' ) ), 'a:1:{i:0;a:2:{i:0;s:1:"a";i:1;s:1:"b";}}' ),
92 array( array( 'content' => 'foo', ApiResult
::META_CONTENT
=> 'content' ),
93 'a:1:{s:7:"content";s:3:"foo";}' ),
96 array( array( 'foo' => 'foo', ApiResult
::META_BC_SUBELEMENTS
=> array( 'foo' ) ),
97 'a:1:{s:3:"foo";s:3:"foo";}' ),
100 // @codingStandardsIgnoreEnd
103 public function testCrossDomainMangling() {
104 $config = new HashConfig( array( 'MangleFlashPolicy' => false ) );
105 $context = new RequestContext
;
106 $context->setConfig( new MultiConfig( array(
108 $context->getConfig(),
110 $main = new ApiMain( $context );
111 $main->getResult()->addValue( null, null, '< Cross-Domain-Policy >' );
113 if ( !function_exists( 'wfOutputHandler' ) ) {
114 function wfOutputHandler( $s ) {
119 $printer = $main->createPrinterByName( 'php' );
120 ob_start( 'wfOutputHandler' );
121 $printer->initPrinter();
123 $printer->closePrinter();
124 $ret = ob_get_clean();
125 $this->assertSame( 'a:1:{i:0;s:23:"< Cross-Domain-Policy >";}', $ret );
127 $config->set( 'MangleFlashPolicy', true );
128 $printer = $main->createPrinterByName( 'php' );
129 ob_start( 'wfOutputHandler' );
131 $printer->initPrinter();
133 $printer->closePrinter();
135 $this->fail( 'Expected exception not thrown' );
136 } catch ( UsageException
$ex ) {
139 'This response cannot be represented using format=php. ' .
140 'See https://phabricator.wikimedia.org/T68776',