Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
# |
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Copyright (c) 2024 |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it |
8 |
# under the terms of the GNU General Public License as published by |
9 |
# the Free Software Foundation; either version 3 of the License, or |
10 |
# (at your option) any later version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but |
13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
# GNU General Public License for more details. |
16 |
# |
17 |
# You should have received a copy of the GNU General Public License |
18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
19 |
|
20 |
use Modern::Perl; |
21 |
|
22 |
use Test::More tests => 4; |
23 |
use t::lib::TestBuilder; |
24 |
|
25 |
use Koha::ClassSortRules; |
26 |
use Koha::ClassSources; |
27 |
|
28 |
BEGIN { |
29 |
use_ok( 'C4::ClassSource', qw( GetClassSources GetClassSource GetClassSortRule ) ); |
30 |
} |
31 |
|
32 |
my $schema = Koha::Database->new->schema; |
33 |
my $builder = t::lib::TestBuilder->new; |
34 |
|
35 |
subtest 'GetClassSources' => sub { |
36 |
|
37 |
plan tests => 5; |
38 |
|
39 |
my $class_rule = $builder->build( |
40 |
{ |
41 |
source => 'ClassSortRule', |
42 |
value => { sort_routine => "LCC" } |
43 |
} |
44 |
); |
45 |
my $class_source_1 = $builder->build( |
46 |
{ |
47 |
source => 'ClassSource', |
48 |
value => { |
49 |
class_sort_rule => $class_rule->{class_sort_rule}, |
50 |
} |
51 |
} |
52 |
); |
53 |
my $source_code = $class_source_1->{cn_source}; |
54 |
|
55 |
Koha::Cache::Memory::Lite->flush(); |
56 |
|
57 |
my $class_sources = GetClassSources(); |
58 |
is_deeply( $class_sources->{$source_code}, $class_source_1, "The retrieved version, from the DB, is the same" ); |
59 |
|
60 |
# Now we add a new one, but expect the old to be cached (same request) |
61 |
my $class_source_2 = $builder->build( |
62 |
{ |
63 |
source => 'ClassSource', |
64 |
value => { |
65 |
class_sort_rule => $class_rule->{class_sort_rule}, |
66 |
} |
67 |
} |
68 |
); |
69 |
$source_code = $class_source_2->{cn_source}; |
70 |
|
71 |
my $class_sources_cached = GetClassSources(); |
72 |
is_deeply( |
73 |
$class_sources, $class_sources_cached, |
74 |
"We have a cached version, so we won't have the new one we added" |
75 |
); |
76 |
is( $class_sources_cached->{$source_code}, undef, "New value not present" ); |
77 |
|
78 |
# Now we clear the cache, i.e. pretend we're a new request |
79 |
Koha::Cache::Memory::Lite->flush(); |
80 |
|
81 |
$class_sources = GetClassSources(); |
82 |
is( $class_sources_cached->{$source_code}, undef, "New value now present after cache cleared" ); |
83 |
$class_sources_cached = GetClassSources(); |
84 |
is_deeply( $class_sources, $class_sources_cached, "New cached version does match the updated fresh version" ); |
85 |
}; |
86 |
|
87 |
subtest 'GetClassSource' => sub { |
88 |
|
89 |
plan tests => 4; |
90 |
|
91 |
my $class_rule = $builder->build( |
92 |
{ |
93 |
source => 'ClassSortRule', |
94 |
value => { sort_routine => "LCC" } |
95 |
} |
96 |
); |
97 |
my $class_source_1 = $builder->build( |
98 |
{ |
99 |
source => 'ClassSource', |
100 |
value => { |
101 |
class_sort_rule => $class_rule->{class_sort_rule}, |
102 |
} |
103 |
} |
104 |
); |
105 |
my $source_code = $class_source_1->{cn_source}; |
106 |
|
107 |
Koha::Cache::Memory::Lite->flush(); |
108 |
my $class_source_db = GetClassSource($source_code); |
109 |
is_deeply( $class_source_db, $class_source_1, "The retrieved version, from the DB, is the same" ); |
110 |
|
111 |
my $class_source_object = Koha::ClassSources->find($source_code); |
112 |
$class_source_object->description("We changed the thing")->store(); |
113 |
|
114 |
my $class_source_cache = GetClassSource($source_code); |
115 |
is( |
116 |
$class_source_cache->{description}, $class_source_db->{description}, |
117 |
"Still have old description in cache (same request)" |
118 |
); |
119 |
|
120 |
Koha::Cache::Memory::Lite->flush(); # New request, that's the gimmick |
121 |
$class_source_db = GetClassSource($source_code); # Refetch from DB to populate cache |
122 |
is( $class_source_db->{description}, "We changed the thing", "DB request got update value" ); |
123 |
$class_source_cache = GetClassSource($source_code); |
124 |
is( $class_source_cache->{description}, $class_source_db->{description}, "Now both get the correct value" ); |
125 |
|
126 |
}; |
127 |
|
128 |
subtest 'GetClassSortRule' => sub { |
129 |
|
130 |
plan tests => 4; |
131 |
|
132 |
my $class_rule_1 = $builder->build( |
133 |
{ |
134 |
source => 'ClassSortRule', |
135 |
value => { sort_routine => "LCC" } |
136 |
} |
137 |
); |
138 |
my $sort_rule = $class_rule_1->{class_sort_rule}; |
139 |
|
140 |
Koha::Cache::Memory::Lite->flush(); |
141 |
my $class_sort_rule_db = GetClassSortRule($sort_rule); |
142 |
is_deeply( $class_sort_rule_db, $class_rule_1, "The retrieved version, from the DB, is the same" ); |
143 |
|
144 |
my $class_source_object = Koha::ClassSortRules->find($sort_rule); |
145 |
$class_source_object->sort_routine('Dewey')->store(); |
146 |
|
147 |
my $class_sort_rule_cache = GetClassSortRule($sort_rule); |
148 |
is_deeply( $class_sort_rule_cache, $class_sort_rule_db, "Still have old sort rule in cache (same request)" ); |
149 |
|
150 |
Koha::Cache::Memory::Lite->flush(); # New request, that's the gimmick |
151 |
$class_sort_rule_db = GetClassSortRule($sort_rule); # Refetch from DB to populate cache |
152 |
is_deeply( $class_sort_rule_db, $class_source_object->unblessed, "DB request got updated value" ); |
153 |
$class_sort_rule_cache = GetClassSortRule($sort_rule); |
154 |
is_deeply( $class_sort_rule_cache, $class_sort_rule_db, "Now both get the correct value" ); |
155 |
|
156 |
}; |