MediaWiki  1.27.1
MediaWikiPageLinkRendererTest.php
Go to the documentation of this file.
1 <?php
29 
30  protected function setUp() {
31  parent::setUp();
32 
33  $this->setMwGlobals( [
34  'wgContLang' => Language::factory( 'en' ),
35  ] );
36  }
37 
43  private function getGenderCache() {
44  $genderCache = $this->getMockBuilder( 'GenderCache' )
45  ->disableOriginalConstructor()
46  ->getMock();
47 
48  $genderCache->expects( $this->any() )
49  ->method( 'getGenderOf' )
50  ->will( $this->returnValue( 'female' ) );
51 
52  return $genderCache;
53  }
54 
55  public static function provideGetPageUrl() {
56  return [
57  [
58  new TitleValue( NS_MAIN, 'Foo_Bar' ),
59  [],
60  '/Foo_Bar'
61  ],
62  [
63  new TitleValue( NS_USER, 'Hansi_Maier', 'stuff' ),
64  [ 'foo' => 'bar' ],
65  '/User:Hansi_Maier?foo=bar#stuff'
66  ],
67  ];
68  }
69 
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.
78 
79  $lang = Language::factory( 'en' );
80 
81  $formatter = new MediaWikiTitleCodec( $lang, $this->getGenderCache() );
82  $renderer = new MediaWikiPageLinkRenderer( $formatter, '/' );
83  $actual = $renderer->getPageUrl( $title, $params );
84 
85  $this->assertEquals( $url, $actual );
86  }
87 
88  public static function provideRenderHtmlLink() {
89  return [
90  [
91  new TitleValue( NS_MAIN, 'Foo_Bar' ),
92  'Foo Bar',
93  '!<a .*href=".*?Foo_Bar.*?".*?>Foo Bar</a>!'
94  ],
95  [
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>!'
101  ],
102  [
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' ),
106  null,
107  '!<a .*href=".*?User:Hansi_Maier.*?>User:Hansi Maier#stuff</a>!'
108  ],
109  ];
110  }
111 
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.
120 
121  $lang = Language::factory( 'en' );
122 
123  $formatter = new MediaWikiTitleCodec( $lang, $this->getGenderCache() );
124  $renderer = new MediaWikiPageLinkRenderer( $formatter );
125  $actual = $renderer->renderHtmlLink( $title, $text );
126 
127  $this->assertRegExp( $pattern, $actual );
128  }
129 
130  public static function provideRenderWikitextLink() {
131  return [
132  [
133  new TitleValue( NS_MAIN, 'Foo_Bar' ),
134  'Foo Bar',
135  '[[:0:Foo Bar|Foo Bar]]'
136  ],
137  [
138  new TitleValue( NS_USER, 'Hansi_Maier', 'stuff' ),
139  'Hansi Maier\'s Stuff',
140  '[[:2:Hansi Maier#stuff|Hansi Maier&#39;s Stuff]]'
141  ],
142  [
143  new TitleValue( NS_USER, 'Hansi_Maier', 'stuff' ),
144  null,
145  '[[:2:Hansi Maier#stuff|2:Hansi Maier#stuff]]'
146  ],
147  ];
148  }
149 
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" );
160  }
161  ) );
162 
163  $renderer = new MediaWikiPageLinkRenderer( $formatter, '/' );
164  $actual = $renderer->renderWikitextLink( $title, $text );
165 
166  $this->assertEquals( $expected, $actual );
167  }
168 }
A codec for MediaWiki page titles.
const NS_MAIN
Definition: Defines.php:69
testRenderWikitextLink(TitleValue $title, $text, $expected)
provideRenderWikitextLink
A service for generating links from page titles.
if(!isset($args[0])) $lang
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:36
getGenderCache()
Returns a mock GenderCache that will return "female" always.
$params
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:912
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
testGetPageUrl(TitleValue $title, $params, $url)
provideGetPageUrl
testRenderHtmlLink(TitleValue $title, $text, $pattern)
provideRenderHtmlLink
static factory($code)
Get a cached or new language object for a given language code.
Definition: Language.php:179
setMwGlobals($pairs, $value=null)