Lines 1-283
Link Here
|
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::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 |
MARC::Field->new('942', ' ', ' ', c => 'ITEMTYPE_T'), |
35 |
); |
36 |
my ($biblionumber, undef) = C4::Biblio::AddBiblio($record, ''); |
37 |
|
38 |
my ($item_bibnum, $item_bibitemnum, $itemnumber) = C4::Items::AddItem( |
39 |
{ |
40 |
homebranch => 'CPL', |
41 |
holdingbranch => 'CPL', |
42 |
new => 'new_value', |
43 |
ccode => 'FIC', |
44 |
}, |
45 |
$biblionumber |
46 |
); |
47 |
|
48 |
my $item = C4::Items::GetItem( $itemnumber ); |
49 |
is ( $item->{new}, 'new_value', q|AddItem insert the 'new' field| ); |
50 |
|
51 |
my ( $tagfield, undef ) = GetMarcFromKohaField('items.itemnumber', ''); |
52 |
my $marc_item = C4::Items::GetMarcItem( $biblionumber, $itemnumber ); |
53 |
is( $marc_item->subfield($tagfield, $new_tagfield), 'new_value', q|Koha mapping is correct|); |
54 |
|
55 |
# Update the items.new field if items.ccode eq 'FIC' => should be updated |
56 |
my @rules = ( |
57 |
{ |
58 |
conditions => [ |
59 |
{ |
60 |
field => 'items.ccode', |
61 |
value => 'FIC', |
62 |
}, |
63 |
], |
64 |
substitutions => [ |
65 |
{ |
66 |
field => 'items.new', |
67 |
value => 'updated_value', |
68 |
}, |
69 |
], |
70 |
age => '0', |
71 |
}, |
72 |
); |
73 |
|
74 |
C4::Items::ToggleNewStatus( { rules => \@rules } ); |
75 |
|
76 |
my $modified_item = C4::Items::GetItem( $itemnumber ); |
77 |
is( $modified_item->{new}, 'updated_value', q|ToggleNewStatus: The new value is updated|); |
78 |
$marc_item = C4::Items::GetMarcItem( $biblionumber, $itemnumber ); |
79 |
is( $marc_item->subfield($tagfield, $new_tagfield), 'updated_value', q|ToggleNewStatus: The new value is updated| ); |
80 |
|
81 |
# Update the items.new field if items.ccode eq 'DONT_EXIST' => should not be updated |
82 |
@rules = ( |
83 |
{ |
84 |
conditions => [ |
85 |
{ |
86 |
field => 'items.ccode', |
87 |
value => 'DONT_EXIST', |
88 |
}, |
89 |
], |
90 |
substitutions => [ |
91 |
{ |
92 |
field => 'items.new', |
93 |
value => 'new_updated_value', |
94 |
}, |
95 |
], |
96 |
age => '0', |
97 |
}, |
98 |
); |
99 |
|
100 |
C4::Items::ToggleNewStatus( { rules => \@rules } ); |
101 |
|
102 |
$modified_item = C4::Items::GetItem( $itemnumber ); |
103 |
is( $modified_item->{new}, 'updated_value', q|ToggleNewStatus: The new value is not updated|); |
104 |
$marc_item = C4::Items::GetMarcItem( $biblionumber, $itemnumber ); |
105 |
is( $marc_item->subfield($tagfield, $new_tagfield), 'updated_value', q|ToggleNewStatus: The new value is not updated| ); |
106 |
|
107 |
# Play with age |
108 |
$item = C4::Items::GetItem( $itemnumber ); |
109 |
my $dt_today = dt_from_string; |
110 |
my $days5ago = $dt_today->add_duration( DateTime::Duration->new( days => -5 ) ); |
111 |
|
112 |
C4::Items::ModItem( { dateaccessioned => $days5ago }, $biblionumber, $itemnumber ); |
113 |
$item = C4::Items::GetItem( $itemnumber ); |
114 |
|
115 |
@rules = ( |
116 |
{ |
117 |
conditions => [ |
118 |
{ |
119 |
field => 'items.ccode', |
120 |
value => 'FIC', |
121 |
}, |
122 |
], |
123 |
substitutions => [ |
124 |
{ |
125 |
field => 'items.new', |
126 |
value => 'new_updated_value', |
127 |
}, |
128 |
], |
129 |
age => '10', |
130 |
}, |
131 |
); |
132 |
C4::Items::ToggleNewStatus( { rules => \@rules } ); |
133 |
$modified_item = C4::Items::GetItem( $itemnumber ); |
134 |
is( $modified_item->{new}, 'updated_value', q|ToggleNewStatus: Age = 10 : The new value is not updated|); |
135 |
|
136 |
$rules[0]->{age} = 5; |
137 |
$rules[0]->{substitutions}[0]{value} = 'new_updated_value5'; |
138 |
C4::Items::ToggleNewStatus( { rules => \@rules } ); |
139 |
$modified_item = C4::Items::GetItem( $itemnumber ); |
140 |
is( $modified_item->{new}, 'new_updated_value5', q|ToggleNewStatus: Age = 5 : The new value is updated|); |
141 |
|
142 |
$rules[0]->{age} = ''; |
143 |
$rules[0]->{substitutions}[0]{value} = 'new_updated_value_empty_string'; |
144 |
C4::Items::ToggleNewStatus( { rules => \@rules } ); |
145 |
$modified_item = C4::Items::GetItem( $itemnumber ); |
146 |
is( $modified_item->{new}, 'new_updated_value_empty_string', q|ToggleNewStatus: Age = '' : The new value is updated|); |
147 |
|
148 |
$rules[0]->{age} = undef; |
149 |
$rules[0]->{substitutions}[0]{value} = 'new_updated_value_undef'; |
150 |
C4::Items::ToggleNewStatus( { rules => \@rules } ); |
151 |
$modified_item = C4::Items::GetItem( $itemnumber ); |
152 |
is( $modified_item->{new}, 'new_updated_value_undef', q|ToggleNewStatus: Age = undef : The new value is updated|); |
153 |
|
154 |
# Field deletion |
155 |
@rules = ( |
156 |
{ |
157 |
conditions => [ |
158 |
{ |
159 |
field => 'items.ccode', |
160 |
value => 'FIC', |
161 |
}, |
162 |
], |
163 |
substitutions => [ |
164 |
{ |
165 |
field => 'items.new', |
166 |
value => '', |
167 |
}, |
168 |
], |
169 |
age => '0', |
170 |
}, |
171 |
); |
172 |
|
173 |
C4::Items::ToggleNewStatus( { rules => \@rules } ); |
174 |
|
175 |
$modified_item = C4::Items::GetItem( $itemnumber ); |
176 |
is( $modified_item->{new}, '', q|ToggleNewStatus: The new value is empty|); |
177 |
$marc_item = C4::Items::GetMarcItem( $biblionumber, $itemnumber ); |
178 |
is( $marc_item->subfield($tagfield, $new_tagfield), undef, q|ToggleNewStatus: The new field is removed from the item marc| ); |
179 |
|
180 |
# conditions multiple |
181 |
@rules = ( |
182 |
{ |
183 |
conditions => [ |
184 |
{ |
185 |
field => 'items.ccode', |
186 |
value => 'FIC', |
187 |
}, |
188 |
{ |
189 |
field => 'items.homebranch', |
190 |
value => 'CPL', |
191 |
}, |
192 |
], |
193 |
substitutions => [ |
194 |
{ |
195 |
field => 'items.new', |
196 |
value => 'new_value', |
197 |
}, |
198 |
], |
199 |
age => '0', |
200 |
}, |
201 |
); |
202 |
|
203 |
C4::Items::ToggleNewStatus( { rules => \@rules } ); |
204 |
|
205 |
$modified_item = C4::Items::GetItem( $itemnumber ); |
206 |
is( $modified_item->{new}, 'new_value', q|ToggleNewStatus: conditions multiple: all match, the new value is updated|); |
207 |
|
208 |
@rules = ( |
209 |
{ |
210 |
conditions => [ |
211 |
{ |
212 |
field => 'items.ccode', |
213 |
value => 'FIC', |
214 |
}, |
215 |
{ |
216 |
field => 'items.homebranch', |
217 |
value => 'DONT_EXIST', |
218 |
}, |
219 |
], |
220 |
substitutions => [ |
221 |
{ |
222 |
field => 'items.new', |
223 |
value => 'new_updated_value', |
224 |
}, |
225 |
], |
226 |
age => '0', |
227 |
}, |
228 |
); |
229 |
|
230 |
C4::Items::ToggleNewStatus( { rules => \@rules } ); |
231 |
|
232 |
$modified_item = C4::Items::GetItem( $itemnumber ); |
233 |
is( $modified_item->{new}, 'new_value', q|ToggleNewStatus: conditions multiple: at least 1 condition does not match, the new value is not updated|); |
234 |
|
235 |
@rules = ( |
236 |
{ |
237 |
conditions => [ |
238 |
{ |
239 |
field => 'items.ccode', |
240 |
value => 'FIC|NFIC', |
241 |
}, |
242 |
{ |
243 |
field => 'items.homebranch', |
244 |
value => 'MPL|CPL', |
245 |
}, |
246 |
], |
247 |
substitutions => [ |
248 |
{ |
249 |
field => 'items.new', |
250 |
value => 'new_updated_value', |
251 |
}, |
252 |
], |
253 |
age => '0', |
254 |
}, |
255 |
); |
256 |
|
257 |
C4::Items::ToggleNewStatus( { rules => \@rules } ); |
258 |
|
259 |
$modified_item = C4::Items::GetItem( $itemnumber ); |
260 |
is( $modified_item->{new}, 'new_updated_value', q|ToggleNewStatus: conditions multiple: the 2 conditions match, the new value is updated|); |
261 |
|
262 |
@rules = ( |
263 |
{ |
264 |
conditions => [ |
265 |
{ |
266 |
field => 'biblioitems.itemtype', |
267 |
value => 'ITEMTYPE_T', |
268 |
}, |
269 |
], |
270 |
substitutions => [ |
271 |
{ |
272 |
field => 'items.new', |
273 |
value => 'another_new_updated_value', |
274 |
}, |
275 |
], |
276 |
age => '0', |
277 |
}, |
278 |
); |
279 |
|
280 |
C4::Items::ToggleNewStatus( { rules => \@rules } ); |
281 |
|
282 |
$modified_item = C4::Items::GetItem( $itemnumber ); |
283 |
is( $modified_item->{new}, 'another_new_updated_value', q|ToggleNewStatus: conditions on biblioitems|); |