3 use Test::More tests => 1;
7 use Koha::Util::Navigation;
9 subtest 'Tests for local_referer' => sub {
12 my ( $referer, $base );
13 my $cgi = Test::MockObject->new;
14 $cgi->mock( 'referer', sub { $referer } );
15 $cgi->mock( 'url', sub { $base } ); # base for [opac-]changelanguage
17 # Start with filled OPACBaseIRL
18 t::lib::Mocks::mock_preference('OPACBaseURL', 'https://koha.nl' );
19 $referer = 'https://somewhere.com/myscript';
20 is( Koha::Util::Navigation::local_referer($cgi), '/', 'External referer' );
22 my $search = '/cgi-bin/koha/opac-search.pl?q=perl';
23 $referer = "https://koha.nl$search";
24 is( Koha::Util::Navigation::local_referer($cgi), $search, 'opac-search' );
26 $referer = 'https://koha.nl/custom/stuff';
27 is( Koha::Util::Navigation::local_referer($cgi), '/', 'custom url' );
29 t::lib::Mocks::mock_preference('OPACBaseURL', 'http://kohadev.myDNSname.org:8080');
30 $referer = "http://kohadev.mydnsname.org:8080$search";
31 is( Koha::Util::Navigation::local_referer($cgi), $search, 'local_referer is comparing $OPACBaseURL case insensitive' );
34 t::lib::Mocks::mock_preference('OPACBaseURL', 'http://koha.nl/' );
35 $referer = "http://koha.nl$search";
36 is( Koha::Util::Navigation::local_referer($cgi), $search, 'opac-search, trailing backslash' );
39 t::lib::Mocks::mock_preference('OPACBaseURL', '');
40 $referer = 'https://somewhere.com/myscript';
41 $base = 'http://koha.nl';
42 is( Koha::Util::Navigation::local_referer($cgi), '/', 'no opacbaseurl, external' );
44 $referer = "https://koha.nl$search";
45 $base = 'https://koha.nl';
46 is( Koha::Util::Navigation::local_referer($cgi), $search, 'no opacbaseurl, opac-search' );
47 $base = 'http://koha.nl';
48 is( Koha::Util::Navigation::local_referer($cgi), $search, 'no opacbaseurl, opac-search, protocol diff' );
50 # base contains https, referer http (this should be very unusual)
51 # test parameters remove_language. staff
52 t::lib::Mocks::mock_preference('staffClientBaseURL', '' );
53 $search = '/cgi-bin/koha/catalogue/search.pl?q=perl'; # staff
54 $referer = "http://koha.nl:8080$search&language=zz-ZZ&debug=1";
55 $base = 'https://koha.nl:8080';
56 is( Koha::Util::Navigation::local_referer($cgi, { remove_language => 1, staff => 1 }), $search.'&debug=1', 'no baseurl, staff search, protocol diff (base https)' );
58 # custom script, test fallback parameter
59 $referer = 'https://koha.nl/custom/stuff';
60 $base = 'https://koha.nl';
61 is( Koha::Util::Navigation::local_referer($cgi, { fallback => 'ZZZ' }), 'ZZZ', 'no opacbaseurl, custom url, test fallback' );
62 $base = 'http://koha.nl';
63 is( Koha::Util::Navigation::local_referer($cgi), '/', 'no opacbaseurl, custom url, protocol diff' );