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