wiki.techinc.nl/tests/phpunit/includes/linker/LinkRendererFactoryTest.php

80 lines
1.8 KiB
PHP
Raw Normal View History

Add LinkRenderer (rewrite of Linker::link()) This is a rewrite of Linker::link() to a non-static, LinkTarget-based interface. Users of plain Linker::link() with no options can use the LinkRenderer instance provided by MediaWikiServices. Others that have specific options should create and configure their own instance, which can be used to create as many links as necessary. The main entrypoints for making links are: * ->makeLink( $target, $text, $attribs, $query ); * ->makeKnownLink( $target, $text, $attribs, $query ); * ->makeBrokenLink( $target, $text, $attribs, $query ); The order of the parameters are the same as Linker::link(), except $options are now part of the LinkRenderer instance, and known/broken status requires calling the function explicitly. Additionally, instead of passing in raw $html for the link text, the $text parameter will automatically be escaped unless it is specially marked as safe HTML using the MediaWiki\Linker\HtmlArmor class. The LinkBegin and LinkEnd hooks are now deprecated, but still function for backwards-compatability. Clients should migrate to the nearly- equivalent LinkRendererBegin and LinkRendererEnd hooks. The main differences between the hooks are: * Passing HtmlPageLinkRenderer object instead of deprecated DummyLinker * Using LinkTarget instead of Title * Begin hook can no longer change known/broken status of link. Use the TitleIsAlwaysKnown hook for that. * $options are no longer passed, they can be read (but shouldn't be modified!) from the LinkRenderer object. Bug: T469 Change-Id: I057cc86ae6404a080aa3c8e0e956ecbb10a897d5
2016-04-21 20:13:21 +00:00
<?php
use MediaWiki\Linker\LinkRenderer;
use MediaWiki\Linker\LinkRendererFactory;
use MediaWiki\MediaWikiServices;
/**
* @covers LinkRendererFactory
*/
class LinkRendererFactoryTest extends MediaWikiLangTestCase {
/**
* @var TitleFormatter
*/
private $titleFormatter;
public function setUp() {
parent::setUp();
$this->titleFormatter = MediaWikiServices::getInstance()->getTitleFormatter();
}
public static function provideCreateFromLegacyOptions() {
return [
[
[ 'noclasses' ],
'getNoClasses',
true
],
[
[ 'forcearticlepath' ],
'getForceArticlePath',
true
],
[
[ 'http' ],
'getExpandURLs',
PROTO_HTTP
],
[
[ 'https' ],
'getExpandURLs',
PROTO_HTTPS
],
[
[ 'stubThreshold' => 150 ],
'getStubThreshold',
150
],
];
}
/**
* @dataProvider provideCreateFromLegacyOptions
*/
public function testCreateFromLegacyOptions( $options, $func, $val ) {
$factory = new LinkRendererFactory( $this->titleFormatter );
$linkRenderer = $factory->createFromLegacyOptions(
$options
);
$this->assertInstanceOf( LinkRenderer::class, $linkRenderer );
$this->assertEquals( $val, $linkRenderer->$func(), $func );
}
public function testCreate() {
$factory = new LinkRendererFactory( $this->titleFormatter );
$this->assertInstanceOf( LinkRenderer::class, $factory->create() );
}
public function testCreateForUser() {
$user = $this->getMock( User::class, [ 'getStubThreshold' ] );
$user->expects( $this->once() )
->method( 'getStubThreshold' )
->willReturn( 15 );
$factory = new LinkRendererFactory( $this->titleFormatter );
$linkRenderer = $factory->createForUser( $user );
$this->assertInstanceOf( LinkRenderer::class, $linkRenderer );
$this->assertEquals( 15, $linkRenderer->getStubThreshold() );
}
}