Bug 14784: (follow-up) Unit tests
[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 => 33;
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 subtest 'csv_delimiter() tests' => sub {
67
68     plan tests => 4;
69
70     t::lib::Mocks::mock_preference( 'CSVDelimiter', undef );
71     is( C4::Context->csv_delimiter, ',', "csv_delimiter returns comma if system preference CSVDelimiter is undefined" );
72
73     t::lib::Mocks::mock_preference( 'CSVDelimiter', '' );
74     is( C4::Context->csv_delimiter, ',', "csv_delimiter returns comma if system preference CSVDelimiter is empty string" );
75
76     t::lib::Mocks::mock_preference( 'CSVDelimiter', ';' );
77     is( C4::Context->csv_delimiter, ';', "csv_delimiter returns semicolon if system preference CSVDelimiter is semicolon" );
78
79     t::lib::Mocks::mock_preference( 'CSVDelimiter', 'tabulation' );
80     is( C4::Context->csv_delimiter, "\t", "csv_delimiter returns '\t' if system preference CSVDelimiter is tabulation" );
81 };
82
83 my $context = Test::MockModule->new('C4::Context');
84 my $userenv = {};
85
86 $context->mock('userenv', sub {
87     return $userenv;
88 });
89
90 local $SIG{__WARN__} = sub { die $_[0] };
91
92 eval { C4::Context::IsSuperLibrarian(); };
93 like ( $@, qr/not defined/, "IsSuperLibrarian logs an error if no userenv is defined" );
94
95 $userenv->{flags} = 42;
96 my $is_super_librarian = eval{ C4::Context::IsSuperLibrarian() };
97 is ( $@, q||, "IsSuperLibrarian does not log an error if userenv is defined" );
98 is ( $is_super_librarian, 0, "With flag=42, it is not a super librarian" );
99
100 $userenv->{flags} = 421;
101 $is_super_librarian = eval{ C4::Context::IsSuperLibrarian() };
102 is ( $@, q||, "IsSuperLibrarian does not log an error if userenv is defined" );
103 is ( $is_super_librarian, 1, "With flag=1, it is a super librarian" );
104
105 $userenv->{flags} = undef;
106 $is_super_librarian = eval{ C4::Context::IsSuperLibrarian() };
107 is ( $@, q||, "IsSuperLibrarian does not log an error if \$userenv->{flags} is undefined" );
108 is ( $is_super_librarian, 0, "With flag=undef, it is not a super librarian" );
109
110 $userenv->{flags} = 0;
111 $is_super_librarian = eval{ C4::Context::IsSuperLibrarian() };
112 is ( $@, q||, "IsSuperLibrarian does not log an error if \$userenv->{flags} is equal to 0" );
113 is ( $is_super_librarian, 0, "With flag=0, it is not a super librarian" );
114
115 # C4::Context::interface
116 my $lastwarn;
117 local $SIG{__WARN__} = sub { $lastwarn = $_[0] };
118 is(C4::Context->interface, 'opac','interface defaults to opac');
119 is(C4::Context->interface('foobar'), 'opac', 'interface foobar');
120 like($lastwarn, qr/invalid interface : 'foobar'/, 'interface warn on foobar');
121 is(C4::Context->interface, 'opac', 'interface still opac');
122 is(C4::Context->interface('intranet'), 'intranet', 'interface intranet');
123 is(C4::Context->interface, 'intranet', 'interface still intranet');
124 is(C4::Context->interface('foobar'), 'intranet', 'interface foobar again');
125 is(C4::Context->interface, 'intranet', 'interface still intranet');
126 is(C4::Context->interface('OPAC'), 'opac', 'interface OPAC uc');
127 is(C4::Context->interface, 'opac', 'interface still opac');
128 #Bug 14751
129 is( C4::Context->interface( 'SiP' ), 'sip', 'interface SiP' );
130 is( C4::Context->interface( 'COMMANDLINE' ), 'commandline', 'interface commandline uc' );
131 is( C4::Context->interface( 'CRON' ), 'cron', 'interface cron uc' );
132
133 {
134     local %ENV = %ENV;
135     delete $ENV{HTTPS};
136     is( C4::Context->https_enabled, 0, "Undefined HTTPS env returns 0");
137     $ENV{HTTPS} = '1';
138     is( C4::Context->https_enabled, 0, "Invalid 1 HTTPS env returns 0");
139     $ENV{HTTPS} = 'off';
140     is( C4::Context->https_enabled, 0, "off HTTPS env returns 0");
141     $ENV{HTTPS} = 'OFF';
142     is( C4::Context->https_enabled, 0, "OFF HTTPS env returns 0");
143     $ENV{HTTPS} = 'on';
144     is( C4::Context->https_enabled, 1, "on HTTPS env returns 1");
145     $ENV{HTTPS} = 'ON';
146     is( C4::Context->https_enabled, 1, "ON HTTPS env returns 1");
147 }
148
149 subtest 'psgi_env and is_internal_PSGI_request' => sub {
150
151     plan tests => 11;
152
153     local %ENV = ( no_plack => 1 );
154     ok( !C4::Context->psgi_env, 'no_plack' );
155     $ENV{plackishere} = 1;
156     ok( !C4::Context->psgi_env, 'plackishere is wrong' );
157     $ENV{'plack.ishere'} = 1;
158     ok( C4::Context->psgi_env, 'plack.ishere' );
159     delete $ENV{'plack.ishere'};
160     ok( !C4::Context->psgi_env, 'plack.ishere was here' );
161     $ENV{'plack_env'} = 1;
162     ok( C4::Context->psgi_env, 'plack_env' );
163     delete $ENV{'plack_env'};
164     $ENV{'psgi_whatever'} = 1;
165     ok( !C4::Context->psgi_env, 'psgi_whatever' );
166     delete $ENV{'psgi_whatever'};
167     $ENV{'psgi.whatever'} = 1;
168     ok( C4::Context->psgi_env, 'psgi.whatever' );
169     delete $ENV{'psgi.whatever'};
170     $ENV{'PSGI.UPPERCASE'} = 1;
171     ok( C4::Context->psgi_env, 'PSGI uppercase' );
172
173     $ENV{'REQUEST_URI'} = '/intranet/whatever';
174     ok( !C4::Context->is_internal_PSGI_request, 'intranet not considered internal in regex' );
175     $ENV{'REQUEST_URI'} = '/api/v1/tralala';
176     ok( C4::Context->is_internal_PSGI_request, 'api considered internal in regex' );
177     delete $ENV{'PSGI.UPPERCASE'};
178     ok( !C4::Context->is_internal_PSGI_request, 'api but no longer PSGI' );
179 };