Bug 31196: Remove 'default_value_for_mod_marc-' clear_from_cache calls
[koha.git] / t / db_dependent / Items / AutomaticItemModificationByAge.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use Test::More tests => 19;
5 use MARC::Record;
6 use MARC::Field;
7 use DateTime;
8 use DateTime::Duration;
9
10 use C4::Items qw( GetMarcItem ToggleNewStatus );
11 use C4::Biblio qw( AddBiblio GetMarcFromKohaField );
12 use C4::Context;
13 use Koha::DateUtils qw( dt_from_string );
14 use Koha::Items;
15 use t::lib::Mocks;
16 use t::lib::TestBuilder;
17
18 my $schema = Koha::Database->new->schema;
19 $schema->storage->txn_begin;
20 my $dbh = C4::Context->dbh;
21
22 my $builder = t::lib::TestBuilder->new;
23
24 # create two branches
25 my $library = $builder->build( { source => 'Branch' })->{branchcode};
26 my $library2 = $builder->build( { source => 'Branch' })->{branchcode};
27
28 my $frameworkcode = ''; # Use Default for Koha to MARC mappings
29 $dbh->do(q|
30     DELETE FROM marc_subfield_structure
31     WHERE ( kohafield = 'items.new_status' OR kohafield = 'items.stocknumber' )
32     AND frameworkcode = ?
33 |, undef, $frameworkcode);
34
35 my $new_tagfield = 'i';
36 $dbh->do(qq|
37     INSERT INTO marc_subfield_structure(tagfield, tagsubfield, kohafield, frameworkcode)
38     VALUES ( 952, ?, 'items.new_status', ? )
39 |, undef, $new_tagfield, $frameworkcode);
40
41 # Clear cache
42 my $cache = Koha::Caches->get_instance();
43 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
44 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
45 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
46
47 my $record = MARC::Record->new();
48 $record->append_fields(
49     MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
50     MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
51     MARC::Field->new('942', ' ', ' ', c => 'ITEMTYPE_T'),
52 );
53 my ($biblionumber, undef) = C4::Biblio::AddBiblio($record, $frameworkcode);
54
55 my $item = $builder->build_sample_item(
56     {
57         biblionumber => $biblionumber,
58         library      => $library,
59         new_status   => 'new_value',
60         ccode        => 'FIC',
61     }
62 );
63 my $itemnumber = $item->itemnumber;
64 is ( $item->new_status, 'new_value', q|AddItem insert the 'new_status' field| );
65
66 my ( $tagfield, undef ) = GetMarcFromKohaField( 'items.itemnumber' );
67 my $marc_item = C4::Items::GetMarcItem( $biblionumber, $itemnumber );
68 is( $marc_item->subfield($tagfield, $new_tagfield), 'new_value', q|Koha mapping is correct|);
69
70 # Update the items.new_status field if items.ccode eq 'FIC' => should be updated
71 my @rules = (
72     {
73         conditions => [
74             {
75                 field => 'items.ccode',
76                 value => 'FIC',
77             },
78         ],
79         substitutions => [
80             {
81                 field => 'items.new_status',
82                 value => 'updated_value',
83              },
84         ],
85         age => '0',
86     },
87 );
88
89 C4::Items::ToggleNewStatus( { rules => \@rules } );
90
91 my $modified_item = Koha::Items->find( $itemnumber );
92 is( $modified_item->new_status, 'updated_value', q|ToggleNewStatus: The new_status value is updated|);
93 $marc_item = C4::Items::GetMarcItem( $biblionumber, $itemnumber );
94 is( $marc_item->subfield($tagfield, $new_tagfield), 'updated_value', q|ToggleNewStatus: The new_status value is updated| );
95
96 # Update the items.new_status field if items.ccode eq 'DONT_EXIST' => should not be updated
97 @rules = (
98     {
99         conditions => [
100             {
101                 field => 'items.ccode',
102                 value => 'DONT_EXIST',
103             },
104         ],
105         substitutions => [
106             {
107                 field => 'items.new_status',
108                 value => 'new_updated_value',
109              },
110         ],
111         age => '0',
112     },
113 );
114
115 C4::Items::ToggleNewStatus( { rules => \@rules } );
116
117 $modified_item = Koha::Items->find( $itemnumber );
118 is( $modified_item->new_status, 'updated_value', q|ToggleNewStatus: The new_status value is not updated|);
119 $marc_item = C4::Items::GetMarcItem( $biblionumber, $itemnumber );
120 is( $marc_item->subfield($tagfield, $new_tagfield), 'updated_value', q|ToggleNewStatus: The new_status value is not updated| );
121
122 # Play with age
123 my $dt_today = dt_from_string;
124 my $days5ago = $dt_today->add_duration( DateTime::Duration->new( days => -5 ) );
125
126 $modified_item->dateaccessioned($days5ago)->store;
127
128 @rules = (
129     {
130         conditions => [
131             {
132                 field => 'items.ccode',
133                 value => 'FIC',
134             },
135         ],
136         substitutions => [
137             {
138                 field => 'items.new_status',
139                 value => 'new_updated_value',
140              },
141         ],
142         age => '10', # Confirm not defining agefield, will default to using items.dateaccessioned
143     },
144 );
145 C4::Items::ToggleNewStatus( { rules => \@rules } );
146 $modified_item = Koha::Items->find( $itemnumber );
147 is( $modified_item->new_status, 'updated_value', q|ToggleNewStatus: Age = 10 : The new_status value is not updated|);
148
149 $rules[0]->{age} = 5;
150 $rules[0]->{substitutions}[0]{value} = 'new_updated_value5';
151 C4::Items::ToggleNewStatus( { rules => \@rules } );
152 $modified_item = Koha::Items->find( $itemnumber );
153 is( $modified_item->new_status, 'new_updated_value5', q|ToggleNewStatus: Age = 5 : The new_status value is updated|);
154
155 $rules[0]->{age} = '';
156 $rules[0]->{substitutions}[0]{value} = 'new_updated_value_empty_string';
157 C4::Items::ToggleNewStatus( { rules => \@rules } );
158 $modified_item = Koha::Items->find( $itemnumber );
159 is( $modified_item->new_status, 'new_updated_value_empty_string', q|ToggleNewStatus: Age = '' : The new_status value is updated|);
160
161 $rules[0]->{age} = undef;
162 $rules[0]->{substitutions}[0]{value} = 'new_updated_value_undef';
163 C4::Items::ToggleNewStatus( { rules => \@rules } );
164 $modified_item = Koha::Items->find( $itemnumber );
165 is( $modified_item->new_status, 'new_updated_value_undef', q|ToggleNewStatus: Age = undef : The new_status value is updated|);
166
167 # Field deletion
168 @rules = (
169     {
170         conditions => [
171             {
172                 field => 'items.ccode',
173                 value => 'FIC',
174             },
175         ],
176         substitutions => [
177             {
178                 field => 'items.new_status',
179                 value => '',
180              },
181         ],
182         age => '0',
183     },
184 );
185
186 C4::Items::ToggleNewStatus( { rules => \@rules } );
187
188 $modified_item = Koha::Items->find( $itemnumber );
189 is( $modified_item->new_status, '', q|ToggleNewStatus: The new_status value is empty|);
190 $marc_item = C4::Items::GetMarcItem( $biblionumber, $itemnumber );
191 is( $marc_item->subfield($tagfield, $new_tagfield), undef, q|ToggleNewStatus: The new_status field is removed from the item marc| );
192
193 # conditions multiple
194 @rules = (
195     {
196         conditions => [
197             {
198                 field => 'items.ccode',
199                 value => 'FIC',
200             },
201             {
202                 field => 'items.homebranch',
203                 value => $library,
204             },
205         ],
206         substitutions => [
207             {
208                 field => 'items.new_status',
209                 value => 'new_value',
210              },
211         ],
212         age => '0',
213     },
214 );
215
216 C4::Items::ToggleNewStatus( { rules => \@rules } );
217
218 $modified_item = Koha::Items->find( $itemnumber );
219 is( $modified_item->new_status, 'new_value', q|ToggleNewStatus: conditions multiple: all match, the new_status value is updated|);
220
221 @rules = (
222     {
223         conditions => [
224             {
225                 field => 'items.ccode',
226                 value => 'FIC',
227             },
228             {
229                 field => 'items.homebranch',
230                 value => 'DONT_EXIST',
231             },
232         ],
233         substitutions => [
234             {
235                 field => 'items.new_status',
236                 value => 'new_updated_value',
237              },
238         ],
239         age => '0',
240     },
241 );
242
243 C4::Items::ToggleNewStatus( { rules => \@rules } );
244
245 $modified_item = Koha::Items->find( $itemnumber );
246 is( $modified_item->new_status, 'new_value', q|ToggleNewStatus: conditions multiple: at least 1 condition does not match, the new_status value is not updated|);
247
248 @rules = (
249     {
250         conditions => [
251             {
252                 field => 'items.ccode',
253                 value => 'FIC|NFIC',
254             },
255             {
256                 field => 'items.homebranch',
257                 value => "$library|$library2",
258             },
259         ],
260         substitutions => [
261             {
262                 field => 'items.new_status',
263                 value => 'new_updated_value',
264              },
265         ],
266         age => '0',
267     },
268 );
269
270 C4::Items::ToggleNewStatus( { rules => \@rules } );
271
272 $modified_item = Koha::Items->find( $itemnumber );
273 is( $modified_item->new_status, 'new_updated_value', q|ToggleNewStatus: conditions multiple: the 2 conditions match, the new_status value is updated|);
274
275 @rules = (
276     {
277         # does not exist
278         conditions => [
279             {
280                 field => 'biblioitems.itemtype',
281                 value => 'ITEMTYPE_T',
282             },
283         ],
284         substitutions => [
285             {
286                 field => 'items.new_status',
287                 value => 'another_new_updated_value',
288              },
289         ],
290         age => '0',
291     },
292 );
293
294 C4::Items::ToggleNewStatus( { rules => \@rules } );
295
296 $modified_item = Koha::Items->find( $itemnumber );
297 is( $modified_item->new_status, 'another_new_updated_value', q|ToggleNewStatus: conditions on biblioitems|);
298
299 # Play with the 'Age field'
300 my $days2ago = $dt_today->add_duration( DateTime::Duration->new( days => -10 ) );
301 my $days20ago = $dt_today->add_duration( DateTime::Duration->new( days => -20 ) );
302 $modified_item->datelastseen($days2ago)->store;
303 $modified_item->dateaccessioned($days20ago)->store;
304
305 # When agefield='items.datelastseen'
306 @rules = (
307     {
308         conditions => [
309             {
310                 field => 'biblioitems.itemtype',
311                 value => 'ITEMTYPE_T',
312             },
313         ],
314         substitutions => [
315             {
316                 field => 'items.new_status',
317                 value => 'agefield_new_value',
318              },
319         ],
320         age => '5',
321         agefield => 'items.datelastseen' # Confirm defining agefield => 'items.datelastseen' will use items.datelastseen
322     },
323 );
324 C4::Items::ToggleNewStatus( { rules => \@rules } );
325 $modified_item = Koha::Items->find( $itemnumber );
326 is( $modified_item->new_status, 'agefield_new_value', q|ToggleNewStatus: Age = 5, agefield = 'items.datelastseen' : The new_status value is not updated|);
327
328 $rules[0]->{age} = 2;
329 C4::Items::ToggleNewStatus( { rules => \@rules } );
330 $modified_item = Koha::Items->find( $itemnumber );
331 is( $modified_item->new_status, 'agefield_new_value', q|ToggleNewStatus: Age = 2, agefield = 'items.datelastseen' : The new_status value is updated|);
332
333 # Run twice
334 t::lib::Mocks::mock_preference('CataloguingLog', 1);
335 my $actions_nb = $schema->resultset('ActionLog')->count();
336 C4::Items::ToggleNewStatus( { rules => \@rules } );
337 is( $schema->resultset('ActionLog')->count(), $actions_nb, q|ToggleNewStatus: no substitution does not generate action logs|);
338
339 # Cleanup
340 $cache = Koha::Caches->get_instance();
341 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
342 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
343 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
344 $schema->storage->txn_rollback;