| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Integration\Edit; |
| 4 |
|
| 5 |
use MediaWiki\Content\Content; |
| 6 |
use MediaWiki\Content\ContentHandler; |
| 7 |
use MediaWiki\Content\ContentJsonCodec; |
| 8 |
use MediaWiki\Content\IContentHandlerFactory; |
| 9 |
use MediaWiki\Content\WikitextContent; |
| 10 |
use MediaWiki\Edit\SelserContext; |
| 11 |
use MediaWiki\Json\JsonCodec; |
| 12 |
use MediaWikiIntegrationTestCase; |
| 13 |
use Psr\Container\ContainerInterface; |
| 14 |
use Wikimedia\Parsoid\Core\PageBundle; |
| 15 |
use Wikimedia\Tests\SerializationTestTrait; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
class SimpleParsoidOutputStashSerializationTest extends MediaWikiIntegrationTestCase { |
| 22 |
use SerializationTestTrait; |
| 23 |
|
| 24 |
public static function getClassToTest(): string { |
| 25 |
return SelserContext::class; |
| 26 |
} |
| 27 |
|
| 28 |
public static function getSerializedDataPath(): string { |
| 29 |
return __DIR__ . '/../../../data/SelserContext'; |
| 30 |
} |
| 31 |
|
| 32 |
public static function getTestInstancesAndAssertions(): array { |
| 33 |
return [ |
| 34 |
'basic' => [ |
| 35 |
'instance' => new SelserContext( |
| 36 |
new PageBundle( |
| 37 |
'<b>html</b>', |
| 38 |
[ |
| 39 |
'counter' => 1234, |
| 40 |
'offsetType' => 'byte', |
| 41 |
'ids' => [ |
| 42 |
'mwAA' => [ 'parsoid' => true ], |
| 43 |
], |
| 44 |
], |
| 45 |
[ |
| 46 |
'ids' => [ |
| 47 |
'mwAA' => [ 'mw' => true ], |
| 48 |
], |
| 49 |
], |
| 50 |
'1.2.3.4', |
| 51 |
[ |
| 52 |
'X-Header-Test' => 'header test', |
| 53 |
], |
| 54 |
CONTENT_MODEL_WIKITEXT, |
| 55 |
), |
| 56 |
5678, |
| 57 |
new WikitextContent( 'wiki wiki wiki' ) |
| 58 |
), |
| 59 |
'assertions' => static function ( MediaWikiIntegrationTestCase $testCase, SelserContext $ss ) { |
| 60 |
$pb = $ss->getPageBundle(); |
| 61 |
$testCase->assertSame( '<b>html</b>', $pb->html ); |
| 62 |
$testCase->assertSame( [ |
| 63 |
'counter' => 1234, |
| 64 |
'offsetType' => 'byte', |
| 65 |
'ids' => [ |
| 66 |
'mwAA' => [ 'parsoid' => true ], |
| 67 |
], |
| 68 |
], $pb->parsoid ); |
| 69 |
$testCase->assertSame( [ |
| 70 |
'ids' => [ |
| 71 |
'mwAA' => [ 'mw' => true ], |
| 72 |
], |
| 73 |
], $pb->mw ); |
| 74 |
$testCase->assertSame( '1.2.3.4', $pb->version ); |
| 75 |
$testCase->assertSame( [ |
| 76 |
'X-Header-Test' => 'header test', |
| 77 |
], $pb->headers ); |
| 78 |
$testCase->assertSame( CONTENT_MODEL_WIKITEXT, $pb->contentmodel ); |
| 79 |
|
| 80 |
$testCase->assertSame( 5678, $ss->getRevisionID() ); |
| 81 |
|
| 82 |
$content = $ss->getContent(); |
| 83 |
$testCase->assertSame( CONTENT_MODEL_WIKITEXT, $content->getModel() ); |
| 84 |
$testCase->assertSame( 'wiki wiki wiki', $content->getText() ); |
| 85 |
}, |
| 86 |
], |
| 87 |
]; |
| 88 |
} |
| 89 |
|
| 90 |
public static function getSupportedSerializationFormats(): array { |
| 91 |
$mockServices = new class implements ContainerInterface { |
| 92 |
private $contents = []; |
| 93 |
|
| 94 |
public function get( $id ) { |
| 95 |
return $this->contents[$id] ?? null; |
| 96 |
} |
| 97 |
|
| 98 |
public function has( $id ): bool { |
| 99 |
return isset( $this->contents[$id] ); |
| 100 |
} |
| 101 |
|
| 102 |
public function set( $id, $value ) { |
| 103 |
$this->contents[$id] = $value; |
| 104 |
} |
| 105 |
}; |
| 106 |
$chFactory = new class implements IContentHandlerFactory { |
| 107 |
public function getContentHandler( string $modelID ): ContentHandler { |
| 108 |
return new class( CONTENT_MODEL_WIKITEXT, [ CONTENT_FORMAT_WIKITEXT ] ) extends ContentHandler { |
| 109 |
public function serializeContent( Content $content, $format = null ) { |
| 110 |
return $content->getText(); |
| 111 |
} |
| 112 |
|
| 113 |
public function unserializeContent( $blob, $format = null ) { |
| 114 |
return new WikitextContent( $blob ); |
| 115 |
} |
| 116 |
|
| 117 |
public function makeEmptyContent() { |
| 118 |
throw new \Error( "unimplemented" ); |
| 119 |
} |
| 120 |
}; |
| 121 |
} |
| 122 |
|
| 123 |
public function getContentModels(): array { |
| 124 |
return [ CONTENT_MODEL_WIKITEXT ]; |
| 125 |
} |
| 126 |
|
| 127 |
public function getAllContentFormats(): array { |
| 128 |
return [ CONTENT_FORMAT_WIKITEXT ]; |
| 129 |
} |
| 130 |
|
| 131 |
public function isDefinedModel( string $modelId ): bool { |
| 132 |
return $modelId === CONTENT_MODEL_WIKITEXT; |
| 133 |
} |
| 134 |
}; |
| 135 |
$mockServices->set( |
| 136 |
'ContentHandlerFactory', $chFactory |
| 137 |
); |
| 138 |
$mockServices->set( |
| 139 |
'ContentJsonCodec', new ContentJsonCodec( $chFactory ) |
| 140 |
); |
| 141 |
$jsonCodec = new JsonCodec( $mockServices ); |
| 142 |
return [ |
| 143 |
[ |
| 144 |
'ext' => 'serialized', |
| 145 |
'serializer' => static function ( $obj ) use ( $jsonCodec ) { |
| 146 |
return serialize( |
| 147 |
$jsonCodec->toJsonArray( $obj, SelserContext::class ) |
| 148 |
); |
| 149 |
}, |
| 150 |
'deserializer' => static function ( $data ) use ( $jsonCodec ) { |
| 151 |
return $jsonCodec->newFromJsonArray( |
| 152 |
unserialize( $data ), SelserContext::class |
| 153 |
); |
| 154 |
}, |
| 155 |
], |
| 156 |
[ |
| 157 |
'ext' => 'json', |
| 158 |
'serializer' => static function ( $obj ) use ( $jsonCodec ) { |
| 159 |
return json_encode( |
| 160 |
$jsonCodec->toJsonArray( $obj, SelserContext::class ) |
| 161 |
); |
| 162 |
}, |
| 163 |
'deserializer' => static function ( $data ) use ( $jsonCodec ) { |
| 164 |
return $jsonCodec->newFromJsonArray( |
| 165 |
json_decode( $data, true ), SelserContext::class |
| 166 |
); |
| 167 |
}, |
| 168 |
], |
| 169 |
]; |
| 170 |
} |
| 171 |
|
| 172 |
} |