MediaWiki  1.27.2
LinkerTest.php
Go to the documentation of this file.
1 <?php
2 
8 
13  public function testUserLink( $expected, $userId, $userName, $altUserName = false, $msg = '' ) {
14  $this->setMwGlobals( [
15  'wgArticlePath' => '/wiki/$1',
16  ] );
17 
18  $this->assertEquals( $expected,
19  Linker::userLink( $userId, $userName, $altUserName, $msg )
20  );
21  }
22 
23  public static function provideCasesForUserLink() {
24  # Format:
25  # - expected
26  # - userid
27  # - username
28  # - optional altUserName
29  # - optional message
30  return [
31 
32  # ## ANONYMOUS USER ########################################
33  [
34  '<a href="/wiki/Special:Contributions/JohnDoe" '
35  . 'title="Special:Contributions/JohnDoe" '
36  . 'class="mw-userlink mw-anonuserlink">JohnDoe</a>',
37  0, 'JohnDoe', false,
38  ],
39  [
40  '<a href="/wiki/Special:Contributions/::1" '
41  . 'title="Special:Contributions/::1" '
42  . 'class="mw-userlink mw-anonuserlink">::1</a>',
43  0, '::1', false,
44  'Anonymous with pretty IPv6'
45  ],
46  [
47  '<a href="/wiki/Special:Contributions/0:0:0:0:0:0:0:1" '
48  . 'title="Special:Contributions/0:0:0:0:0:0:0:1" '
49  . 'class="mw-userlink mw-anonuserlink">::1</a>',
50  0, '0:0:0:0:0:0:0:1', false,
51  'Anonymous with almost pretty IPv6'
52  ],
53  [
54  '<a href="/wiki/Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001" '
55  . 'title="Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001" '
56  . 'class="mw-userlink mw-anonuserlink">::1</a>',
57  0, '0000:0000:0000:0000:0000:0000:0000:0001', false,
58  'Anonymous with full IPv6'
59  ],
60  [
61  '<a href="/wiki/Special:Contributions/::1" '
62  . 'title="Special:Contributions/::1" '
63  . 'class="mw-userlink mw-anonuserlink">AlternativeUsername</a>',
64  0, '::1', 'AlternativeUsername',
65  'Anonymous with pretty IPv6 and an alternative username'
66  ],
67 
68  # IPV4
69  [
70  '<a href="/wiki/Special:Contributions/127.0.0.1" '
71  . 'title="Special:Contributions/127.0.0.1" '
72  . 'class="mw-userlink mw-anonuserlink">127.0.0.1</a>',
73  0, '127.0.0.1', false,
74  'Anonymous with IPv4'
75  ],
76  [
77  '<a href="/wiki/Special:Contributions/127.0.0.1" '
78  . 'title="Special:Contributions/127.0.0.1" '
79  . 'class="mw-userlink mw-anonuserlink">AlternativeUsername</a>',
80  0, '127.0.0.1', 'AlternativeUsername',
81  'Anonymous with IPv4 and an alternative username'
82  ],
83 
84  # ## Regular user ##########################################
85  # TODO!
86  ];
87  }
88 
95  public function testFormatComment(
96  $expected, $comment, $title = false, $local = false, $wikiId = null
97  ) {
98  $conf = new SiteConfiguration();
99  $conf->settings = [
100  'wgServer' => [
101  'enwiki' => '//en.example.org',
102  'dewiki' => '//de.example.org',
103  ],
104  'wgArticlePath' => [
105  'enwiki' => '/w/$1',
106  'dewiki' => '/w/$1',
107  ],
108  ];
109  $conf->suffixes = [ 'wiki' ];
110 
111  $this->setMwGlobals( [
112  'wgScript' => '/wiki/index.php',
113  'wgArticlePath' => '/wiki/$1',
114  'wgCapitalLinks' => true,
115  'wgConf' => $conf,
116  ] );
117 
118  if ( $title === false ) {
119  // We need a page title that exists
120  $title = Title::newFromText( 'Special:BlankPage' );
121  }
122 
123  $this->assertEquals(
124  $expected,
125  Linker::formatComment( $comment, $title, $local, $wikiId )
126  );
127  }
128 
129  public function provideCasesForFormatComment() {
130  $wikiId = 'enwiki'; // $wgConf has a fake entry for this
131 
132  // @codingStandardsIgnoreStart Generic.Files.LineLength
133  return [
134  // Linker::formatComment
135  [
136  'a&lt;script&gt;b',
137  'a<script>b',
138  ],
139  [
140  'a—b',
141  'a&mdash;b',
142  ],
143  [
144  "&#039;&#039;&#039;not bolded&#039;&#039;&#039;",
145  "'''not bolded'''",
146  ],
147  [
148  "try &lt;script&gt;evil&lt;/scipt&gt; things",
149  "try <script>evil</scipt> things",
150  ],
151  // Linker::formatAutocomments
152  [
153  '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
154  "/* autocomment */",
155  ],
156  [
157  '<a href="/wiki/Special:BlankPage#linkie.3F" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment"><a href="/wiki/index.php?title=Linkie%3F&amp;action=edit&amp;redlink=1" class="new" title="Linkie? (page does not exist)">linkie?</a></span></span>',
158  "/* [[linkie?]] */",
159  ],
160  [
161  '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment: </span> post</span>',
162  "/* autocomment */ post",
163  ],
164  [
165  'pre <a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
166  "pre /* autocomment */",
167  ],
168  [
169  'pre <a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment: </span> post</span>',
170  "pre /* autocomment */ post",
171  ],
172  [
173  '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment: </span> multiple? <a href="/wiki/Special:BlankPage#autocomment2" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment2: </span> </span></span>',
174  "/* autocomment */ multiple? /* autocomment2 */ ",
175  ],
176  [
177  '<a href="/wiki/Special:BlankPage#autocomment_containing_.2F.2A" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment containing /*: </span> T70361</span>',
178  "/* autocomment containing /* */ T70361"
179  ],
180  [
181  '<a href="/wiki/Special:BlankPage#autocomment_containing_.22quotes.22" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment containing &quot;quotes&quot;</span></span>',
182  "/* autocomment containing \"quotes\" */"
183  ],
184  [
185  '<a href="/wiki/Special:BlankPage#autocomment_containing_.3Cscript.3Etags.3C.2Fscript.3E" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment containing &lt;script&gt;tags&lt;/script&gt;</span></span>',
186  "/* autocomment containing <script>tags</script> */"
187  ],
188  [
189  '<a href="#autocomment">→</a>‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
190  "/* autocomment */",
191  false, true
192  ],
193  [
194  '‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
195  "/* autocomment */",
196  null
197  ],
198  [
199  '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
200  "/* autocomment */",
201  false, false
202  ],
203  [
204  '<a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage#autocomment">→</a>‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
205  "/* autocomment */",
206  false, false, $wikiId
207  ],
208  // Linker::formatLinksInComment
209  [
210  'abc <a href="/wiki/index.php?title=Link&amp;action=edit&amp;redlink=1" class="new" title="Link (page does not exist)">link</a> def',
211  "abc [[link]] def",
212  ],
213  [
214  'abc <a href="/wiki/index.php?title=Link&amp;action=edit&amp;redlink=1" class="new" title="Link (page does not exist)">text</a> def',
215  "abc [[link|text]] def",
216  ],
217  [
218  'abc <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a> def',
219  "abc [[Special:BlankPage|]] def",
220  ],
221  [
222  'abc <a href="/wiki/index.php?title=%C4%84%C5%9B%C5%BC&amp;action=edit&amp;redlink=1" class="new" title="Ąśż (page does not exist)">ąśż</a> def',
223  "abc [[%C4%85%C5%9B%C5%BC]] def",
224  ],
225  [
226  'abc <a href="/wiki/Special:BlankPage#section" title="Special:BlankPage">#section</a> def',
227  "abc [[#section]] def",
228  ],
229  [
230  'abc <a href="/wiki/index.php?title=/subpage&amp;action=edit&amp;redlink=1" class="new" title="/subpage (page does not exist)">/subpage</a> def',
231  "abc [[/subpage]] def",
232  ],
233  [
234  'abc <a href="/wiki/index.php?title=%22evil!%22&amp;action=edit&amp;redlink=1" class="new" title="&quot;evil!&quot; (page does not exist)">&quot;evil!&quot;</a> def',
235  "abc [[\"evil!\"]] def",
236  ],
237  [
238  'abc [[&lt;script&gt;very evil&lt;/script&gt;]] def',
239  "abc [[<script>very evil</script>]] def",
240  ],
241  [
242  'abc [[|]] def',
243  "abc [[|]] def",
244  ],
245  [
246  'abc <a href="/wiki/index.php?title=Link&amp;action=edit&amp;redlink=1" class="new" title="Link (page does not exist)">link</a> def',
247  "abc [[link]] def",
248  false, false
249  ],
250  [
251  'abc <a class="external" rel="nofollow" href="//en.example.org/w/Link">link</a> def',
252  "abc [[link]] def",
253  false, false, $wikiId
254  ],
255  ];
256  // @codingStandardsIgnoreEnd
257  }
258 
263  public function testFormatLinksInComment( $expected, $input, $wiki ) {
264 
265  $conf = new SiteConfiguration();
266  $conf->settings = [
267  'wgServer' => [
268  'enwiki' => '//en.example.org'
269  ],
270  'wgArticlePath' => [
271  'enwiki' => '/w/$1',
272  ],
273  ];
274  $conf->suffixes = [ 'wiki' ];
275  $this->setMwGlobals( [
276  'wgScript' => '/wiki/index.php',
277  'wgArticlePath' => '/wiki/$1',
278  'wgCapitalLinks' => true,
279  'wgConf' => $conf,
280  ] );
281 
282  $this->assertEquals(
283  $expected,
284  Linker::formatLinksInComment( $input, Title::newFromText( 'Special:BlankPage' ), false, $wiki )
285  );
286  }
287 
288  public static function provideCasesForFormatLinksInComment() {
289  // @codingStandardsIgnoreStart Generic.Files.LineLength
290  return [
291  [
292  'foo bar <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
293  'foo bar [[Special:BlankPage]]',
294  null,
295  ],
296  [
297  '<a class="external" rel="nofollow" href="//en.example.org/w/Foo%27bar">Foo\'bar</a>',
298  "[[Foo'bar]]",
299  'enwiki',
300  ],
301  [
302  'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage">Special:BlankPage</a>',
303  'foo bar [[Special:BlankPage]]',
304  'enwiki',
305  ],
306  ];
307  // @codingStandardsIgnoreEnd
308  }
309 }
testFormatLinksInComment($expected, $input, $wiki)
Linker::formatLinksInComment provideCasesForFormatLinksInComment.
Definition: LinkerTest.php:263
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
$comment
testUserLink($expected, $userId, $userName, $altUserName=false, $msg= '')
provideCasesForUserLink Linker::userLink
Definition: LinkerTest.php:13
static newFromText($text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:277
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:1798
testFormatComment($expected, $comment, $title=false, $local=false, $wikiId=null)
provideCasesForFormatComment Linker::formatComment Linker::formatAutocomments Linker::formatLinksInCo...
Definition: LinkerTest.php:95
static formatLinksInComment($comment, $title=null, $local=false, $wikiId=null)
Formats wiki links and media links in text; all other wiki formatting is ignored. ...
Definition: Linker.php:1413
static provideCasesForFormatLinksInComment()
Definition: LinkerTest.php:288
Base class that store and restore the Language objects.
static provideCasesForUserLink()
Definition: LinkerTest.php:23
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:912
static formatComment($comment, $title=null, $local=false, $wikiId=null)
This function is called by all recent changes variants, by the page history, and by the user contribu...
Definition: Linker.php:1290
provideCasesForFormatComment()
Definition: LinkerTest.php:129
static userLink($userId, $userName, $altUserName=false)
Make user link (or user contributions for unregistered users)
Definition: Linker.php:1102
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
Database.
Definition: LinkerTest.php:7
This is a class for holding configuration settings, particularly for multi-wiki sites.
setMwGlobals($pairs, $value=null)