MediaWiki  1.32.5
TextSlotDiffRendererTest.php
Go to the documentation of this file.
1 <?php
2 
3 use Wikimedia\Assert\ParameterTypeException;
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', [
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 }
MediaWikiTestCase\mergeMwGlobalArrayValue
mergeMwGlobalArrayValue( $name, $values)
Merges the given values into a MW global array variable.
Definition: MediaWikiTestCase.php:901
NullStatsdDataFactory
Definition: NullStatsdDataFactory.php:10
TextSlotDiffRendererTest
TextSlotDiffRenderer.
Definition: TextSlotDiffRendererTest.php:8
TextSlotDiffRendererTest\testGetDiff
testGetDiff(Content $oldContent=null, Content $newContent=null, $expectedResult)
provideGetDiff
Definition: TextSlotDiffRendererTest.php:17
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
MediaWikiTestCase
Definition: MediaWikiTestCase.php:16
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
TextSlotDiffRenderer\ENGINE_PHP
const ENGINE_PHP
Use the PHP diff implementation (DiffEngine).
Definition: TextSlotDiffRenderer.php:40
TextSlotDiffRendererTest\makeContent
makeContent( $str, $model=CONTENT_MODEL_TEXT)
Definition: TextSlotDiffRendererTest.php:111
ContentHandler\makeContent
static makeContent( $text, Title $title=null, $modelId=null, $format=null)
Convenience function for creating a Content object from a given textual representation.
Definition: ContentHandler.php:133
Content
Base interface for content objects.
Definition: Content.php:34
TextSlotDiffRendererTest\provideGetDiff
provideGetDiff()
Definition: TextSlotDiffRendererTest.php:33
TextSlotDiffRendererTest\getPlainDiff
getPlainDiff( $diff)
Convert a HTML diff to a human-readable format and hopefully make the test less fragile.
Definition: TextSlotDiffRendererTest.php:97
Language\factory
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:214
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
CONTENT_MODEL_TEXT
const CONTENT_MODEL_TEXT
Definition: Defines.php:238
TextSlotDiffRendererTest\getTextSlotDiffRenderer
getTextSlotDiffRenderer()
Definition: TextSlotDiffRendererTest.php:83
TextSlotDiffRenderer
Renders a slot diff by doing a text diff on the native representation.
Definition: TextSlotDiffRenderer.php:37