| 1 |
<?php |
| 2 |
|
| 3 |
namespace Test\Parsoid\Html2Wt; |
| 4 |
|
| 5 |
use PHPUnit\Framework\TestCase; |
| 6 |
use Wikimedia\Parsoid\Core\SelectiveUpdateData; |
| 7 |
use Wikimedia\Parsoid\Mocks\MockDataAccess; |
| 8 |
use Wikimedia\Parsoid\Mocks\MockPageConfig; |
| 9 |
use Wikimedia\Parsoid\Mocks\MockPageContent; |
| 10 |
use Wikimedia\Parsoid\Mocks\MockSiteConfig; |
| 11 |
use Wikimedia\Parsoid\Parsoid; |
| 12 |
|
| 13 |
class TemplateDataTest extends TestCase { |
| 14 |
|
| 15 |
private static $defaultContentVersion = Parsoid::AVAILABLE_VERSIONS[0]; |
| 16 |
|
| 17 |
private function verifyTransformation( string $newHTML, ?string $origHTML, ?string $origWT, string $expectedWT, |
| 18 |
string $description, ?string $contentVersion = null ) { |
| 19 |
$parserOpts = []; |
| 20 |
$opts = []; |
| 21 |
|
| 22 |
$siteConfig = new MockSiteConfig( $opts ); |
| 23 |
$dataAccess = new MockDataAccess( $siteConfig, $opts ); |
| 24 |
$parsoid = new Parsoid( $siteConfig, $dataAccess ); |
| 25 |
|
| 26 |
$pageContent = new MockPageContent( [ 'main' => '' ] ); |
| 27 |
$pageConfig = new MockPageConfig( $siteConfig, $opts, $pageContent ); |
| 28 |
|
| 29 |
if ( isset( $origHTML ) && strlen( $origHTML ) > 0 ) { |
| 30 |
$selserData = new SelectiveUpdateData( $origWT, $origHTML ); |
| 31 |
} else { |
| 32 |
$selserData = null; |
| 33 |
} |
| 34 |
if ( isset( $contentVersion ) ) { |
| 35 |
$parserOpts['inputContentVersion'] = $contentVersion; |
| 36 |
} |
| 37 |
|
| 38 |
$wt = $parsoid->html2wikitext( $pageConfig, $newHTML, $parserOpts, $selserData ); |
| 39 |
$this->assertEquals( $expectedWT, $wt, $description ); |
| 40 |
} |
| 41 |
|
| 42 |
public function defineTestData(): array { |
| 43 |
return [ |
| 44 |
// 1. Transclusions without template data |
| 45 |
[ |
| 46 |
'name' => 'Transclusions without template data', |
| 47 |
'html' => '<span about="#mwt1" typeof="mw:Transclusion" data-parsoid=' . "'" . |
| 48 |
'{"pi":[[{"k":"f2","spc":[""," "," ","\\n"]},{"k":"f1","spc":[""," "," ","\\n"]}]]}' . |
| 49 |
"' data-mw='" . |
| 50 |
'{"parts":[{"template":{"target":{"wt":"TplWithoutTemplateData\\n",' . |
| 51 |
'"href":"./Template:TplWithoutTemplateData"},"params":{"f1":{"wt":"foo"},' . |
| 52 |
'"f2":{"wt":"foo"}},"i":0}}]}' . "'" . '>foo</span>', |
| 53 |
'wt' => [ |
| 54 |
'no_selser' => "{{TplWithoutTemplateData\n|f2 = foo\n|f1 = foo\n}}", |
| 55 |
'new_content' => "{{TplWithoutTemplateData|f1=foo|f2=foo}}", |
| 56 |
'edited' => "{{TplWithoutTemplateData\n|f2 = foo\n|f1 = BAR\n}}" |
| 57 |
] |
| 58 |
], |
| 59 |
|
| 60 |
// 2. normal |
| 61 |
[ |
| 62 |
'name' => 'normal', |
| 63 |
'html' => '<span about="#mwt1" typeof="mw:Transclusion" data-parsoid=' . "'" . |
| 64 |
'{"pi":[[{"k":"f1"},{"k":"f2"}]]}' . "' data-mw='" . |
| 65 |
'{"parts":[{"template":{"target":{"wt":"NoFormatWithParamOrder",' . |
| 66 |
'"href":"./Template:NoFormatWithParamOrder"},"params":{"f1":{"wt":"foo"},' . |
| 67 |
'"f2":{"wt":"foo"}},"i":0}}]}' . "'" . '>foo</span>', |
| 68 |
'wt' => [ |
| 69 |
'no_selser' => "{{NoFormatWithParamOrder|f1=foo|f2=foo}}", |
| 70 |
'new_content' => "{{NoFormatWithParamOrder|f1=foo|f2=foo}}", |
| 71 |
'edited' => "{{NoFormatWithParamOrder|f1=BAR|f2=foo}}" |
| 72 |
] |
| 73 |
], |
| 74 |
|
| 75 |
// 3. flipped f1 & f2 in data-parsoid + newly added f0 |
| 76 |
[ |
| 77 |
'name' => 'Preserve original param order + smart insertion of new params', |
| 78 |
'html' => '<span about="#mwt1" typeof="mw:Transclusion" data-parsoid=' . "'" . |
| 79 |
'{"pi":[[{"k":"f2"},{"k":"f1"}]]}' . "' data-mw='" . |
| 80 |
'{"parts":[{"template":{"target":{"wt":"NoFormatWithParamOrder",' . |
| 81 |
'"href":"./Template:NoFormatWithParamOrder"},"params":{"f1":{"wt":"foo"},' . |
| 82 |
'"f2":{"wt":"foo"},"f0":{"wt":"BOO"}},"i":0}}]}' . "'" . '>foo</span>', |
| 83 |
'wt' => [ |
| 84 |
'no_selser' => "{{NoFormatWithParamOrder|f2=foo|f1=foo|f0=BOO}}", |
| 85 |
'new_content' => "{{NoFormatWithParamOrder|f0=BOO|f1=foo|f2=foo}}", |
| 86 |
// Preserve partial templatedata order |
| 87 |
'edited' => "{{NoFormatWithParamOrder|f2=foo|f0=BOO|f1=BAR}}" |
| 88 |
] |
| 89 |
], |
| 90 |
|
| 91 |
// 4. inline-tpl (but written in block format originally); no param order |
| 92 |
[ |
| 93 |
'name' => 'Enforce inline format', |
| 94 |
'html' => '<span about="#mwt1" typeof="mw:Transclusion" data-parsoid=' . "'" . |
| 95 |
'{"pi":[[{"k":"f1","spc":[""," "," ","\\n"]},{"k":"f2","spc":[""," "," ","\\n"]}]]}' . |
| 96 |
"' data-mw='" . '{"parts":[{"template":{"target":{"wt":"InlineTplNoParamOrder\\n",' . |
| 97 |
'"href":"./Template:InlineTplNoParamOrder"},"params":{"f1":{"wt":"foo"},' . |
| 98 |
'"f2":{"wt":"foo"}},"i":0}}]}' . "'" . '>foo</span>', |
| 99 |
'wt' => [ |
| 100 |
'no_selser' => "{{InlineTplNoParamOrder\n|f1 = foo\n|f2 = foo\n}}", |
| 101 |
'new_content' => "{{InlineTplNoParamOrder|f1=foo|f2=foo}}", |
| 102 |
'edited' => "{{InlineTplNoParamOrder|f1=BAR|f2=foo}}" |
| 103 |
] |
| 104 |
], |
| 105 |
|
| 106 |
// 5. block-tpl (but written in inline format originally); no param order |
| 107 |
[ |
| 108 |
'name' => 'Enforce block format', |
| 109 |
'html' => '<span about="#mwt1" typeof="mw:Transclusion" data-parsoid=' . "'" . |
| 110 |
'{"pi":[[{"k":"f1"},{"k":"f2"}]]}' . "' data-mw='" . |
| 111 |
'{"parts":[{"template":{"target":{"wt":"BlockTplNoParamOrder",' . |
| 112 |
'"href":"./Template:BlockTplNoParamOrder"},"params":{"f1":{"wt":"foo"},' . |
| 113 |
'"f2":{"wt":"foo"}},"i":0}}]}' . "'" . '>foo</span>', |
| 114 |
'wt' => [ |
| 115 |
'no_selser' => "{{BlockTplNoParamOrder|f1=foo|f2=foo}}", |
| 116 |
'new_content' => "{{BlockTplNoParamOrder\n| f1 = foo\n| f2 = foo\n}}", |
| 117 |
'edited' => "{{BlockTplNoParamOrder\n| f1 = BAR\n| f2 = foo\n}}" |
| 118 |
] |
| 119 |
], |
| 120 |
|
| 121 |
// 6. block-tpl (with non-standard spaces before pipe); no param order |
| 122 |
[ |
| 123 |
'name' => 'Enforce block format (while preserving non-standard space before pipes)', |
| 124 |
'html' => '<span about="#mwt1" typeof="mw:Transclusion" data-parsoid=' . "'" . |
| 125 |
'{"pi":[[{"k":"f1","spc":[" ", " ", " ", "\\n <!--ha--> "]},' . |
| 126 |
'{"k":"f2","spc":[" ", " ", " ", ""]}]]}' . "' data-mw='" . |
| 127 |
'{"parts":[{"template":{"target":{"wt":"BlockTplNoParamOrder\\n ",' . |
| 128 |
'"href":"./Template:BlockTplNoParamOrder"},"params":{"f1":{"wt":"foo"},' . |
| 129 |
'"f2":{"wt":"foo"}},"i":0}}]}' . "'" . '>foo</span>', |
| 130 |
'wt' => [ |
| 131 |
'no_selser' => "{{BlockTplNoParamOrder\n | f1 = foo\n <!--ha--> | f2 = foo}}", |
| 132 |
'new_content' => "{{BlockTplNoParamOrder\n| f1 = foo\n| f2 = foo\n}}", |
| 133 |
'edited' => "{{BlockTplNoParamOrder\n| f1 = BAR\n <!--ha--> | f2 = foo\n}}" |
| 134 |
] |
| 135 |
], |
| 136 |
|
| 137 |
// 7. inline-tpl (but written in block format originally); with param order |
| 138 |
[ |
| 139 |
'name' => 'Enforce inline format + param order', |
| 140 |
'html' => '<span about="#mwt1" typeof="mw:Transclusion" data-parsoid=' . "'" . |
| 141 |
'{"pi":[[{"k":"f2","spc":[""," "," ","\\n"]},{"k":"f1","spc":[""," "," ","\\n"]}]]}' . |
| 142 |
"' data-mw='" . '{"parts":[{"template":{"target":{"wt":"InlineTplWithParamOrder\\n",' . |
| 143 |
'"href":"./Template:InlineTplWithParamOrder"},"params":{"f1":{"wt":"foo"},' . |
| 144 |
'"f2":{"wt":"foo"}},"i":0}}]}' . "'" . '>foo</span>', |
| 145 |
'wt' => [ |
| 146 |
'no_selser' => "{{InlineTplWithParamOrder\n|f2 = foo\n|f1 = foo\n}}", |
| 147 |
'new_content' => "{{InlineTplWithParamOrder|f1=foo|f2=foo}}", |
| 148 |
'edited' => "{{InlineTplWithParamOrder|f2=foo|f1=BAR}}" |
| 149 |
] |
| 150 |
], |
| 151 |
|
| 152 |
// 8. block-tpl (but written in inline format originally); with param order |
| 153 |
[ |
| 154 |
'name' => 'Enforce block format + param order', |
| 155 |
'html' => '<span about="#mwt1" typeof="mw:Transclusion" data-parsoid=' . "'" . |
| 156 |
'{"pi":[[{"k":"f2"},{"k":"f1"}]]}' . "'" . ' data-mw=' . "'" . |
| 157 |
'{"parts":[{"template":{"target":{"wt":"BlockTplWithParamOrder",' . |
| 158 |
'"href":"./Template:BlockTplWithParamOrder"},"params":{"f1":{"wt":"foo"},' . |
| 159 |
'"f2":{"wt":"foo"}},"i":0}}]}' . "'" . '>foo</span>', |
| 160 |
'wt' => [ |
| 161 |
'no_selser' => "{{BlockTplWithParamOrder|f2=foo|f1=foo}}", |
| 162 |
'new_content' => "{{BlockTplWithParamOrder\n| f1 = foo\n| f2 = foo\n}}", |
| 163 |
'edited' => "{{BlockTplWithParamOrder\n| f2 = foo\n| f1 = BAR\n}}" |
| 164 |
] |
| 165 |
], |
| 166 |
|
| 167 |
// 9. Multiple transclusions |
| 168 |
[ |
| 169 |
'name' => 'Multiple transclusions', |
| 170 |
'html' => '<span about="#mwt1" typeof="mw:Transclusion" data-parsoid=' . "'" . |
| 171 |
'{"pi":[[{"k":"f2","spc":[""," "," ","\\n"]},{"k":"f1","spc":[""," "," ","\\n"]}]]}' . |
| 172 |
"' data-mw='" . '{"parts":[{"template":{"target":{"wt":"TplWithoutTemplateData\\n",' . |
| 173 |
'"href":"./Template:TplWithoutTemplateData"},"params":{"f1":{"wt":"foo"},' . |
| 174 |
'"f2":{"wt":"foo"}},"i":0}}]}' . "'" . '>foo</span>' . |
| 175 |
' <span about="#mwt2" typeof="mw:Transclusion" data-parsoid=' . "'" . |
| 176 |
'{"pi":[[{"k":"f2"},{"k":"f1"}]]}' . "'" . ' data-mw=' . "'" . |
| 177 |
'{"parts":[{"template":{"target":{"wt":"BlockTplWithParamOrder",' . |
| 178 |
'"href":"./Template:BlockTplWithParamOrder"},"params":{"f1":{"wt":"foo"},' . |
| 179 |
'"f2":{"wt":"foo"}},"i":0}}]}' . "'" . '>foo</span>', |
| 180 |
'wt' => [ |
| 181 |
'no_selser' => "{{TplWithoutTemplateData\n|f2 = foo\n|f1 = foo\n}} " . |
| 182 |
"{{BlockTplWithParamOrder|f2=foo|f1=foo}}", |
| 183 |
'new_content' => "{{TplWithoutTemplateData|f1=foo|f2=foo}} " . |
| 184 |
"{{BlockTplWithParamOrder\n| f1 = foo\n| f2 = foo\n}}", |
| 185 |
'edited' => "{{TplWithoutTemplateData\n|f2 = foo\n|f1 = BAR\n}} " . |
| 186 |
"{{BlockTplWithParamOrder|f2=foo|f1=foo}}" |
| 187 |
] |
| 188 |
], |
| 189 |
|
| 190 |
// 10. data-mw with multiple transclusions |
| 191 |
[ |
| 192 |
'name' => 'Multiple transclusions', |
| 193 |
'html' => '<span about="#mwt1" typeof="mw:Transclusion" data-parsoid=' . "'" . |
| 194 |
'{"pi":[[{"k":"f2"},{"k":"f1"}], [{"k":"f2","spc":[""," "," ","\\n"]},' . |
| 195 |
'{"k":"f1","spc":[""," "," ","\\n"]}]]}' . "' data-mw='" . |
| 196 |
'{"parts":[{"template":{"target":{"wt":"BlockTplWithParamOrder",' . |
| 197 |
'"href":"./Template:BlockTplWithParamOrder"},"params":{"f1":{"wt":"foo"},' . |
| 198 |
'"f2":{"wt":"foo"}},"i":0}},"SOME TEXT",{"template":{"target":' . |
| 199 |
'{"wt":"InlineTplNoParamOrder\\n","href":"./Template:InlineTplNoParamOrder"},' . |
| 200 |
'"params":{"f1":{"wt":"foo"},"f2":{"wt":"foo"}},"i":1}}]}' . "'" . '>foo</span>', |
| 201 |
'wt' => [ |
| 202 |
'no_selser' => "{{BlockTplWithParamOrder|f2=foo|f1=foo}}SOME TEXT" . |
| 203 |
"{{InlineTplNoParamOrder\n|f2 = foo\n|f1 = foo\n}}", |
| 204 |
'new_content' => "{{BlockTplWithParamOrder\n| f1 = foo\n| f2 = foo\n}}SOME TEXT" . |
| 205 |
"{{InlineTplNoParamOrder|f1=foo|f2=foo}}", |
| 206 |
'edited' => "{{BlockTplWithParamOrder\n| f2 = foo\n| f1 = BAR\n}}SOME TEXT" . |
| 207 |
"{{InlineTplNoParamOrder|f2=foo|f1=foo}}" |
| 208 |
] |
| 209 |
], |
| 210 |
|
| 211 |
// 11. Alias sort order |
| 212 |
[ |
| 213 |
'name' => 'Enforce param order with aliases', |
| 214 |
'html' => '<span about="#mwt1" typeof="mw:Transclusion"' . " data-mw='" . |
| 215 |
'{"parts":[{"template":{"target":{"wt":"WithParamOrderAndAliases\\n",' . |
| 216 |
'"href":"./Template:WithParamOrderAndAliases"},"params":{"f2":{"wt":"foo"},' . |
| 217 |
'"f3":{"wt":"foo"}},"i":1}}]}' . "'" . '>foo</span>', |
| 218 |
'wt' => [ |
| 219 |
'no_selser' => "{{WithParamOrderAndAliases|f3=foo|f2=foo}}", |
| 220 |
'new_content' => "{{WithParamOrderAndAliases|f3=foo|f2=foo}}", |
| 221 |
'edited' => "{{WithParamOrderAndAliases|f3=foo|f2=BAR}}" |
| 222 |
] |
| 223 |
], |
| 224 |
|
| 225 |
// 12. Alias sort order, with both original and alias params |
| 226 |
// Even aliased parameters should appear in the original order by default. |
| 227 |
[ |
| 228 |
'name' => 'Enforce param order with aliases (aliases in original order)', |
| 229 |
'html' => '<span about="#mwt1" typeof="mw:Transclusion" data-parsoid=' . "'" . |
| 230 |
'{"pi":[[{"k":"f4"},{"k":"f3"},{"k":"f1"}]]}' . "' data-mw='" . |
| 231 |
'{"parts":[{"template":{"target":{"wt":"WithParamOrderAndAliases",' . |
| 232 |
'"href":"./Template:WithParamOrderAndAliases"},"params":{"f4":{"wt":"foo"},' . |
| 233 |
'"f3":{"wt":"foo"},"f1":{"wt":"foo"}},"i":0}}]}' . "'" . '>foo</span>', |
| 234 |
'wt' => [ |
| 235 |
'no_selser' => "{{WithParamOrderAndAliases|f4=foo|f3=foo|f1=foo}}", |
| 236 |
'new_content' => "{{WithParamOrderAndAliases|f1=foo|f4=foo|f3=foo}}", |
| 237 |
'edited' => "{{WithParamOrderAndAliases|f4=BAR|f3=foo|f1=foo}}" |
| 238 |
] |
| 239 |
], |
| 240 |
|
| 241 |
// 13. Inline Formatted template 1 |
| 242 |
[ |
| 243 |
'name' => 'Inline Formatted template 1', |
| 244 |
'html' => 'x <span about="#mwt1" typeof="mw:Transclusion" data-parsoid=' . "'" . |
| 245 |
'{"pi":[[{"k":"f1"},{"k":"x"}]]}' . "' data-mw='" . |
| 246 |
'{"parts":[{"template":{"target":{"wt":"InlineFormattedTpl_1",' . |
| 247 |
'"href":"./Template:InlineFormattedTpl_1"},"params":{"f1":{"wt":""},' . |
| 248 |
'"x":{"wt":"foo"}},"i":0}}]}' . "'" . '>something</span> y', |
| 249 |
'wt' => [ |
| 250 |
'no_selser' => "x {{InlineFormattedTpl_1|f1=|x=foo}} y", |
| 251 |
'new_content' => "x {{InlineFormattedTpl_1|f1=|x=foo}} y", |
| 252 |
'edited' => "x {{InlineFormattedTpl_1|f1=|x=BAR}} y" |
| 253 |
] |
| 254 |
], |
| 255 |
|
| 256 |
// 14. Inline Formatted template 2 |
| 257 |
[ |
| 258 |
'name' => 'Inline Formatted template 2', |
| 259 |
'html' => 'x <span about="#mwt1" typeof="mw:Transclusion" data-parsoid=' . "'" . |
| 260 |
'{"pi":[[{"k":"f1"},{"k":"x"}]]}' . "' data-mw='" . |
| 261 |
'{"parts":[{"template":{"target":{"wt":"InlineFormattedTpl_2",' . |
| 262 |
'"href":"./Template:InlineFormattedTpl_2"},"params":{"f1":{"wt":""},' . |
| 263 |
'"x":{"wt":"foo"}},"i":0}}]}' . "'" . '>something</span> y', |
| 264 |
'wt' => [ |
| 265 |
'no_selser' => "x {{InlineFormattedTpl_2|f1=|x=foo}} y", |
| 266 |
'new_content' => "x \n{{InlineFormattedTpl_2 | f1 = | x = foo}} y", |
| 267 |
'edited' => "x \n{{InlineFormattedTpl_2 | f1 = | x = BAR}} y" |
| 268 |
] |
| 269 |
], |
| 270 |
|
| 271 |
// 15.1 Inline Formatted template 3 |
| 272 |
[ |
| 273 |
'name' => 'Inline Formatted template 3', |
| 274 |
'html' => 'x <span about="#mwt1" typeof="mw:Transclusion" data-parsoid=' . "'" . |
| 275 |
'{"pi":[[{"k":"f1"},{"k":"x"}]]}' . "' data-mw='" . |
| 276 |
'{"parts":[{"template":{"target":{"wt":"InlineFormattedTpl_3",' . |
| 277 |
'"href":"./Template:InlineFormattedTpl_3"},"params":{"f1":{"wt":""},' . |
| 278 |
'"x":{"wt":"foo"}},"i":0}}]}' . "'" . '>something</span> y', |
| 279 |
'wt' => [ |
| 280 |
'no_selser' => "x {{InlineFormattedTpl_3|f1=|x=foo}} y", |
| 281 |
'new_content' => "x {{InlineFormattedTpl_3| f1 = | x = foo}} y", |
| 282 |
'edited' => "x {{InlineFormattedTpl_3| f1 = | x = BAR}} y" |
| 283 |
] |
| 284 |
], |
| 285 |
|
| 286 |
// 15.2 Inline Formatted template 3 with multibyte unicode chars (T245627) |
| 287 |
[ |
| 288 |
'name' => 'Inline Formatted template 3 (multibyte chars)', |
| 289 |
'html' => 'x <span about="#mwt1" typeof="mw:Transclusion" data-parsoid=' . "'" . |
| 290 |
'{"pi":[[{"k":"f1"},{"k":"é"}]]}' . "' data-mw='" . |
| 291 |
'{"parts":[{"template":{"target":{"wt":"InlineFormattedTpl_3",' . |
| 292 |
'"href":"./Template:InlineFormattedTpl_3"},"params":{"f1":{"wt":""},' . |
| 293 |
'"é":{"wt":"foo"}},"i":0}}]}' . "'" . '>something</span> y', |
| 294 |
'wt' => [ |
| 295 |
'no_selser' => "x {{InlineFormattedTpl_3|f1=|é=foo}} y", |
| 296 |
'new_content' => "x {{InlineFormattedTpl_3| f1 = | é = foo}} y", |
| 297 |
'edited' => "x {{InlineFormattedTpl_3| f1 = | é = BAR}} y" |
| 298 |
] |
| 299 |
], |
| 300 |
|
| 301 |
// 16. Custom block formatting 1 |
| 302 |
[ |
| 303 |
'name' => 'Custom block formatting 1', |
| 304 |
'html' => 'x<span about="#mwt1" typeof="mw:Transclusion" data-parsoid=' . "'" . |
| 305 |
'{"pi":[[{"k":"f1"},{"k":"f2"}]]}' . "' data-mw='" . |
| 306 |
'{"parts":[{"template":{"target":{"wt":"BlockFormattedTpl_1",' . |
| 307 |
'"href":"./Template:BlockFormattedTpl_1"},"params":{"f1":{"wt":""},' . |
| 308 |
'"f2":{"wt":"foo"}},"i":0}}]}' . "'" . '>something</span>y', |
| 309 |
'wt' => [ |
| 310 |
'no_selser' => "x{{BlockFormattedTpl_1|f1=|f2=foo}}y", // dp spacing info is preserved |
| 311 |
'new_content' => "x{{BlockFormattedTpl_1\n| f1 = \n| f2 = foo\n}}y", // normalized |
| 312 |
'edited' => "x{{BlockFormattedTpl_1\n| f1 = \n| f2 = BAR\n}}y" // normalized |
| 313 |
] |
| 314 |
], |
| 315 |
|
| 316 |
// 17. Custom block formatting 2 |
| 317 |
[ |
| 318 |
'name' => 'Custom block formatting 2', |
| 319 |
'html' => 'x<span about="#mwt1" typeof="mw:Transclusion" data-parsoid=' . "'" . |
| 320 |
'{"pi":[[{"k":"f1"},{"k":"f2"}]]}' . "' data-mw='" . |
| 321 |
'{"parts":[{"template":{"target":{"wt":"BlockFormattedTpl_2",' . |
| 322 |
'"href":"./Template:BlockFormattedTpl_2"},"params":{"f1":{"wt":""},' . |
| 323 |
'"f2":{"wt":"foo"}},"i":0}}]}' . "'" . '>something</span>y', |
| 324 |
'wt' => [ |
| 325 |
'no_selser' => "x{{BlockFormattedTpl_2|f1=|f2=foo}}y", // dp spacing info is preserved |
| 326 |
'new_content' => "x\n{{BlockFormattedTpl_2\n| f1 = \n| f2 = foo\n}}\ny", // normalized |
| 327 |
'edited' => "x\n{{BlockFormattedTpl_2\n| f1 = \n| f2 = BAR\n}}\ny" // normalized |
| 328 |
] |
| 329 |
], |
| 330 |
|
| 331 |
// 18. Custom block formatting 3 - T199849 |
| 332 |
[ |
| 333 |
'name' => 'Custom block formatting 3 - T199849', |
| 334 |
'html' => "x\n" . '<span about="#mwt1" typeof="mw:Transclusion" data-mw=' . "'" . |
| 335 |
'{"parts":[{"template":{"target":{"wt":"BlockFormattedTpl_2",' . |
| 336 |
'"href":"./Template:BlockFormattedTpl_2"},"params":{"f1":{"wt":""},' . |
| 337 |
'"f2":{"wt":"foo"}},"i":0}}]}' . "'" . '>something</span>' . "\ny", |
| 338 |
'wt' => [ |
| 339 |
'new_content' => "x\n{{BlockFormattedTpl_2\n| f1 = \n| f2 = foo\n}}\ny", // normalized |
| 340 |
'edited' => "x\n{{BlockFormattedTpl_2\n| f1 = \n| f2 = BAR\n}}\ny" // normalized |
| 341 |
] |
| 342 |
], |
| 343 |
|
| 344 |
// 19. Custom block formatting 4 - T199849 |
| 345 |
[ |
| 346 |
'name' => 'Custom block formatting 4 - T199849', |
| 347 |
'html' => "x\n" . '<span about="#mwt1" typeof="mw:Transclusion" data-mw=' . "'" . |
| 348 |
'{"parts":["X", {"template":{"target":{"wt":"BlockFormattedTpl_2",' . |
| 349 |
'"href":"./Template:BlockFormattedTpl_2"},"params":{"f1":{"wt":""},' . |
| 350 |
'"f2":{"wt":"foo"}},"i":0}}, "Y"]}' . "'" . '>something</span>' . "\ny", |
| 351 |
'wt' => [ |
| 352 |
'new_content' => "x\nX\n{{BlockFormattedTpl_2\n| f1 = \n| f2 = foo\n}}\nY\ny", // normalized |
| 353 |
'edited' => "x\nX\n{{BlockFormattedTpl_2\n| f1 = \n| f2 = BAR\n}}\nY\ny"// normalized |
| 354 |
] |
| 355 |
], |
| 356 |
|
| 357 |
// 20. Custom block formatting 5 - T199849 |
| 358 |
[ |
| 359 |
'name' => 'Custom block formatting 5 - T199849', |
| 360 |
'html' => "x\n" . '<span about="#mwt1" typeof="mw:Transclusion" data-mw=' . "'" . |
| 361 |
'{"parts":[{"template":{"target":{"wt":"BlockFormattedTpl_2",' . |
| 362 |
'"href":"./Template:BlockFormattedTpl_2"},"params":{"g1":{"wt":""},' . |
| 363 |
'"g2":{"wt":""}},"i":0}}, {"template":{"target":{"wt":"BlockFormattedTpl_2",' . |
| 364 |
'"href":"./Template:BlockFormattedTpl_2"},"params":{"f1":{"wt":""},' . |
| 365 |
'"f2":{"wt":"foo"}},"i":1}}]}' . "'" . '>something</span><!--cmt-->' . "\ny", |
| 366 |
'wt' => [ |
| 367 |
'new_content' => "x\n{{BlockFormattedTpl_2\n| g1 = \n| g2 = \n}}\n{{BlockFormattedTpl_2\n| " . |
| 368 |
"f1 = \n| f2 = foo\n}}<!--cmt-->\ny", // normalized |
| 369 |
'edited' => "x\n{{BlockFormattedTpl_2\n| g1 = \n| g2 = \n}}\n{{BlockFormattedTpl_2\n| " . |
| 370 |
"f1 = \n| f2 = BAR\n}}<!--cmt-->\ny" // normalized |
| 371 |
] |
| 372 |
], |
| 373 |
|
| 374 |
// 21. Custom block formatting 6 |
| 375 |
[ |
| 376 |
'name' => 'Custom block formatting 6', |
| 377 |
'html' => 'x<span about="#mwt1" typeof="mw:Transclusion" data-parsoid=' . "'" . |
| 378 |
'{"pi":[[{"k":"f1"},{"k":"f2"}]]}' . "' data-mw='" . |
| 379 |
'{"parts":[{"template":{"target":{"wt":"BlockFormattedTpl_3",' . |
| 380 |
'"href":"./Template:BlockFormattedTpl_3"},"params":{"f1":{"wt":""},' . |
| 381 |
'"f2":{"wt":"foo"}},"i":0}}]}' . "'" . '>something</span>y', |
| 382 |
'wt' => [ |
| 383 |
'no_selser' => "x{{BlockFormattedTpl_3|f1=|f2=foo}}y", // dp spacing info is preserved |
| 384 |
'new_content' => "x{{BlockFormattedTpl_3|\n f1 = |\n f2 = foo}}y", // normalized |
| 385 |
'edited' => "x{{BlockFormattedTpl_3|\n f1 = |\n f2 = BAR}}y" // normalized |
| 386 |
] |
| 387 |
], |
| 388 |
|
| 389 |
// 22. Block formatting without params - T282031 |
| 390 |
[ |
| 391 |
'name' => 'Block formatting without params', |
| 392 |
'html' => 'x<span about="#mwt1" typeof="mw:Transclusion" data-parsoid=' . "'" . |
| 393 |
'{"pi":[[]]}' . "' data-mw='" . |
| 394 |
'{"parts":[{"template":{"target":{"wt":"BlockTplNoParamOrder",' . |
| 395 |
'"href":"./Template:BlockTplNoParamOrder"},"params":{' . |
| 396 |
'},"i":0}}]}' . "'" . '>something</span>y', |
| 397 |
'wt' => [ |
| 398 |
'no_selser' => "x{{BlockTplNoParamOrder}}y", |
| 399 |
'new_content' => "x{{BlockTplNoParamOrder}}y", // normalized |
| 400 |
'edited' => "x{{BlockTplNoParamOrder}}y" // normalized |
| 401 |
] |
| 402 |
], |
| 403 |
|
| 404 |
// 23. Sort arguments as in data-mw - T304730 |
| 405 |
[ |
| 406 |
'name' => 'Sort arguments as in data-mw', |
| 407 |
'html' => '<span about="#mwt1" typeof="mw:Transclusion" data-mw=\'' . |
| 408 |
'{"parts":[{"template":{"target":{"wt":"Meh", "href":"./Template:Meh"},"params":{' . |
| 409 |
'"f1":{"wt":"foo"},' . |
| 410 |
'"f2":{"wt":"foo"},' . |
| 411 |
'"f3":{"wt":"foo"},' . |
| 412 |
'"f4":{"wt":"foo"},' . |
| 413 |
'"f5":{"wt":"foo"},' . |
| 414 |
'"f6":{"wt":"foo"},' . |
| 415 |
'"f7":{"wt":"foo"},' . |
| 416 |
'"f8":{"wt":"foo"},' . |
| 417 |
'"f9":{"wt":"foo"},' . |
| 418 |
'"f10":{"wt":"foo"},' . |
| 419 |
'"f11":{"wt":"foo"},' . |
| 420 |
'"f12":{"wt":"foo"},' . |
| 421 |
'"f13":{"wt":"foo"},' . |
| 422 |
'"f14":{"wt":"foo"},' . |
| 423 |
'"f15":{"wt":"foo"},' . |
| 424 |
'"f16":{"wt":"foo"},' . |
| 425 |
'"f17":{"wt":"foo"}' . |
| 426 |
'},"i":0}}]}' . "'" . '>foo</span>', |
| 427 |
'wt' => [ |
| 428 |
'no_selser' => "{{Meh|f1=foo|f2=foo|f3=foo|f4=foo|f5=foo|f6=foo|f7=foo|f8=foo|" . |
| 429 |
"f9=foo|f10=foo|f11=foo|f12=foo|f13=foo|f14=foo|f15=foo|f16=foo|f17=foo}}", |
| 430 |
'new_content' => "{{Meh|f1=foo|f2=foo|f3=foo|f4=foo|f5=foo|f6=foo|f7=foo|f8=foo|" . |
| 431 |
"f9=foo|f10=foo|f11=foo|f12=foo|f13=foo|f14=foo|f15=foo|f16=foo|f17=foo}}", |
| 432 |
'edited' => "{{Meh|f1=BAR|f2=foo|f3=foo|f4=foo|f5=foo|f6=foo|f7=foo|f8=foo|" . |
| 433 |
"f9=foo|f10=foo|f11=foo|f12=foo|f13=foo|f14=foo|f15=foo|f16=foo|f17=foo}}" |
| 434 |
] |
| 435 |
], |
| 436 |
|
| 437 |
]; |
| 438 |
} |
| 439 |
|
| 440 |
/** |
| 441 |
* @covers \Wikimedia\Parsoid\Parsoid::html2wikitext |
| 442 |
* @covers \Wikimedia\Parsoid\Wikitext\ContentModelHandler::fromDOM |
| 443 |
* @dataProvider defineTestData |
| 444 |
*/ |
| 445 |
public function testTemplateData( |
| 446 |
string $name, string $html, array $wt |
| 447 |
): void { |
| 448 |
// Non-selser test |
| 449 |
if ( isset( $wt['no_selser'] ) ) { |
| 450 |
$desc = "$name: Default non-selser serialization should ignore templatedata"; |
| 451 |
$this->verifyTransformation( $html, null, null, $wt['no_selser'], $desc ); |
| 452 |
} |
| 453 |
|
| 454 |
// New content test |
| 455 |
$desc = "$name: Serialization of new content (no data-parsoid) should respect templatedata"; |
| 456 |
// Remove data-parsoid making it look like new content |
| 457 |
$newHTML = preg_replace( '/data-parsoid.*? data-mw/', ' data-mw', $html ); |
| 458 |
$this->verifyTransformation( $newHTML, '', '', $wt['new_content'], $desc ); |
| 459 |
|
| 460 |
// Transclusion edit test |
| 461 |
$desc = "$name: Serialization of edited content should respect templatedata"; |
| 462 |
// Replace only the first instance of 'foo' with 'BAR' |
| 463 |
// to simulate an edit of a transclusion. |
| 464 |
$newHTML = preg_replace( '/foo/', 'BAR', $html, 1 ); |
| 465 |
$this->verifyTransformation( |
| 466 |
$newHTML, $html, $wt['no_selser'] ?? '', $wt['edited'], $desc |
| 467 |
); |
| 468 |
} |
| 469 |
|
| 470 |
public function defineVersionTestData(): array { |
| 471 |
return [ |
| 472 |
[ |
| 473 |
'contentVersion' => self::$defaultContentVersion, |
| 474 |
'html' => '<span about="#mwt1" typeof="mw:Transclusion" data-parsoid=' . "'" . |
| 475 |
'{"pi":[[{"k":"f1"},{"k":"f1"}]]}' . "' data-mw='" . |
| 476 |
'{"parts":[{"template":{"target":{"wt":"TplWithoutTemplateData",' . |
| 477 |
'"href":"./Template:TplWithoutTemplateData"},"params":{"f1":{"wt":"foo"},' . |
| 478 |
'"f2":{"wt":"foo"}},"i":0}}]}' . "'" . '>foo</span>', |
| 479 |
'wt' => [ |
| 480 |
'orig' => "{{TplWithoutTemplateData|f1=foo|f2=foo}}", |
| 481 |
'edited' => "{{TplWithoutTemplateData|f1=BAR|f2=foo}}" |
| 482 |
] |
| 483 |
] |
| 484 |
]; |
| 485 |
} |
| 486 |
|
| 487 |
/** |
| 488 |
* @covers \Wikimedia\Parsoid\Parsoid::html2wikitext |
| 489 |
* @covers \Wikimedia\Parsoid\Wikitext\ContentModelHandler::fromDOM |
| 490 |
* @dataProvider defineVersionTestData |
| 491 |
*/ |
| 492 |
public function testTemplateDataVersion( |
| 493 |
string $contentVersion, string $html, array $wt |
| 494 |
): void { |
| 495 |
$desc = "Serialization should use correct arg space defaults for " . |
| 496 |
"data-parsoid version $contentVersion"; |
| 497 |
// Replace only the first instance of 'foo' with 'BAR' |
| 498 |
// to simulate an edit of a transclusion. |
| 499 |
$newHTML = preg_replace( '/foo/', 'BAR', $html, 1 ); |
| 500 |
$this->verifyTransformation( |
| 501 |
$newHTML, $html, $wt['orig'], $wt['edited'], $desc, $contentVersion |
| 502 |
); |
| 503 |
} |
| 504 |
|
| 505 |
} |