Skip to content

Commit

Permalink
Use dataProvider in TemplateDataTest.php
Browse files Browse the repository at this point in the history
Change-Id: Ib19a012bc40b523bcd9837c1122539fca8aeec93
  • Loading branch information
arlolra committed Dec 4, 2019
1 parent 462ac75 commit 6d710bd
Showing 1 changed file with 59 additions and 53 deletions.
112 changes: 59 additions & 53 deletions tests/phpunit/Parsoid/Html2Wt/TemplateDataTest.php
Expand Up @@ -39,8 +39,8 @@ private function verifyTransformation( $newHTML, $origHTML, $origWT, $expectedWT
$this->assertEquals( $expectedWT, $wt, $description );
}

private function defineTestData(): array {
$testData = [
public function defineTestData(): array {
return [
// 1. Transclusions without template data
[
'name' => 'Transclusions without template data',
Expand All @@ -59,6 +59,7 @@ private function defineTestData(): array {

// 2. normal
[
'name' => 'normal',
'html' => '<span about="#mwt1" typeof="mw:Transclusion" data-parsoid=' . "'" .
'{"pi":[[{"k":"f1"},{"k":"f2"}]]}' . "' data-mw='" .
'{"parts":[{"template":{"target":{"wt":"NoFormatWithParamOrder",' .
Expand Down Expand Up @@ -238,6 +239,7 @@ private function defineTestData(): array {

// 13. Inline Formatted template 1
[
'name' => 'Inline Formatted template 1',
'html' => 'x <span about="#mwt1" typeof="mw:Transclusion" data-parsoid=' . "'" .
'{"pi":[[{"k":"f1"},{"k":"x"}]]}' . "' data-mw='" .
'{"parts":[{"template":{"target":{"wt":"InlineFormattedTpl_1",' .
Expand All @@ -252,6 +254,7 @@ private function defineTestData(): array {

// 14. Inline Formatted template 2
[
'name' => 'Inline Formatted template 2',
'html' => 'x <span about="#mwt1" typeof="mw:Transclusion" data-parsoid=' . "'" .
'{"pi":[[{"k":"f1"},{"k":"x"}]]}' . "' data-mw='" .
'{"parts":[{"template":{"target":{"wt":"InlineFormattedTpl_2",' .
Expand All @@ -266,6 +269,7 @@ private function defineTestData(): array {

// 15. Inline Formatted template 3
[
'name' => 'Inline Formatted template 3',
'html' => 'x <span about="#mwt1" typeof="mw:Transclusion" data-parsoid=' . "'" .
'{"pi":[[{"k":"f1"},{"k":"x"}]]}' . "' data-mw='" .
'{"parts":[{"template":{"target":{"wt":"InlineFormattedTpl_3",' .
Expand All @@ -280,6 +284,7 @@ private function defineTestData(): array {

// 16. Custom block formatting 1
[
'name' => 'Custom block formatting 1',
'html' => 'x<span about="#mwt1" typeof="mw:Transclusion" data-parsoid=' . "'" .
'{"pi":[[{"k":"f1"},{"k":"f2"}]]}' . "' data-mw='" .
'{"parts":[{"template":{"target":{"wt":"BlockFormattedTpl_1",' .
Expand All @@ -294,6 +299,7 @@ private function defineTestData(): array {

// 17. Custom block formatting 2
[
'name' => 'Custom block formatting 2',
'html' => 'x<span about="#mwt1" typeof="mw:Transclusion" data-parsoid=' . "'" .
'{"pi":[[{"k":"f1"},{"k":"f2"}]]}' . "' data-mw='" .
'{"parts":[{"template":{"target":{"wt":"BlockFormattedTpl_2",' .
Expand All @@ -308,6 +314,7 @@ private function defineTestData(): array {

// 18. Custom block formatting 3 - T199849
[
'name' => 'Custom block formatting 3 - T199849',
'html' => "x\n" . '<span about="#mwt1" typeof="mw:Transclusion" data-mw=' . "'" .
'{"parts":[{"template":{"target":{"wt":"BlockFormattedTpl_2",' .
'"href":"./Template:BlockFormattedTpl_2"},"params":{"f1":{"wt":""},' .
Expand All @@ -318,8 +325,9 @@ private function defineTestData(): array {
]
],

// 19. Custom block formatting 3 - T199849
// 19. Custom block formatting 4 - T199849
[
'name' => 'Custom block formatting 4 - T199849',
'html' => "x\n" . '<span about="#mwt1" typeof="mw:Transclusion" data-mw=' . "'" .
'{"parts":["X", {"template":{"target":{"wt":"BlockFormattedTpl_2",' .
'"href":"./Template:BlockFormattedTpl_2"},"params":{"f1":{"wt":""},' .
Expand All @@ -330,8 +338,9 @@ private function defineTestData(): array {
]
],

// 19. Custom block formatting 3 - T199849
// 19. Custom block formatting 5 - T199849
[
'name' => 'Custom block formatting 5 - T199849',
'html' => "x\n" . '<span about="#mwt1" typeof="mw:Transclusion" data-mw=' . "'" .
'{"parts":[{"template":{"target":{"wt":"BlockFormattedTpl_2",' .
'"href":"./Template:BlockFormattedTpl_2"},"params":{"g1":{"wt":""},' .
Expand All @@ -346,8 +355,9 @@ private function defineTestData(): array {
]
],

// 20. Custom block formatting 4
// 20. Custom block formatting 6
[
'name' => 'Custom block formatting 6',
'html' => 'x<span about="#mwt1" typeof="mw:Transclusion" data-parsoid=' . "'" .
'{"pi":[[{"k":"f1"},{"k":"f2"}]]}' . "' data-mw='" .
'{"parts":[{"template":{"target":{"wt":"BlockFormattedTpl_3",' .
Expand All @@ -360,11 +370,40 @@ private function defineTestData(): array {
]
]
];
return $testData;
}

private function defineVersionTestData(): array {
$testData = [
/**
* @covers \Parsoid\Parsoid::html2wikitext
* @covers \Parsoid\WikitextContentModelHandler::fromHTML
* @dataProvider defineTestData
*/
public function testTemplateData(
string $name, string $html, array $wt
): void {
// Non-selser test
if ( isset( $wt['no_selser'] ) ) {
$desc = "$name: Default non-selser serialization should ignore templatedata";
self::verifyTransformation( $html, null, null, $wt['no_selser'], $desc );
}

// New content test
$desc = "$name: Serialization of new content (no data-parsoid) should respect templatedata";
// Remove data-parsoid making it look like new content
$newHTML = preg_replace( '/data-parsoid.*? data-mw/', ' data-mw', $html );
self::verifyTransformation( $newHTML, '', '', $wt['new_content'], $desc );

// Transclusion edit test
$desc = "$name: Serialization of edited content should respect templatedata";
// Replace only the first instance of 'foo' with 'BAR'
// to simulate an edit of a transclusion.
$newHTML = preg_replace( '/foo/', 'BAR', $html, 1 );
self::verifyTransformation(
$newHTML, $html, $wt['no_selser'] ?? '', $wt['edited'], $desc
);
}

public function defineVersionTestData(): array {
return [
[
'contentVersion' => $this->defaultContentVersion,
'html' => '<span about="#mwt1" typeof="mw:Transclusion" data-parsoid=' . "'" .
Expand All @@ -378,57 +417,24 @@ private function defineVersionTestData(): array {
]
]
];
return $testData;
}

/**
* @covers \Parsoid\Parsoid::html2wikitext
* @covers \Parsoid\WikitextContentModelHandler::fromHTML
* @dataProvider defineVersionTestData
*/
public function testTemplateData(): void {
$tests = self::defineTestData();

foreach ( $tests as $key => $test ) {
$html = $test['html'];
$name = 'Single Template Test ' . ( $key . 1 );
if ( isset( $test['name'] ) ) {
$name .= ' (' . $test['name'] . ')';
}
$name .= ': ';

// Non-selser test
if ( isset( $test['wt']['no_selser'] ) ) {
$desc = $name . 'Default non-selser serialization should ignore templatedata';
self::verifyTransformation( $html, null, null, $test['wt']['no_selser'], $desc );
}

// New content test
$desc = $name . 'Serialization of new content (no data-parsoid) should respect templatedata';
// Remove data-parsoid making it look like new content
$newHTML = preg_replace( '/data-parsoid.*? data-mw/', ' data-mw', $html );
self::verifyTransformation( $newHTML, '', '', $test['wt']['new_content'], $desc );

// Transclusion edit test
$desc = $name . 'Serialization of edited content should respect templatedata';
// Replace only the first instance of 'foo' with 'BAR'
// to simulate an edit of a transclusion.
$newHTML = preg_replace( '/foo/', 'BAR', $html, 1 );
self::verifyTransformation( $newHTML, $html, $test['wt']['no_selser'] ?? '',
$test['wt']['edited'], $desc );
}

$tests = self::defineVersionTestData();

// forEach(function(test) {
foreach ( $tests as $test ) {
$desc = 'Serialization should use correct arg space defaults for data-parsoid version ' .
$test['contentVersion'];
// Replace only the first instance of 'foo' with 'BAR'
// to simulate an edit of a transclusion.
$newHTML = preg_replace( '/foo/', 'BAR', $test['html'], 1 );
self::verifyTransformation( $newHTML, $test['html'], $test['wt']['orig'], $test['wt']['edited'],
$desc, $test['contentVersion'] );
}
public function testTemplateDataVersion(
string $contentVersion, string $html, array $wt
): void {
$desc = "Serialization should use correct arg space defaults for " .
"data-parsoid version $contentVersion";
// Replace only the first instance of 'foo' with 'BAR'
// to simulate an edit of a transclusion.
$newHTML = preg_replace( '/foo/', 'BAR', $html, 1 );
self::verifyTransformation(
$newHTML, $html, $wt['orig'], $wt['edited'], $desc, $contentVersion
);
}

}

0 comments on commit 6d710bd

Please sign in to comment.