Bug 13069 - (follow-up) Enable sort by title to ignore articles
[koha.git] / t / db_dependent / Context.t
1 #!/usr/bin/perl
2 #
3
4 use strict;
5 use warnings;
6
7 use Test::More;
8 use Test::MockModule;
9 use vars qw($debug $koha $dbh $config $ret);
10
11 BEGIN {
12                 $debug = $ENV{DEBUG} || 0;
13         # Note: The overall number of tests may vary by configuration.
14         # First we need to check your environmental variables
15                 for (qw(KOHA_CONF PERL5LIB)) {
16                         ok($ret = $ENV{$_}, "ENV{$_} = $ret");
17                 }
18                 use_ok('C4::Context');
19 }
20
21 ok($koha = C4::Context->new,  'C4::Context->new');
22 ok($dbh = C4::Context->dbh(), 'Getting dbh from C4::Context');
23 ok($ret = C4::Context->KOHAVERSION, '  (function)  KOHAVERSION = ' . ($ret||''));
24 ok($ret =       $koha->KOHAVERSION, '       $koha->KOHAVERSION = ' . ($ret||''));
25 ok(
26     TransformVersionToNum( C4::Context->final_linear_version ) <=
27       TransformVersionToNum( C4::Context->KOHAVERSION ),
28     'Final linear version is less than or equal to kohaversion.pl'
29 );
30 my @keys = keys %$koha;
31 my $width = 0;
32 if (ok(@keys)) { 
33     $width = (sort {$a <=> $b} map {length} @keys)[-1];
34     $debug and diag "widest key is $width";
35 }
36 foreach (sort @keys) {
37         ok(exists $koha->{$_}, 
38                 '$koha->{' . sprintf('%' . $width . 's', $_)  . '} exists '
39                 . ((defined $koha->{$_}) ? "and is defined." : "but is not defined.")
40         );
41 }
42 ok($config = $koha->{config}, 'Getting $koha->{config} ');
43
44 # Testing syspref caching
45 my $module = new Test::MockModule('C4::Context');
46 $module->mock(
47     '_new_dbh',
48     sub {
49         my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
50           || die "Cannot create handle: $DBI::errstr\n";
51         return $dbh;
52     }
53 );
54
55 my $history;
56 $dbh = C4::Context->dbh({ new => 1 });
57
58 $dbh->{mock_add_resultset} = [ ['value'], ['thing1'] ];
59 $dbh->{mock_add_resultset} = [ ['value'], ['thing2'] ];
60 $dbh->{mock_add_resultset} = [ ['value'], ['thing3'] ];
61 $dbh->{mock_add_resultset} = [ ['value'], ['thing4'] ];
62
63 is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
64 $history = $dbh->{mock_all_history};
65 is(scalar(@{$history}), 1, 'Retrieved syspref from database');
66
67 $dbh->{mock_clear_history} = 1;
68 is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
69 $history = $dbh->{mock_all_history};
70 is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
71
72 C4::Context->disable_syspref_cache();
73 is(C4::Context->preference("SillyPreference"), 'thing2', "Retrieved syspref (value='thing2') successfully with disabled cache");
74 $history = $dbh->{mock_all_history};
75 is(scalar(@{$history}), 1, 'Retrieved syspref from database');
76
77 $dbh->{mock_clear_history} = 1;
78 is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully with disabled cache");
79 $history = $dbh->{mock_all_history};
80 is(scalar(@{$history}), 1, 'Retrieved syspref from database');
81
82 C4::Context->enable_syspref_cache();
83 $dbh->{mock_clear_history} = 1;
84 is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully from cache");
85 $history = $dbh->{mock_all_history};
86 is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
87
88 C4::Context->clear_syspref_cache();
89 $dbh->{mock_clear_history} = 1;
90 is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully after clearing cache");
91 $history = $dbh->{mock_all_history};
92 is(scalar(@{$history}), 1, 'Retrieved syspref from database');
93
94 $dbh->{mock_clear_history} = 1;
95 is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully from cache");
96 $history = $dbh->{mock_all_history};
97 is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
98
99 my $oConnection = C4::Context->Zconn('biblioserver', 0);
100 isnt($oConnection->option('async'), 1, "ZOOM connection is synchronous");
101 $oConnection = C4::Context->Zconn('biblioserver', 1);
102 is($oConnection->option('async'), 1, "ZOOM connection is asynchronous");
103
104 done_testing();
105
106 sub TransformVersionToNum {
107     my $version = shift;
108
109     # remove the 3 last . to have a Perl number
110     $version =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
111
112     # three X's at the end indicate that you are testing patch with dbrev
113     # change it into 999
114     # prevents error on a < comparison between strings (should be: lt)
115     $version =~ s/XXX$/999/;
116     return $version;
117 }
118 1;