#!/usr/bin/perl
-#
-use strict;
-use warnings;
-
-use Test::More tests => 91;
-use vars qw($debug $koha $dbh $config $ret);
+use Modern::Perl;
+use DBI;
+use Test::More tests => 14;
+use Test::MockModule;
BEGIN {
- $debug = $ENV{DEBUG} || 0;
- diag("Note: The overall number of tests may vary by configuration.");
- diag("First we need to check your environmental variables");
- for (qw(KOHA_CONF PERL5LIB)) {
- ok($ret = $ENV{$_}, "ENV{$_} = $ret");
- }
- use_ok('C4::Context');
- use_ok('C4::Utils', qw/ :all /);
+ use_ok('C4::Context');
}
-ok($koha = C4::Context->new, 'C4::Context->new');
-ok($dbh = C4::Context->dbh(), 'Getting dbh from C4::Context');
-ok($ret = C4::Context->KOHAVERSION, ' (function) KOHAVERSION = ' . ($ret||''));
-ok($ret = $koha->KOHAVERSION, ' $koha->KOHAVERSION = ' . ($ret||''));
-my @keys = keys %$koha;
-diag("Number of keys in \%\$koha: " . scalar @keys);
-our $width = 0;
-if (ok(@keys)) {
- $width = maxwidth(@keys);
- $debug and diag "widest key is $width";
-}
-foreach (sort @keys) {
- ok(exists $koha->{$_},
- '$koha->{' . sprintf('%' . $width . 's', $_) . '} exists '
- . ((defined $koha->{$_}) ? "and is defined." : "but is not defined.")
- );
-}
-diag "Examining defined key values.";
-foreach (grep {defined $koha->{$_}} sort @keys) {
- print "\n";
- hashdump('$koha->{' . sprintf('%' . $width . 's', $_) . '}', $koha->{$_});
-}
-ok($config = $koha->{config}, 'Getting $koha->{config} ');
+my $context = new Test::MockModule('C4::Context');
+my $userenv = {};
+
+$context->mock('userenv', sub {
+ return $userenv;
+});
+
+local $SIG{__WARN__} = sub { die $_[0] };
+
+eval { C4::Context::IsSuperLibrarian(); };
+like ( $@, qr/not defined/, "IsSuperLibrarian logs an error if no userenv is defined" );
+
+$userenv->{flags} = 42;
+my $is_super_librarian = eval{ C4::Context::IsSuperLibrarian() };
+is ( $@, q||, "IsSuperLibrarian does not log an error if userenv is defined" );
+is ( $is_super_librarian, 0, "With flag=42, it is not a super librarian" );
+
+$userenv->{flags} = 421;
+$is_super_librarian = eval{ C4::Context::IsSuperLibrarian() };
+is ( $@, q||, "IsSuperLibrarian does not log an error if userenv is defined" );
+is ( $is_super_librarian, 1, "With flag=1, it is a super librarian" );
+
+$userenv->{flags} = undef;
+$is_super_librarian = eval{ C4::Context::IsSuperLibrarian() };
+is ( $@, q||, "IsSuperLibrarian does not log an error if \$userenv->{flags} is undefined" );
+is ( $is_super_librarian, 0, "With flag=undef, it is not a super librarian" );
-# diag("Examining configuration.");
-diag("Note: The overall number of tests may vary by configuration. Disregard the projected number.");
-1;
-__END__
+$userenv->{flags} = 0;
+$is_super_librarian = eval{ C4::Context::IsSuperLibrarian() };
+is ( $@, q||, "IsSuperLibrarian does not log an error if \$userenv->{flags} is equal to 0" );
+is ( $is_super_librarian, 0, "With flag=0, it is not a super librarian" );
+is(C4::Context::db_scheme2dbi('mysql'), 'mysql', 'ask for mysql, get mysql');
+is(C4::Context::db_scheme2dbi('Pg'), 'Pg', 'ask for Pg, get Pg');
+is(C4::Context::db_scheme2dbi('xxx'), 'mysql', 'ask for unsupported DBMS, get mysql');
+is(C4::Context::db_scheme2dbi(), 'mysql', 'ask for nothing, get mysql');