4 use Test::More tests => 16;
8 use DateTime::Duration;
14 use t::lib::TestBuilder;
16 my $schema = Koha::Database->new->schema;
17 $schema->storage->txn_begin;
18 my $dbh = C4::Context->dbh;
20 my $builder = t::lib::TestBuilder->new;
23 my $library = $builder->build( { source => 'Branch' })->{branchcode};
24 my $library2 = $builder->build( { source => 'Branch' })->{branchcode};
26 my $frameworkcode = ''; # FIXME We do not want to insert the whole mapping, but we should use another frameworkcode
28 DELETE FROM marc_subfield_structure
29 WHERE ( kohafield = 'items.new_status' OR kohafield = 'items.stocknumber' )
31 |, undef, $frameworkcode);
33 my $new_tagfield = 'i';
35 INSERT INTO marc_subfield_structure(tagfield, tagsubfield, kohafield, frameworkcode)
36 VALUES ( 952, ?, 'items.new_status', ? )
37 |, undef, $new_tagfield, $frameworkcode);
40 my $cache = Koha::Caches->get_instance();
41 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
42 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
43 $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
44 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
46 my $record = MARC::Record->new();
47 $record->append_fields(
48 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
49 MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
50 MARC::Field->new('942', ' ', ' ', c => 'ITEMTYPE_T'),
52 my ($biblionumber, undef) = C4::Biblio::AddBiblio($record, $frameworkcode);
54 my ($item_bibnum, $item_bibitemnum, $itemnumber) = C4::Items::AddItem(
56 homebranch => $library,
57 holdingbranch => $library,
58 new_status => 'new_value',
64 my $item = C4::Items::GetItem( $itemnumber );
65 is ( $item->{new_status}, 'new_value', q|AddItem insert the 'new_status' field| );
67 my ( $tagfield, undef ) = GetMarcFromKohaField('items.itemnumber', $frameworkcode);
68 my $marc_item = C4::Items::GetMarcItem( $biblionumber, $itemnumber );
69 is( $marc_item->subfield($tagfield, $new_tagfield), 'new_value', q|Koha mapping is correct|);
71 # Update the items.new_status field if items.ccode eq 'FIC' => should be updated
76 field => 'items.ccode',
82 field => 'items.new_status',
83 value => 'updated_value',
90 C4::Items::ToggleNewStatus( { rules => \@rules } );
92 my $modified_item = C4::Items::GetItem( $itemnumber );
93 is( $modified_item->{new_status}, 'updated_value', q|ToggleNewStatus: The new_status value is updated|);
94 $marc_item = C4::Items::GetMarcItem( $biblionumber, $itemnumber );
95 is( $marc_item->subfield($tagfield, $new_tagfield), 'updated_value', q|ToggleNewStatus: The new_status value is updated| );
97 # Update the items.new_status field if items.ccode eq 'DONT_EXIST' => should not be updated
102 field => 'items.ccode',
103 value => 'DONT_EXIST',
108 field => 'items.new_status',
109 value => 'new_updated_value',
116 C4::Items::ToggleNewStatus( { rules => \@rules } );
118 $modified_item = C4::Items::GetItem( $itemnumber );
119 is( $modified_item->{new_status}, 'updated_value', q|ToggleNewStatus: The new_status value is not updated|);
120 $marc_item = C4::Items::GetMarcItem( $biblionumber, $itemnumber );
121 is( $marc_item->subfield($tagfield, $new_tagfield), 'updated_value', q|ToggleNewStatus: The new_status value is not updated| );
124 $item = C4::Items::GetItem( $itemnumber );
125 my $dt_today = dt_from_string;
126 my $days5ago = $dt_today->add_duration( DateTime::Duration->new( days => -5 ) );
128 C4::Items::ModItem( { dateaccessioned => $days5ago }, $biblionumber, $itemnumber );
129 $item = C4::Items::GetItem( $itemnumber );
135 field => 'items.ccode',
141 field => 'items.new_status',
142 value => 'new_updated_value',
148 C4::Items::ToggleNewStatus( { rules => \@rules } );
149 $modified_item = C4::Items::GetItem( $itemnumber );
150 is( $modified_item->{new_status}, 'updated_value', q|ToggleNewStatus: Age = 10 : The new_status value is not updated|);
152 $rules[0]->{age} = 5;
153 $rules[0]->{substitutions}[0]{value} = 'new_updated_value5';
154 C4::Items::ToggleNewStatus( { rules => \@rules } );
155 $modified_item = C4::Items::GetItem( $itemnumber );
156 is( $modified_item->{new_status}, 'new_updated_value5', q|ToggleNewStatus: Age = 5 : The new_status value is updated|);
158 $rules[0]->{age} = '';
159 $rules[0]->{substitutions}[0]{value} = 'new_updated_value_empty_string';
160 C4::Items::ToggleNewStatus( { rules => \@rules } );
161 $modified_item = C4::Items::GetItem( $itemnumber );
162 is( $modified_item->{new_status}, 'new_updated_value_empty_string', q|ToggleNewStatus: Age = '' : The new_status value is updated|);
164 $rules[0]->{age} = undef;
165 $rules[0]->{substitutions}[0]{value} = 'new_updated_value_undef';
166 C4::Items::ToggleNewStatus( { rules => \@rules } );
167 $modified_item = C4::Items::GetItem( $itemnumber );
168 is( $modified_item->{new_status}, 'new_updated_value_undef', q|ToggleNewStatus: Age = undef : The new_status value is updated|);
175 field => 'items.ccode',
181 field => 'items.new_status',
189 C4::Items::ToggleNewStatus( { rules => \@rules } );
191 $modified_item = C4::Items::GetItem( $itemnumber );
192 is( $modified_item->{new_status}, '', q|ToggleNewStatus: The new_status value is empty|);
193 $marc_item = C4::Items::GetMarcItem( $biblionumber, $itemnumber );
194 is( $marc_item->subfield($tagfield, $new_tagfield), undef, q|ToggleNewStatus: The new_status field is removed from the item marc| );
196 # conditions multiple
201 field => 'items.ccode',
205 field => 'items.homebranch',
211 field => 'items.new_status',
212 value => 'new_value',
219 C4::Items::ToggleNewStatus( { rules => \@rules } );
221 $modified_item = C4::Items::GetItem( $itemnumber );
222 is( $modified_item->{new_status}, 'new_value', q|ToggleNewStatus: conditions multiple: all match, the new_status value is updated|);
228 field => 'items.ccode',
232 field => 'items.homebranch',
233 value => 'DONT_EXIST',
238 field => 'items.new_status',
239 value => 'new_updated_value',
246 C4::Items::ToggleNewStatus( { rules => \@rules } );
248 $modified_item = C4::Items::GetItem( $itemnumber );
249 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|);
255 field => 'items.ccode',
259 field => 'items.homebranch',
260 value => "$library|$library2",
265 field => 'items.new_status',
266 value => 'new_updated_value',
273 C4::Items::ToggleNewStatus( { rules => \@rules } );
275 $modified_item = C4::Items::GetItem( $itemnumber );
276 is( $modified_item->{new_status}, 'new_updated_value', q|ToggleNewStatus: conditions multiple: the 2 conditions match, the new_status value is updated|);
282 field => 'biblioitems.itemtype',
283 value => 'ITEMTYPE_T',
288 field => 'items.new_status',
289 value => 'another_new_updated_value',
296 C4::Items::ToggleNewStatus( { rules => \@rules } );
298 $modified_item = C4::Items::GetItem( $itemnumber );
299 is( $modified_item->{new_status}, 'another_new_updated_value', q|ToggleNewStatus: conditions on biblioitems|);
302 $cache = Koha::Caches->get_instance();
303 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
304 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
305 $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
306 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");