MediaWiki REL1_33
TextSlotDiffRendererTest.php
Go to the documentation of this file.
1<?php
2
4
9
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
83 private function getTextSlotDiffRenderer() {
84 $slotDiffRenderer = new TextSlotDiffRenderer();
85 $slotDiffRenderer->setStatsdDataFactory( new NullStatsdDataFactory() );
86 $slotDiffRenderer->setLanguage( Language::factory( 'en' ) );
87 $slotDiffRenderer->setWikiDiff2MovedParagraphDetectionCutoff( 0 );
88 $slotDiffRenderer->setEngine( TextSlotDiffRenderer::ENGINE_PHP );
89 return $slotDiffRenderer;
90 }
91
97 private function getPlainDiff( $diff ) {
98 $replacements = [
99 html_entity_decode( '&nbsp;' ) => ' ',
100 html_entity_decode( '&minus;' ) => '-',
101 ];
102 return str_replace( array_keys( $replacements ), array_values( $replacements ),
103 trim( strip_tags( $diff ), "\n" ) );
104 }
105
111 private function makeContent( $str, $model = CONTENT_MODEL_TEXT ) {
112 return ContentHandler::makeContent( $str, null, $model );
113 }
114
115}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
mergeMwGlobalArrayValue( $name, $values)
Merges the given values into a MW global array variable.
makeContent( $str, $model=CONTENT_MODEL_TEXT)
getPlainDiff( $diff)
Convert a HTML diff to a human-readable format and hopefully make the test less fragile.
testGetDiff(Content $oldContent=null, Content $newContent=null, $expectedResult)
provideGetDiff
Renders a slot diff by doing a text diff on the native representation.
const ENGINE_PHP
Use the PHP diff implementation (DiffEngine).
const CONTENT_MODEL_TEXT
Definition Defines.php:247
Base interface for content objects.
Definition Content.php:34