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