Fix tests.

This commit is contained in:
Daniel Kraus
2021-03-30 21:09:54 +02:00
parent 767df5c4bf
commit 5c3dac9dc3
4 changed files with 48 additions and 35 deletions

View File

@ -37,7 +37,8 @@
class LinkTitlesLinkerTest extends LinkTitles\TestCase { class LinkTitlesLinkerTest extends LinkTitles\TestCase {
protected $title; protected $title;
protected function setUp() { protected function setUp(): void
{
parent::setUp(); // call last to have the Targets object invalidated after inserting the page parent::setUp(); // call last to have the Targets object invalidated after inserting the page
} }
@ -315,13 +316,13 @@ class LinkTitlesLinkerTest extends LinkTitles\TestCase {
// Reset namespace caches. // Reset namespace caches.
// See https://stackoverflow.com/q/45974979/270712 // See https://stackoverflow.com/q/45974979/270712
MWNamespace::getCanonicalNamespaces( true ); \MWNamespace::getCanonicalNamespaces(true);
global $wgContLang; global $wgContLang;
$wgContLang->resetNamespaces(); $wgContLang->resetNamespaces();
$this->assertTrue( MWNamespace::exists( $ns ), "The namespace with id $ns should exist!" ); $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 ); $this->insertPage( "in custom namespace", 'This is a page in a custom namespace', $ns );
LinKTitles\Targets::invalidate(); LinkTitles\Targets::invalidate();
$linker = new LinkTitles\Linker( $config ); $linker = new LinkTitles\Linker( $config );
$source = LinkTitles\Source::createFromTitleAndText( $this->title, $input, $config ); $source = LinkTitles\Source::createFromTitleAndText( $this->title, $input, $config );
$result = $linker->linkContent( $source ); $result = $linker->linkContent( $source );

View File

@ -1,4 +1,5 @@
<?php <?php
/** /**
* Copyright 2012-2018 Daniel Kraus <bovender@bovender.de> ('bovender') * Copyright 2012-2018 Daniel Kraus <bovender@bovender.de> ('bovender')
* *
@ -25,78 +26,81 @@
* *
* @group bovender * @group bovender
*/ */
class SplitterTest extends MediaWikiTestCase { class SplitterTest extends \MediaWikiTestCase
{
/** /**
* @dataProvider provideSplitData * @dataProvider provideSplitData
*/ */
public function testSplit( $skipTemplates, $parseHeadings, $input, $expectedOutput ) { public function testSplit($skipTemplates, $parseHeadings, $input, $expectedOutput)
{
$config = new LinkTitles\Config(); $config = new LinkTitles\Config();
$config->skipTemplates = $skipTemplates; $config->skipTemplates = $skipTemplates;
$config->parseHeadings = $parseHeadings; $config->parseHeadings = $parseHeadings;
LinkTitles\Splitter::invalidate(); LinkTitles\Splitter::invalidate();
$splitter = LinkTitles\Splitter::singleton( $config ); $splitter = LinkTitles\Splitter::singleton($config);
$this->assertSame( $skipTemplates, $splitter->config->skipTemplates, 'Splitter has incorrect skipTemplates config'); $this->assertSame($skipTemplates, $splitter->config->skipTemplates, 'Splitter has incorrect skipTemplates config');
$this->assertSame( $parseHeadings, $splitter->config->parseHeadings, 'Splitter has incorrect parseHeadings config'); $this->assertSame($parseHeadings, $splitter->config->parseHeadings, 'Splitter has incorrect parseHeadings config');
$this->assertSame( $expectedOutput, $splitter->split( $input ) ); $this->assertSame($expectedOutput, $splitter->split($input));
} }
// TODO: Add more examples. // TODO: Add more examples.
public static function provideSplitData() { public static function provideSplitData()
{
return [ return [
[ [
true, // skipTemplates true, // skipTemplates
false, // parseHeadings false, // parseHeadings
'this may be linked [[this may not be linked]]', 'this may be linked [[this may not be linked]]',
[ 'this may be linked ', '[[this may not be linked]]', '' ] ['this may be linked ', '[[this may not be linked]]', '']
], ],
[ [
true, // skipTemplates true, // skipTemplates
false, // parseHeadings false, // parseHeadings
'this may be linked <gallery>this may not be linked</gallery>', 'this may be linked <gallery>this may not be linked</gallery>',
[ 'this may be linked ', '<gallery>this may not be linked</gallery>', '' ] ['this may be linked ', '<gallery>this may not be linked</gallery>', '']
], ],
[ [
true, // skipTemplates true, // skipTemplates
false, // parseHeadings false, // parseHeadings
'With skipTemplates = true, this may be linked {{mytemplate|param=link target}}', 'With skipTemplates = true, this may be linked {{mytemplate|param=link target}}',
[ 'With skipTemplates = true, this may be linked ', '{{mytemplate|param=link target}}', '' ] ['With skipTemplates = true, this may be linked ', '{{mytemplate|param=link target}}', '']
], ],
[ [
false, // skipTemplates false, // skipTemplates
false, // parseHeadings false, // parseHeadings
'With skipTemplates = false, this may be linked {{mytemplate|param=link target}}', 'With skipTemplates = false, this may be linked {{mytemplate|param=link target}}',
[ 'With skipTemplates = false, this may be linked ', '{{mytemplate|param=', 'link target}}' ] ['With skipTemplates = false, this may be linked ', '{{mytemplate|param=', 'link target}}']
], ],
[ [
true, // skipTemplates true, // skipTemplates
false, // parseHeadings false, // parseHeadings
'With skipTemplates = true, this may be linked {{mytemplate|param={{transcluded}}}}', 'With skipTemplates = true, this may be linked {{mytemplate|param={{transcluded}}}}',
[ 'With skipTemplates = true, this may be linked ', '{{mytemplate|param={{transcluded}}}}', '' ] ['With skipTemplates = true, this may be linked ', '{{mytemplate|param={{transcluded}}}}', '']
], ],
[ [
true, // skipTemplates true, // skipTemplates
true, // parseHeadings true, // parseHeadings
"With parseHeadings = true,\n==a heading may be linked==\n", "With parseHeadings = true,\n==a heading may be linked==\n",
[ "With parseHeadings = true,\n==a heading may be linked==\n" ] ["With parseHeadings = true,\n==a heading may be linked==\n"]
], ],
[ [
true, // skipTemplates true, // skipTemplates
false, // parseHeadings false, // parseHeadings
// no trailing newline in the following string because it would be swallowed // no trailing newline in the following string because it would be swallowed
"With parseHeadings = false,\n==a heading may not be linked==", "With parseHeadings = false,\n==a heading may not be linked==",
[ "With parseHeadings = false,\n", "==a heading may not be linked==", '' ] ["With parseHeadings = false,\n", "==a heading may not be linked==", '']
], ],
[ [
true, // skipTemplates true, // skipTemplates
true, // parseHeadings true, // parseHeadings
"With parseHeadings = true,\n==<span>a heading with spans may be linked</span>==\n", "With parseHeadings = true,\n==<span>a heading with spans may be linked</span>==\n",
[ "With parseHeadings = true,\n==", "<span>", "a heading with spans may be linked", "</span>", "==\n" ] ["With parseHeadings = true,\n==", "<span>", "a heading with spans may be linked", "</span>", "==\n"]
], ],
[ [
true, // skipTemplates true, // skipTemplates
true, // parseHeadings true, // parseHeadings
"With parseHeadings = true,\n==<div>a heading with divs may be linked</div>==\n", "With parseHeadings = true,\n==<div>a heading with divs may be linked</div>==\n",
[ "With parseHeadings = true,\n==", "<div>", "a heading with divs may be linked", "</div>", "==\n" ] ["With parseHeadings = true,\n==", "<div>", "a heading with divs may be linked", "</div>", "==\n"]
], ],
// Improperly formatted headings cannot be dealt with appropriately for now // Improperly formatted headings cannot be dealt with appropriately for now
// [ // [
@ -109,7 +113,7 @@ class SplitterTest extends MediaWikiTestCase {
true, // skipTemplates true, // skipTemplates
true, // parseHeadings true, // parseHeadings
"Text <noautolinks>in noautolinks tag</noautolinks>should be excluded", "Text <noautolinks>in noautolinks tag</noautolinks>should be excluded",
[ "Text ", "<noautolinks>in noautolinks tag</noautolinks>", "should be excluded" ] ["Text ", "<noautolinks>in noautolinks tag</noautolinks>", "should be excluded"]
], ],
]; ];
} }

View File

@ -1,4 +1,5 @@
<?php <?php
/** /**
* Copyright 2012-2018 Daniel Kraus <bovender@bovender.de> ('bovender') * Copyright 2012-2018 Daniel Kraus <bovender@bovender.de> ('bovender')
* *
@ -23,39 +24,44 @@
/** /**
* @group bovender * @group bovender
*/ */
class TargetTest extends MediaWikiTestCase { class TargetTest extends \MediaWikiTestCase
{
/** /**
* @dataProvider provideStartOnly * @dataProvider provideStartOnly
*/ */
public function testTargetWordStartOnly( $enabled, $delimiter ) { public function testTargetWordStartOnly($enabled, $delimiter)
{
$config = new LinkTitles\Config(); $config = new LinkTitles\Config();
$config->wordStartOnly = $enabled; $config->wordStartOnly = $enabled;
$target = new LinKTitles\Target( NS_MAIN, 'test page', $config ); $target = new LinKTitles\Target(NS_MAIN, 'test page', $config);
$this->assertSame( $delimiter, $target->wordStart ); $this->assertSame($delimiter, $target->wordStart);
} }
public static function provideStartOnly() { public static function provideStartOnly()
{
return [ return [
[ true, '(?<!\pL|\pN)' ], [true, '(?<!\pL|\pN)'],
[ false, '' ] [false, '']
]; ];
} }
/** /**
* @dataProvider provideEndOnly * @dataProvider provideEndOnly
*/ */
public function testTargetWordEndOnly( $enabled, $delimiter ) { public function testTargetWordEndOnly($enabled, $delimiter)
{
$config = new LinkTitles\Config(); $config = new LinkTitles\Config();
$config->wordEndOnly = $enabled; $config->wordEndOnly = $enabled;
$target = new LinKTitles\Target( NS_MAIN, 'test page', $config ); $target = new LinKTitles\Target(NS_MAIN, 'test page', $config);
$this->assertSame( $delimiter, $target->wordEnd ); $this->assertSame($delimiter, $target->wordEnd);
} }
public static function provideEndOnly() { public static function provideEndOnly()
{
return [ return [
[ true, '(?!\pL|\pN)' ], [true, '(?!\pL|\pN)'],
[ false, '' ] [false, '']
]; ];
} }
} }

View File

@ -22,11 +22,13 @@
namespace LinkTitles; namespace LinkTitles;
abstract class TestCase extends \MediaWikiTestCase { abstract class TestCase extends \MediaWikiTestCase {
protected function setUp() { protected function setUp(): void
{
parent::setUp(); parent::setUp();
} }
protected function tearDown() { protected function tearDown(): void
{
parent::tearDown(); parent::tearDown();
} }