MediaWiki  master
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', [
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->setEngine( TextSlotDiffRenderer::ENGINE_PHP );
88  return $slotDiffRenderer;
89  }
90 
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 
110  private function makeContent( $str, $model = CONTENT_MODEL_TEXT ) {
111  return ContentHandler::makeContent( $str, null, $model );
112  }
113 
114 }
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
getPlainDiff( $diff)
Convert a HTML diff to a human-readable format and hopefully make the test less fragile.
mergeMwGlobalArrayValue( $name, $values)
Merges the given values into a MW global array variable.
const CONTENT_MODEL_TEXT
Definition: Defines.php:234
Renders a slot diff by doing a text diff on the native representation.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:216
const ENGINE_PHP
Use the PHP diff implementation (DiffEngine).
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
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
testGetDiff(Content $oldContent=null, Content $newContent=null, $expectedResult)
provideGetDiff
static makeContent( $text, Title $title=null, $modelId=null, $format=null)
Convenience function for creating a Content object from a given textual representation.
makeContent( $str, $model=CONTENT_MODEL_TEXT)