Bug 21299: (QA follow-up) Rename module and subroutine
authorMarcel de Rooy <m.de.rooy@rijksmuseum.nl>
Thu, 13 Sep 2018 06:41:10 +0000 (08:41 +0200)
committerNick Clemens <nick@bywatersolutions.com>
Wed, 7 Nov 2018 21:52:17 +0000 (21:52 +0000)
See QA request at comment7.
Removing camelcase from subroutinename.
Renaming module to Koha::Util::Navigation (after cmty feedback).

Test plan:
Run t/Koha/Util/Navigation.t.

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Koha/Util.pm [deleted file]
Koha/Util/Navigation.pm [new file with mode: 0644]
changelanguage.pl
opac/opac-changelanguage.pl
t/Koha/Util/Navigation.t [new file with mode: 0644]
t/Koha/Util/localReferer.t [deleted file]

diff --git a/Koha/Util.pm b/Koha/Util.pm
deleted file mode 100644 (file)
index ce61863..0000000
+++ /dev/null
@@ -1,87 +0,0 @@
-package Koha::Util;
-
-# Copyright Rijksmuseum 2018
-#
-# This file is part of Koha.
-#
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 3 of the License, or (at your option) any later
-# version.
-#
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-
-use Modern::Perl;
-use C4::Context;
-
-=head1 NAME
-
-Koha::Util
-
-=head1 SYNOPSIS
-
-    use Koha::Util;
-    my $cgi = CGI->new;
-    my $referer = Koha::Util::localReferer($cgi);
-
-=head1 DESCRIPTION
-
-Utility class
-
-=head1 FUNCTIONS
-
-=head2 localReferer
-
-    my $referer = Koha::Util::localReferer( $cgi, { fallback => '/', remove_language => 1, staff => 1 });
-
-    If the referer is a local URI, return local path.
-    Otherwise return fallback.
-    Optional parameters fallback, remove_language and staff. If you do not
-    pass staff, opac is assumed.
-
-=cut
-
-sub localReferer {
-    my ( $cgi, $params ) = @_;
-    my $referer = $cgi->referer;
-    my $fallback = $params->{fallback} // '/';
-    my $staff = $params->{staff}; # no staff means OPAC
-    return $fallback if !$referer;
-
-    my $base = C4::Context->preference($staff ? 'staffClientBaseURL' : 'OPACBaseURL');
-    my $rv;
-
-    # Try ..BaseURL first, otherwise use CGI::url
-    if( $base ) {
-        if( substr($referer, 0, length($base)) eq $base &&
-            $referer =~ /\/cgi-bin\/koha\// )
-        {
-            $rv = substr( $referer, length($base) );
-            $rv =~ s/^\///;
-            $rv = '/'.$rv;
-        }
-    } else {
-        my $cgibase = $cgi->url( -base => 1 );
-        $cgibase =~ s/^https?://;
-        if( $referer =~ /$cgibase(\/cgi-bin\/koha\/.*)$/ ) {
-            $rv = $1;
-        }
-    }
-    $rv =~ s/(?<=[?&])language=[\w-]+(&|$)// if $rv and $params->{remove_language};
-    return $rv // $fallback;
-}
-
-1;
-
-=head1 AUTHOR
-
-    Marcel de Rooy, Rijksmuseum Amsterdam, The Netherlands
-    Koha development team
-
-=cut
diff --git a/Koha/Util/Navigation.pm b/Koha/Util/Navigation.pm
new file mode 100644 (file)
index 0000000..57b0d9d
--- /dev/null
@@ -0,0 +1,87 @@
+package Koha::Util::Navigation;
+
+# Copyright Rijksmuseum 2018
+#
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 3 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+use Modern::Perl;
+use C4::Context;
+
+=head1 NAME
+
+Koha::Util
+
+=head1 SYNOPSIS
+
+    use Koha::Util::Navigation;
+    my $cgi = CGI->new;
+    my $referer = Koha::Util::Navigation::local_referer($cgi);
+
+=head1 DESCRIPTION
+
+Utility class
+
+=head1 FUNCTIONS
+
+=head2 local_referer
+
+    my $referer = Koha::Util::Navigation::local_referer( $cgi, { fallback => '/', remove_language => 1, staff => 1 });
+
+    If the referer is a local URI, return local path.
+    Otherwise return fallback.
+    Optional parameters fallback, remove_language and staff. If you do not
+    pass staff, opac is assumed.
+
+=cut
+
+sub local_referer {
+    my ( $cgi, $params ) = @_;
+    my $referer = $cgi->referer;
+    my $fallback = $params->{fallback} // '/';
+    my $staff = $params->{staff}; # no staff means OPAC
+    return $fallback if !$referer;
+
+    my $base = C4::Context->preference($staff ? 'staffClientBaseURL' : 'OPACBaseURL');
+    my $rv;
+
+    # Try ..BaseURL first, otherwise use CGI::url
+    if( $base ) {
+        if( substr($referer, 0, length($base)) eq $base &&
+            $referer =~ /\/cgi-bin\/koha\// )
+        {
+            $rv = substr( $referer, length($base) );
+            $rv =~ s/^\///;
+            $rv = '/'.$rv;
+        }
+    } else {
+        my $cgibase = $cgi->url( -base => 1 );
+        $cgibase =~ s/^https?://;
+        if( $referer =~ /$cgibase(\/cgi-bin\/koha\/.*)$/ ) {
+            $rv = $1;
+        }
+    }
+    $rv =~ s/(?<=[?&])language=[\w-]+(&|$)// if $rv and $params->{remove_language};
+    return $rv // $fallback;
+}
+
+1;
+
+=head1 AUTHOR
+
+    Marcel de Rooy, Rijksmuseum Amsterdam, The Netherlands
+    Koha development team
+
+=cut
index c7a5c94..6fd3113 100755 (executable)
 use Modern::Perl;
 use CGI qw ( -utf8 );
 use C4::Templates;
-use Koha::Util;
+use Koha::Util::Navigation;
 
 my $query = new CGI;
 my $language = $query->param('language');
-my $url = Koha::Util::localReferer($query, {remove_language => 1, staff => 1});
+my $url = Koha::Util::Navigation::local_referer($query, {remove_language => 1, staff => 1});
 
 C4::Templates::setlanguagecookie( $query, $language, $url );
index 2c01b28..895bc80 100755 (executable)
 use Modern::Perl;
 use CGI qw ( -utf8 );
 use C4::Templates;
-use Koha::Util;
+use Koha::Util::Navigation;
 
 my $query = new CGI;
 my $language = $query->param('language');
-my $url = Koha::Util::localReferer($query, { remove_language => 1 });
+my $url = Koha::Util::Navigation::local_referer($query, { remove_language => 1 });
 
 C4::Templates::setlanguagecookie( $query, $language, $url );
diff --git a/t/Koha/Util/Navigation.t b/t/Koha/Util/Navigation.t
new file mode 100644 (file)
index 0000000..466f296
--- /dev/null
@@ -0,0 +1,60 @@
+use Modern::Perl;
+
+use Test::More tests => 1;
+use Test::MockObject;
+
+use t::lib::Mocks;
+use Koha::Util::Navigation;
+
+subtest 'Tests for local_referer' => sub {
+    plan tests => 10;
+
+    my ( $referer, $base );
+    my $cgi = Test::MockObject->new;
+    $cgi->mock( 'referer', sub { $referer } );
+    $cgi->mock( 'url', sub { $base } ); # base for [opac-]changelanguage
+
+    # Start with filled OPACBaseIRL
+    t::lib::Mocks::mock_preference('OPACBaseURL', 'https://koha.nl' );
+    $referer = 'https://somewhere.com/myscript';
+    is( Koha::Util::Navigation::local_referer($cgi), '/', 'External referer' );
+
+    my $search = '/cgi-bin/koha/opac-search.pl?q=perl';
+    $referer = "https://koha.nl$search";
+    is( Koha::Util::Navigation::local_referer($cgi), $search, 'opac-search' );
+
+    $referer = 'https://koha.nl/custom/stuff';
+    is( Koha::Util::Navigation::local_referer($cgi), '/', 'custom url' );
+
+    # trailing backslash
+    t::lib::Mocks::mock_preference('OPACBaseURL', 'http://koha.nl/' );
+    $referer = "http://koha.nl$search";
+    is( Koha::Util::Navigation::local_referer($cgi), $search, 'opac-search, trailing backslash' );
+
+    # no OPACBaseURL
+    t::lib::Mocks::mock_preference('OPACBaseURL', '');
+    $referer = 'https://somewhere.com/myscript';
+    $base = 'http://koha.nl';
+    is( Koha::Util::Navigation::local_referer($cgi), '/', 'no opacbaseurl, external' );
+
+    $referer = "https://koha.nl$search";
+    $base = 'https://koha.nl';
+    is( Koha::Util::Navigation::local_referer($cgi), $search, 'no opacbaseurl, opac-search' );
+    $base = 'http://koha.nl';
+    is( Koha::Util::Navigation::local_referer($cgi), $search, 'no opacbaseurl, opac-search, protocol diff' );
+
+    # base contains https, referer http (this should be very unusual)
+    # test parameters remove_language. staff
+    t::lib::Mocks::mock_preference('staffClientBaseURL', '' );
+    $search = '/cgi-bin/koha/catalogue/search.pl?q=perl'; # staff
+    $referer = "http://koha.nl:8080$search&language=zz-ZZ&debug=1";
+    $base = 'https://koha.nl:8080';
+    is( Koha::Util::Navigation::local_referer($cgi, { remove_language => 1, staff => 1 }), $search.'&debug=1', 'no baseurl, staff search, protocol diff (base https)' );
+
+    # custom script, test fallback parameter
+    $referer = 'https://koha.nl/custom/stuff';
+    $base = 'https://koha.nl';
+    is( Koha::Util::Navigation::local_referer($cgi, { fallback => 'ZZZ' }), 'ZZZ', 'no opacbaseurl, custom url, test fallback' );
+    $base = 'http://koha.nl';
+    is( Koha::Util::Navigation::local_referer($cgi), '/', 'no opacbaseurl, custom url, protocol diff' );
+};
diff --git a/t/Koha/Util/localReferer.t b/t/Koha/Util/localReferer.t
deleted file mode 100644 (file)
index a1d860d..0000000
+++ /dev/null
@@ -1,60 +0,0 @@
-use Modern::Perl;
-
-use Test::More tests => 1;
-use Test::MockObject;
-
-use t::lib::Mocks;
-use Koha::Util;
-
-subtest 'Tests for localReferer' => sub {
-    plan tests => 10;
-
-    my ( $referer, $base );
-    my $cgi = Test::MockObject->new;
-    $cgi->mock( 'referer', sub { $referer } );
-    $cgi->mock( 'url', sub { $base } ); # base for [opac-]changelanguage
-
-    # Start with filled OPACBaseIRL
-    t::lib::Mocks::mock_preference('OPACBaseURL', 'https://koha.nl' );
-    $referer = 'https://somewhere.com/myscript';
-    is( Koha::Util::localReferer($cgi), '/', 'External referer' );
-
-    my $search = '/cgi-bin/koha/opac-search.pl?q=perl';
-    $referer = "https://koha.nl$search";
-    is( Koha::Util::localReferer($cgi), $search, 'opac-search' );
-
-    $referer = 'https://koha.nl/custom/stuff';
-    is( Koha::Util::localReferer($cgi), '/', 'custom url' );
-
-    # trailing backslash
-    t::lib::Mocks::mock_preference('OPACBaseURL', 'http://koha.nl/' );
-    $referer = "http://koha.nl$search";
-    is( Koha::Util::localReferer($cgi), $search, 'opac-search, trailing backslash' );
-
-    # no OPACBaseURL
-    t::lib::Mocks::mock_preference('OPACBaseURL', '');
-    $referer = 'https://somewhere.com/myscript';
-    $base = 'http://koha.nl';
-    is( Koha::Util::localReferer($cgi), '/', 'no opacbaseurl, external' );
-
-    $referer = "https://koha.nl$search";
-    $base = 'https://koha.nl';
-    is( Koha::Util::localReferer($cgi), $search, 'no opacbaseurl, opac-search' );
-    $base = 'http://koha.nl';
-    is( Koha::Util::localReferer($cgi), $search, 'no opacbaseurl, opac-search, protocol diff' );
-
-    # base contains https, referer http (this should be very unusual)
-    # test parameters remove_language. staff
-    t::lib::Mocks::mock_preference('staffClientBaseURL', '' );
-    $search = '/cgi-bin/koha/catalogue/search.pl?q=perl'; # staff
-    $referer = "http://koha.nl:8080$search&language=zz-ZZ&debug=1";
-    $base = 'https://koha.nl:8080';
-    is( Koha::Util::localReferer($cgi, { remove_language => 1, staff => 1 }), $search.'&debug=1', 'no baseurl, staff search, protocol diff (base https)' );
-
-    # custom script, test fallback parameter
-    $referer = 'https://koha.nl/custom/stuff';
-    $base = 'https://koha.nl';
-    is( Koha::Util::localReferer($cgi, { fallback => 'ZZZ' }), 'ZZZ', 'no opacbaseurl, custom url, test fallback' );
-    $base = 'http://koha.nl';
-    is( Koha::Util::localReferer($cgi), '/', 'no opacbaseurl, custom url, protocol diff' );
-};