Skip to content

Commit

Permalink
Item13590: Update ExternalLinkPlugin
Browse files Browse the repository at this point in the history
 - Remove some unused code
 - Add unit tests
 - Fix one bug exposed by the unit tests in url calculation
  • Loading branch information
gac410 committed Aug 1, 2015
1 parent 2e482f1 commit 1e5be1e
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 21 deletions.
23 changes: 8 additions & 15 deletions lib/Foswiki/Plugins/ExternalLinkPlugin.pm
Expand Up @@ -18,8 +18,8 @@

package Foswiki::Plugins::ExternalLinkPlugin;

our $VERSION = '$Rev$';
our $RELEASE = '1.21';
our $VERSION = '1.30';
our $RELEASE = '1.30';
our $NO_PREFS_IN_TOPIC = 1;
our $SHORTDESCRIPTION = 'Adds a visual indicator to outgoing links';
our $pluginName = 'ExternalLinkPlugin'; # Name of this Plugin
Expand All @@ -42,7 +42,6 @@ sub initPlugin {
$addedToHead = 0;
addStyleToHead();

# Plugin correctly initialized
Foswiki::Func::writeDebug(
"- Foswiki::Plugins::${pluginName}::initPlugin( $web.$topic ) is OK")
if $Foswiki::cfg{Plugins}{ExternalLinkPlugin}{Debug};
Expand Down Expand Up @@ -83,21 +82,15 @@ sub commonTagsHandler {
s!(\[\[($protocolsPattern://[^]]+?)\]\[[^]]+?\]\]([&]nbsp;)?)! handleExternalLink($1, $2) !ge;
}

sub internalLink { _internalLink(@_) }

sub _internalLink {
my $pre = shift;

# my( $web, $topic, $label, $anchor, $anchor, $createLink ) = @_;
return 'blo';
}

sub handleExternalLink {
my ( $wholeLink, $url ) = @_;

my $scriptUrl =
Foswiki::Func::getUrlHost() . Foswiki::Func::getScriptUrlPath();
my $pubUrl = Foswiki::Func::getUrlHost() . Foswiki::Func::getPubUrlPath();
my $scriptUrl = "$Foswiki::cfg{DefaultUrlHost}$Foswiki::cfg{ScriptUrlPath}";
my $pubUrl = "$Foswiki::cfg{DefaultUrlHost}$Foswiki::cfg{PubUrlPath}";

Foswiki::Func::writeDebug(
"Comparing SCRIPTURL ($scriptUrl)\n pubUrl ($pubUrl)\n with ($wholeLink)")
if $Foswiki::cfg{Plugins}{ExternalLinkPlugin}{Debug};

if ( ( $url =~ /^$scriptUrl/ )
|| ( $url =~ /^$pubUrl/ )
Expand Down
13 changes: 7 additions & 6 deletions lib/Foswiki/Plugins/ExternalLinkPlugin/MANIFEST
@@ -1,6 +1,7 @@
lib/Foswiki/Plugins/ExternalLinkPlugin.pm 0644 Perl module
lib/Foswiki/Plugins/ExternalLinkPlugin/Config.spec 0444 Settings
data/System/ExternalLinkPlugin.txt 0644 0644 Documentation
pub/System/ExternalLinkPlugin/diagonal.gif 0777
pub/System/ExternalLinkPlugin/external.gif 0777
templates/externallinkplugin.tmpl 0444 Template for CSS style written to head
data/System/ExternalLinkPlugin.txt 0664
lib/Foswiki/Plugins/ExternalLinkPlugin.pm 0664
lib/Foswiki/Plugins/ExternalLinkPlugin/Config.spec 0664
pub/System/ExternalLinkPlugin/diagonal.gif 0664
pub/System/ExternalLinkPlugin/external.gif 0664
templates/externallinkplugin.tmpl 0664
#test/unit/ExternalLinkPlugin/ExternalLinkPluginTests.pm 0664
172 changes: 172 additions & 0 deletions test/unit/ExternalLinkPlugin/ExternalLinkPluginTests.pm
@@ -0,0 +1,172 @@
use strict;

package ExternalLinkPluginTests;

use FoswikiFnTestCase;
our @ISA = qw( FoswikiFnTestCase );

use Error qw( :try );

sub new {
my $self = shift()->SUPER::new(@_);
return $self;
}

sub set_up {
my $this = shift;

$this->SUPER::set_up();
}

sub loadExtraConfig {
my $this = shift;

$this->SUPER::loadExtraConfig();

$Foswiki::cfg{Plugins}{ExternalLinkPlugin}{MarkerImage} =
'%PUBURLPATH%/%SYSTEMWEB%/ExternalLinkPlugin/external.gif';
$Foswiki::cfg{Plugins}{ExternalLinkPlugin}{Debug} = 1;
$Foswiki::cfg{Plugins}{ExternalLinkPlugin}{Module} =
'Foswiki::Plugins::ExternalLinkPlugin';
$Foswiki::cfg{Plugins}{ExternalLinkPlugin}{Enabled} = 1;

$Foswiki::cfg{DefaultUrlHost}='http://foobar.xxx';
$Foswiki::cfg{ScriptUrlPath}='/foswiki/bin';
$Foswiki::cfg{ScriptUrlPaths}{view} = '/foswiki/bin/view';
}

sub tear_down {
my $this = shift;

# Always do this, and always do it last
$this->SUPER::tear_down();
}

sub test_renderExternalLinks {
my ($this) = @_;

# External link with text
$this->assert_html_equals(
"<span class='externalLink'><a href='http://google.com/'>Link to Google</a></span>",
Foswiki::Func::renderText(
Foswiki::Func::expandCommonVariables(
" [[http://google.com/][Link to Google]]", 'Sandbox'
)
)
);

# External ssl link with text
$this->assert_html_equals(
"<span class='externalLink'><a href='https://google.com/'>SSL to Google</a></span>",
Foswiki::Func::renderText(
Foswiki::Func::expandCommonVariables(
" [[https://google.com/][SSL to Google]]", 'Sandbox'
)
)
);

# Simple external link, no text (Does not get external mark)
$this->assert_html_equals(
"<a href='http://google.com/'>http://google.com/</a>",
Foswiki::Func::renderText(
Foswiki::Func::expandCommonVariables(
" [[http://google.com/]]", 'Sandbox'
)
)
);

return;
# External link with text - expand called twice - Item8446
$this->assert_html_equals(
"<span class='externalLink'><a href='http://google.com/'>Link to Google</a></span>",
Foswiki::Func::renderText(
Foswiki::Func::expandCommonVariables(
Foswiki::Func::expandCommonVariables(
" [[http://google.com/][Link to Google]]", 'Sandbox'
)
)
)
);
}

sub test_renderInternalLinks {
my ($this) = @_;

my $url = $Foswiki::cfg{DefaultUrlHost};
my $viewfile = $Foswiki::cfg{ScriptUrlPath}.'/viewfile';
my $pubpath = $Foswiki::cfg{PubUrlPath};

$this->assert_html_equals(
"<a href='$url$pubpath/System/ProjectLogos/favicon.ico'>project logo</a>",
Foswiki::Func::renderText(
Foswiki::Func::expandCommonVariables(
" [[%PUBURL%/System/ProjectLogos/favicon.ico][project logo]]", 'Sandbox'
)
)
);

$this->assert_html_equals(
"<a href='$url$viewfile/System/ProjectLogos/favicon.ico'>project logo</a>",
Foswiki::Func::renderText(
Foswiki::Func::expandCommonVariables(
"[[%SCRIPTURL{viewfile}%/System/ProjectLogos/favicon.ico][project logo]]", 'Sandbox'
)
)
);


# Setup for short URLs

$Foswiki::cfg{ScriptUrlPaths}{view} = '';
$Foswiki::cfg{ScriptUrlPath} = '/bin';
$this->createNewFoswikiSession();

return;

# External link with text - Fails - short URLs Item8563
$this->assert_html_equals(
"<a href='$url/Main/WebHome'>$url/Main/WebHome</a>",
Foswiki::Func::renderText(
Foswiki::Func::expandCommonVariables(
" [[%SCRIPTURL{view}%/Main/WebHome][main webhome]]", 'Sandbox'
)
)
);

}

sub test_renderI18nLinks {
my ($this) = @_;

$Foswiki::cfg{DefaultUrlHost}='http://εμπορικ.xxx';
$Foswiki::cfg{ScriptUrlPath}='/foswiki/bin';
$Foswiki::cfg{ScriptUrlPaths}{view} = '/foswiki/bin/view';
$this->createNewFoswikiSession();

my $url = $Foswiki::cfg{DefaultUrlHost};
my $viewfile = $Foswiki::cfg{ScriptUrlPath}.'/viewfile';
my $pubpath = $Foswiki::cfg{PubUrlPath};

# External link with text
$this->assert_html_equals(
"<span class='externalLink'><a href='http://εμπορικόσήμα.eu/'>Link to greek site</a></span>",
Foswiki::Func::renderText(
Foswiki::Func::expandCommonVariables(
" [[http://εμπορικόσήμα.eu/][Link to greek site]]", 'Sandbox'
)
)
);

$this->assert_html_equals(
"<a href='$url$pubpath/System/ProjectLogos/favicon.ico'>project logo</a>",
Foswiki::Func::renderText(
Foswiki::Func::expandCommonVariables(
" [[%PUBURL%/System/ProjectLogos/favicon.ico][project logo]]", 'Sandbox'
)
)
);


}

1;

0 comments on commit 1e5be1e

Please sign in to comment.