Bug 16699: Reference new x-primitives in currently defined objects
[koha.git] / t / Context.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use DBI;
5 use Test::More tests => 26;
6 use Test::MockModule;
7
8 BEGIN {
9     use_ok('C4::Context');
10 }
11
12 my $context = new Test::MockModule('C4::Context');
13 my $userenv = {};
14
15 $context->mock('userenv', sub {
16     return $userenv;
17 });
18
19 local $SIG{__WARN__} = sub { die $_[0] };
20
21 eval { C4::Context::IsSuperLibrarian(); };
22 like ( $@, qr/not defined/, "IsSuperLibrarian logs an error if no userenv is defined" );
23
24 $userenv->{flags} = 42;
25 my $is_super_librarian = eval{ C4::Context::IsSuperLibrarian() };
26 is ( $@, q||, "IsSuperLibrarian does not log an error if userenv is defined" );
27 is ( $is_super_librarian, 0, "With flag=42, it is not a super librarian" );
28
29 $userenv->{flags} = 421;
30 $is_super_librarian = eval{ C4::Context::IsSuperLibrarian() };
31 is ( $@, q||, "IsSuperLibrarian does not log an error if userenv is defined" );
32 is ( $is_super_librarian, 1, "With flag=1, it is a super librarian" );
33
34 $userenv->{flags} = undef;
35 $is_super_librarian = eval{ C4::Context::IsSuperLibrarian() };
36 is ( $@, q||, "IsSuperLibrarian does not log an error if \$userenv->{flags} is undefined" );
37 is ( $is_super_librarian, 0, "With flag=undef, it is not a super librarian" );
38
39 $userenv->{flags} = 0;
40 $is_super_librarian = eval{ C4::Context::IsSuperLibrarian() };
41 is ( $@, q||, "IsSuperLibrarian does not log an error if \$userenv->{flags} is equal to 0" );
42 is ( $is_super_librarian, 0, "With flag=0, it is not a super librarian" );
43
44 is(C4::Context::db_scheme2dbi('mysql'), 'mysql', 'ask for mysql, get mysql');
45 is(C4::Context::db_scheme2dbi('Pg'),    'Pg',    'ask for Pg, get Pg');
46 is(C4::Context::db_scheme2dbi('xxx'),   'mysql', 'ask for unsupported DBMS, get mysql');
47 is(C4::Context::db_scheme2dbi(),        'mysql', 'ask for nothing, get mysql');
48
49 # C4::Context::interface
50 my $lastwarn;
51 local $SIG{__WARN__} = sub { $lastwarn = $_[0] };
52 is(C4::Context->interface, 'opac','interface defaults to opac');
53 is(C4::Context->interface('foobar'), 'opac', 'interface foobar');
54 like($lastwarn, qr/invalid interface : 'foobar'/, 'interface warn on foobar');
55 is(C4::Context->interface, 'opac', 'interface still opac');
56 is(C4::Context->interface('intranet'), 'intranet', 'interface intranet');
57 is(C4::Context->interface, 'intranet', 'interface still intranet');
58 is(C4::Context->interface('foobar'), 'intranet', 'interface foobar again');
59 is(C4::Context->interface, 'intranet', 'interface still intranet');
60 is(C4::Context->interface('OPAC'), 'opac', 'interface OPAC uc');
61 is(C4::Context->interface, 'opac', 'interface still opac');
62 #Bug 14751
63 is( C4::Context->interface( 'SiP' ), 'sip', 'interface SiP' );
64 is( C4::Context->interface( 'COMMANDLINE' ), 'commandline', 'interface commandline uc' );