| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Content\ContentHandler; |
| 4 |
use MediaWiki\Content\TextContent; |
| 5 |
use MediaWiki\Context\RequestContext; |
| 6 |
use MediaWiki\Diff\TextDiffer\ManifoldTextDiffer; |
| 7 |
use MediaWiki\Diff\TextDiffer\Wikidiff2TextDiffer; |
| 8 |
use MediaWiki\Tests\Diff\TextDiffer\TextDifferData; |
| 9 |
use Wikimedia\Assert\ParameterTypeException; |
| 10 |
use Wikimedia\Stats\StatsFactory; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class TextSlotDiffRendererTest extends MediaWikiIntegrationTestCase { |
| 16 |
|
| 17 |
public function setUp(): void { |
| 18 |
Wikidiff2TextDiffer::$fakeVersionForTesting = '1.14.1'; |
| 19 |
} |
| 20 |
|
| 21 |
public function tearDown(): void { |
| 22 |
Wikidiff2TextDiffer::$fakeVersionForTesting = null; |
| 23 |
} |
| 24 |
|
| 25 |
public function testGetExtraCacheKeys() { |
| 26 |
$slotDiffRenderer = $this->getTextSlotDiffRenderer(); |
| 27 |
$key = $slotDiffRenderer->getExtraCacheKeys(); |
| 28 |
$slotDiffRenderer->setEngine( TextSlotDiffRenderer::ENGINE_WIKIDIFF2_INLINE ); |
| 29 |
$inlineKey = $slotDiffRenderer->getExtraCacheKeys(); |
| 30 |
|
| 31 |
ksort( $key ); |
| 32 |
ksort( $inlineKey ); |
| 33 |
|
| 34 |
$this->assertSame( [ '10-formats-and-engines' => 'php=table' ], $key ); |
| 35 |
$this->assertSame( |
| 36 |
[ |
| 37 |
'10-formats-and-engines' => 'wikidiff2=inline', |
| 38 |
'20-wikidiff2-version' => '1.14.1', |
| 39 |
'21-wikidiff2-options' => 'bc2a06be', |
| 40 |
], |
| 41 |
$inlineKey |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public function testGetDiff( |
| 52 |
?array $oldContentArgs, ?array $newContentArgs, $expectedResult |
| 53 |
) { |
| 54 |
$this->mergeMwGlobalArrayValue( 'wgContentHandlers', [ |
| 55 |
'testing' => DummyContentHandlerForTesting::class, |
| 56 |
'testing-nontext' => DummyNonTextContentHandler::class, |
| 57 |
] ); |
| 58 |
|
| 59 |
$oldContent = $oldContentArgs ? self::makeContent( ...$oldContentArgs ) : null; |
| 60 |
$newContent = $newContentArgs ? self::makeContent( ...$newContentArgs ) : null; |
| 61 |
|
| 62 |
if ( $expectedResult instanceof Exception ) { |
| 63 |
$this->expectException( get_class( $expectedResult ) ); |
| 64 |
$this->expectExceptionMessage( $expectedResult->getMessage() ); |
| 65 |
} |
| 66 |
|
| 67 |
$slotDiffRenderer = $this->getTextSlotDiffRenderer(); |
| 68 |
$diff = $slotDiffRenderer->getDiff( $oldContent, $newContent ); |
| 69 |
if ( $expectedResult instanceof Exception ) { |
| 70 |
return; |
| 71 |
} |
| 72 |
$plainDiff = $this->getPlainDiff( $diff ); |
| 73 |
$this->assertSame( $expectedResult, $plainDiff ); |
| 74 |
} |
| 75 |
|
| 76 |
public static function provideGetDiff() { |
| 77 |
return [ |
| 78 |
'same text' => [ |
| 79 |
[ "aaa\nbbb\nccc" ], |
| 80 |
[ "aaa\nbbb\nccc" ], |
| 81 |
"", |
| 82 |
], |
| 83 |
'different text' => [ |
| 84 |
[ "aaa\nbbb\nccc" ], |
| 85 |
[ "aaa\nxxx\nccc" ], |
| 86 |
" aaa aaa\n-bbb+xxx\n ccc ccc", |
| 87 |
], |
| 88 |
'no right content' => [ |
| 89 |
[ "aaa\nbbb\nccc" ], |
| 90 |
null, |
| 91 |
"-aaa+ \n-bbb \n-ccc ", |
| 92 |
], |
| 93 |
'no left content' => [ |
| 94 |
null, |
| 95 |
[ "aaa\nbbb\nccc" ], |
| 96 |
"- +aaa\n +bbb\n +ccc", |
| 97 |
], |
| 98 |
'no content' => [ |
| 99 |
null, |
| 100 |
null, |
| 101 |
new InvalidArgumentException( '$oldContent and $newContent cannot both be null' ), |
| 102 |
], |
| 103 |
'non-text left content' => [ |
| 104 |
[ '', 'testing-nontext' ], |
| 105 |
[ "aaa\nbbb\nccc" ], |
| 106 |
new IncompatibleDiffTypesException( 'testing-nontext', 'text' ), |
| 107 |
], |
| 108 |
'non-text right content' => [ |
| 109 |
[ "aaa\nbbb\nccc" ], |
| 110 |
[ '', 'testing-nontext' ], |
| 111 |
new ParameterTypeException( '$newContent', 'MediaWiki\Content\TextContent|null' ), |
| 112 |
], |
| 113 |
]; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
private function getTextSlotDiffRenderer( $langCode = 'en' ) { |
| 123 |
$slotDiffRenderer = new TextSlotDiffRenderer(); |
| 124 |
$slotDiffRenderer->setStatsFactory( StatsFactory::newNull() ); |
| 125 |
$lang = $this->getServiceContainer()->getLanguageFactory()->getLanguage( $langCode ); |
| 126 |
$context = new RequestContext; |
| 127 |
$context->setLanguage( $lang ); |
| 128 |
$differ = new ManifoldTextDiffer( |
| 129 |
$context, |
| 130 |
$lang, |
| 131 |
'php', |
| 132 |
null, |
| 133 |
[] |
| 134 |
); |
| 135 |
$slotDiffRenderer->setTextDiffer( $differ ); |
| 136 |
$slotDiffRenderer->setFormat( 'table' ); |
| 137 |
return $slotDiffRenderer; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
private function getPlainDiff( $diff ) { |
| 146 |
$replacements = [ |
| 147 |
html_entity_decode( ' ' ) => ' ', |
| 148 |
html_entity_decode( '−' ) => '-', |
| 149 |
]; |
| 150 |
|
| 151 |
$diff = str_replace( '<td class="diff-marker"></td>', ' ', $diff ); |
| 152 |
$diff = preg_replace( '@<td colspan="2"( class="(?:diff-side-deleted|diff-side-added)")?></td>@', ' ', $diff ); |
| 153 |
$diff = preg_replace( '/data-marker="([^"]*)">/', '>$1', $diff ); |
| 154 |
return str_replace( array_keys( $replacements ), array_values( $replacements ), |
| 155 |
trim( strip_tags( $diff ), "\n" ) ); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
private static function makeContent( $str, $model = CONTENT_MODEL_TEXT ) { |
| 164 |
return ContentHandler::makeContent( $str, null, $model ); |
| 165 |
} |
| 166 |
|
| 167 |
public static function provideGetTablePrefix() { |
| 168 |
return [ |
| 169 |
'php' => [ |
| 170 |
TextSlotDiffRenderer::ENGINE_PHP, |
| 171 |
[ |
| 172 |
TextSlotDiffRenderer::INLINE_LEGEND_KEY => '<div></div>', |
| 173 |
TextSlotDiffRenderer::INLINE_SWITCHER_KEY => null |
| 174 |
] |
| 175 |
], |
| 176 |
'wikidiff2' => [ |
| 177 |
TextSlotDiffRenderer::ENGINE_WIKIDIFF2, |
| 178 |
[ |
| 179 |
TextSlotDiffRenderer::INLINE_LEGEND_KEY => |
| 180 |
'class="mw-diff-inline-legend oo-ui-element-hidden"', |
| 181 |
TextSlotDiffRenderer::INLINE_SWITCHER_KEY => 'mw-diffPage-inlineToggle-container' |
| 182 |
] |
| 183 |
], |
| 184 |
'inline' => [ |
| 185 |
TextSlotDiffRenderer::ENGINE_WIKIDIFF2_INLINE, |
| 186 |
[ |
| 187 |
TextSlotDiffRenderer::INLINE_LEGEND_KEY => |
| 188 |
'class="mw-diff-inline-legend".*\(diff-inline-tooltip-ins\)', |
| 189 |
TextSlotDiffRenderer::INLINE_SWITCHER_KEY => 'mw-diffPage-inlineToggle-container' |
| 190 |
] |
| 191 |
] |
| 192 |
]; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
public function testGetTablePrefix( $engine, $expectedPatterns ) { |
| 201 |
OOUI\Theme::setSingleton( new OOUI\BlankTheme() ); |
| 202 |
|
| 203 |
$slotDiffRenderer = $this->getTextSlotDiffRenderer( 'qqx' ); |
| 204 |
$slotDiffRenderer->setHookContainer( $this->createHookContainer() ); |
| 205 |
$slotDiffRenderer->setEngine( $engine ); |
| 206 |
$slotDiffRenderer->setInlineToggleEnabled(); |
| 207 |
|
| 208 |
$context = new RequestContext; |
| 209 |
$context->setLanguage( 'qqx' ); |
| 210 |
|
| 211 |
$title = $this->getServiceContainer()->getTitleFactory()->newFromText( 'Test' ); |
| 212 |
$result = $slotDiffRenderer->getTablePrefix( $context, $title ); |
| 213 |
$this->assertSameSize( $expectedPatterns, $result ); |
| 214 |
foreach ( $expectedPatterns as $key => $pattern ) { |
| 215 |
if ( $pattern === null ) { |
| 216 |
$this->assertNull( $result[$key], "\$result[$key]" ); |
| 217 |
} else { |
| 218 |
$this->assertMatchesRegularExpression( |
| 219 |
"#$pattern#", $result[$key], "\$result[$key]" ); |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
public function testLocalizeDiff() { |
| 225 |
$slotDiffRenderer = $this->getTextSlotDiffRenderer( 'en' ); |
| 226 |
$slotDiffRenderer->setHookContainer( $this->createHookContainer() ); |
| 227 |
$slotDiffRenderer->setEngine( 'php' ); |
| 228 |
$result = $slotDiffRenderer->localizeDiff( TextDifferData::PHP_TABLE ); |
| 229 |
$this->assertStringContainsString( 'Line 1:', $result ); |
| 230 |
} |
| 231 |
} |