MediaWiki  1.31.0
JavaScriptContentTest.php
Go to the documentation of this file.
1 <?php
2 
9 
10  public function newContent( $text ) {
11  return new JavaScriptContent( $text );
12  }
13 
14  public static function dataGetParserOutput() {
15  return [
16  [
17  'MediaWiki:Test.js',
18  null,
19  "hello <world>\n",
20  "<pre class=\"mw-code mw-js\" dir=\"ltr\">\nhello &lt;world&gt;\n\n</pre>"
21  ],
22  [
23  'MediaWiki:Test.js',
24  null,
25  "hello(); // [[world]]\n",
26  "<pre class=\"mw-code mw-js\" dir=\"ltr\">\nhello(); // [[world]]\n\n</pre>",
27  [
28  'Links' => [
29  [ 'World' => 0 ]
30  ]
31  ]
32  ],
33 
34  // TODO: more...?
35  ];
36  }
37 
38  // XXX: Unused function
39  public static function dataGetSection() {
40  return [
42  '0',
43  null
44  ],
46  '2',
47  null
48  ],
50  '8',
51  null
52  ],
53  ];
54  }
55 
56  // XXX: Unused function
57  public static function dataReplaceSection() {
58  return [
60  '0',
61  'No more',
62  null,
63  null
64  ],
66  '',
67  'No more',
68  null,
69  null
70  ],
72  '2',
73  "== TEST ==\nmore fun",
74  null,
75  null
76  ],
78  '8',
79  'No more',
80  null,
81  null
82  ],
84  'new',
85  'No more',
86  'New',
87  null
88  ],
89  ];
90  }
91 
95  public function testAddSectionHeader() {
96  $content = $this->newContent( 'hello world' );
97  $c = $content->addSectionHeader( 'test' );
98 
99  $this->assertTrue( $content->equals( $c ) );
100  }
101 
102  // XXX: currently, preSaveTransform is applied to scripts. this may change or become optional.
103  public static function dataPreSaveTransform() {
104  return [
105  [ 'hello this is ~~~',
106  "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
107  ],
108  [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
109  'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
110  ],
111  [ " Foo \n ",
112  " Foo",
113  ],
114  ];
115  }
116 
117  public static function dataPreloadTransform() {
118  return [
119  [
120  'hello this is ~~~',
121  'hello this is ~~~',
122  ],
123  [
124  'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
125  'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
126  ],
127  ];
128  }
129 
130  public static function dataGetRedirectTarget() {
131  return [
132  [ '#REDIRECT [[Test]]',
133  null,
134  ],
135  [ '#REDIRECT Test',
136  null,
137  ],
138  [ '* #REDIRECT [[Test]]',
139  null,
140  ],
141  ];
142  }
143 
144  public static function dataIsCountable() {
145  return [
146  [ '',
147  null,
148  'any',
149  true
150  ],
151  [ 'Foo',
152  null,
153  'any',
154  true
155  ],
156  [ 'Foo',
157  null,
158  'link',
159  false
160  ],
161  [ 'Foo [[bar]]',
162  null,
163  'link',
164  false
165  ],
166  [ 'Foo',
167  true,
168  'link',
169  false
170  ],
171  [ 'Foo [[bar]]',
172  false,
173  'link',
174  false
175  ],
176  [ '#REDIRECT [[bar]]',
177  true,
178  'any',
179  true
180  ],
181  [ '#REDIRECT [[bar]]',
182  true,
183  'link',
184  false
185  ],
186  ];
187  }
188 
189  public static function dataGetTextForSummary() {
190  return [
191  [ "hello\nworld.",
192  16,
193  'hello world.',
194  ],
195  [ 'hello world.',
196  8,
197  'hello...',
198  ],
199  [ '[[hello world]].',
200  8,
201  '[[hel...',
202  ],
203  ];
204  }
205 
209  public function testMatchMagicWord() {
210  $mw = MagicWord::get( "staticredirect" );
211 
212  $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
213  $this->assertFalse(
214  $content->matchMagicWord( $mw ),
215  "should not have matched magic word, since it's not wikitext"
216  );
217  }
218 
223  public function testUpdateRedirect( $oldText, $expectedText ) {
224  $this->setMwGlobals( [
225  'wgServer' => '//example.org',
226  'wgScriptPath' => '/w',
227  'wgScript' => '/w/index.php',
228  'wgResourceBasePath' => '/w',
229  ] );
230  $target = Title::newFromText( "testUpdateRedirect_target" );
231 
232  $content = new JavaScriptContent( $oldText );
233  $newContent = $content->updateRedirect( $target );
234 
235  $this->assertEquals( $expectedText, $newContent->getNativeData() );
236  }
237 
238  public static function provideUpdateRedirect() {
239  // phpcs:disable Generic.Files.LineLength
240  return [
241  [
242  '#REDIRECT [[Someplace]]',
243  '#REDIRECT [[Someplace]]',
244  ],
245  [
246  '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=MediaWiki:MonoBook.js\u0026action=raw\u0026ctype=text/javascript");',
247  '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=TestUpdateRedirect_target\u0026action=raw\u0026ctype=text/javascript");'
248  ]
249  ];
250  // phpcs:enable
251  }
252 
256  public function testGetModel() {
257  $content = $this->newContent( "hello world." );
258 
259  $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $content->getModel() );
260  }
261 
265  public function testGetContentHandler() {
266  $content = $this->newContent( "hello world." );
267 
268  $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $content->getContentHandler()->getModelID() );
269  }
270 
271  public static function dataEquals() {
272  return [
273  [ new JavaScriptContent( "hallo" ), null, false ],
274  [ new JavaScriptContent( "hallo" ), new JavaScriptContent( "hallo" ), true ],
275  [ new JavaScriptContent( "hallo" ), new CssContent( "hallo" ), false ],
276  [ new JavaScriptContent( "hallo" ), new JavaScriptContent( "HALLO" ), false ],
277  ];
278  }
279 
284  public function testGetRedirectTarget( $title, $text ) {
285  $this->setMwGlobals( [
286  'wgServer' => '//example.org',
287  'wgScriptPath' => '/w',
288  'wgScript' => '/w/index.php',
289  'wgResourceBasePath' => '/w',
290  ] );
291  $content = new JavaScriptContent( $text );
292  $target = $content->getRedirectTarget();
293  $this->assertEquals( $title, $target ? $target->getPrefixedText() : null );
294  }
295 
299  public static function provideGetRedirectTarget() {
300  // phpcs:disable Generic.Files.LineLength
301  return [
302  [
303  'MediaWiki:MonoBook.js',
304  '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=MediaWiki:MonoBook.js\u0026action=raw\u0026ctype=text/javascript");'
305  ],
306  [
307  'User:FooBar/common.js',
308  '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=User:FooBar/common.js\u0026action=raw\u0026ctype=text/javascript");'
309  ],
310  [
311  'Gadget:FooBaz.js',
312  '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=Gadget:FooBaz.js\u0026action=raw\u0026ctype=text/javascript");'
313  ],
314  // No #REDIRECT comment
315  [
316  null,
317  'mw.loader.load("//example.org/w/index.php?title=MediaWiki:NoRedirect.js\u0026action=raw\u0026ctype=text/javascript");'
318  ],
319  // Different domain
320  [
321  null,
322  '/* #REDIRECT */mw.loader.load("//example.com/w/index.php?title=MediaWiki:OtherWiki.js\u0026action=raw\u0026ctype=text/javascript");'
323  ],
324  ];
325  // phpcs:enable
326  }
327 }
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:273
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
MagicWord\get
static & get( $id)
Factory: creates an object representing an ID.
Definition: MagicWord.php:277
TextContentTest
ContentHandler Database ^— needed, because we do need the database to test link updates.
Definition: TextContentTest.php:8
JavaScriptContentTest\testGetRedirectTarget
testGetRedirectTarget( $title, $text)
JavaScriptContent::getRedirectTarget provideGetRedirectTarget.
Definition: JavaScriptContentTest.php:284
CssContent
Content object for CSS pages.
Definition: CssContent.php:33
JavaScriptContentTest\dataIsCountable
static dataIsCountable()
Definition: JavaScriptContentTest.php:144
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
WikitextContentTest\$sections
static $sections
Definition: WikitextContentTest.php:10
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:934
JavaScriptContentTest\dataGetParserOutput
static dataGetParserOutput()
Definition: JavaScriptContentTest.php:14
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:678
JavaScriptContentTest\provideGetRedirectTarget
static provideGetRedirectTarget()
Keep this in sync with JavaScriptContentHandlerTest::provideMakeRedirectContent()
Definition: JavaScriptContentTest.php:299
JavaScriptContent
Content for JavaScript pages.
Definition: JavaScriptContent.php:33
JavaScriptContentTest\testGetModel
testGetModel()
JavaScriptContent::getModel.
Definition: JavaScriptContentTest.php:256
JavaScriptContentTest\dataGetRedirectTarget
static dataGetRedirectTarget()
Definition: JavaScriptContentTest.php:130
JavaScriptContentTest\testAddSectionHeader
testAddSectionHeader()
JavaScriptContent::addSectionHeader.
Definition: JavaScriptContentTest.php:95
JavaScriptContentTest\testUpdateRedirect
testUpdateRedirect( $oldText, $expectedText)
JavaScriptContent::updateRedirect provideUpdateRedirect.
Definition: JavaScriptContentTest.php:223
JavaScriptContentTest\dataGetTextForSummary
static dataGetTextForSummary()
Definition: JavaScriptContentTest.php:189
JavaScriptContentTest\testGetContentHandler
testGetContentHandler()
JavaScriptContent::getContentHandler.
Definition: JavaScriptContentTest.php:265
JavaScriptContentTest
ContentHandler Database ^— needed, because we do need the database to test link updates.
Definition: JavaScriptContentTest.php:8
JavaScriptContentTest\dataReplaceSection
static dataReplaceSection()
Definition: JavaScriptContentTest.php:57
JavaScriptContentTest\dataPreloadTransform
static dataPreloadTransform()
Definition: JavaScriptContentTest.php:117
JavaScriptContentTest\dataGetSection
static dataGetSection()
Definition: JavaScriptContentTest.php:39
JavaScriptContentTest\newContent
newContent( $text)
Definition: JavaScriptContentTest.php:10
JavaScriptContentTest\provideUpdateRedirect
static provideUpdateRedirect()
Definition: JavaScriptContentTest.php:238
true
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1987
CONTENT_MODEL_JAVASCRIPT
const CONTENT_MODEL_JAVASCRIPT
Definition: Defines.php:237
JavaScriptContentTest\testMatchMagicWord
testMatchMagicWord()
JavaScriptContent::matchMagicWord.
Definition: JavaScriptContentTest.php:209
JavaScriptContentTest\dataEquals
static dataEquals()
Definition: JavaScriptContentTest.php:271
JavaScriptContentTest\dataPreSaveTransform
static dataPreSaveTransform()
Definition: JavaScriptContentTest.php:103