mirror of
https://github.com/diocloid/LinkTitles.git
synced 2025-07-13 01:39:30 +02:00
Linker tests for templates.
This commit is contained in:
@ -1,5 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
|
* Unit tests for the Linker class, i.e. the core functionality
|
||||||
|
*
|
||||||
* Copyright 2012-2017 Daniel Kraus <bovender@bovender.de> ('bovender')
|
* Copyright 2012-2017 Daniel Kraus <bovender@bovender.de> ('bovender')
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* This program is free software; you can redistribute it and/or modify
|
||||||
@ -26,136 +28,206 @@
|
|||||||
* The test class is prefixed with 'LinkTitles' to avoid a naming collision
|
* The test class is prefixed with 'LinkTitles' to avoid a naming collision
|
||||||
* with a class that exists in the MediaWiki core.
|
* with a class that exists in the MediaWiki core.
|
||||||
*
|
*
|
||||||
* Ideally the test classes should be namespaced, but when you do that, they
|
* (Ideally the test classes should be namespaced, but when you do that, they
|
||||||
* will no longer be automatically discovered.
|
* will no longer be automatically discovered.)
|
||||||
*
|
*
|
||||||
* @group Database
|
* @group Database
|
||||||
*/
|
*/
|
||||||
class LinkTitlesLinkerTest extends LinkTitles\TestCase {
|
class LinkTitlesLinkerTest extends LinkTitles\TestCase {
|
||||||
protected $title;
|
protected $title;
|
||||||
|
|
||||||
protected function setUp() {
|
protected function setUp() {
|
||||||
$this->title = $this->insertPage( 'source page', 'This page is the test page' )['title'];
|
$this->title = $this->insertPage( 'source page', 'This page is the test page' )['title'];
|
||||||
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
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function addDBDataOnce() {
|
||||||
|
$this->insertPage( 'link target', 'This page serves as a link target' );
|
||||||
|
parent::addDBDataOnce(); // call parent after adding page to have targets invalidated
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @dataProvider provideLinkContentSmartModeData
|
|
||||||
*/
|
|
||||||
public function testLinkContentSmartMode( $capitalLinks, $smartMode, $input, $expectedOutput ) {
|
|
||||||
$this->setMwGlobals( 'wgCapitalLinks', $capitalLinks );
|
|
||||||
$config = new LinkTitles\Config();
|
|
||||||
$config->firstOnly = false;
|
|
||||||
$config->smartMode = $smartMode;
|
|
||||||
$linker = new LinkTitles\Linker( $config );
|
|
||||||
$this->assertSame( $expectedOutput, $linker->linkContent( $this->title, $input ));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function provideLinkContentSmartModeData() {
|
/**
|
||||||
return [
|
* @dataProvider provideLinkContentData
|
||||||
[
|
*/
|
||||||
true, // wgCapitalLinks
|
public function testLinkContentTemplates( $skipTemplates, $input, $expectedOutput ) {
|
||||||
true, // smartMode
|
$config = new LinkTitles\Config();
|
||||||
'With smart mode on and $wgCapitalLinks = true, this page should link to link target',
|
$config->firstOnly = false;
|
||||||
'With smart mode on and $wgCapitalLinks = true, this page should link to [[link target]]'
|
$config->skipTemplates = $skipTemplates;
|
||||||
],
|
$linker = new LinkTitles\Linker( $config );
|
||||||
[
|
$this->assertSame( $expectedOutput, $linker->linkContent( $this->title, $input ));
|
||||||
true, // wgCapitalLinks
|
}
|
||||||
false, // smartMode
|
|
||||||
'With smart mode off and $wgCapitalLinks = true, this page should link to link target',
|
|
||||||
'With smart mode off and $wgCapitalLinks = true, this page should link to [[link target]]'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
true, // wgCapitalLinks
|
|
||||||
true, // smartMode
|
|
||||||
'With smart mode on and $wgCapitalLinks = true, this page should link to Link Target',
|
|
||||||
'With smart mode on and $wgCapitalLinks = true, this page should link to [[Link target|Link Target]]'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
true, // wgCapitalLinks
|
|
||||||
false, // smartMode
|
|
||||||
'With smart mode off and $wgCapitalLinks = true, this page should not link to Link Target',
|
|
||||||
'With smart mode off and $wgCapitalLinks = true, this page should not link to Link Target'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
false, // wgCapitalLinks
|
|
||||||
true, // smartMode
|
|
||||||
'With smart mode on and $wgCapitalLinks = false, this page should link to Link target',
|
|
||||||
'With smart mode on and $wgCapitalLinks = false, this page should link to [[Link target]]'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
false, // wgCapitalLinks
|
|
||||||
true, // smartMode
|
|
||||||
'With smart mode on and $wgCapitalLinks = false, this page should link to link target',
|
|
||||||
'With smart mode on and $wgCapitalLinks = false, this page should link to [[Link target|link target]]'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
false, // wgCapitalLinks
|
|
||||||
false, // smartMode
|
|
||||||
'With smart mode off and $wgCapitalLinks = false, this page should not link to link target',
|
|
||||||
'With smart mode off and $wgCapitalLinks = false, this page should not link to link target'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
false, // wgCapitalLinks
|
|
||||||
false, // smartMode
|
|
||||||
'With smart mode off and $wgCapitalLinks = false, this page should not link to Link target',
|
|
||||||
'With smart mode off and $wgCapitalLinks = false, this page should not link to [[Link target]]'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
false, // wgCapitalLinks
|
|
||||||
true, // smartMode
|
|
||||||
'With smart mode on and $wgCapitalLinks = false, this page should link to Link Target',
|
|
||||||
'With smart mode on and $wgCapitalLinks = false, this page should link to [[Link target|Link Target]]'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
false, // wgCapitalLinks
|
|
||||||
false, // smartMode
|
|
||||||
'With smart mode off and $wgCapitalLinks = false, this page should not link to Link Target',
|
|
||||||
'With smart mode off and $wgCapitalLinks = false, this page should not link to Link Target'
|
|
||||||
],
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
public function provideLinkContentTemplatesData() {
|
||||||
* @dataProvider provideLinkContentFirstOnlyData
|
return [
|
||||||
*/
|
[
|
||||||
public function testLinkContentFirstOnly( $firstOnly, $input, $expectedOutput ) {
|
true, // skipTemplates
|
||||||
$config = new LinkTitles\Config();
|
'With skipTemplates = true, a {{template|with=link target}} in it should not be linked',
|
||||||
$config->firstOnly = $firstOnly;
|
'With skipTemplates = true, a {{template|with=link target}} in it should not be linked',
|
||||||
$linker = new LinkTitles\Linker( $config );
|
],
|
||||||
$this->assertSame( $expectedOutput, $linker->linkContent( $this->title, $input ));
|
[
|
||||||
}
|
false, // skipTemplates
|
||||||
|
'With skipTemplates = false, a {{template|with=link target}} in it should be linked',
|
||||||
|
'With skipTemplates = false, a {{template|with=[[link target]]}} in it should be linked',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
false, // skipTemplates
|
||||||
|
'With skipTemplates = false, a {{template|with=already linked [[link target]]}} in it should not be linked again',
|
||||||
|
'With skipTemplates = false, a {{template|with=already linked [[link target]]}} in it should not be linked again',
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
public function provideLinkContentFirstOnlyData() {
|
|
||||||
return [
|
|
||||||
[
|
|
||||||
false, // firstOnly
|
|
||||||
'With firstOnly = false, link target is a link target multiple times',
|
|
||||||
'With firstOnly = false, [[link target]] is a [[link target]] multiple times'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
false, // firstOnly
|
|
||||||
'With firstOnly = false, [[link target]] is a link target multiple times',
|
|
||||||
'With firstOnly = false, [[link target]] is a [[link target]] multiple times'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
true, // firstOnly
|
|
||||||
'With firstOnly = true, link target is a link target only once',
|
|
||||||
'With firstOnly = true, [[link target]] is a link target only once'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
true, // firstOnly
|
|
||||||
'With firstOnly = true, [[link target]] is a link target only once',
|
|
||||||
'With firstOnly = true, [[link target]] is a link target only once'
|
|
||||||
],
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testLinkContentBlackList() {
|
/**
|
||||||
$config = new LinkTitles\Config();
|
* @dataProvider provideLinkContentSmartModeData
|
||||||
$config->blackList = [ 'Foo', 'Link target', 'Bar' ];
|
*/
|
||||||
$linker = new LinkTitles\Linker( $config );
|
public function testLinkContentSmartMode( $capitalLinks, $smartMode, $input, $expectedOutput ) {
|
||||||
$text = 'If the link target is blacklisted, it should not be linked';
|
$this->setMwGlobals( 'wgCapitalLinks', $capitalLinks );
|
||||||
$this->assertSame( $text, $linker->linkContent( $this->title, $text ) );
|
$config = new LinkTitles\Config();
|
||||||
}
|
$config->firstOnly = false;
|
||||||
|
$config->smartMode = $smartMode;
|
||||||
|
$linker = new LinkTitles\Linker( $config );
|
||||||
|
$this->assertSame( $expectedOutput, $linker->linkContent( $this->title, $input ));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function provideLinkContentSmartModeData() {
|
||||||
|
return [
|
||||||
|
[
|
||||||
|
true, // wgCapitalLinks
|
||||||
|
true, // smartMode
|
||||||
|
'With smart mode on and $wgCapitalLinks = true, this page should link to link target',
|
||||||
|
'With smart mode on and $wgCapitalLinks = true, this page should link to [[link target]]'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
true, // wgCapitalLinks
|
||||||
|
false, // smartMode
|
||||||
|
'With smart mode off and $wgCapitalLinks = true, this page should link to link target',
|
||||||
|
'With smart mode off and $wgCapitalLinks = true, this page should link to [[link target]]'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
true, // wgCapitalLinks
|
||||||
|
true, // smartMode
|
||||||
|
'With smart mode on and $wgCapitalLinks = true, this page should link to Link Target',
|
||||||
|
'With smart mode on and $wgCapitalLinks = true, this page should link to [[Link target|Link Target]]'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
true, // wgCapitalLinks
|
||||||
|
false, // smartMode
|
||||||
|
'With smart mode off and $wgCapitalLinks = true, this page should not link to Link Target',
|
||||||
|
'With smart mode off and $wgCapitalLinks = true, this page should not link to Link Target'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
false, // wgCapitalLinks
|
||||||
|
true, // smartMode
|
||||||
|
'With smart mode on and $wgCapitalLinks = false, this page should link to Link target',
|
||||||
|
'With smart mode on and $wgCapitalLinks = false, this page should link to [[Link target]]'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
false, // wgCapitalLinks
|
||||||
|
true, // smartMode
|
||||||
|
'With smart mode on and $wgCapitalLinks = false, this page should link to link target',
|
||||||
|
'With smart mode on and $wgCapitalLinks = false, this page should link to [[Link target|link target]]'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
false, // wgCapitalLinks
|
||||||
|
false, // smartMode
|
||||||
|
'With smart mode off and $wgCapitalLinks = false, this page should not link to link target',
|
||||||
|
'With smart mode off and $wgCapitalLinks = false, this page should not link to link target'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
false, // wgCapitalLinks
|
||||||
|
false, // smartMode
|
||||||
|
'With smart mode off and $wgCapitalLinks = false, this page should not link to Link target',
|
||||||
|
'With smart mode off and $wgCapitalLinks = false, this page should not link to [[Link target]]'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
false, // wgCapitalLinks
|
||||||
|
true, // smartMode
|
||||||
|
'With smart mode on and $wgCapitalLinks = false, this page should link to Link Target',
|
||||||
|
'With smart mode on and $wgCapitalLinks = false, this page should link to [[Link target|Link Target]]'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
false, // wgCapitalLinks
|
||||||
|
false, // smartMode
|
||||||
|
'With smart mode off and $wgCapitalLinks = false, this page should not link to Link Target',
|
||||||
|
'With smart mode off and $wgCapitalLinks = false, this page should not link to Link Target'
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider provideLinkContentFirstOnlyData
|
||||||
|
*/
|
||||||
|
public function testLinkContentFirstOnly( $firstOnly, $input, $expectedOutput ) {
|
||||||
|
$config = new LinkTitles\Config();
|
||||||
|
$config->firstOnly = $firstOnly;
|
||||||
|
$linker = new LinkTitles\Linker( $config );
|
||||||
|
$this->assertSame( $expectedOutput, $linker->linkContent( $this->title, $input ));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function provideLinkContentFirstOnlyData() {
|
||||||
|
return [
|
||||||
|
[
|
||||||
|
false, // firstOnly
|
||||||
|
'With firstOnly = false, link target is a link target multiple times',
|
||||||
|
'With firstOnly = false, [[link target]] is a [[link target]] multiple times'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
false, // firstOnly
|
||||||
|
'With firstOnly = false, [[link target]] is a link target multiple times',
|
||||||
|
'With firstOnly = false, [[link target]] is a [[link target]] multiple times'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
true, // firstOnly
|
||||||
|
'With firstOnly = true, link target is a link target only once',
|
||||||
|
'With firstOnly = true, [[link target]] is a link target only once'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
true, // firstOnly
|
||||||
|
'With firstOnly = true, [[link target]] is a link target only once',
|
||||||
|
'With firstOnly = true, [[link target]] is a link target only once'
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testLinkContentBlackList() {
|
||||||
|
$config = new LinkTitles\Config();
|
||||||
|
$config->blackList = [ 'Foo', 'link target', 'Bar' ];
|
||||||
|
$linker = new LinkTitles\Linker( $config );
|
||||||
|
$text = 'If the link target is blacklisted, it should not be linked';
|
||||||
|
$this->assertSame( $text, $linker->linkContent( $this->title, $text ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider provideLinkContentNameSpacesData
|
||||||
|
*/
|
||||||
|
public function testLinkContentNameSpaces( $nameSpaces, $input, $expectedOutput ) {
|
||||||
|
$ns = 3000;
|
||||||
|
$this->setMwGlobals( [
|
||||||
|
"wgExtraNameSpaces[$ns]" => 'custom_namespace'
|
||||||
|
] );
|
||||||
|
$this->insertPage( 'in custom namespace', 'This is a page in a custom namespace', $ns );
|
||||||
|
$config = new LinkTitles\Config();
|
||||||
|
$config->nameSpaces = $nameSpaces;
|
||||||
|
$linker = new LinkTitles\Linker( $config );
|
||||||
|
$this->assertSame( $expectedOutput, $linker->linkContent( $this->title, $input ));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function provideLinkContentNameSpacesData() {
|
||||||
|
return [
|
||||||
|
[
|
||||||
|
[], // nameSpaces
|
||||||
|
'With nameSpaces = [], page in custom namespace should not be linked',
|
||||||
|
'With firstOnly = [], page in custom namespace should not be linked'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
[ 3000 ], // nameSpaces
|
||||||
|
'With nameSpaces = [], page in custom namespace should be linked',
|
||||||
|
'With firstOnly = [], page [[in custom namespace]] should be linked'
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user