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