Bug 12032: disable automatic URL conversion by TinyMCE in news
[koha.git] / t / Cache.t
blob044206c26609c06a98ffd8db5d24e995817786a7
1 #!/usr/bin/perl
3 # Tests Koha::Cache and whichever type of cache is enabled (through Koha::Cache)
5 use strict;
6 use warnings;
8 use Test::More tests => 29;
10 my $destructorcount = 0;
12 BEGIN {
13 use_ok('Koha::Cache');
14 use_ok('Koha::Cache::Object');
15 use_ok('C4::Context');
18 SKIP: {
19 my $cache = Koha::Cache->new();
21 skip "Cache not enabled", 25
22 unless ( Koha::Cache->is_cache_active() && defined $cache );
24 # test fetching an item that isnt in the cache
25 is( $cache->get_from_cache("not in here"),
26 undef, "fetching item NOT in cache" );
28 # test expiry time in cache
29 $cache->set_in_cache( "timeout", "I AM DATA", 1 ); # expiry time of 1 second
30 sleep 2;
31 is( $cache->get_from_cache("timeout"),
32 undef, "fetching expired item from cache" );
34 # test fetching a valid, non expired, item from cache
35 $cache->set_in_cache( "clear_me", "I AM MORE DATA", 1000 )
36 ; # overly large expiry time, clear below
37 $cache->set_in_cache( "dont_clear_me", "I AM MORE DATA22", 1000 )
38 ; # overly large expiry time, clear below
39 is(
40 $cache->get_from_cache("clear_me"),
41 "I AM MORE DATA",
42 "fetching valid item from cache"
45 # test clearing from cache
46 $cache->clear_from_cache("clear_me");
47 is( $cache->get_from_cache("clear_me"),
48 undef, "fetching cleared item from cache" );
49 is(
50 $cache->get_from_cache("dont_clear_me"),
51 "I AM MORE DATA22",
52 "fetching valid item from cache (after clearing another item)"
55 #test flushing from cache
56 $cache->set_in_cache( "flush_me", "testing 1 data" );
57 $cache->flush_all;
58 is( $cache->get_from_cache("flush_me"),
59 undef, "fetching flushed item from cache" );
60 is( $cache->get_from_cache("dont_clear_me"),
61 undef, "fetching flushed item from cache" );
63 my $constructorcount = 0;
64 my $myscalar = $cache->create_scalar(
66 'key' => 'myscalar',
67 'timeout' => 1,
68 'allowupdate' => 1,
69 'unset' => 1,
70 'constructor' => sub { return ++$constructorcount; },
71 'destructor' => sub { return ++$destructorcount; },
74 ok( defined($myscalar), 'Created tied scalar' );
75 is( $$myscalar, 1, 'Constructor called to first initialize' );
76 is( $$myscalar, 1, 'Data retrieved from cache' );
77 sleep 2;
78 is( $$myscalar, 2, 'Constructor called again when timeout reached' );
79 $$myscalar = 5;
80 is( $$myscalar, 5, 'Stored new value to cache' );
81 is( $constructorcount, 2, 'Constructor not called after storing value' );
82 undef $myscalar;
84 is( $cache->get_from_cache("myscalar"),
85 undef, 'Item removed from cache on destruction' );
87 my %hash = ( 'key' => 'value' );
89 my $myhash = $cache->create_hash(
91 'key' => 'myhash',
92 'timeout' => 1,
93 'allowupdate' => 1,
94 'unset' => 1,
95 'constructor' => sub { return { %hash }; },
99 ok(defined $myhash, 'Created tied hash');
101 is($myhash->{'key'}, 'value', 'Found expected value in hash');
102 ok(exists $myhash->{'key'}, 'Exists works');
103 $myhash->{'key2'} = 'surprise';
104 is($myhash->{'key2'}, 'surprise', 'Setting hash member worked');
105 $hash{'key2'} = 'nosurprise';
106 sleep 2;
107 is($myhash->{'key2'}, 'nosurprise', 'Cache change caught');
110 my $foundkeys = 0;
111 foreach my $key (keys %{$myhash}) {
112 $foundkeys++;
115 is($foundkeys, 2, 'Found expected 2 keys when iterating through hash');
117 isnt(scalar %{$myhash}, undef, 'scalar knows the hash is not empty');
119 $hash{'anotherkey'} = 'anothervalue';
121 sleep 2;
123 ok(exists $myhash->{'anotherkey'}, 'Cache reset properly');
125 delete $hash{'anotherkey'};
126 delete $myhash->{'anotherkey'};
128 ok(!exists $myhash->{'anotherkey'}, 'Key successfully deleted');
130 undef %hash;
131 %{$myhash} = ();
133 is(scalar %{$myhash}, 0, 'hash cleared');
135 $hash{'key'} = 'value';
136 is($myhash->{'key'}, 'value', 'retrieved value after clearing cache');
139 END {
140 SKIP: {
141 skip "Cache not enabled", 1
142 unless ( Koha::Cache->is_cache_active() );
143 is( $destructorcount, 1, 'Destructor run exactly once' );