Merge branch 'bug_8977' into 3.12-master
[koha_fer] / t / db_dependent / Context.t
index 31b0684..32ea441 100755 (executable)
@@ -4,7 +4,8 @@
 use strict;
 use warnings;
 
-use Test::More tests => 91;
+use Test::More;
+use Test::MockModule;
 use vars qw($debug $koha $dbh $config $ret);
 
 BEGIN {
@@ -22,6 +23,11 @@ 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||''));
+ok(
+    TransformVersionToNum( C4::Context->final_linear_version ) <=
+      TransformVersionToNum( C4::Context->KOHAVERSION ),
+    'Final linear version is less than or equal to kohaversion.pl'
+);
 my @keys = keys %$koha;
 diag("Number of keys in \%\$koha: " . scalar @keys); 
 our $width = 0;
@@ -42,8 +48,77 @@ foreach (grep {defined $koha->{$_}} sort @keys) {
 }
 ok($config = $koha->{config}, 'Getting $koha->{config} ');
 
-# diag("Examining configuration.");
-diag("Note: The overall number of tests may vary by configuration.  Disregard the projected number.");
-1;
-__END__
+diag "Testing syspref caching.";
+
+my $dbh = C4::Context->dbh;
+$dbh->disconnect;
+
+my $module = new Test::MockModule('C4::Context');
+$module->mock(
+    '_new_dbh',
+    sub {
+        my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
+          || die "Cannot create handle: $DBI::errstr\n";
+        return $dbh;
+    }
+);
+
+my $history;
+$dbh = C4::Context->dbh;
+
+$dbh->{mock_add_resultset} = [ ['value'], ['thing1'] ];
+$dbh->{mock_add_resultset} = [ ['value'], ['thing2'] ];
+$dbh->{mock_add_resultset} = [ ['value'], ['thing3'] ];
+$dbh->{mock_add_resultset} = [ ['value'], ['thing4'] ];
+
+is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
+$history = $dbh->{mock_all_history};
+is(scalar(@{$history}), 1, 'Retrieved syspref from database');
+
+$dbh->{mock_clear_history} = 1;
+is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
+$history = $dbh->{mock_all_history};
+is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
+
+C4::Context->disable_syspref_cache();
+is(C4::Context->preference("SillyPreference"), 'thing2', "Retrieved syspref (value='thing2') successfully with disabled cache");
+$history = $dbh->{mock_all_history};
+is(scalar(@{$history}), 1, 'Retrieved syspref from database');
+
+$dbh->{mock_clear_history} = 1;
+is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully with disabled cache");
+$history = $dbh->{mock_all_history};
+is(scalar(@{$history}), 1, 'Retrieved syspref from database');
 
+C4::Context->enable_syspref_cache();
+$dbh->{mock_clear_history} = 1;
+is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully from cache");
+$history = $dbh->{mock_all_history};
+is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
+
+C4::Context->clear_syspref_cache();
+$dbh->{mock_clear_history} = 1;
+is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully after clearing cache");
+$history = $dbh->{mock_all_history};
+is(scalar(@{$history}), 1, 'Retrieved syspref from database');
+
+$dbh->{mock_clear_history} = 1;
+is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully from cache");
+$history = $dbh->{mock_all_history};
+is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
+
+done_testing();
+
+sub TransformVersionToNum {
+    my $version = shift;
+
+    # remove the 3 last . to have a Perl number
+    $version =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
+
+    # three X's at the end indicate that you are testing patch with dbrev
+    # change it into 999
+    # prevents error on a < comparison between strings (should be: lt)
+    $version =~ s/XXX$/999/;
+    return $version;
+}
+1;