MediaWiki  master
LinkRendererTest.php
Go to the documentation of this file.
1 <?php
2 
6 
11 
15  private $factory;
16 
17  public function setUp() {
18  parent::setUp();
19  $this->setMwGlobals( [
20  'wgArticlePath' => '/wiki/$1',
21  'wgServer' => '//example.org',
22  'wgCanonicalServer' => 'http://example.org',
23  'wgScriptPath' => '/w',
24  'wgScript' => '/w/index.php',
25  ] );
26  $this->factory = MediaWikiServices::getInstance()->getLinkRendererFactory();
27 
28  }
29 
30  public function testMergeAttribs() {
31  $target = new TitleValue( NS_SPECIAL, 'Blankpage' );
32  $linkRenderer = $this->factory->create();
33  $link = $linkRenderer->makeBrokenLink( $target, null, [
34  // Appended to class
35  'class' => 'foobar',
36  // Suppresses href attribute
37  'href' => false,
38  // Extra attribute
39  'bar' => 'baz'
40  ] );
41  $this->assertEquals(
42  '<a href="/wiki/Special:BlankPage" class="new foobar" '
43  . 'title="Special:BlankPage (page does not exist)" bar="baz">'
44  . 'Special:BlankPage</a>',
45  $link
46  );
47  }
48 
49  public function testMakeKnownLink() {
50  $target = new TitleValue( NS_MAIN, 'Foobar' );
51  $linkRenderer = $this->factory->create();
52 
53  // Query added
54  $this->assertEquals(
55  '<a href="/w/index.php?title=Foobar&amp;foo=bar" '. 'title="Foobar">Foobar</a>',
56  $linkRenderer->makeKnownLink( $target, null, [], [ 'foo' => 'bar' ] )
57  );
58 
59  // forcearticlepath
60  $linkRenderer->setForceArticlePath( true );
61  $this->assertEquals(
62  '<a href="/wiki/Foobar?foo=bar" title="Foobar">Foobar</a>',
63  $linkRenderer->makeKnownLink( $target, null, [], [ 'foo' => 'bar' ] )
64  );
65 
66  // expand = HTTPS
67  $linkRenderer->setForceArticlePath( false );
68  $linkRenderer->setExpandURLs( PROTO_HTTPS );
69  $this->assertEquals(
70  '<a href="https://example.org/wiki/Foobar" title="Foobar">Foobar</a>',
71  $linkRenderer->makeKnownLink( $target )
72  );
73  }
74 
75  public function testMakeBrokenLink() {
76  $target = new TitleValue( NS_MAIN, 'Foobar' );
77  $special = new TitleValue( NS_SPECIAL, 'Foobar' );
78  $linkRenderer = $this->factory->create();
79 
80  // action=edit&redlink=1 added
81  $this->assertEquals(
82  '<a href="/w/index.php?title=Foobar&amp;action=edit&amp;redlink=1" '
83  . 'class="new" title="Foobar (page does not exist)">Foobar</a>',
84  $linkRenderer->makeBrokenLink( $target )
85  );
86 
87  // action=edit&redlink=1 not added due to action query parameter
88  $this->assertEquals(
89  '<a href="/w/index.php?title=Foobar&amp;action=foobar" class="new" '
90  . 'title="Foobar (page does not exist)">Foobar</a>',
91  $linkRenderer->makeBrokenLink( $target, null, [], [ 'action' => 'foobar' ] )
92  );
93 
94  // action=edit&redlink=1 not added due to NS_SPECIAL
95  $this->assertEquals(
96  '<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
97  . '(page does not exist)">Special:Foobar</a>',
98  $linkRenderer->makeBrokenLink( $special )
99  );
100 
101  // fragment stripped
102  $this->assertEquals(
103  '<a href="/w/index.php?title=Foobar&amp;action=edit&amp;redlink=1" '
104  . 'class="new" title="Foobar (page does not exist)">Foobar</a>',
105  $linkRenderer->makeBrokenLink( $target->createFragmentTarget( 'foobar' ) )
106  );
107  }
108 
109  public function testMakeLink() {
110  $linkRenderer = $this->factory->create();
111  $foobar = new TitleValue( NS_SPECIAL, 'Foobar' );
112  $blankpage = new TitleValue( NS_SPECIAL, 'Blankpage' );
113  $this->assertEquals(
114  '<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
115  . '(page does not exist)">foo</a>',
116  $linkRenderer->makeLink( $foobar, 'foo' )
117  );
118 
119  $this->assertEquals(
120  '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">blank</a>',
121  $linkRenderer->makeLink( $blankpage, 'blank' )
122  );
123 
124  $this->assertEquals(
125  '<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
126  . '(page does not exist)">&lt;script&gt;evil()&lt;/script&gt;</a>',
127  $linkRenderer->makeLink( $foobar, '<script>evil()</script>' )
128  );
129 
130  $this->assertEquals(
131  '<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
132  . '(page does not exist)"><script>evil()</script></a>',
133  $linkRenderer->makeLink( $foobar, new HtmlArmor( '<script>evil()</script>' ) )
134  );
135  }
136 
137  public function testGetLinkClasses() {
138  $titleFormatter = MediaWikiServices::getInstance()->getTitleFormatter();
139  $linkCache = new LinkCache( $titleFormatter );
140  $foobarTitle = new TitleValue( NS_MAIN, 'FooBar' );
141  $redirectTitle = new TitleValue( NS_MAIN, 'Redirect' );
142  $userTitle = new TitleValue( NS_USER, 'Someuser' );
143  $linkCache->addGoodLinkObj(
144  1, // id
145  $foobarTitle,
146  10, // len
147  0 // redir
148  );
149  $linkCache->addGoodLinkObj(
150  2, // id
151  $redirectTitle,
152  10, // len
153  1 // redir
154  );
155 
156  $linkCache->addGoodLinkObj(
157  3, // id
158  $userTitle,
159  10, // len
160  0 // redir
161  );
162 
163  $linkRenderer = new LinkRenderer( $titleFormatter, $linkCache );
164  $linkRenderer->setStubThreshold( 0 );
165  $this->assertEquals(
166  '',
167  $linkRenderer->getLinkClasses( $foobarTitle )
168  );
169 
170  $linkRenderer->setStubThreshold( 20 );
171  $this->assertEquals(
172  'stub',
173  $linkRenderer->getLinkClasses( $foobarTitle )
174  );
175 
176  $linkRenderer->setStubThreshold( 0 );
177  $this->assertEquals(
178  'mw-redirect',
179  $linkRenderer->getLinkClasses( $redirectTitle )
180  );
181 
182  $linkRenderer->setStubThreshold( 20 );
183  $this->assertEquals(
184  '',
185  $linkRenderer->getLinkClasses( $userTitle )
186  );
187  }
188 
189 }
this hook is for auditing only RecentChangesLinked and Watchlist $special
Definition: hooks.txt:981
const NS_MAIN
Definition: Defines.php:69
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
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 an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing after in associative array form before processing starts Return false to skip default processing and return $ret $linkRenderer
Definition: hooks.txt:1816
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:36
const NS_SPECIAL
Definition: Defines.php:58
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 MediaWikiServices
Definition: injection.txt:23
the value to return A Title object or null for latest to be modified or replaced by the hook handler or if authentication is not possible after cache objects are set for highlighting & $link
Definition: hooks.txt:2621
MediaWiki\Linker\LinkRenderer.
const PROTO_HTTPS
Definition: Defines.php:263
LinkRendererFactory $factory
Class that generates HTML links for pages.
Base class that store and restore the Language objects.
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
Cache for article titles (prefixed DB keys) and ids linked from one source.
Definition: LinkCache.php:31
Factory to create LinkRender objects.
Marks HTML that shouldn't be escaped.
Definition: HtmlArmor.php:28
setMwGlobals($pairs, $value=null)