3eb12011d283c54b249df836d62425cad1cf237c
[lhc/web/wiklou.git] / tests / phpunit / includes / diff / TextSlotDiffRendererTest.php
1 <?php
2
3 use Wikimedia\Assert\ParameterTypeException;
4
5 /**
6 * @covers TextSlotDiffRenderer
7 */
8 class TextSlotDiffRendererTest extends MediaWikiTestCase {
9
10 /**
11 * @dataProvider provideGetDiff
12 * @param array|null $oldContentArgs To pass to makeContent() (if not null)
13 * @param array|null $newContentArgs
14 * @param string|Exception $expectedResult
15 * @throws Exception
16 */
17 public function testGetDiff(
18 array $oldContentArgs = null, array $newContentArgs = null, $expectedResult
19 ) {
20 $this->mergeMwGlobalArrayValue( 'wgContentHandlers', [
21 'testing' => DummyContentHandlerForTesting::class,
22 'testing-nontext' => DummyNonTextContentHandler::class,
23 ] );
24
25 $oldContent = $oldContentArgs ? self::makeContent( ...$oldContentArgs ) : null;
26 $newContent = $newContentArgs ? self::makeContent( ...$newContentArgs ) : null;
27
28 if ( $expectedResult instanceof Exception ) {
29 $this->setExpectedException( get_class( $expectedResult ), $expectedResult->getMessage() );
30 }
31
32 $slotDiffRenderer = $this->getTextSlotDiffRenderer();
33 $diff = $slotDiffRenderer->getDiff( $oldContent, $newContent );
34 if ( $expectedResult instanceof Exception ) {
35 return;
36 }
37 $plainDiff = $this->getPlainDiff( $diff );
38 $this->assertSame( $expectedResult, $plainDiff );
39 }
40
41 public static function provideGetDiff() {
42 return [
43 'same text' => [
44 [ "aaa\nbbb\nccc" ],
45 [ "aaa\nbbb\nccc" ],
46 "",
47 ],
48 'different text' => [
49 [ "aaa\nbbb\nccc" ],
50 [ "aaa\nxxx\nccc" ],
51 " aaa aaa\n-bbb+xxx\n ccc ccc",
52 ],
53 'no right content' => [
54 [ "aaa\nbbb\nccc" ],
55 null,
56 "-aaa+ \n-bbb \n-ccc ",
57 ],
58 'no left content' => [
59 null,
60 [ "aaa\nbbb\nccc" ],
61 "- +aaa\n +bbb\n +ccc",
62 ],
63 'no content' => [
64 null,
65 null,
66 new InvalidArgumentException( '$oldContent and $newContent cannot both be null' ),
67 ],
68 'non-text left content' => [
69 [ '', 'testing-nontext' ],
70 [ "aaa\nbbb\nccc" ],
71 new ParameterTypeException( '$oldContent', 'TextContent|null' ),
72 ],
73 'non-text right content' => [
74 [ "aaa\nbbb\nccc" ],
75 [ '', 'testing-nontext' ],
76 new ParameterTypeException( '$newContent', 'TextContent|null' ),
77 ],
78 ];
79 }
80
81 // no separate test for getTextDiff() as getDiff() is just a thin wrapper around it
82
83 /**
84 * @return TextSlotDiffRenderer
85 */
86 private function getTextSlotDiffRenderer() {
87 $slotDiffRenderer = new TextSlotDiffRenderer();
88 $slotDiffRenderer->setStatsdDataFactory( new NullStatsdDataFactory() );
89 $slotDiffRenderer->setLanguage( Language::factory( 'en' ) );
90 $slotDiffRenderer->setEngine( TextSlotDiffRenderer::ENGINE_PHP );
91 return $slotDiffRenderer;
92 }
93
94 /**
95 * Convert a HTML diff to a human-readable format and hopefully make the test less fragile.
96 * @param string diff
97 * @return string
98 */
99 private function getPlainDiff( $diff ) {
100 $replacements = [
101 html_entity_decode( '&nbsp;' ) => ' ',
102 html_entity_decode( '&minus;' ) => '-',
103 ];
104 return str_replace( array_keys( $replacements ), array_values( $replacements ),
105 trim( strip_tags( $diff ), "\n" ) );
106 }
107
108 /**
109 * @param string $str
110 * @param string $model
111 * @return null|TextContent
112 */
113 private static function makeContent( $str, $model = CONTENT_MODEL_TEXT ) {
114 return ContentHandler::makeContent( $str, null, $model );
115 }
116
117 }