Improve testing for ApiFormatBase subclasses
[lhc/web/wiklou.git] / tests / phpunit / includes / api / format / ApiFormatPhpTest.php
1 <?php
2
3 /**
4 * @group API
5 * @covers ApiFormatPhp
6 */
7 class ApiFormatPhpTest extends ApiFormatTestBase {
8
9 protected $printerName = 'php';
10
11 public static function provideGeneralEncoding() {
12 return array(
13 // Basic types
14 array( array( null ), 'a:1:{i:0;N;}' ),
15 array( array( true ), 'a:1:{i:0;b:1;}' ),
16 array( array( false ), 'a:1:{i:0;b:0;}' ),
17 array( array( 42 ), 'a:1:{i:0;i:42;}' ),
18 array( array( 42.5 ), 'a:1:{i:0;d:42.5;}' ),
19 array( array( 1e42 ), 'a:1:{i:0;d:1.0E+42;}' ),
20 array( array( 'foo' ), 'a:1:{i:0;s:3:"foo";}' ),
21 array( array( 'fóo' ), 'a:1:{i:0;s:4:"fóo";}' ),
22
23 // Arrays and objects
24 array( array( array() ), 'a:1:{i:0;a:0:{}}' ),
25 array( array( array( 1 ) ), 'a:1:{i:0;a:1:{i:0;i:1;}}' ),
26 array( array( array( 'x' => 1 ) ), 'a:1:{i:0;a:1:{s:1:"x";i:1;}}' ),
27 array( array( array( 2 => 1 ) ), 'a:1:{i:0;a:1:{i:2;i:1;}}' ),
28
29 // Content
30 array( array( '*' => 'foo' ), 'a:1:{s:1:"*";s:3:"foo";}' ),
31 );
32 }
33
34 public function testCrossDomainMangling() {
35 $config = new HashConfig( array( 'MangleFlashPolicy' => false ) );
36 $context = new RequestContext;
37 $context->setConfig( new MultiConfig( array(
38 $config,
39 $context->getConfig(),
40 ) ) );
41 $main = new ApiMain( $context );
42 $main->getResult()->addValue( null, null, '< Cross-Domain-Policy >' );
43
44 if ( !function_exists( 'wfOutputHandler' ) ) {
45 function wfOutputHandler( $s ) {
46 return $s;
47 }
48 }
49
50 $printer = $main->createPrinterByName( 'php' );
51 ob_start( 'wfOutputHandler' );
52 $printer->initPrinter();
53 $printer->execute();
54 $printer->closePrinter();
55 $ret = ob_get_clean();
56 $this->assertSame( 'a:1:{i:0;s:23:"< Cross-Domain-Policy >";}', $ret );
57
58 $config->set( 'MangleFlashPolicy', true );
59 $printer = $main->createPrinterByName( 'php' );
60 ob_start( 'wfOutputHandler' );
61 try {
62 $printer->initPrinter();
63 $printer->execute();
64 $printer->closePrinter();
65 ob_end_clean();
66 $this->fail( 'Expected exception not thrown' );
67 } catch ( UsageException $ex ) {
68 ob_end_clean();
69 $this->assertSame(
70 'This response cannot be represented using format=php. See https://bugzilla.wikimedia.org/show_bug.cgi?id=66776',
71 $ex->getMessage(),
72 'Expected exception'
73 );
74 }
75 }
76
77 }