From e66d8905f81e87caa2b53cb3c534bc864cc2abf3 Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Wed, 11 May 2016 15:50:29 -0700 Subject: [PATCH] Add tests for 'LinkBegin' hook Change-Id: I663c25377fb899ea97ef959efac82dc836a9940a --- tests/phpunit/includes/LinkerTest.php | 70 +++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/tests/phpunit/includes/LinkerTest.php b/tests/phpunit/includes/LinkerTest.php index 1372d8513b..d701a81c8d 100644 --- a/tests/phpunit/includes/LinkerTest.php +++ b/tests/phpunit/includes/LinkerTest.php @@ -310,6 +310,76 @@ class LinkerTest extends MediaWikiLangTestCase { // @codingStandardsIgnoreEnd } + public static function provideLinkBeginHook() { + // @codingStandardsIgnoreStart Generic.Files.LineLength + return [ + // Modify $html + [ + function( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) { + $html = 'foobar'; + }, + 'foobar' + ], + // Modify $attribs + [ + function( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) { + $attribs['bar'] = 'baz'; + }, + 'Special:BlankPage' + ], + // Modify $query + [ + function( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) { + $query['bar'] = 'baz'; + }, + 'Special:BlankPage' + ], + // Force HTTP $options + [ + function( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) { + $options = [ 'http' ]; + }, + 'Special:BlankPage' + ], + // Force 'forcearticlepath' in $options + [ + function( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) { + $options = [ 'forcearticlepath' ]; + $query['foo'] = 'bar'; + }, + 'Special:BlankPage' + ], + // Abort early + [ + function( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) { + $ret = 'foobar'; + return false; + }, + 'foobar' + ], + ]; + // @codingStandardsIgnoreEnd + } + + /** + * @dataProvider provideLinkBeginHook + */ + public function testLinkBeginHook( $callback, $expected ) { + $this->setMwGlobals( [ + 'wgArticlePath' => '/wiki/$1', + 'wgWellFormedXml' => true, + 'wgServer' => '//example.org', + 'wgCanonicalServer' => 'http://example.org', + 'wgScriptPath' => '/w', + 'wgScript' => '/w/index.php', + ] ); + + $this->setMwGlobals( 'wgHooks', [ 'LinkBegin' => [ $callback ] ] ); + $title = SpecialPage::getTitleFor( 'Blankpage' ); + $out = Linker::link( $title ); + $this->assertEquals( $expected, $out ); + } + public static function provideLinkEndHook() { return [ // Override $html -- 2.20.1