3 # This file is part of Koha.
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.
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.
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>.
21 use Test::More tests => 34;
29 use_ok('C4::Context');
32 subtest 'yaml_preference() tests' => sub {
36 my $data = [ 'uno', 'dos', { 'tres' => 'cuatro' } ];
38 my $context = Test::MockModule->new( 'C4::Context' );
39 $context->mock( 'preference', YAML::XS::Dump($data) );
41 my $pref = C4::Context->new->yaml_preference( 'nothing' );
43 is_deeply( $pref, $data, 'yaml_preference returns the right structure' );
45 $context->mock( 'preference', qq{- uno: dsa\n\t- dos: asd} );
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' );
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' );
58 $context->unmock( 'preference' );
61 subtest 'needs_install() tests' => sub {
65 t::lib::Mocks::mock_preference( 'Version', '3.0.0' );
66 is( C4::Context->needs_install, 0, 'Preference is defined, no need to install' );
68 t::lib::Mocks::mock_preference( 'Version', undef ); # the behaviour when ->preference fails to fetch
69 is( C4::Context->needs_install, 1, "->preference(Version) is not defined, need to install" );
72 subtest 'csv_delimiter() tests' => sub {
76 t::lib::Mocks::mock_preference( 'CSVDelimiter', undef );
77 is( C4::Context->csv_delimiter, ',', "csv_delimiter returns comma if system preference CSVDelimiter is undefined" );
79 t::lib::Mocks::mock_preference( 'CSVDelimiter', '' );
80 is( C4::Context->csv_delimiter, ',', "csv_delimiter returns comma if system preference CSVDelimiter is empty string" );
82 t::lib::Mocks::mock_preference( 'CSVDelimiter', ';' );
83 is( C4::Context->csv_delimiter, ';', "csv_delimiter returns semicolon if system preference CSVDelimiter is semicolon" );
85 t::lib::Mocks::mock_preference( 'CSVDelimiter', 'tabulation' );
86 is( C4::Context->csv_delimiter, "\t", "csv_delimiter returns '\t' if system preference CSVDelimiter is tabulation" );
89 subtest 'default_catalog_sort_by() tests' => sub {
93 my $context = Test::MockModule->new( 'C4::Context' );
95 $context->mock( 'interface', 'intranet' );
97 t::lib::Mocks::mock_preference( 'defaultSortField', undef );
98 is( C4::Context->default_catalog_sort_by, undef, "default_catalog_sort_by() returns undef if system preference defaultSortField is undefined" );
100 t::lib::Mocks::mock_preference( 'defaultSortField', 'title' );
101 t::lib::Mocks::mock_preference( 'defaultSortOrder', 'az' );
102 is( C4::Context->default_catalog_sort_by, 'title_az', "default_catalog_sort_by() returns concatenation of system preferences defaultSortField and defaultSortOrder" );
104 t::lib::Mocks::mock_preference( 'defaultSortField', 'relevance' );
105 is( C4::Context->default_catalog_sort_by, 'relevance', "default_catalog_sort_by() returns only system preference defaultSortField if it is relevance" );
107 $context->mock( 'interface', 'opac' );
109 t::lib::Mocks::mock_preference( 'OPACdefaultSortField', 'pubdate' );
110 t::lib::Mocks::mock_preference( 'OPACdefaultSortOrder', 'desc' );
111 is( C4::Context->default_catalog_sort_by, 'pubdate_desc', "default_catalog_sort_by() returns concatenation of system preferences OPACdefaultSortField and OPACdefaultSortOrder" );
113 t::lib::Mocks::mock_preference( 'OPACdefaultSortField', 'relevance' );
114 is( C4::Context->default_catalog_sort_by, 'relevance', "default_catalog_sort_by() returns only system preference OPACdefaultSortField if it is relevance" );
116 $context->unmock( 'interface' );
119 my $context = Test::MockModule->new('C4::Context');
122 $context->mock('userenv', sub {
126 local $SIG{__WARN__} = sub { die $_[0] };
128 eval { C4::Context::IsSuperLibrarian(); };
129 like ( $@, qr/not defined/, "IsSuperLibrarian logs an error if no userenv is defined" );
131 $userenv->{flags} = 42;
132 my $is_super_librarian = eval{ C4::Context::IsSuperLibrarian() };
133 is ( $@, q||, "IsSuperLibrarian does not log an error if userenv is defined" );
134 is ( $is_super_librarian, 0, "With flag=42, it is not a super librarian" );
136 $userenv->{flags} = 421;
137 $is_super_librarian = eval{ C4::Context::IsSuperLibrarian() };
138 is ( $@, q||, "IsSuperLibrarian does not log an error if userenv is defined" );
139 is ( $is_super_librarian, 1, "With flag=1, it is a super librarian" );
141 $userenv->{flags} = undef;
142 $is_super_librarian = eval{ C4::Context::IsSuperLibrarian() };
143 is ( $@, q||, "IsSuperLibrarian does not log an error if \$userenv->{flags} is undefined" );
144 is ( $is_super_librarian, 0, "With flag=undef, it is not a super librarian" );
146 $userenv->{flags} = 0;
147 $is_super_librarian = eval{ C4::Context::IsSuperLibrarian() };
148 is ( $@, q||, "IsSuperLibrarian does not log an error if \$userenv->{flags} is equal to 0" );
149 is ( $is_super_librarian, 0, "With flag=0, it is not a super librarian" );
151 # C4::Context::interface
153 local $SIG{__WARN__} = sub { $lastwarn = $_[0] };
154 is(C4::Context->interface, 'opac','interface defaults to opac');
155 is(C4::Context->interface('foobar'), 'opac', 'interface foobar');
156 like($lastwarn, qr/invalid interface : 'foobar'/, 'interface warn on foobar');
157 is(C4::Context->interface, 'opac', 'interface still opac');
158 is(C4::Context->interface('intranet'), 'intranet', 'interface intranet');
159 is(C4::Context->interface, 'intranet', 'interface still intranet');
160 is(C4::Context->interface('foobar'), 'intranet', 'interface foobar again');
161 is(C4::Context->interface, 'intranet', 'interface still intranet');
162 is(C4::Context->interface('OPAC'), 'opac', 'interface OPAC uc');
163 is(C4::Context->interface, 'opac', 'interface still opac');
165 is( C4::Context->interface( 'SiP' ), 'sip', 'interface SiP' );
166 is( C4::Context->interface( 'COMMANDLINE' ), 'commandline', 'interface commandline uc' );
167 is( C4::Context->interface( 'CRON' ), 'cron', 'interface cron uc' );
172 is( C4::Context->https_enabled, 0, "Undefined HTTPS env returns 0");
174 is( C4::Context->https_enabled, 0, "Invalid 1 HTTPS env returns 0");
176 is( C4::Context->https_enabled, 0, "off HTTPS env returns 0");
178 is( C4::Context->https_enabled, 0, "OFF HTTPS env returns 0");
180 is( C4::Context->https_enabled, 1, "on HTTPS env returns 1");
182 is( C4::Context->https_enabled, 1, "ON HTTPS env returns 1");
185 subtest 'psgi_env and is_internal_PSGI_request' => sub {
189 local %ENV = ( no_plack => 1 );
190 ok( !C4::Context->psgi_env, 'no_plack' );
191 $ENV{plackishere} = 1;
192 ok( !C4::Context->psgi_env, 'plackishere is wrong' );
193 $ENV{'plack.ishere'} = 1;
194 ok( C4::Context->psgi_env, 'plack.ishere' );
195 delete $ENV{'plack.ishere'};
196 ok( !C4::Context->psgi_env, 'plack.ishere was here' );
197 $ENV{'plack_env'} = 1;
198 ok( C4::Context->psgi_env, 'plack_env' );
199 delete $ENV{'plack_env'};
200 $ENV{'psgi_whatever'} = 1;
201 ok( !C4::Context->psgi_env, 'psgi_whatever' );
202 delete $ENV{'psgi_whatever'};
203 $ENV{'psgi.whatever'} = 1;
204 ok( C4::Context->psgi_env, 'psgi.whatever' );
205 delete $ENV{'psgi.whatever'};
206 $ENV{'PSGI.UPPERCASE'} = 1;
207 ok( C4::Context->psgi_env, 'PSGI uppercase' );
209 $ENV{'REQUEST_URI'} = '/intranet/whatever';
210 ok( !C4::Context->is_internal_PSGI_request, 'intranet not considered internal in regex' );
211 $ENV{'REQUEST_URI'} = '/api/v1/tralala';
212 ok( C4::Context->is_internal_PSGI_request, 'api considered internal in regex' );
213 delete $ENV{'PSGI.UPPERCASE'};
214 ok( !C4::Context->is_internal_PSGI_request, 'api but no longer PSGI' );