| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Context\RequestContext; |
| 4 |
use MediaWiki\Diff\TextDiffer\TextDiffer; |
| 5 |
use MediaWiki\Diff\TextDiffer\Wikidiff2TextDiffer; |
| 6 |
use MediaWiki\Tests\Diff\TextDiffer\TextDifferData; |
| 7 |
use Wikimedia\TestingAccessWrapper; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class Wikidiff2TextDifferTest extends MediaWikiIntegrationTestCase { |
| 13 |
private function createDiffer() { |
| 14 |
$differ = new Wikidiff2TextDiffer( [] ); |
| 15 |
$localizer = RequestContext::getMain(); |
| 16 |
$localizer->setLanguage( 'qqx' ); |
| 17 |
$differ->setLocalizer( $localizer ); |
| 18 |
TestingAccessWrapper::newFromObject( $differ )->haveMoveSupport = true; |
| 19 |
return $differ; |
| 20 |
} |
| 21 |
|
| 22 |
public static function provideRenderBatch() { |
| 23 |
return [ |
| 24 |
[ false ], |
| 25 |
[ true ] |
| 26 |
]; |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public function testRenderBatch( $useMultiFormat ) { |
| 35 |
if ( !function_exists( 'wikidiff2_multi_format_diff' ) && $useMultiFormat ) { |
| 36 |
$this->markTestSkipped( 'Need wikidiff2 1.14.0+' ); |
| 37 |
} |
| 38 |
$oldText = 'foo'; |
| 39 |
$newText = 'bar'; |
| 40 |
$differ = new Wikidiff2TextDiffer( [ 'useMultiFormat' => $useMultiFormat ] ); |
| 41 |
|
| 42 |
$result = $differ->renderBatch( $oldText, $newText, [ 'table', 'inline' ] ); |
| 43 |
$this->assertSame( |
| 44 |
[ |
| 45 |
'table' => TextDifferData::WIKIDIFF2_TABLE, |
| 46 |
'inline' => TextDifferData::WIKIDIFF2_INLINE |
| 47 |
], |
| 48 |
$result |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
public function testGetName() { |
| 53 |
$differ = new Wikidiff2TextDiffer( [] ); |
| 54 |
$this->assertSame( 'wikidiff2', $differ->getName() ); |
| 55 |
} |
| 56 |
|
| 57 |
public function testGetFormatContext() { |
| 58 |
$differ = new Wikidiff2TextDiffer( [] ); |
| 59 |
$this->assertSame( TextDiffer::CONTEXT_ROW, $differ->getFormatContext( 'table' ) ); |
| 60 |
} |
| 61 |
|
| 62 |
public static function provideGetTablePrefixes() { |
| 63 |
return [ |
| 64 |
[ |
| 65 |
'table', |
| 66 |
'class="mw-diff-inline-legend oo-ui-element-hidden".*\(diff-inline-tooltip-ins\)' |
| 67 |
], |
| 68 |
[ |
| 69 |
'inline', |
| 70 |
'class="mw-diff-inline-legend".*\(diff-inline-tooltip-ins\)' |
| 71 |
], |
| 72 |
]; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public function testGetTablePrefixes( $format, $pattern ) { |
| 81 |
$differ = $this->createDiffer(); |
| 82 |
$result = $differ->getTablePrefixes( $format ); |
| 83 |
$this->assertMatchesRegularExpression( |
| 84 |
'{' . $pattern . '}s', |
| 85 |
$result[TextSlotDiffRenderer::INLINE_LEGEND_KEY] |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
public static function provideLocalize() { |
| 90 |
return [ |
| 91 |
'normal table' => [ |
| 92 |
'table', |
| 93 |
TextDifferData::WIKIDIFF2_TABLE, |
| 94 |
[], |
| 95 |
'<td colspan="2" class="diff-lineno">\(lineno: 1\)</td>' |
| 96 |
], |
| 97 |
'table with move tooltip' => [ |
| 98 |
'table', |
| 99 |
|
| 100 |
'<td class="diff-marker"><a class="mw-diff-movedpara-left" href="#movedpara_7_0_rhs">⚫</a></td>', |
| 101 |
[], |
| 102 |
'title="\(diff-paragraph-moved-tonew\)"' |
| 103 |
], |
| 104 |
'table with reduced line numbers' => [ |
| 105 |
'table', |
| 106 |
TextDifferData::WIKIDIFF2_TABLE, |
| 107 |
[ 'reducedLineNumbers' => true ], |
| 108 |
'<td colspan="2" class="diff-lineno"></td>' |
| 109 |
], |
| 110 |
'inline tooltip' => [ |
| 111 |
'inline', |
| 112 |
TextDifferData::WIKIDIFF2_INLINE, |
| 113 |
[], |
| 114 |
'<ins title="\(diff-inline-tooltip-ins\)">' |
| 115 |
], |
| 116 |
]; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public function testLocalize( $format, $input, $options, $pattern ) { |
| 127 |
$differ = $this->createDiffer(); |
| 128 |
$result = $differ->localize( $format, $input, $options ); |
| 129 |
$this->assertMatchesRegularExpression( |
| 130 |
'{' . $pattern . '}s', |
| 131 |
$result |
| 132 |
); |
| 133 |
} |
| 134 |
|
| 135 |
public static function provideAddLocalizedTitleTooltips() { |
| 136 |
return [ |
| 137 |
'moved paragraph left should get new location title' => [ |
| 138 |
'<a class="mw-diff-movedpara-left">⚫</a>', |
| 139 |
'<a class="mw-diff-movedpara-left" title="(diff-paragraph-moved-tonew)">⚫</a>', |
| 140 |
], |
| 141 |
'moved paragraph right should get old location title' => [ |
| 142 |
'<a class="mw-diff-movedpara-right">⚫</a>', |
| 143 |
'<a class="mw-diff-movedpara-right" title="(diff-paragraph-moved-toold)">⚫</a>', |
| 144 |
], |
| 145 |
'nothing changed when key not hit' => [ |
| 146 |
'<a class="mw-diff-movedpara-rightis">⚫</a>', |
| 147 |
'<a class="mw-diff-movedpara-rightis">⚫</a>', |
| 148 |
], |
| 149 |
]; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public function testAddLocalizedTitleTooltips( $input, $expected ) { |
| 156 |
$differ = TestingAccessWrapper::newFromObject( $this->createDiffer() ); |
| 157 |
|
| 158 |
$this->assertEquals( $expected, $differ->addLocalizedTitleTooltips( 'table', $input ) ); |
| 159 |
} |
| 160 |
|
| 161 |
} |