MediaWiki  master
JsonContentTest.php
Go to the documentation of this file.
1 <?php
2 
8 
9  public static function provideValidConstruction() {
10  return [
11  [ 'foo', false, null ],
12  [ '[]', true, [] ],
13  [ '{}', true, (object)[] ],
14  [ '""', true, '' ],
15  [ '"0"', true, '0' ],
16  [ '"bar"', true, 'bar' ],
17  [ '0', true, '0' ],
18  [ '{ "0": "bar" }', true, (object)[ 'bar' ] ],
19  ];
20  }
21 
25  public function testIsValid( $text, $isValid, $expected ) {
26  $obj = new JsonContent( $text, CONTENT_MODEL_JSON );
27  $this->assertEquals( $isValid, $obj->isValid() );
28  $this->assertEquals( $expected, $obj->getData()->getValue() );
29  }
30 
31  public static function provideDataToEncode() {
32  return [
33  [
34  // Round-trip empty array
35  '[]',
36  '[]',
37  ],
38  [
39  // Round-trip empty object
40  '{}',
41  '{}',
42  ],
43  [
44  // Round-trip empty array/object (nested)
45  '{ "foo": {}, "bar": [] }',
46  "{\n \"foo\": {},\n \"bar\": []\n}",
47  ],
48  [
49  '{ "foo": "bar" }',
50  "{\n \"foo\": \"bar\"\n}",
51  ],
52  [
53  '{ "foo": 1000 }',
54  "{\n \"foo\": 1000\n}",
55  ],
56  [
57  '{ "foo": 1000, "0": "bar" }',
58  "{\n \"foo\": 1000,\n \"0\": \"bar\"\n}",
59  ],
60  ];
61  }
62 
66  public function testBeautifyJson( $input, $beautified ) {
67  $obj = new JsonContent( $input );
68  $this->assertEquals( $beautified, $obj->beautifyJSON() );
69  }
70 
74  public function testPreSaveTransform( $input, $transformed ) {
75  $obj = new JsonContent( $input );
76  $newObj = $obj->preSaveTransform(
77  $this->getMockTitle(),
78  $this->getMockUser(),
79  $this->getMockParserOptions()
80  );
81  $this->assertTrue( $newObj->equals( new JsonContent( $transformed ) ) );
82  }
83 
84  private function getMockTitle() {
85  return $this->getMockBuilder( Title::class )
86  ->disableOriginalConstructor()
87  ->getMock();
88  }
89 
90  private function getMockUser() {
91  return $this->getMockBuilder( User::class )
92  ->disableOriginalConstructor()
93  ->getMock();
94  }
95 
96  private function getMockParserOptions() {
97  return $this->getMockBuilder( ParserOptions::class )
98  ->disableOriginalConstructor()
99  ->getMock();
100  }
101 
102  public static function provideDataAndParserText() {
103  return [
104  [
105  [],
106  '<table class="mw-json"><tbody><tr><td>' .
107  '<table class="mw-json"><tbody><tr><td class="mw-json-empty">Empty array</td></tr>'
108  . '</tbody></table></td></tr></tbody></table>'
109  ],
110  [
111  (object)[],
112  '<table class="mw-json"><tbody><tr><td class="mw-json-empty">Empty object</td></tr>' .
113  '</tbody></table>'
114  ],
115  [
116  (object)[ 'foo' ],
117  '<table class="mw-json"><tbody><tr><th>0</th><td class="mw-json-value">"foo"</td></tr>' .
118  '</tbody></table>'
119  ],
120  [
121  (object)[ 'foo', 'bar' ],
122  '<table class="mw-json"><tbody><tr><th>0</th><td class="mw-json-value">"foo"</td></tr>' .
123  '<tr><th>1</th><td class="mw-json-value">"bar"</td></tr></tbody></table>'
124  ],
125  [
126  (object)[ 'baz' => 'foo', 'bar' ],
127  '<table class="mw-json"><tbody><tr><th>baz</th><td class="mw-json-value">"foo"</td></tr>' .
128  '<tr><th>0</th><td class="mw-json-value">"bar"</td></tr></tbody></table>'
129  ],
130  [
131  (object)[ 'baz' => 1000, 'bar' ],
132  '<table class="mw-json"><tbody><tr><th>baz</th><td class="mw-json-value">1000</td></tr>' .
133  '<tr><th>0</th><td class="mw-json-value">"bar"</td></tr></tbody></table>'
134  ],
135  [
136  (object)[ '<script>alert("evil!")</script>' ],
137  '<table class="mw-json"><tbody><tr><th>0</th><td class="mw-json-value">"' .
138  '&lt;script>alert("evil!")&lt;/script>"' .
139  '</td></tr></tbody></table>',
140  ],
141  ];
142  }
143 
147  public function testFillParserOutput( $data, $expected ) {
148  $obj = new JsonContent( FormatJson::encode( $data ) );
149  $parserOutput = $obj->getParserOutput( $this->getMockTitle(), null, null, true );
150  $this->assertInstanceOf( ParserOutput::class, $parserOutput );
151  $this->assertEquals( $expected, $parserOutput->getText() );
152  }
153 }
if(is_array( $mode)) switch( $mode) $input
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration which are documented in DefaultSettings php There is no comprehensive documentation for the remaining however some of the most important ones are listed below They are typically initialised either in index php or in Setup php $wgTitle Title object created from the request URL $wgOut OutputPage object for HTTP response $wgUser User object for the user associated with the current request $wgLang Language object selected by user preferences $wgContLang Language object associated with the wiki being viewed $wgParser Parser object Parser extensions register their hooks here $wgRequest WebRequest object
Definition: globals.txt:25
static encode( $value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
Definition: FormatJson.php:115
const CONTENT_MODEL_JSON
Definition: Defines.php:219
testFillParserOutput( $data, $expected)
provideDataAndParserText
static provideValidConstruction()
testIsValid( $text, $isValid, $expected)
provideValidConstruction
static provideDataToEncode()
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:773
testPreSaveTransform( $input, $transformed)
provideDataToEncode
testBeautifyJson( $input, $beautified)
provideDataToEncode
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
Represents the content of a JSON content.
Definition: JsonContent.php:15
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
static provideDataAndParserText()