View | Details | Raw Unified | Return to bug 21299
Collapse All | Expand All

(-)a/Koha/Util.pm (-6 / +6 lines)
Lines 1-4 Link Here
1
package Koha::Util;
1
package Koha::Util::Navigation;
2
2
3
# Copyright Rijksmuseum 2018
3
# Copyright Rijksmuseum 2018
4
#
4
#
Lines 26-34 Koha::Util Link Here
26
26
27
=head1 SYNOPSIS
27
=head1 SYNOPSIS
28
28
29
    use Koha::Util;
29
    use Koha::Util::Navigation;
30
    my $cgi = CGI->new;
30
    my $cgi = CGI->new;
31
    my $referer = Koha::Util::localReferer($cgi);
31
    my $referer = Koha::Util::Navigation::local_referer($cgi);
32
32
33
=head1 DESCRIPTION
33
=head1 DESCRIPTION
34
34
Lines 36-44 Utility class Link Here
36
36
37
=head1 FUNCTIONS
37
=head1 FUNCTIONS
38
38
39
=head2 localReferer
39
=head2 local_referer
40
40
41
    my $referer = Koha::Util::localReferer( $cgi, { fallback => '/', remove_language => 1, staff => 1 });
41
    my $referer = Koha::Util::Navigation::local_referer( $cgi, { fallback => '/', remove_language => 1, staff => 1 });
42
42
43
    If the referer is a local URI, return local path.
43
    If the referer is a local URI, return local path.
44
    Otherwise return fallback.
44
    Otherwise return fallback.
Lines 47-53 Utility class Link Here
47
47
48
=cut
48
=cut
49
49
50
sub localReferer {
50
sub local_referer {
51
    my ( $cgi, $params ) = @_;
51
    my ( $cgi, $params ) = @_;
52
    my $referer = $cgi->referer;
52
    my $referer = $cgi->referer;
53
    my $fallback = $params->{fallback} // '/';
53
    my $fallback = $params->{fallback} // '/';
(-)a/changelanguage.pl (-2 / +2 lines)
Lines 18-27 Link Here
18
use Modern::Perl;
18
use Modern::Perl;
19
use CGI qw ( -utf8 );
19
use CGI qw ( -utf8 );
20
use C4::Templates;
20
use C4::Templates;
21
use Koha::Util;
21
use Koha::Util::Navigation;
22
22
23
my $query = new CGI;
23
my $query = new CGI;
24
my $language = $query->param('language');
24
my $language = $query->param('language');
25
my $url = Koha::Util::localReferer($query, {remove_language => 1, staff => 1});
25
my $url = Koha::Util::Navigation::local_referer($query, {remove_language => 1, staff => 1});
26
26
27
C4::Templates::setlanguagecookie( $query, $language, $url );
27
C4::Templates::setlanguagecookie( $query, $language, $url );
(-)a/opac/opac-changelanguage.pl (-2 / +2 lines)
Lines 18-27 Link Here
18
use Modern::Perl;
18
use Modern::Perl;
19
use CGI qw ( -utf8 );
19
use CGI qw ( -utf8 );
20
use C4::Templates;
20
use C4::Templates;
21
use Koha::Util;
21
use Koha::Util::Navigation;
22
22
23
my $query = new CGI;
23
my $query = new CGI;
24
my $language = $query->param('language');
24
my $language = $query->param('language');
25
my $url = Koha::Util::localReferer($query, { remove_language => 1 });
25
my $url = Koha::Util::Navigation::local_referer($query, { remove_language => 1 });
26
26
27
C4::Templates::setlanguagecookie( $query, $language, $url );
27
C4::Templates::setlanguagecookie( $query, $language, $url );
(-)a/t/Koha/Util/localReferer.t (-13 / +12 lines)
Lines 4-12 use Test::More tests => 1; Link Here
4
use Test::MockObject;
4
use Test::MockObject;
5
5
6
use t::lib::Mocks;
6
use t::lib::Mocks;
7
use Koha::Util;
7
use Koha::Util::Navigation;
8
8
9
subtest 'Tests for localReferer' => sub {
9
subtest 'Tests for local_referer' => sub {
10
    plan tests => 10;
10
    plan tests => 10;
11
11
12
    my ( $referer, $base );
12
    my ( $referer, $base );
Lines 17-47 subtest 'Tests for localReferer' => sub { Link Here
17
    # Start with filled OPACBaseIRL
17
    # Start with filled OPACBaseIRL
18
    t::lib::Mocks::mock_preference('OPACBaseURL', 'https://koha.nl' );
18
    t::lib::Mocks::mock_preference('OPACBaseURL', 'https://koha.nl' );
19
    $referer = 'https://somewhere.com/myscript';
19
    $referer = 'https://somewhere.com/myscript';
20
    is( Koha::Util::localReferer($cgi), '/', 'External referer' );
20
    is( Koha::Util::Navigation::local_referer($cgi), '/', 'External referer' );
21
21
22
    my $search = '/cgi-bin/koha/opac-search.pl?q=perl';
22
    my $search = '/cgi-bin/koha/opac-search.pl?q=perl';
23
    $referer = "https://koha.nl$search";
23
    $referer = "https://koha.nl$search";
24
    is( Koha::Util::localReferer($cgi), $search, 'opac-search' );
24
    is( Koha::Util::Navigation::local_referer($cgi), $search, 'opac-search' );
25
25
26
    $referer = 'https://koha.nl/custom/stuff';
26
    $referer = 'https://koha.nl/custom/stuff';
27
    is( Koha::Util::localReferer($cgi), '/', 'custom url' );
27
    is( Koha::Util::Navigation::local_referer($cgi), '/', 'custom url' );
28
28
29
    # trailing backslash
29
    # trailing backslash
30
    t::lib::Mocks::mock_preference('OPACBaseURL', 'http://koha.nl/' );
30
    t::lib::Mocks::mock_preference('OPACBaseURL', 'http://koha.nl/' );
31
    $referer = "http://koha.nl$search";
31
    $referer = "http://koha.nl$search";
32
    is( Koha::Util::localReferer($cgi), $search, 'opac-search, trailing backslash' );
32
    is( Koha::Util::Navigation::local_referer($cgi), $search, 'opac-search, trailing backslash' );
33
33
34
    # no OPACBaseURL
34
    # no OPACBaseURL
35
    t::lib::Mocks::mock_preference('OPACBaseURL', '');
35
    t::lib::Mocks::mock_preference('OPACBaseURL', '');
36
    $referer = 'https://somewhere.com/myscript';
36
    $referer = 'https://somewhere.com/myscript';
37
    $base = 'http://koha.nl';
37
    $base = 'http://koha.nl';
38
    is( Koha::Util::localReferer($cgi), '/', 'no opacbaseurl, external' );
38
    is( Koha::Util::Navigation::local_referer($cgi), '/', 'no opacbaseurl, external' );
39
39
40
    $referer = "https://koha.nl$search";
40
    $referer = "https://koha.nl$search";
41
    $base = 'https://koha.nl';
41
    $base = 'https://koha.nl';
42
    is( Koha::Util::localReferer($cgi), $search, 'no opacbaseurl, opac-search' );
42
    is( Koha::Util::Navigation::local_referer($cgi), $search, 'no opacbaseurl, opac-search' );
43
    $base = 'http://koha.nl';
43
    $base = 'http://koha.nl';
44
    is( Koha::Util::localReferer($cgi), $search, 'no opacbaseurl, opac-search, protocol diff' );
44
    is( Koha::Util::Navigation::local_referer($cgi), $search, 'no opacbaseurl, opac-search, protocol diff' );
45
45
46
    # base contains https, referer http (this should be very unusual)
46
    # base contains https, referer http (this should be very unusual)
47
    # test parameters remove_language. staff
47
    # test parameters remove_language. staff
Lines 49-60 subtest 'Tests for localReferer' => sub { Link Here
49
    $search = '/cgi-bin/koha/catalogue/search.pl?q=perl'; # staff
49
    $search = '/cgi-bin/koha/catalogue/search.pl?q=perl'; # staff
50
    $referer = "http://koha.nl:8080$search&language=zz-ZZ&debug=1";
50
    $referer = "http://koha.nl:8080$search&language=zz-ZZ&debug=1";
51
    $base = 'https://koha.nl:8080';
51
    $base = 'https://koha.nl:8080';
52
    is( Koha::Util::localReferer($cgi, { remove_language => 1, staff => 1 }), $search.'&debug=1', 'no baseurl, staff search, protocol diff (base https)' );
52
    is( Koha::Util::Navigation::local_referer($cgi, { remove_language => 1, staff => 1 }), $search.'&debug=1', 'no baseurl, staff search, protocol diff (base https)' );
53
53
54
    # custom script, test fallback parameter
54
    # custom script, test fallback parameter
55
    $referer = 'https://koha.nl/custom/stuff';
55
    $referer = 'https://koha.nl/custom/stuff';
56
    $base = 'https://koha.nl';
56
    $base = 'https://koha.nl';
57
    is( Koha::Util::localReferer($cgi, { fallback => 'ZZZ' }), 'ZZZ', 'no opacbaseurl, custom url, test fallback' );
57
    is( Koha::Util::Navigation::local_referer($cgi, { fallback => 'ZZZ' }), 'ZZZ', 'no opacbaseurl, custom url, test fallback' );
58
    $base = 'http://koha.nl';
58
    $base = 'http://koha.nl';
59
    is( Koha::Util::localReferer($cgi), '/', 'no opacbaseurl, custom url, protocol diff' );
59
    is( Koha::Util::Navigation::local_referer($cgi), '/', 'no opacbaseurl, custom url, protocol diff' );
60
};
60
};
61
- 

Return to bug 21299