Bugzilla – Attachment 77323 Details for
Bug 19799
Changing language on OPAC redirects back to homepage
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 19799: Add localReferer function to module, and test
Bug-19799-Add-localReferer-function-to-module-and-.patch (text/plain), 5.61 KB, created by
Marcel de Rooy
on 2018-07-30 11:39:20 UTC
(
hide
)
Description:
Bug 19799: Add localReferer function to module, and test
Filename:
MIME Type:
Creator:
Marcel de Rooy
Created:
2018-07-30 11:39:20 UTC
Size:
5.61 KB
patch
obsolete
>From c2e0e1432713e25a4975434302c7b3f86953855b Mon Sep 17 00:00:00 2001 >From: Marcel de Rooy <m.de.rooy@rijksmuseum.nl> >Date: Mon, 30 Jul 2018 12:18:31 +0200 >Subject: [PATCH] Bug 19799: Add localReferer function to module, and test >Content-Type: text/plain; charset=utf-8 > >Before implementing the change in opac-changelanguage, we define a new >function (in Koha/Util.pm) and add tests. > >Test plan: >Run t/Koha/Util/localReferer.t > >Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl> >--- > Koha/Util.pm | 84 ++++++++++++++++++++++++++++++++++++++++++++++ > t/Koha/Util/localReferer.t | 58 ++++++++++++++++++++++++++++++++ > 2 files changed, 142 insertions(+) > create mode 100644 Koha/Util.pm > create mode 100644 t/Koha/Util/localReferer.t > >diff --git a/Koha/Util.pm b/Koha/Util.pm >new file mode 100644 >index 0000000..f6cdeda >--- /dev/null >+++ b/Koha/Util.pm >@@ -0,0 +1,84 @@ >+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 }); >+ >+ If the referer is a local URI, return local path. >+ Otherwise return fallback. >+ >+=cut >+ >+sub localReferer { >+ my ( $cgi, $params ) = @_; >+ my $referer = $cgi->referer; >+ my $fallback = $params->{fallback} // '/'; >+ return $fallback if !$referer; >+ >+ my $opacbase = C4::Context->preference('OPACBaseURL'); >+ my $rv; >+ if( $opacbase ) { >+ # Check OPACBaseURL >+ if( substr($referer, 0, length($opacbase)) eq $opacbase && >+ $referer =~ /\/cgi-bin\/koha\// ) >+ { >+ $rv = substr( $referer, length($opacbase) ); >+ $rv =~ s/^\///; >+ $rv = '/'.$rv; >+ } >+ } else { >+ # Check $cgi->url; but note that protocol returned by CGI is not reliable under Plack >+ 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/t/Koha/Util/localReferer.t b/t/Koha/Util/localReferer.t >new file mode 100644 >index 0000000..4db0dae >--- /dev/null >+++ b/t/Koha/Util/localReferer.t >@@ -0,0 +1,58 @@ >+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 remove_language >+ $referer = "http://koha.nl$search&language=zz-ZZ&debug=1"; >+ $base = 'https://koha.nl'; >+ is( Koha::Util::localReferer($cgi, { remove_language => 1 }), $search.'&debug=1', 'no opacbaseurl, opac-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' ); >+}; >-- >2.1.4
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 19799
:
77323
|
77324