Bug 19532: (follow-up) aria-hidden attr on OPAC, and more
[koha.git] / t / Context.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use DBI;
21 use Test::More tests => 31;
22 use Test::MockModule;
23 use Test::Warn;
24 use YAML::XS;
25
26 use t::lib::Mocks;
27
28 BEGIN {
29     use_ok('C4::Context');
30 }
31
32 subtest 'yaml_preference() tests' => sub {
33
34     plan tests => 3;
35
36     my $data = [ 'uno', 'dos', { 'tres' => 'cuatro' } ];
37
38     my $context = Test::MockModule->new( 'C4::Context' );
39     $context->mock( 'preference', YAML::XS::Dump($data) );
40
41     my $pref = C4::Context->new->yaml_preference( 'nothing' );
42
43     is_deeply( $pref, $data, 'yaml_preference returns the right structure' );
44
45     $context->mock( 'preference', qq{- uno: dsa\n\t- dos: asd} );
46     warning_like
47         { $pref = C4::Context->new->yaml_preference('nothing') }
48         qr/^Unable to parse nothing syspref/,
49         'Invalid YAML on syspref throws a warning';
50     is( $pref, undef, 'Invalid YAML on syspref makes it return undef' );
51
52     $context->unmock( 'preference' );
53 };
54
55 subtest 'needs_install() tests' => sub {
56
57     plan tests => 2;
58
59     t::lib::Mocks::mock_preference( 'Version', '3.0.0' );
60     is( C4::Context->needs_install, 0, 'Preference is defined, no need to install' );
61
62     t::lib::Mocks::mock_preference( 'Version', undef ); # the behaviour when ->preference fails to fetch
63     is( C4::Context->needs_install, 1, "->preference(Version) is not defined, need to install" );
64 };
65
66 my $context = Test::MockModule->new('C4::Context');
67 my $userenv = {};
68
69 $context->mock('userenv', sub {
70     return $userenv;
71 });
72
73 local $SIG{__WARN__} = sub { die $_[0] };
74
75 eval { C4::Context::IsSuperLibrarian(); };
76 like ( $@, qr/not defined/, "IsSuperLibrarian logs an error if no userenv is defined" );
77
78 $userenv->{flags} = 42;
79 my $is_super_librarian = eval{ C4::Context::IsSuperLibrarian() };
80 is ( $@, q||, "IsSuperLibrarian does not log an error if userenv is defined" );
81 is ( $is_super_librarian, 0, "With flag=42, it is not a super librarian" );
82
83 $userenv->{flags} = 421;
84 $is_super_librarian = eval{ C4::Context::IsSuperLibrarian() };
85 is ( $@, q||, "IsSuperLibrarian does not log an error if userenv is defined" );
86 is ( $is_super_librarian, 1, "With flag=1, it is a super librarian" );
87
88 $userenv->{flags} = undef;
89 $is_super_librarian = eval{ C4::Context::IsSuperLibrarian() };
90 is ( $@, q||, "IsSuperLibrarian does not log an error if \$userenv->{flags} is undefined" );
91 is ( $is_super_librarian, 0, "With flag=undef, it is not a super librarian" );
92
93 $userenv->{flags} = 0;
94 $is_super_librarian = eval{ C4::Context::IsSuperLibrarian() };
95 is ( $@, q||, "IsSuperLibrarian does not log an error if \$userenv->{flags} is equal to 0" );
96 is ( $is_super_librarian, 0, "With flag=0, it is not a super librarian" );
97
98 # C4::Context::interface
99 my $lastwarn;
100 local $SIG{__WARN__} = sub { $lastwarn = $_[0] };
101 is(C4::Context->interface, 'opac','interface defaults to opac');
102 is(C4::Context->interface('foobar'), 'opac', 'interface foobar');
103 like($lastwarn, qr/invalid interface : 'foobar'/, 'interface warn on foobar');
104 is(C4::Context->interface, 'opac', 'interface still opac');
105 is(C4::Context->interface('intranet'), 'intranet', 'interface intranet');
106 is(C4::Context->interface, 'intranet', 'interface still intranet');
107 is(C4::Context->interface('foobar'), 'intranet', 'interface foobar again');
108 is(C4::Context->interface, 'intranet', 'interface still intranet');
109 is(C4::Context->interface('OPAC'), 'opac', 'interface OPAC uc');
110 is(C4::Context->interface, 'opac', 'interface still opac');
111 #Bug 14751
112 is( C4::Context->interface( 'SiP' ), 'sip', 'interface SiP' );
113 is( C4::Context->interface( 'COMMANDLINE' ), 'commandline', 'interface commandline uc' );
114 is( C4::Context->interface( 'CRON' ), 'cron', 'interface cron uc' );
115
116 {
117     local %ENV = %ENV;
118     delete $ENV{HTTPS};
119     is( C4::Context->https_enabled, 0, "Undefined HTTPS env returns 0");
120     $ENV{HTTPS} = '1';
121     is( C4::Context->https_enabled, 0, "Invalid 1 HTTPS env returns 0");
122     $ENV{HTTPS} = 'off';
123     is( C4::Context->https_enabled, 0, "off HTTPS env returns 0");
124     $ENV{HTTPS} = 'OFF';
125     is( C4::Context->https_enabled, 0, "OFF HTTPS env returns 0");
126     $ENV{HTTPS} = 'on';
127     is( C4::Context->https_enabled, 1, "on HTTPS env returns 1");
128     $ENV{HTTPS} = 'ON';
129     is( C4::Context->https_enabled, 1, "ON HTTPS env returns 1");
130 }