Bug 23580: 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 => 28;
22 use Test::MockModule;
23 use Test::Warn;
24 use YAML;
25
26 BEGIN {
27     use_ok('C4::Context');
28 }
29
30 subtest 'yaml_preference() tests' => sub {
31
32     plan tests => 3;
33
34     my $data = [ 'uno', 'dos', { 'tres' => 'cuatro' } ];
35
36     my $context = Test::MockModule->new( 'C4::Context' );
37     $context->mock( 'preference', YAML::Dump($data) );
38
39     my $pref = C4::Context->new->yaml_preference( 'nothing' );
40
41     is_deeply( $pref, $data, 'yaml_preference returns the right structure' );
42
43     $context->mock( 'preference', q{- uno - dos: asd} );
44     warning_like
45         { $pref = C4::Context->new->yaml_preference('nothing') }
46         qr/^Unable to parse nothing syspref/,
47         'Invalid YAML on syspref throws a warning';
48     is( $pref, undef, 'Invalid YAML on syspref makes it return undef' );
49
50     $context->unmock( 'preference' );
51 };
52
53 my $context = new Test::MockModule('C4::Context');
54 my $userenv = {};
55
56 $context->mock('userenv', sub {
57     return $userenv;
58 });
59
60 local $SIG{__WARN__} = sub { die $_[0] };
61
62 eval { C4::Context::IsSuperLibrarian(); };
63 like ( $@, qr/not defined/, "IsSuperLibrarian logs an error if no userenv is defined" );
64
65 $userenv->{flags} = 42;
66 my $is_super_librarian = eval{ C4::Context::IsSuperLibrarian() };
67 is ( $@, q||, "IsSuperLibrarian does not log an error if userenv is defined" );
68 is ( $is_super_librarian, 0, "With flag=42, it is not a super librarian" );
69
70 $userenv->{flags} = 421;
71 $is_super_librarian = eval{ C4::Context::IsSuperLibrarian() };
72 is ( $@, q||, "IsSuperLibrarian does not log an error if userenv is defined" );
73 is ( $is_super_librarian, 1, "With flag=1, it is a super librarian" );
74
75 $userenv->{flags} = undef;
76 $is_super_librarian = eval{ C4::Context::IsSuperLibrarian() };
77 is ( $@, q||, "IsSuperLibrarian does not log an error if \$userenv->{flags} is undefined" );
78 is ( $is_super_librarian, 0, "With flag=undef, it is not a super librarian" );
79
80 $userenv->{flags} = 0;
81 $is_super_librarian = eval{ C4::Context::IsSuperLibrarian() };
82 is ( $@, q||, "IsSuperLibrarian does not log an error if \$userenv->{flags} is equal to 0" );
83 is ( $is_super_librarian, 0, "With flag=0, it is not a super librarian" );
84
85 is(C4::Context::db_scheme2dbi('mysql'), 'mysql', 'ask for mysql, get mysql');
86 is(C4::Context::db_scheme2dbi('Pg'),    'Pg',    'ask for Pg, get Pg');
87 is(C4::Context::db_scheme2dbi('xxx'),   'mysql', 'ask for unsupported DBMS, get mysql');
88 is(C4::Context::db_scheme2dbi(),        'mysql', 'ask for nothing, get mysql');
89
90 # C4::Context::interface
91 my $lastwarn;
92 local $SIG{__WARN__} = sub { $lastwarn = $_[0] };
93 is(C4::Context->interface, 'opac','interface defaults to opac');
94 is(C4::Context->interface('foobar'), 'opac', 'interface foobar');
95 like($lastwarn, qr/invalid interface : 'foobar'/, 'interface warn on foobar');
96 is(C4::Context->interface, 'opac', 'interface still opac');
97 is(C4::Context->interface('intranet'), 'intranet', 'interface intranet');
98 is(C4::Context->interface, 'intranet', 'interface still intranet');
99 is(C4::Context->interface('foobar'), 'intranet', 'interface foobar again');
100 is(C4::Context->interface, 'intranet', 'interface still intranet');
101 is(C4::Context->interface('OPAC'), 'opac', 'interface OPAC uc');
102 is(C4::Context->interface, 'opac', 'interface still opac');
103 #Bug 14751
104 is( C4::Context->interface( 'SiP' ), 'sip', 'interface SiP' );
105 is( C4::Context->interface( 'COMMANDLINE' ), 'commandline', 'interface commandline uc' );
106 is( C4::Context->interface( 'CRON' ), 'cron', 'interface cron uc' );