3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
19 * @author Daniel Kinzler
23 * @covers MediaWikiPageLinkRenderer
28 class MediaWikiPageLinkRendererTest
extends MediaWikiTestCase
{
30 protected function setUp() {
33 $this->setMwGlobals( array(
34 'wgContLang' => Language
::factory( 'en' ),
39 * Returns a mock GenderCache that will return "female" always.
43 private function getGenderCache() {
44 $genderCache = $this->getMockBuilder( 'GenderCache' )
45 ->disableOriginalConstructor()
48 $genderCache->expects( $this->any() )
49 ->method( 'getGenderOf' )
50 ->will( $this->returnValue( 'female' ) );
55 public static function provideGetPageUrl() {
58 new TitleValue( NS_MAIN
, 'Foo_Bar' ),
63 new TitleValue( NS_USER
, 'Hansi_Maier', 'stuff' ),
64 array( 'foo' => 'bar' ),
65 '/User:Hansi_Maier?foo=bar#stuff'
71 * @dataProvider provideGetPageUrl
73 public function testGetPageUrl( TitleValue
$title, $params, $url ) {
74 // NOTE: was of Feb 2014, MediaWikiPageLinkRenderer *ignores* the
75 // WikitextTitleFormatter we pass here, and relies on the Linker
76 // class for generating the link! This may break the test e.g.
77 // of Linker uses a different language for the namespace names.
79 $lang = Language
::factory( 'en' );
81 $formatter = new MediaWikiTitleCodec( $lang, $this->getGenderCache() );
82 $renderer = new MediaWikiPageLinkRenderer( $formatter, '/' );
83 $actual = $renderer->getPageUrl( $title, $params );
85 $this->assertEquals( $url, $actual );
88 public static function provideRenderHtmlLink() {
91 new TitleValue( NS_MAIN
, 'Foo_Bar' ),
93 '!<a .*href=".*?Foo_Bar.*?".*?>Foo Bar</a>!'
96 // NOTE: Linker doesn't include fragments in "broken" links
97 // NOTE: once this no longer uses Linker, we will get "2" instead of "User" for the namespace.
98 new TitleValue( NS_USER
, 'Hansi_Maier', 'stuff' ),
99 'Hansi Maier\'s Stuff',
100 '!<a .*href=".*?User:Hansi_Maier.*?>Hansi Maier\'s Stuff</a>!'
103 // NOTE: Linker doesn't include fragments in "broken" links
104 // NOTE: once this no longer uses Linker, we will get "2" instead of "User" for the namespace.
105 new TitleValue( NS_USER
, 'Hansi_Maier', 'stuff' ),
107 '!<a .*href=".*?User:Hansi_Maier.*?>User:Hansi Maier#stuff</a>!'
113 * @dataProvider provideRenderHtmlLink
115 public function testRenderHtmlLink( TitleValue
$title, $text, $pattern ) {
116 // NOTE: was of Feb 2014, MediaWikiPageLinkRenderer *ignores* the
117 // WikitextTitleFormatter we pass here, and relies on the Linker
118 // class for generating the link! This may break the test e.g.
119 // of Linker uses a different language for the namespace names.
121 $lang = Language
::factory( 'en' );
123 $formatter = new MediaWikiTitleCodec( $lang, $this->getGenderCache() );
124 $renderer = new MediaWikiPageLinkRenderer( $formatter );
125 $actual = $renderer->renderHtmlLink( $title, $text );
127 $this->assertRegExp( $pattern, $actual );
130 public static function provideRenderWikitextLink() {
133 new TitleValue( NS_MAIN
, 'Foo_Bar' ),
135 '[[:0:Foo Bar|Foo Bar]]'
138 new TitleValue( NS_USER
, 'Hansi_Maier', 'stuff' ),
139 'Hansi Maier\'s Stuff',
140 '[[:2:Hansi Maier#stuff|Hansi Maier's Stuff]]'
143 new TitleValue( NS_USER
, 'Hansi_Maier', 'stuff' ),
145 '[[:2:Hansi Maier#stuff|2:Hansi Maier#stuff]]'
151 * @dataProvider provideRenderWikitextLink
153 public function testRenderWikitextLink( TitleValue
$title, $text, $expected ) {
154 $formatter = $this->getMock( 'TitleFormatter' );
155 $formatter->expects( $this->any() )
156 ->method( 'getFullText' )
157 ->will( $this->returnCallback(
158 function ( TitleValue
$title ) {
159 return str_replace( '_', ' ', "$title" );
163 $renderer = new MediaWikiPageLinkRenderer( $formatter, '/' );
164 $actual = $renderer->renderWikitextLink( $title, $text );
166 $this->assertEquals( $expected, $actual );