Bug 29826: Manage call of Template Plugin Branches GetName() with null or empty branc...
[koha.git] / t / Koha / Util / Navigation.t
1 use Modern::Perl;
2
3 use Test::More tests => 1;
4 use Test::MockObject;
5
6 use t::lib::Mocks;
7 use Koha::Util::Navigation;
8
9 subtest 'Tests for local_referer' => sub {
10     plan tests => 11;
11
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
16
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' );
21
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' );
25
26     $referer = 'https://koha.nl/custom/stuff';
27     is( Koha::Util::Navigation::local_referer($cgi), '/', 'custom url' );
28
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' );
32
33     # trailing backslash
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' );
37
38     # no OPACBaseURL
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' );
43
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' );
49
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)' );
57
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' );
64 };