diff --git a/tests/phpunit/LinkerTest.php b/tests/phpunit/LinkerTest.php index 6f9c4bc..dcade3f 100644 --- a/tests/phpunit/LinkerTest.php +++ b/tests/phpunit/LinkerTest.php @@ -41,7 +41,6 @@ class LinkTitlesLinkerTest extends LinkTitles\TestCase { parent::setUp(); // call last to have the Targets object invalidated after inserting the page } - public function addDBData() { $this->title = $this->insertPage( 'source page', 'This page is the test page' )['title']; $this->insertPage( 'link target', 'This page serves as a link target' ); @@ -83,7 +82,6 @@ class LinkTitlesLinkerTest extends LinkTitles\TestCase { ]; } - /** * @dataProvider provideLinkContentSmartModeData */ @@ -217,40 +215,45 @@ class LinkTitlesLinkerTest extends LinkTitles\TestCase { // Tests for namespace handling are commented out until I find a way to add // a custom namespace during testing. (The assertTrue assertion below fails.) - // /** - // * @dataProvider provideLinkContentNamespacesData - // */ - // public function testLinkContentNamespaces( $namespaces, $input, $expectedOutput ) { - // $ns = 4000; - // $this->setMwGlobals( [ - // "wgExtraNamespaces[$ns]" => 'custom_namespace' - // ] ); - // // global $wgExtraNamespaces; - // // global $wgContentNamespaces; - // // $wgContentNamespaces[] = $ns; - // // $wgExtraNamespaces[$ns] = 'custom_adsf'; - // $this->insertPage( 'in custom namespace', 'This is a page in a custom namespace', $ns ); - // $this->assertTrue( MWNamespace::exists( $ns ), "The name space with id $ns should exist!" ); - // LinKTitles\Targets::invalidate(); - // $config = new LinkTitles\Config(); - // $config->namespaces = $namespaces; - // $linker = new LinkTitles\Linker( $config ); - // $this->assertSame( $expectedOutput, $linker->linkContent( $this->title, $input )); - // } + /** + * @dataProvider provideLinkContentNamespacesData + */ + public function testLinkContentTargetNamespaces( $namespaces, $input, $expectedOutput ) { + $config = new LinkTitles\Config(); + $config->targetNamespaces = $namespaces; - // public function provideLinkContentNamespacesData() { - // return [ - // [ - // [], // namespaces - // 'With namespaces = [], page in custom namespace should not be linked', - // 'With namespaces = [], page in custom namespace should not be linked' - // ], - // [ - // [ 4000 ], // namespaces - // 'With namespaces = [ 4000 ], page in custom namespace should be linked', - // 'With namespaces = [ 4000 ], page [[custom_namespace:in custom namespace]] should be linked' - // ], - // ]; - // } + $ns = 4000; + $nsText = 'customnamespace'; + $this->mergeMwGlobalArrayValue( 'wgExtraNamespaces', [ $ns => $nsText ] ); + // Reset namespace caches. + // See https://stackoverflow.com/q/45974979/270712 + MWNamespace::getCanonicalNamespaces( true ); + global $wgContLang; + $wgContLang->resetNamespaces(); + $this->assertTrue( MWNamespace::exists( $ns ), "The namespace with id $ns should exist!" ); + + $this->insertPage( "in custom namespace", 'This is a page in a custom namespace', $ns ); + LinKTitles\Targets::invalidate(); + $linker = new LinkTitles\Linker( $config ); + $source = LinkTitles\Source::createFromTitleAndText( $this->title, $input, $config ); + $result = $linker->linkContent( $source ); + if ( !$result ) { $result = $input; } + $this->assertSame( $expectedOutput, $result ); + } + + public function provideLinkContentNamespacesData() { + return [ + [ + [], // namespaces + 'With targetNamespaces = [], page in custom namespace should not be linked', + 'With targetNamespaces = [], page in custom namespace should not be linked' + ], + [ + [ 4000 ], // namespaces + 'With targetNamespaces = [ 4000 ], page in custom namespace should be linked', + 'With targetNamespaces = [ 4000 ], page [[customnamespace:In custom namespace|in custom namespace]] should be linked' + ], + ]; + } } diff --git a/tests/phpunit/TargetsTest.php b/tests/phpunit/TargetsTest.php index 351a0b2..a42397a 100644 --- a/tests/phpunit/TargetsTest.php +++ b/tests/phpunit/TargetsTest.php @@ -28,13 +28,15 @@ */ class TargetsTest extends LinkTitles\TestCase { - /** - * This test asserts that the list of potential link targets is 0 - * @return [type] [description] - */ public function testTargets() { + $config = new LinkTitles\Config(); + // Include the custom namespace with index 4000 in the count. This is a + // very ugly hack. If the custom namespace index in + // LinkTitlesLinkerTest::testLinkContentTargetNamespaces() is every changed, + // this test will fail. + $config->targetNamespaces = [ 4000 ]; $title = \Title::newFromText( 'link target' ); - $targets = LinkTitles\Targets::singleton( $title, new LinkTitles\Config() ); + $targets = LinkTitles\Targets::singleton( $title, $config ); // Count number of articles: Inspired by updateArticleCount.php maintenance // script: https://doc.wikimedia.org/mediawiki-core/master/php/updateArticleCount_8php_source.html