| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Api\Format; |
| 4 |
|
| 5 |
use MediaWiki\Api\ApiFormatJson; |
| 6 |
use MediaWiki\Api\ApiFormatRaw; |
| 7 |
use MediaWiki\Api\ApiMain; |
| 8 |
use MWException; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class ApiFormatRawTest extends ApiFormatTestBase { |
| 15 |
|
| 16 |
|
| 17 |
protected $printerName = 'raw'; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public static function provideGeneralEncoding() { |
| 24 |
$options = [ |
| 25 |
'class' => ApiFormatRaw::class, |
| 26 |
'factory' => static function ( ApiMain $main ) { |
| 27 |
return new ApiFormatRaw( $main, new ApiFormatJson( $main, 'json' ) ); |
| 28 |
} |
| 29 |
]; |
| 30 |
|
| 31 |
return [ |
| 32 |
[ |
| 33 |
[ 'mime' => 'text/plain', 'text' => 'foo' ], |
| 34 |
'foo', |
| 35 |
[], |
| 36 |
$options |
| 37 |
], |
| 38 |
[ |
| 39 |
[ 'mime' => 'text/plain', 'text' => 'fóo' ], |
| 40 |
'fóo', |
| 41 |
[], |
| 42 |
$options |
| 43 |
], |
| 44 |
[ |
| 45 |
[ 'text' => 'some text' ], |
| 46 |
new MWException( 'No MIME type set for raw formatter' ), |
| 47 |
[], |
| 48 |
$options |
| 49 |
], |
| 50 |
[ |
| 51 |
[ 'mime' => 'text/plain' ], |
| 52 |
new MWException( 'No text given for raw formatter' ), |
| 53 |
[], |
| 54 |
$options |
| 55 |
], |
| 56 |
'test error fallback' => [ |
| 57 |
[ 'mime' => 'text/plain', 'text' => 'some text', 'error' => 'some error' ], |
| 58 |
'{"mime":"text/plain","text":"some text","error":"some error"}', |
| 59 |
[], |
| 60 |
$options |
| 61 |
] |
| 62 |
]; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public function testFilename() { |
| 69 |
$printer = new ApiFormatRaw( new ApiMain ); |
| 70 |
$printer->getResult()->addValue( null, 'filename', 'whatever.raw' ); |
| 71 |
$this->assertSame( 'whatever.raw', $printer->getFilename() ); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public function testErrorFallbackFilename() { |
| 78 |
$apiMain = new ApiMain; |
| 79 |
$printer = new ApiFormatRaw( $apiMain, new ApiFormatJson( $apiMain, 'json' ) ); |
| 80 |
$printer->getResult()->addValue( null, 'error', 'some error' ); |
| 81 |
$printer->getResult()->addValue( null, 'filename', 'whatever.raw' ); |
| 82 |
$this->assertSame( 'api-result.json', $printer->getFilename() ); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public function testMime() { |
| 89 |
$printer = new ApiFormatRaw( new ApiMain ); |
| 90 |
$printer->getResult()->addValue( null, 'mime', 'text/plain' ); |
| 91 |
$this->assertSame( 'text/plain', $printer->getMimeType() ); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public function testErrorFallbackMime() { |
| 98 |
$apiMain = new ApiMain; |
| 99 |
$printer = new ApiFormatRaw( $apiMain, new ApiFormatJson( $apiMain, 'json' ) ); |
| 100 |
$printer->getResult()->addValue( null, 'error', 'some error' ); |
| 101 |
$printer->getResult()->addValue( null, 'mime', 'text/plain' ); |
| 102 |
$this->assertSame( 'application/json', $printer->getMimeType() ); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public function testFailWithHTTPError() { |
| 109 |
$apiMain = null; |
| 110 |
|
| 111 |
$this->testGeneralEncoding( |
| 112 |
[ 'mime' => 'text/plain', 'text' => 'some text', 'error' => 'some error' ], |
| 113 |
'{"mime":"text/plain","text":"some text","error":"some error"}', |
| 114 |
[], |
| 115 |
[ |
| 116 |
'class' => ApiFormatRaw::class, |
| 117 |
'factory' => static function ( ApiMain $main ) use ( &$apiMain ) { |
| 118 |
$apiMain = $main; |
| 119 |
$printer = new ApiFormatRaw( $main, new ApiFormatJson( $main, 'json' ) ); |
| 120 |
$printer->setFailWithHTTPError( true ); |
| 121 |
return $printer; |
| 122 |
} |
| 123 |
] |
| 124 |
); |
| 125 |
$this->assertEquals( 400, $apiMain->getRequest()->response()->getStatusCode() ); |
| 126 |
} |
| 127 |
|
| 128 |
} |