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  public function testMergeAttribs() {
30  $target = new TitleValue( NS_SPECIAL, 'Blankpage' );
31  $linkRenderer = $this->factory->create();
32  $link = $linkRenderer->makeBrokenLink( $target, null, [
33  // Appended to class
34  'class' => 'foobar',
35  // Suppresses href attribute
36  'href' => false,
37  // Extra attribute
38  'bar' => 'baz'
39  ] );
40  $this->assertEquals(
41  '<a href="/wiki/Special:BlankPage" class="new foobar" '
42  . 'title="Special:BlankPage (page does not exist)" bar="baz">'
43  . 'Special:BlankPage</a>',
44  $link
45  );
46  }
47 
48  public function testMakeKnownLink() {
49  $target = new TitleValue( NS_MAIN, 'Foobar' );
50  $linkRenderer = $this->factory->create();
51 
52  // Query added
53  $this->assertEquals(
54  '<a href="/w/index.php?title=Foobar&amp;foo=bar" title="Foobar">Foobar</a>',
55  $linkRenderer->makeKnownLink( $target, null, [], [ 'foo' => 'bar' ] )
56  );
57 
58  $linkRenderer->setForceArticlePath( true );
59  $this->assertEquals(
60  '<a href="/wiki/Foobar?foo=bar" title="Foobar">Foobar</a>',
61  $linkRenderer->makeKnownLink( $target, null, [], [ 'foo' => 'bar' ] )
62  );
63 
64  // expand = HTTPS
65  $linkRenderer->setForceArticlePath( false );
66  $linkRenderer->setExpandURLs( PROTO_HTTPS );
67  $this->assertEquals(
68  '<a href="https://example.org/wiki/Foobar" title="Foobar">Foobar</a>',
69  $linkRenderer->makeKnownLink( $target )
70  );
71  }
72 
73  public function testMakeBrokenLink() {
74  $target = new TitleValue( NS_MAIN, 'Foobar' );
75  $special = new TitleValue( NS_SPECIAL, 'Foobar' );
76  $linkRenderer = $this->factory->create();
77 
78  // action=edit&redlink=1 added
79  $this->assertEquals(
80  '<a href="/w/index.php?title=Foobar&amp;action=edit&amp;redlink=1" '
81  . 'class="new" title="Foobar (page does not exist)">Foobar</a>',
82  $linkRenderer->makeBrokenLink( $target )
83  );
84 
85  // action=edit&redlink=1 not added due to action query parameter
86  $this->assertEquals(
87  '<a href="/w/index.php?title=Foobar&amp;action=foobar" class="new" '
88  . 'title="Foobar (page does not exist)">Foobar</a>',
89  $linkRenderer->makeBrokenLink( $target, null, [], [ 'action' => 'foobar' ] )
90  );
91 
92  // action=edit&redlink=1 not added due to NS_SPECIAL
93  $this->assertEquals(
94  '<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
95  . '(page does not exist)">Special:Foobar</a>',
96  $linkRenderer->makeBrokenLink( $special )
97  );
98 
99  // fragment stripped
100  $this->assertEquals(
101  '<a href="/w/index.php?title=Foobar&amp;action=edit&amp;redlink=1" '
102  . 'class="new" title="Foobar (page does not exist)">Foobar</a>',
103  $linkRenderer->makeBrokenLink( $target->createFragmentTarget( 'foobar' ) )
104  );
105  }
106 
107  public function testMakeLink() {
108  $linkRenderer = $this->factory->create();
109  $foobar = new TitleValue( NS_SPECIAL, 'Foobar' );
110  $blankpage = new TitleValue( NS_SPECIAL, 'Blankpage' );
111  $this->assertEquals(
112  '<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
113  . '(page does not exist)">foo</a>',
114  $linkRenderer->makeLink( $foobar, 'foo' )
115  );
116 
117  $this->assertEquals(
118  '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">blank</a>',
119  $linkRenderer->makeLink( $blankpage, 'blank' )
120  );
121 
122  $this->assertEquals(
123  '<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
124  . '(page does not exist)">&lt;script&gt;evil()&lt;/script&gt;</a>',
125  $linkRenderer->makeLink( $foobar, '<script>evil()</script>' )
126  );
127 
128  $this->assertEquals(
129  '<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
130  . '(page does not exist)"><script>evil()</script></a>',
131  $linkRenderer->makeLink( $foobar, new HtmlArmor( '<script>evil()</script>' ) )
132  );
133 
134  $this->assertEquals(
135  '<a href="#fragment">fragment</a>',
136  $linkRenderer->makeLink( Title::newFromText( '#fragment' ) )
137  );
138  }
139 
140  public function testGetLinkClasses() {
141  $wanCache = ObjectCache::getMainWANInstance();
142  $titleFormatter = MediaWikiServices::getInstance()->getTitleFormatter();
143  $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
144  $linkCache = new LinkCache( $titleFormatter, $wanCache, $nsInfo );
145  $foobarTitle = new TitleValue( NS_MAIN, 'FooBar' );
146  $redirectTitle = new TitleValue( NS_MAIN, 'Redirect' );
147  $userTitle = new TitleValue( NS_USER, 'Someuser' );
148  $linkCache->addGoodLinkObj(
149  1, // id
150  $foobarTitle,
151  10, // len
152  0 // redir
153  );
154  $linkCache->addGoodLinkObj(
155  2, // id
156  $redirectTitle,
157  10, // len
158  1 // redir
159  );
160 
161  $linkCache->addGoodLinkObj(
162  3, // id
163  $userTitle,
164  10, // len
165  0 // redir
166  );
167 
168  $linkRenderer = new LinkRenderer( $titleFormatter, $linkCache, $nsInfo );
169  $linkRenderer->setStubThreshold( 0 );
170  $this->assertEquals(
171  '',
172  $linkRenderer->getLinkClasses( $foobarTitle )
173  );
174 
175  $linkRenderer->setStubThreshold( 20 );
176  $this->assertEquals(
177  'stub',
178  $linkRenderer->getLinkClasses( $foobarTitle )
179  );
180 
181  $linkRenderer->setStubThreshold( 0 );
182  $this->assertEquals(
183  'mw-redirect',
184  $linkRenderer->getLinkClasses( $redirectTitle )
185  );
186 
187  $linkRenderer->setStubThreshold( 20 );
188  $this->assertEquals(
189  '',
190  $linkRenderer->getLinkClasses( $userTitle )
191  );
192  }
193 
194  function tearDown() {
196  parent::tearDown();
197  }
198 }
static getMainWANInstance()
Get the main WAN cache object.
static clearCaches()
Definition: Title.php:3146
const NS_MAIN
Definition: Defines.php:60
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:1982
const NS_SPECIAL
Definition: Defines.php:49
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
usually copyright or history_copyright This message must be in HTML not wikitext & $link
Definition: hooks.txt:3051
MediaWiki\Linker\LinkRenderer.
const PROTO_HTTPS
Definition: Defines.php:216
LinkRendererFactory $factory
Class that generates HTML links for pages.
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
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown...
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
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:319