Skip to content

Commit

Permalink
Merge "Permit Parsoid minor version bumps"
Browse files Browse the repository at this point in the history
  • Loading branch information
jenkins-bot authored and Gerrit Code Review committed Dec 6, 2022
2 parents 13d8b87 + dc02ef2 commit 93cd2cb
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
12 changes: 11 additions & 1 deletion includes/parser/Parsoid/HtmlToContentTransform.php
Expand Up @@ -508,7 +508,17 @@ private function needsDowngrade( PageBundle $pb ): bool {
$vOriginal = $pb->version;
$vEdited = $this->getSchemaVersion();

return $vOriginal !== null && $vOriginal !== $vEdited;
// Downgrades are only expected to be between major version
//
// RESTBase was only expected to store latest version. If a client asked for a version
// not satisfied by the latest version, it would downgrade the stored version where
// possible. So, it's the original version that needs to satisfy the edited version,
// otherwise it needs downgrading.
//
// There's also the case where an old version is not stored and a re-parse must occur.
// Here again the original version generated will be the latest, either satisfying
// the edited or needing downgrading.
return $vOriginal !== null && !Semver::satisfies( $vOriginal, "^{$vEdited}" );
}

private function downgradeOriginalData( PageBundle $pb, string $targetSchemaVersion ) {
Expand Down
Expand Up @@ -2,6 +2,7 @@

namespace MediaWiki\Tests\Rest\Handler;

use Composer\Semver\Semver;
use Exception;
use Generator;
use MediaWiki\MainConfigNames;
Expand Down Expand Up @@ -355,6 +356,38 @@ private function getJsonFromFile( string $name ): array {
return json_decode( $text, JSON_OBJECT_AS_ARRAY );
}

// Mostly lifted from the contentTypeMatcher in tests/api-testing/REST/Transform.js
private function contentTypeMatcher( string $expected, string $actual ): bool {
if ( $expected === 'application/json' ) {
return $actual === $expected;
}

$pattern = '/^([-\w]+\/[-\w]+); charset=utf-8; profile="https:\/\/www.mediawiki.org\/wiki\/Specs\/([-\w]+)\/(\d+\.\d+\.\d+)"$/';

preg_match( $pattern, $expected, $expectedParts );
if ( !$expectedParts ) {
return false;
}
[ , $expectedMime, $expectedSpec, $expectedVersion ] = $expectedParts;

preg_match( $pattern, $actual, $actualParts );
if ( !$actualParts ) {
return false;
}
[ , $actualMime, $actualSpec, $actualVersion ] = $actualParts;

// Match version using caret semantics
if ( !Semver::satisfies( $actualVersion, "^{$expectedVersion}" ) ) {
return false;
}

if ( $actualMime !== $expectedMime || $actualSpec !== $expectedSpec ) {
return false;
}

return true;
}

public function provideHtml2wt() {
$profileVersion = '2.6.0';
$wikitextProfileUri = 'https://www.mediawiki.org/wiki/Specs/wikitext/1.0.0';
Expand Down Expand Up @@ -1961,7 +1994,12 @@ public function testWt2html(
$data = $body->getContents();

foreach ( $expectedHeaders as $name => $value ) {
$this->assertSame( $value, $response->getHeaderLine( $name ) );
$responseHeaderValue = $response->getHeaderLine( $name );
if ( $name === 'content-type' ) {
$this->assertTrue( $this->contentTypeMatcher( $value, $responseHeaderValue ) );
} else {
$this->assertSame( $value, $responseHeaderValue );
}
}

// HACK: try to parse as json, just in case:
Expand All @@ -1972,7 +2010,13 @@ public function testWt2html(
$this->assertStringContainsString( $exp, $data );
} else {
$this->assertArrayHasKey( $index, $jsonData );
$this->assertSame( $exp, $jsonData[$index] );
if ( $index === 'data-parsoid' ) {
// FIXME: Assert headers as well
$this->assertArrayHasKey( 'body', $jsonData[$index] );
$this->assertSame( $exp['body'], $jsonData[$index]['body'] );
} else {
$this->assertSame( $exp, $jsonData[$index] );
}
}
}

Expand Down

0 comments on commit 93cd2cb

Please sign in to comment.