|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
use Modern::Perl; |
| 4 |
use Test::More tests => 15; |
| 5 |
use MARC::Record; |
| 6 |
use MARC::Field; |
| 7 |
use DateTime; |
| 8 |
use DateTime::Duration; |
| 9 |
|
| 10 |
use C4::Biblio; |
| 11 |
use C4::Context; |
| 12 |
use C4::Items; |
| 13 |
use Koha::DateUtils; |
| 14 |
|
| 15 |
my $dbh = C4::Context->dbh; |
| 16 |
$dbh->{AutoCommit} = 0; |
| 17 |
$dbh->{RaiseError} = 1; |
| 18 |
|
| 19 |
$dbh->do(q| |
| 20 |
DELETE FROM marc_subfield_structure |
| 21 |
WHERE kohafield = 'items.new' OR kohafield = 'items.stocknumber' |
| 22 |
|); |
| 23 |
|
| 24 |
my $new_tagfield = 'i'; |
| 25 |
$dbh->do(qq| |
| 26 |
INSERT INTO marc_subfield_structure(tagfield, tagsubfield, kohafield, frameworkcode) |
| 27 |
VALUES ( 952, '$new_tagfield', 'items.new', '' ) |
| 28 |
|); |
| 29 |
|
| 30 |
my $record = MARC::Record->new(); |
| 31 |
$record->append_fields( |
| 32 |
MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'), |
| 33 |
MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'), |
| 34 |
); |
| 35 |
my ($biblionumber, undef) = C4::Biblio::AddBiblio($record, ''); |
| 36 |
|
| 37 |
my ($item_bibnum, $item_bibitemnum, $itemnumber) = C4::Items::AddItem( |
| 38 |
{ |
| 39 |
homebranch => 'CPL', |
| 40 |
holdingbranch => 'CPL', |
| 41 |
new => 'new_value', |
| 42 |
ccode => 'FIC', |
| 43 |
}, |
| 44 |
$biblionumber |
| 45 |
); |
| 46 |
|
| 47 |
my $item = C4::Items::GetItem( $itemnumber ); |
| 48 |
is ( $item->{new}, 'new_value', q|AddItem insert the 'new' field| ); |
| 49 |
|
| 50 |
my ( $tagfield, undef ) = GetMarcFromKohaField('items.itemnumber', ''); |
| 51 |
my $marc_item = C4::Items::GetMarcItem( $biblionumber, $itemnumber ); |
| 52 |
is( $marc_item->subfield($tagfield, $new_tagfield), 'new_value', q|Koha mapping is correct|); |
| 53 |
|
| 54 |
# Update the items.new field if items.ccode eq 'FIC' => should be updated |
| 55 |
my @rules = ( |
| 56 |
{ |
| 57 |
conditions => [ |
| 58 |
{ |
| 59 |
field => 'items.ccode', |
| 60 |
value => 'FIC', |
| 61 |
}, |
| 62 |
], |
| 63 |
substitutions => [ |
| 64 |
{ |
| 65 |
field => 'items.new', |
| 66 |
value => 'updated_value', |
| 67 |
}, |
| 68 |
], |
| 69 |
duration => '0', |
| 70 |
}, |
| 71 |
); |
| 72 |
|
| 73 |
C4::Items::ToggleNewStatus( { rules => \@rules } ); |
| 74 |
|
| 75 |
my $modified_item = C4::Items::GetItem( $itemnumber ); |
| 76 |
is( $modified_item->{new}, 'updated_value', q|ToggleNewStatus: The new value is updated|); |
| 77 |
$marc_item = C4::Items::GetMarcItem( $biblionumber, $itemnumber ); |
| 78 |
is( $marc_item->subfield($tagfield, $new_tagfield), 'updated_value', q|ToggleNewStatus: The new value is updated| ); |
| 79 |
|
| 80 |
# Update the items.new field if items.ccode eq 'DONT_EXIST' => should not be updated |
| 81 |
@rules = ( |
| 82 |
{ |
| 83 |
conditions => [ |
| 84 |
{ |
| 85 |
field => 'items.ccode', |
| 86 |
value => 'DONT_EXIST', |
| 87 |
}, |
| 88 |
], |
| 89 |
substitutions => [ |
| 90 |
{ |
| 91 |
field => 'items.new', |
| 92 |
value => 'new_updated_value', |
| 93 |
}, |
| 94 |
], |
| 95 |
duration => '0', |
| 96 |
}, |
| 97 |
); |
| 98 |
|
| 99 |
C4::Items::ToggleNewStatus( { rules => \@rules } ); |
| 100 |
|
| 101 |
$modified_item = C4::Items::GetItem( $itemnumber ); |
| 102 |
is( $modified_item->{new}, 'updated_value', q|ToggleNewStatus: The new value is not updated|); |
| 103 |
$marc_item = C4::Items::GetMarcItem( $biblionumber, $itemnumber ); |
| 104 |
is( $marc_item->subfield($tagfield, $new_tagfield), 'updated_value', q|ToggleNewStatus: The new value is not updated| ); |
| 105 |
|
| 106 |
# Play with duration |
| 107 |
$item = C4::Items::GetItem( $itemnumber ); |
| 108 |
my $dt_today = dt_from_string; |
| 109 |
my $days5ago = $dt_today->add_duration( DateTime::Duration->new( days => -5 ) ); |
| 110 |
|
| 111 |
C4::Items::ModItem( { dateaccessioned => $days5ago }, $biblionumber, $itemnumber ); |
| 112 |
$item = C4::Items::GetItem( $itemnumber ); |
| 113 |
|
| 114 |
@rules = ( |
| 115 |
{ |
| 116 |
conditions => [ |
| 117 |
{ |
| 118 |
field => 'items.ccode', |
| 119 |
value => 'FIC', |
| 120 |
}, |
| 121 |
], |
| 122 |
substitutions => [ |
| 123 |
{ |
| 124 |
field => 'items.new', |
| 125 |
value => 'new_updated_value', |
| 126 |
}, |
| 127 |
], |
| 128 |
duration => '10', |
| 129 |
}, |
| 130 |
); |
| 131 |
C4::Items::ToggleNewStatus( { rules => \@rules } ); |
| 132 |
$modified_item = C4::Items::GetItem( $itemnumber ); |
| 133 |
is( $modified_item->{new}, 'updated_value', q|ToggleNewStatus: Duration = 10 : The new value is not updated|); |
| 134 |
|
| 135 |
$rules[0]->{duration} = 5; |
| 136 |
$rules[0]->{substitutions}[0]{value} = 'new_updated_value5'; |
| 137 |
C4::Items::ToggleNewStatus( { rules => \@rules } ); |
| 138 |
$modified_item = C4::Items::GetItem( $itemnumber ); |
| 139 |
is( $modified_item->{new}, 'new_updated_value5', q|ToggleNewStatus: Duration = 5 : The new value is updated|); |
| 140 |
|
| 141 |
$rules[0]->{duration} = ''; |
| 142 |
$rules[0]->{substitutions}[0]{value} = 'new_updated_value_empty_string'; |
| 143 |
C4::Items::ToggleNewStatus( { rules => \@rules } ); |
| 144 |
$modified_item = C4::Items::GetItem( $itemnumber ); |
| 145 |
is( $modified_item->{new}, 'new_updated_value_empty_string', q|ToggleNewStatus: Duration = '' : The new value is updated|); |
| 146 |
|
| 147 |
$rules[0]->{duration} = undef; |
| 148 |
$rules[0]->{substitutions}[0]{value} = 'new_updated_value_undef'; |
| 149 |
C4::Items::ToggleNewStatus( { rules => \@rules } ); |
| 150 |
$modified_item = C4::Items::GetItem( $itemnumber ); |
| 151 |
is( $modified_item->{new}, 'new_updated_value_undef', q|ToggleNewStatus: Duration = undef : The new value is updated|); |
| 152 |
|
| 153 |
# Field deletion |
| 154 |
@rules = ( |
| 155 |
{ |
| 156 |
conditions => [ |
| 157 |
{ |
| 158 |
field => 'items.ccode', |
| 159 |
value => 'FIC', |
| 160 |
}, |
| 161 |
], |
| 162 |
substitutions => [ |
| 163 |
{ |
| 164 |
field => 'items.new', |
| 165 |
value => '', |
| 166 |
}, |
| 167 |
], |
| 168 |
duration => '0', |
| 169 |
}, |
| 170 |
); |
| 171 |
|
| 172 |
C4::Items::ToggleNewStatus( { rules => \@rules } ); |
| 173 |
|
| 174 |
$modified_item = C4::Items::GetItem( $itemnumber ); |
| 175 |
is( $modified_item->{new}, '', q|ToggleNewStatus: The new value is empty|); |
| 176 |
$marc_item = C4::Items::GetMarcItem( $biblionumber, $itemnumber ); |
| 177 |
is( $marc_item->subfield($tagfield, $new_tagfield), undef, q|ToggleNewStatus: The new field is removed from the item marc| ); |
| 178 |
|
| 179 |
# conditions multiple |
| 180 |
@rules = ( |
| 181 |
{ |
| 182 |
conditions => [ |
| 183 |
{ |
| 184 |
field => 'items.ccode', |
| 185 |
value => 'FIC', |
| 186 |
}, |
| 187 |
{ |
| 188 |
field => 'items.homebranch', |
| 189 |
value => 'CPL', |
| 190 |
}, |
| 191 |
], |
| 192 |
substitutions => [ |
| 193 |
{ |
| 194 |
field => 'items.new', |
| 195 |
value => 'new_value', |
| 196 |
}, |
| 197 |
], |
| 198 |
duration => '0', |
| 199 |
}, |
| 200 |
); |
| 201 |
|
| 202 |
C4::Items::ToggleNewStatus( { rules => \@rules } ); |
| 203 |
|
| 204 |
$modified_item = C4::Items::GetItem( $itemnumber ); |
| 205 |
is( $modified_item->{new}, 'new_value', q|ToggleNewStatus: conditions multiple: all match, the new value is updated|); |
| 206 |
|
| 207 |
@rules = ( |
| 208 |
{ |
| 209 |
conditions => [ |
| 210 |
{ |
| 211 |
field => 'items.ccode', |
| 212 |
value => 'FIC', |
| 213 |
}, |
| 214 |
{ |
| 215 |
field => 'items.homebranch', |
| 216 |
value => 'DONT_EXIST', |
| 217 |
}, |
| 218 |
], |
| 219 |
substitutions => [ |
| 220 |
{ |
| 221 |
field => 'items.new', |
| 222 |
value => 'new_updated_value', |
| 223 |
}, |
| 224 |
], |
| 225 |
duration => '0', |
| 226 |
}, |
| 227 |
); |
| 228 |
|
| 229 |
C4::Items::ToggleNewStatus( { rules => \@rules } ); |
| 230 |
|
| 231 |
$modified_item = C4::Items::GetItem( $itemnumber ); |
| 232 |
is( $modified_item->{new}, 'new_value', q|ToggleNewStatus: conditions multiple: at least 1 condition does not match, the new value is not updated|); |
| 233 |
|
| 234 |
@rules = ( |
| 235 |
{ |
| 236 |
conditions => [ |
| 237 |
{ |
| 238 |
field => 'items.ccode', |
| 239 |
value => 'FIC|NFIC', |
| 240 |
}, |
| 241 |
{ |
| 242 |
field => 'items.homebranch', |
| 243 |
value => 'MPL|CPL', |
| 244 |
}, |
| 245 |
], |
| 246 |
substitutions => [ |
| 247 |
{ |
| 248 |
field => 'items.new', |
| 249 |
value => 'new_updated_value', |
| 250 |
}, |
| 251 |
], |
| 252 |
duration => '0', |
| 253 |
}, |
| 254 |
); |
| 255 |
|
| 256 |
C4::Items::ToggleNewStatus( { rules => \@rules } ); |
| 257 |
|
| 258 |
$modified_item = C4::Items::GetItem( $itemnumber ); |
| 259 |
is( $modified_item->{new}, 'new_updated_value', q|ToggleNewStatus: conditions multiple: the 2 conditions match, the new value is updated|); |
| 260 |
|