Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / api / format / ApiFormatRawTest.php
bloba5e911e6347196209412528070a0755926c475eb
1 <?php
3 namespace MediaWiki\Tests\Api\Format;
5 use MediaWiki\Api\ApiFormatJson;
6 use MediaWiki\Api\ApiFormatRaw;
7 use MediaWiki\Api\ApiMain;
8 use MWException;
10 /**
11 * @group API
12 * @covers \MediaWiki\Api\ApiFormatRaw
14 class ApiFormatRawTest extends ApiFormatTestBase {
16 /** @inheritDoc */
17 protected $printerName = 'raw';
19 /**
20 * Test basic encoding and missing mime and text exceptions
21 * @return array datasets
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' ) );
31 return [
33 [ 'mime' => 'text/plain', 'text' => 'foo' ],
34 'foo',
35 [],
36 $options
39 [ 'mime' => 'text/plain', 'text' => 'fóo' ],
40 'fóo',
41 [],
42 $options
45 [ 'text' => 'some text' ],
46 new MWException( 'No MIME type set for raw formatter' ),
47 [],
48 $options
51 [ 'mime' => 'text/plain' ],
52 new MWException( 'No text given for raw formatter' ),
53 [],
54 $options
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
65 /**
66 * Test specifying filename
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() );
74 /**
75 * Test specifying filename with error fallback printer
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() );
85 /**
86 * Test specifying mime
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() );
94 /**
95 * Test specifying mime with error fallback printer
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() );
106 * Check that setting failWithHTTPError to true will result in 400 response status code
108 public function testFailWithHTTPError() {
109 $apiMain = null;
111 $this->testGeneralEncoding(
112 [ 'mime' => 'text/plain', 'text' => 'some text', 'error' => 'some error' ],
113 '{"mime":"text/plain","text":"some text","error":"some error"}',
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;
125 $this->assertEquals( 400, $apiMain->getRequest()->response()->getStatusCode() );