MediaWiki  1.28.1
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  $wanCache = ObjectCache::getMainWANInstance();
139  $titleFormatter = MediaWikiServices::getInstance()->getTitleFormatter();
140  $linkCache = new LinkCache( $titleFormatter, $wanCache );
141  $foobarTitle = new TitleValue( NS_MAIN, 'FooBar' );
142  $redirectTitle = new TitleValue( NS_MAIN, 'Redirect' );
143  $userTitle = new TitleValue( NS_USER, 'Someuser' );
144  $linkCache->addGoodLinkObj(
145  1, // id
146  $foobarTitle,
147  10, // len
148  0 // redir
149  );
150  $linkCache->addGoodLinkObj(
151  2, // id
152  $redirectTitle,
153  10, // len
154  1 // redir
155  );
156 
157  $linkCache->addGoodLinkObj(
158  3, // id
159  $userTitle,
160  10, // len
161  0 // redir
162  );
163 
164  $linkRenderer = new LinkRenderer( $titleFormatter, $linkCache );
165  $linkRenderer->setStubThreshold( 0 );
166  $this->assertEquals(
167  '',
168  $linkRenderer->getLinkClasses( $foobarTitle )
169  );
170 
171  $linkRenderer->setStubThreshold( 20 );
172  $this->assertEquals(
173  'stub',
174  $linkRenderer->getLinkClasses( $foobarTitle )
175  );
176 
177  $linkRenderer->setStubThreshold( 0 );
178  $this->assertEquals(
179  'mw-redirect',
180  $linkRenderer->getLinkClasses( $redirectTitle )
181  );
182 
183  $linkRenderer->setStubThreshold( 20 );
184  $this->assertEquals(
185  '',
186  $linkRenderer->getLinkClasses( $userTitle )
187  );
188  }
189 
190 }
this hook is for auditing only RecentChangesLinked and Watchlist $special
Definition: hooks.txt:1007
static getMainWANInstance()
Get the main WAN cache object.
const NS_MAIN
Definition: Defines.php:56
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:1936
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:36
const NS_SPECIAL
Definition: Defines.php:45
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:2889
MediaWiki\Linker\LinkRenderer.
const PROTO_HTTPS
Definition: Defines.php:224
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
setMwGlobals($pairs, $value=null)