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
20 * @author Daniel Kinzler
24 * @covers MediaWikiPageLinkRenderer
29 class MediaWikiPageLinkRendererTest
extends MediaWikiTestCase
{
31 protected function setUp() {
34 $this->setMwGlobals( array(
35 'wgContLang' => Language
::factory( 'en' ),
40 * Returns a mock GenderCache that will return "female" always.
44 private function getGenderCache() {
45 $genderCache = $this->getMockBuilder( 'GenderCache' )
46 ->disableOriginalConstructor()
49 $genderCache->expects( $this->any() )
50 ->method( 'getGenderOf' )
51 ->will( $this->returnValue( 'female' ) );
56 public static function provideGetPageUrl() {
59 new TitleValue( NS_MAIN
, 'Foo_Bar' ),
64 new TitleValue( NS_USER
, 'Hansi_Maier', 'stuff' ),
65 array( 'foo' => 'bar' ),
66 '/User:Hansi_Maier?foo=bar#stuff'
72 * @dataProvider provideGetPageUrl
74 public function testGetPageUrl( TitleValue
$title, $params, $url ) {
75 // NOTE: was of Feb 2014, MediaWikiPageLinkRenderer *ignores* the
76 // WikitextTitleFormatter we pass here, and relies on the Linker
77 // class for generating the link! This may break the test e.g.
78 // of Linker uses a different language for the namespace names.
80 $lang = Language
::factory( 'en' );
82 $formatter = new MediaWikiTitleCodec( $lang, $this->getGenderCache() );
83 $renderer = new MediaWikiPageLinkRenderer( $formatter, '/' );
84 $actual = $renderer->getPageUrl( $title, $params );
86 $this->assertEquals( $url, $actual );
89 public static function provideRenderHtmlLink() {
92 new TitleValue( NS_MAIN
, 'Foo_Bar' ),
94 '!<a .*href=".*?Foo_Bar.*?".*?>Foo Bar</a>!'
97 //NOTE: Linker doesn't include fragments in "broken" links
98 //NOTE: once this no longer uses Linker, we will get "2" instead of "User" for the namespace.
99 new TitleValue( NS_USER
, 'Hansi_Maier', 'stuff' ),
100 'Hansi Maier\'s Stuff',
101 '!<a .*href=".*?User:Hansi_Maier.*?>Hansi Maier\'s Stuff</a>!'
104 //NOTE: Linker doesn't include fragments in "broken" links
105 //NOTE: once this no longer uses Linker, we will get "2" instead of "User" for the namespace.
106 new TitleValue( NS_USER
, 'Hansi_Maier', 'stuff' ),
108 '!<a .*href=".*?User:Hansi_Maier.*?>User:Hansi Maier#stuff</a>!'
114 * @dataProvider provideRenderHtmlLink
116 public function testRenderHtmlLink( TitleValue
$title, $text, $pattern ) {
117 // NOTE: was of Feb 2014, MediaWikiPageLinkRenderer *ignores* the
118 // WikitextTitleFormatter we pass here, and relies on the Linker
119 // class for generating the link! This may break the test e.g.
120 // of Linker uses a different language for the namespace names.
122 $lang = Language
::factory( 'en' );
124 $formatter = new MediaWikiTitleCodec( $lang, $this->getGenderCache() );
125 $renderer = new MediaWikiPageLinkRenderer( $formatter );
126 $actual = $renderer->renderHtmlLink( $title, $text );
128 $this->assertRegExp( $pattern, $actual );
131 public static function provideRenderWikitextLink() {
134 new TitleValue( NS_MAIN
, 'Foo_Bar' ),
136 '[[:0:Foo Bar|Foo Bar]]'
139 new TitleValue( NS_USER
, 'Hansi_Maier', 'stuff' ),
140 'Hansi Maier\'s Stuff',
141 '[[:2:Hansi Maier#stuff|Hansi Maier's Stuff]]'
144 new TitleValue( NS_USER
, 'Hansi_Maier', 'stuff' ),
146 '[[:2:Hansi Maier#stuff|2:Hansi Maier#stuff]]'
152 * @dataProvider provideRenderWikitextLink
154 public function testRenderWikitextLink( TitleValue
$title, $text, $expected ) {
155 $formatter = $this->getMock( 'TitleFormatter' );
156 $formatter->expects( $this->any() )
157 ->method( 'getFullText' )
158 ->will( $this->returnCallback(
159 function ( TitleValue
$title ) {
160 return str_replace( '_', ' ', "$title" );
164 $renderer = new MediaWikiPageLinkRenderer( $formatter, '/' );
165 $actual = $renderer->renderWikitextLink( $title, $text );
167 $this->assertEquals( $expected, $actual );