2020-10-23 17:50:06 +00:00
|
|
|
<?php
|
|
|
|
|
2023-03-09 10:43:14 +00:00
|
|
|
use MediaWiki\Extension\TemplateData\TemplateDataStatus;
|
2021-11-25 21:35:46 +00:00
|
|
|
|
2020-10-23 17:50:06 +00:00
|
|
|
/**
|
2023-03-09 10:43:14 +00:00
|
|
|
* @covers \MediaWiki\Extension\TemplateData\TemplateDataStatus
|
2023-08-23 07:29:25 +00:00
|
|
|
* @license GPL-2.0-or-later
|
2020-10-23 17:50:06 +00:00
|
|
|
*/
|
2021-10-11 23:06:08 +00:00
|
|
|
class SerializationTest extends MediaWikiIntegrationTestCase {
|
2023-03-09 10:43:14 +00:00
|
|
|
|
2020-10-23 17:50:06 +00:00
|
|
|
public function testParserOutputPersistenceForwardCompatibility() {
|
|
|
|
$output = new ParserOutput();
|
|
|
|
|
|
|
|
$status = Status::newFatal( 'a', 'b', 'c' );
|
|
|
|
$status->fatal( 'f' );
|
|
|
|
$status->warning( 'd', 'e' );
|
|
|
|
|
|
|
|
// Set JSONified state. Should work before we set JSON-serializable data,
|
|
|
|
// to be robust against old code reading new data after a rollback.
|
|
|
|
$output->setExtensionData( 'TemplateDataStatus',
|
2023-03-09 10:43:14 +00:00
|
|
|
TemplateDataStatus::jsonSerialize( $status )
|
2021-11-25 21:35:46 +00:00
|
|
|
);
|
2020-10-23 17:50:06 +00:00
|
|
|
|
2023-03-09 10:43:14 +00:00
|
|
|
$result = TemplateDataStatus::newFromJson( $output->getExtensionData( 'TemplateDataStatus' ) );
|
2020-10-23 17:50:06 +00:00
|
|
|
$this->assertEquals( $status->getStatusValue(), $result->getStatusValue() );
|
2021-08-28 09:08:24 +00:00
|
|
|
$this->assertSame( (string)$status, (string)$result );
|
2020-10-23 17:50:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testParserOutputPersistenceBackwardCompatibility() {
|
|
|
|
$output = new ParserOutput();
|
|
|
|
|
|
|
|
$status = Status::newFatal( 'a', 'b', 'c' );
|
|
|
|
$status->fatal( 'f' );
|
|
|
|
$status->warning( 'd', 'e' );
|
|
|
|
|
|
|
|
// Set the object directly. Should still work once we normally set JSON-serializable data.
|
|
|
|
$output->setExtensionData( 'TemplateDataStatus', $status );
|
|
|
|
|
2023-03-09 10:43:14 +00:00
|
|
|
$result = TemplateDataStatus::newFromJson( $output->getExtensionData( 'TemplateDataStatus' ) );
|
2020-10-23 17:50:06 +00:00
|
|
|
$this->assertEquals( $status->getStatusValue(), $result->getStatusValue() );
|
2021-08-28 09:08:24 +00:00
|
|
|
$this->assertSame( (string)$status, (string)$result );
|
2020-10-23 17:50:06 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 12:10:49 +00:00
|
|
|
public static function provideStatus() {
|
2020-10-23 17:50:06 +00:00
|
|
|
yield [ Status::newGood() ];
|
|
|
|
$status = Status::newFatal( 'a', 'b', 'c' );
|
|
|
|
$status->fatal( 'f' );
|
|
|
|
$status->warning( 'd', 'e' );
|
|
|
|
yield [ $status ];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideStatus
|
|
|
|
*/
|
|
|
|
public function testParserOutputPersistenceRoundTrip( Status $status ) {
|
|
|
|
$parserOutput = new ParserOutput();
|
2023-03-09 10:43:14 +00:00
|
|
|
$parserOutput->setExtensionData( 'TemplateDataStatus', TemplateDataStatus::jsonSerialize( $status ) );
|
|
|
|
$result = TemplateDataStatus::newFromJson( $parserOutput->getExtensionData( 'TemplateDataStatus' ) );
|
2020-10-23 17:50:06 +00:00
|
|
|
$this->assertEquals( $status->getStatusValue(), $result->getStatusValue() );
|
2021-08-28 09:08:24 +00:00
|
|
|
$this->assertSame( (string)$status, (string)$result );
|
2020-10-23 17:50:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideStatus
|
|
|
|
*/
|
|
|
|
public function testJsonRoundTrip( Status $status ) {
|
2023-03-09 10:43:14 +00:00
|
|
|
$json = TemplateDataStatus::jsonSerialize( $status );
|
|
|
|
$result = TemplateDataStatus::newFromJson( $json );
|
2020-10-23 17:50:06 +00:00
|
|
|
$this->assertEquals( $status->getStatusValue(), $result->getStatusValue() );
|
2021-08-28 09:08:24 +00:00
|
|
|
$this->assertSame( (string)$status, (string)$result );
|
2020-10-23 17:50:06 +00:00
|
|
|
}
|
2023-03-09 10:43:14 +00:00
|
|
|
|
2020-10-23 17:50:06 +00:00
|
|
|
}
|