| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Parser; |
| 4 |
|
| 5 |
use MediaWiki\Parser\Parsoid\PageBundleJsonTrait; |
| 6 |
use MediaWikiIntegrationTestCase; |
| 7 |
use Wikimedia\Parsoid\Core\PageBundle; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class PageBundleJsonTraitTest extends MediaWikiIntegrationTestCase { |
| 13 |
|
| 14 |
private const BUNDLE_DATA = [ |
| 15 |
'html' => '<h1>woohoo</h1>', |
| 16 |
'parsoid' => [ 'metadata' => 'foo' ], |
| 17 |
'mw' => null, |
| 18 |
'version' => '1.0', |
| 19 |
'headers' => [ 'bar' => 'baz' ], |
| 20 |
'contentmodel' => 'default' |
| 21 |
]; |
| 22 |
|
| 23 |
public function testNewPageBundleFromJson() { |
| 24 |
$trait = new class { |
| 25 |
use PageBundleJsonTrait { |
| 26 |
newPageBundleFromJson as public; |
| 27 |
} |
| 28 |
}; |
| 29 |
$bundle = $trait->newPageBundleFromJson( self::BUNDLE_DATA ); |
| 30 |
$this->assertInstanceOf( PageBundle::class, $bundle ); |
| 31 |
$this->assertEquals( '<h1>woohoo</h1>', $bundle->html ); |
| 32 |
$this->assertEquals( 'default', $bundle->contentmodel ); |
| 33 |
$this->assertNull( $bundle->mw ); |
| 34 |
} |
| 35 |
|
| 36 |
public function testJsonSerializePageBundle() { |
| 37 |
$trait = new class { |
| 38 |
use PageBundleJsonTrait { |
| 39 |
jsonSerializePageBundle as public; |
| 40 |
} |
| 41 |
}; |
| 42 |
$bundle = new PageBundle( ...array_values( self::BUNDLE_DATA ) ); |
| 43 |
$json = $trait->jsonSerializePageBundle( $bundle ); |
| 44 |
$this->assertEquals( PageBundle::class, $json['_type_'] ); |
| 45 |
$this->assertEquals( '<h1>woohoo</h1>', $json['html'] ); |
| 46 |
$this->assertNull( $json['mw'] ); |
| 47 |
} |
| 48 |
} |