Bug 36356: Fix rollback from FrameworkPlugin.t
[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 => 35;
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 => 6;
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->mock( 'preference', sub { return '{ a : 1 }' });
53     is( ref( C4::Context->new->yaml_preference('ItemsDeniedRenewal') ), 'HASH', 'Got a hash as expected' );
54     $context->mock( 'preference', sub { return '[ 1, 2 ]' });
55     warning_like { $pref = C4::Context->new->yaml_preference('ITEMSDENIEDRENEWAL') } qr/Hashref expected/, 'Array not accepted for ItemsDeniedRenewal';
56     is( $pref, undef, 'Returned undef' );
57
58     $context->unmock( 'preference' );
59 };
60
61 subtest 'multivalue_preference() tests' => sub {
62
63     plan tests => 3;
64
65     t::lib::Mocks::mock_preference( 'MultiValuedSyspref', '' );
66     is_deeply( C4::Context->multivalue_preference('MultiValuedSyspref'), [] );
67
68     t::lib::Mocks::mock_preference( 'MultiValuedSyspref', 'some' );
69     is_deeply( C4::Context->multivalue_preference('MultiValuedSyspref'), ['some'] );
70
71     t::lib::Mocks::mock_preference( 'MultiValuedSyspref', 'some|more|values' );
72     is_deeply( C4::Context->multivalue_preference('MultiValuedSyspref'), [ 'some', 'more', 'values' ] );
73 };
74
75 subtest 'needs_install() tests' => sub {
76
77     plan tests => 2;
78
79     t::lib::Mocks::mock_preference( 'Version', '3.0.0' );
80     is( C4::Context->needs_install, 0, 'Preference is defined, no need to install' );
81
82     t::lib::Mocks::mock_preference( 'Version', undef ); # the behaviour when ->preference fails to fetch
83     is( C4::Context->needs_install, 1, "->preference(Version) is not defined, need to install" );
84 };
85
86 subtest 'csv_delimiter() tests' => sub {
87
88     plan tests => 4;
89
90     t::lib::Mocks::mock_preference( 'CSVDelimiter', undef );
91     is( C4::Context->csv_delimiter, ',', "csv_delimiter returns comma if system preference CSVDelimiter is undefined" );
92
93     t::lib::Mocks::mock_preference( 'CSVDelimiter', '' );
94     is( C4::Context->csv_delimiter, ',', "csv_delimiter returns comma if system preference CSVDelimiter is empty string" );
95
96     t::lib::Mocks::mock_preference( 'CSVDelimiter', ';' );
97     is( C4::Context->csv_delimiter, ';', "csv_delimiter returns semicolon if system preference CSVDelimiter is semicolon" );
98
99     t::lib::Mocks::mock_preference( 'CSVDelimiter', 'tabulation' );
100     is( C4::Context->csv_delimiter, "\t", "csv_delimiter returns '\t' if system preference CSVDelimiter is tabulation" );
101 };
102
103 subtest 'default_catalog_sort_by() tests' => sub {
104
105     plan tests => 5;
106
107     my $context = Test::MockModule->new( 'C4::Context' );
108
109     $context->mock( 'interface', 'intranet' );
110
111     t::lib::Mocks::mock_preference( 'defaultSortField', undef );
112     is( C4::Context->default_catalog_sort_by, undef, "default_catalog_sort_by() returns undef if system preference defaultSortField is undefined" );
113
114     t::lib::Mocks::mock_preference( 'defaultSortField', 'title' );
115     t::lib::Mocks::mock_preference( 'defaultSortOrder', 'az' );
116     is( C4::Context->default_catalog_sort_by, 'title_az', "default_catalog_sort_by() returns concatenation of system preferences defaultSortField and defaultSortOrder" );
117
118     t::lib::Mocks::mock_preference( 'defaultSortField', 'relevance' );
119     is( C4::Context->default_catalog_sort_by, 'relevance', "default_catalog_sort_by() returns only system preference defaultSortField if it is relevance" );
120
121     $context->mock( 'interface', 'opac' );
122
123     t::lib::Mocks::mock_preference( 'OPACdefaultSortField', 'pubdate' );
124     t::lib::Mocks::mock_preference( 'OPACdefaultSortOrder', 'desc' );
125     is( C4::Context->default_catalog_sort_by, 'pubdate_desc', "default_catalog_sort_by() returns concatenation of system preferences OPACdefaultSortField and OPACdefaultSortOrder" );
126
127     t::lib::Mocks::mock_preference( 'OPACdefaultSortField', 'relevance' );
128     is( C4::Context->default_catalog_sort_by, 'relevance', "default_catalog_sort_by() returns only system preference OPACdefaultSortField if it is relevance" );
129
130     $context->unmock( 'interface' );
131 };
132
133 my $context = Test::MockModule->new('C4::Context');
134 my $userenv = {};
135
136 $context->mock('userenv', sub {
137     return $userenv;
138 });
139
140 local $SIG{__WARN__} = sub { die $_[0] };
141
142 eval { C4::Context::IsSuperLibrarian(); };
143 like ( $@, qr/not defined/, "IsSuperLibrarian logs an error if no userenv is defined" );
144
145 $userenv->{flags} = 42;
146 my $is_super_librarian = eval{ C4::Context::IsSuperLibrarian() };
147 is ( $@, q||, "IsSuperLibrarian does not log an error if userenv is defined" );
148 is ( $is_super_librarian, 0, "With flag=42, it is not a super librarian" );
149
150 $userenv->{flags} = 421;
151 $is_super_librarian = eval{ C4::Context::IsSuperLibrarian() };
152 is ( $@, q||, "IsSuperLibrarian does not log an error if userenv is defined" );
153 is ( $is_super_librarian, 1, "With flag=1, it is a super librarian" );
154
155 $userenv->{flags} = undef;
156 $is_super_librarian = eval{ C4::Context::IsSuperLibrarian() };
157 is ( $@, q||, "IsSuperLibrarian does not log an error if \$userenv->{flags} is undefined" );
158 is ( $is_super_librarian, 0, "With flag=undef, it is not a super librarian" );
159
160 $userenv->{flags} = 0;
161 $is_super_librarian = eval{ C4::Context::IsSuperLibrarian() };
162 is ( $@, q||, "IsSuperLibrarian does not log an error if \$userenv->{flags} is equal to 0" );
163 is ( $is_super_librarian, 0, "With flag=0, it is not a super librarian" );
164
165 # C4::Context::interface
166 my $lastwarn;
167 local $SIG{__WARN__} = sub { $lastwarn = $_[0] };
168 is(C4::Context->interface, 'opac','interface defaults to opac');
169 is(C4::Context->interface('foobar'), 'opac', 'interface foobar');
170 like($lastwarn, qr/invalid interface : 'foobar'/, 'interface warn on foobar');
171 is(C4::Context->interface, 'opac', 'interface still opac');
172 is(C4::Context->interface('intranet'), 'intranet', 'interface intranet');
173 is(C4::Context->interface, 'intranet', 'interface still intranet');
174 is(C4::Context->interface('foobar'), 'intranet', 'interface foobar again');
175 is(C4::Context->interface, 'intranet', 'interface still intranet');
176 is(C4::Context->interface('OPAC'), 'opac', 'interface OPAC uc');
177 is(C4::Context->interface, 'opac', 'interface still opac');
178 #Bug 14751
179 is( C4::Context->interface( 'SiP' ), 'sip', 'interface SiP' );
180 is( C4::Context->interface( 'COMMANDLINE' ), 'commandline', 'interface commandline uc' );
181 is( C4::Context->interface( 'CRON' ), 'cron', 'interface cron uc' );
182
183 {
184     local %ENV = %ENV;
185     delete $ENV{HTTPS};
186     is( C4::Context->https_enabled, 0, "Undefined HTTPS env returns 0");
187     $ENV{HTTPS} = '1';
188     is( C4::Context->https_enabled, 0, "Invalid 1 HTTPS env returns 0");
189     $ENV{HTTPS} = 'off';
190     is( C4::Context->https_enabled, 0, "off HTTPS env returns 0");
191     $ENV{HTTPS} = 'OFF';
192     is( C4::Context->https_enabled, 0, "OFF HTTPS env returns 0");
193     $ENV{HTTPS} = 'on';
194     is( C4::Context->https_enabled, 1, "on HTTPS env returns 1");
195     $ENV{HTTPS} = 'ON';
196     is( C4::Context->https_enabled, 1, "ON HTTPS env returns 1");
197 }
198
199 subtest 'psgi_env and is_internal_PSGI_request' => sub {
200
201     plan tests => 11;
202
203     local %ENV = ( no_plack => 1 );
204     ok( !C4::Context->psgi_env, 'no_plack' );
205     $ENV{plackishere} = 1;
206     ok( !C4::Context->psgi_env, 'plackishere is wrong' );
207     $ENV{'plack.ishere'} = 1;
208     ok( C4::Context->psgi_env, 'plack.ishere' );
209     delete $ENV{'plack.ishere'};
210     ok( !C4::Context->psgi_env, 'plack.ishere was here' );
211     $ENV{'plack_env'} = 1;
212     ok( C4::Context->psgi_env, 'plack_env' );
213     delete $ENV{'plack_env'};
214     $ENV{'psgi_whatever'} = 1;
215     ok( !C4::Context->psgi_env, 'psgi_whatever' );
216     delete $ENV{'psgi_whatever'};
217     $ENV{'psgi.whatever'} = 1;
218     ok( C4::Context->psgi_env, 'psgi.whatever' );
219     delete $ENV{'psgi.whatever'};
220     $ENV{'PSGI.UPPERCASE'} = 1;
221     ok( C4::Context->psgi_env, 'PSGI uppercase' );
222
223     $ENV{'REQUEST_URI'} = '/intranet/whatever';
224     ok( !C4::Context->is_internal_PSGI_request, 'intranet not considered internal in regex' );
225     $ENV{'REQUEST_URI'} = '/api/v1/tralala';
226     ok( C4::Context->is_internal_PSGI_request, 'api considered internal in regex' );
227     delete $ENV{'PSGI.UPPERCASE'};
228     ok( !C4::Context->is_internal_PSGI_request, 'api but no longer PSGI' );
229 };