3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use t::lib::TestBuilder;
22 use C4::CourseReserves qw/ModCourseItem ModCourseReserve DelCourseReserve GetCourseItem/;
26 use Test::More tests => 35;
29 require_ok('C4::CourseReserves');
32 my $schema = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
35 my $builder = t::lib::TestBuilder->new();
37 create_dependent_objects();
38 my ($biblionumber, $itemnumber) = create_bib_and_item();
40 my $ci_id = ModCourseItem(
41 itemnumber => $itemnumber,
42 biblionumber => $biblionumber,
45 homebranch_enabled => 1,
46 holdingbranch_enabled => 1,
47 location_enabled => 1,
51 holdingbranch => 'B2',
55 my $course = $builder->build({
56 source => 'CourseReserve',
63 my $cr_id = ModCourseReserve(
64 course_id => $course->{course_id},
70 my $course_item = GetCourseItem( ci_id => $ci_id );
71 is($course_item->{itype_storage}, 'CD_foo', 'Course item itype storage should be CD_foo');
72 is($course_item->{ccode_storage}, 'CD', 'Course item ccode storage should be CD');
73 is($course_item->{homebranch_storage}, 'B1', 'Course item holding branch storage should be B1');
74 is($course_item->{holdingbranch_storage}, 'B1', 'Course item holding branch storage should be B1');
75 is($course_item->{location_storage}, 'HR', 'Course item location storage should be HR');
77 my $item = Koha::Items->find($itemnumber);
78 is($item->effective_itemtype, 'BK_foo', 'Item type in course should be BK_foo');
79 is($item->ccode, 'BOOK', 'Item ccode in course should be BOOK');
80 is($item->homebranch, 'B2', 'Item home branch in course should be B2');
81 is($item->holdingbranch, 'B2', 'Item holding branch in course should be B2');
82 is($item->location, 'TH', 'Item location in course should be TH');
85 itemnumber => $itemnumber,
86 biblionumber => $biblionumber,
89 homebranch_enabled => 1,
90 holdingbranch_enabled => 1,
91 location_enabled => 1,
95 holdingbranch => 'B3',
100 course_id => $course->{course_id},
106 $course_item = GetCourseItem( ci_id => $ci_id );
107 is($course_item->{itype_storage}, 'CD_foo', 'Course item itype storage should be CD_foo');
108 is($course_item->{ccode_storage}, 'CD', 'Course item ccode storage should be CD');
109 is($course_item->{homebranch_storage}, 'B1', 'Course item home branch storage should be B1');
110 is($course_item->{holdingbranch_storage}, 'B1', 'Course item holding branch storage should be B1');
111 is($course_item->{location_storage}, 'HR', 'Course item location storage should be HR');
113 $item = Koha::Items->find($itemnumber);
114 is($item->effective_itemtype, 'BK_foo', 'Item type in course should be BK_foo');
115 is($item->ccode, 'DVD', 'Item ccode in course should be DVD');
116 is($item->homebranch, 'B3', 'Item home branch in course should be B3');
117 is($item->holdingbranch, 'B3', 'Item holding branch in course should be B3');
118 is($item->location, 'TH', 'Item location in course should be TH');
120 DelCourseReserve( cr_id => $cr_id );
121 $item = Koha::Items->find($itemnumber);
122 is($item->effective_itemtype, 'CD_foo', 'Item type removed from course should be set back to CD_foo');
123 is($item->ccode, 'CD', 'Item ccode removed from course should be set back to CD');
124 is($item->homebranch, 'B1', 'Item home branch removed from course should be set back B1');
125 is($item->holdingbranch, 'B1', 'Item holding branch removed from course should be set back B1');
126 is($item->location, 'HR', 'Item location removed from course should be TH');
128 # Test the specific case described on bug #10382.
129 $item->ccode('')->store;
131 $item = Koha::Items->find($itemnumber);
132 is($item->ccode, '', 'Item ccode should be empty');
134 my $ci_id2 = ModCourseItem(
135 itemnumber => $itemnumber,
138 homebranch_enabled => 1,
139 holdingbranch_enabled => 1,
140 location_enabled => 1,
144 holdingbranch => 'B1',
148 my $cr_id2 = ModCourseReserve(
149 course_id => $course->{course_id},
155 $item = Koha::Items->find($itemnumber);
156 is($item->ccode, 'BOOK', 'Item ccode should be BOOK');
158 my $course_item2 = GetCourseItem( ci_id => $ci_id2 );
159 is($course_item2->{ccode_storage}, '', 'Course item ccode storage should be empty');
162 itemnumber => $itemnumber,
163 biblionumber => $biblionumber,
166 homebranch_enabled => 1,
167 holdingbranch_enabled => 1,
168 location_enabled => 1,
172 holdingbranch => 'B1',
177 course_id => $course->{course_id},
183 $item = Koha::Items->find($itemnumber);
184 is($item->ccode, 'DVD', 'Item ccode should be DVD');
187 itemnumber => $itemnumber,
188 biblionumber => $biblionumber,
191 homebranch_enabled => 0, # LEAVE UNCHANGED
192 holdingbranch_enabled => 0, # LEAVE UNCHANGED
193 location_enabled => 1,
196 holdingbranch => undef, # LEAVE UNCHANGED
199 $item = Koha::Items->find($itemnumber);
200 is($item->ccode, 'BOOK', 'Item ccode should be BOOK');
202 $course_item2 = GetCourseItem( ci_id => $ci_id2 );
203 is($course_item2->{ccode_storage}, '', 'Course item ccode storage should be empty');
205 DelCourseReserve( cr_id => $cr_id2 );
206 $item = Koha::Items->find($itemnumber);
207 is($item->ccode, '', 'Item ccode should be set back to empty');
209 subtest 'Ensure modifying fields on existing course items updates the item and course item' => sub {
212 my ($biblionumber, $itemnumber) = create_bib_and_item();
213 my $ci_id = ModCourseItem(
214 itemnumber => $itemnumber,
215 biblionumber => $biblionumber,
218 homebranch_enabled => 0,
219 holdingbranch_enabled => 0,
220 location_enabled => 0,
223 my $course = $builder->build({
224 source => 'CourseReserve',
231 my $cr_id = ModCourseReserve(
232 course_id => $course->{course_id},
238 my $course_item = GetCourseItem( ci_id => $ci_id );
239 is($course_item->{itype_storage}, undef, 'Course item itype storage should be undef');
240 is($course_item->{ccode_storage}, undef, 'Course item ccode storage should be undef');
241 is($course_item->{homebranch_storage}, undef, 'Course item holding branch storage should be undef');
242 is($course_item->{holdingbranch_storage}, undef, 'Course item holding branch storage should be undef');
243 is($course_item->{location_storage}, undef, 'Course item location storage should be undef');
245 is($course_item->{itype}, undef, 'Course item itype should be undef');
246 is($course_item->{ccode}, undef, 'Course item ccode should be undef');
247 is($course_item->{homebranch}, undef, 'Course item holding branch should be undef');
248 is($course_item->{holdingbranch}, undef, 'Course item holding branch should be undef');
249 is($course_item->{location}, undef, 'Course item location should be undef');
251 is($course_item->{itype_enabled}, 0, 'Course item itype enabled should be 0');
252 is($course_item->{ccode_enabled}, 0, 'Course item ccode enabled should be 0');
253 is($course_item->{homebranch_enabled}, 0, 'Course item holding branch enabled should be 0');
254 is($course_item->{holdingbranch_enabled}, 0, 'Course item holding branch enabled should be 0');
255 is($course_item->{location_enabled}, 0, 'Course item location enabled should be 0');
257 my $item = Koha::Items->find($itemnumber);
258 is($item->effective_itemtype, 'CD_foo', 'Item type in course should be CD_foo');
259 is($item->ccode, 'CD', 'Item ccode in course should be CD');
260 is($item->homebranch, 'B1', 'Item home branch in course should be B1');
261 is($item->holdingbranch, 'B1', 'Item holding branch in course should be B1');
262 is($item->location, 'HR', 'Item location in course should be HR');
265 itemnumber => $itemnumber,
268 homebranch_enabled => 1,
269 holdingbranch_enabled => 1,
270 location_enabled => 1,
274 holdingbranch => 'B2',
278 $course_item = GetCourseItem( ci_id => $ci_id );
279 is($course_item->{itype_storage}, 'CD_foo', 'Course item itype storage should be CD_foo');
280 is($course_item->{ccode_storage}, 'CD', 'Course item ccode storage should be CD');
281 is($course_item->{homebranch_storage}, 'B1', 'Course item holding branch storage should be B1');
282 is($course_item->{holdingbranch_storage}, 'B1', 'Course item holding branch storage should be B1');
283 is($course_item->{location_storage}, 'HR', 'Course item location storage should be HR');
285 is($course_item->{itype}, 'BK_foo', 'Course item itype should be BK_foo');
286 is($course_item->{ccode}, 'BOOK', 'Course item ccode should be BOOK');
287 is($course_item->{homebranch}, 'B2', 'Course item holding branch should be B2');
288 is($course_item->{holdingbranch}, 'B2', 'Course item holding branch should be B2');
289 is($course_item->{location}, 'TH', 'Course item location should be TH');
291 is($course_item->{itype_enabled}, 1, 'Course item itype enabled should be 1');
292 is($course_item->{ccode_enabled}, 1, 'Course item ccode enabled should be 1');
293 is($course_item->{homebranch_enabled}, 1, 'Course item holding branch enabled should be 1');
294 is($course_item->{holdingbranch_enabled}, 1, 'Course item holding branch enabled should be 1');
295 is($course_item->{location_enabled}, 1, 'Course item location enabled should be 1');
297 $item = Koha::Items->find($itemnumber);
298 is($item->effective_itemtype, 'BK_foo', 'Item type in course should be BK_foo');
299 is($item->ccode, 'BOOK', 'Item ccode in course should be BOOK');
300 is($item->homebranch, 'B2', 'Item home branch in course should be B2');
301 is($item->holdingbranch, 'B2', 'Item holding branch in course should be B2');
302 is($item->location, 'TH', 'Item location in course should be TH');
304 # Test removing fields from an active course item
306 itemnumber => $itemnumber,
307 biblionumber => $biblionumber,
310 homebranch_enabled => 0,
311 holdingbranch_enabled => 0,
312 location_enabled => 0,
315 $course_item = GetCourseItem( ci_id => $ci_id );
316 is($course_item->{itype_storage}, undef, 'Course item itype storage should be undef');
317 is($course_item->{ccode_storage}, undef, 'Course item ccode storage should be undef');
318 is($course_item->{homebranch_storage}, undef, 'Course item holding branch storage should be undef');
319 is($course_item->{holdingbranch_storage}, undef, 'Course item holding branch storage should be undef');
320 is($course_item->{location_storage}, undef, 'Course item location storage should be undef');
322 is($course_item->{itype}, undef, 'Course item itype should be undef');
323 is($course_item->{ccode}, undef, 'Course item ccode should be undef');
324 is($course_item->{homebranch}, undef, 'Course item holding branch should be undef');
325 is($course_item->{holdingbranch}, undef, 'Course item holding branch should be undef');
326 is($course_item->{location}, undef, 'Course item location should be undef');
328 is($course_item->{itype_enabled}, 0, 'Course item itype enabled should be 0');
329 is($course_item->{ccode_enabled}, 0, 'Course item ccode enabled should be 0');
330 is($course_item->{homebranch_enabled}, 0, 'Course item holding branch enabled should be 0');
331 is($course_item->{holdingbranch_enabled}, 0, 'Course item holding branch enabled should be 0');
332 is($course_item->{location_enabled}, 0, 'Course item location enabled should be 0');
334 $item = Koha::Items->find($itemnumber);
335 is($item->effective_itemtype, 'CD_foo', 'Item type in course should be CD_foo');
336 is($item->ccode, 'CD', 'Item ccode in course should be CD');
337 is($item->homebranch, 'B1', 'Item home branch in course should be B1');
338 is($item->holdingbranch, 'B1', 'Item holding branch in course should be B1');
339 is($item->location, 'HR', 'Item location in course should be HR');
342 subtest 'Ensure item info is preserved' => sub {
345 my $course = $builder->build({
351 my $item = $builder->build_sample_item({ ccode=>"grasshopper", location=>"transylvania"});
352 #Add course item but change nothing
353 my $course_item_id = ModCourseItem(
354 itemnumber => $item->itemnumber,
355 biblionumber => $biblionumber,
362 my $course_reserve_id = ModCourseReserve(
363 course_id => $course->{course_id},
364 ci_id => $course_item_id,
368 #Remove course reservei
369 DelCourseReserve( cr_id => $course_reserve_id );
370 my $item_after = Koha::Items->find( $item->itemnumber );
371 is( $item->effective_itemtype, $item_after->itype, "Itemtype is unchanged after adding to and removing from course reserves for inactive course");
372 is( $item->location, $item_after->location, "Location is unchanged after adding to and removing from course reserves for inactive course");
373 is( $item->holdingbranch, $item_after->holdingbranch, "Holdingbranch is unchanged after adding to and removing from course reserves for inactive course");
374 is( $item->ccode, $item_after->ccode, "Collection is unchanged after adding to and removing from course reserves for inactive course");
376 $course = $builder->build({
382 $item = $builder->build_sample_item({ ccode=>"grasshopper", location=>"transylvania"});
383 #Add course item but change nothing
384 $course_item_id = ModCourseItem(
385 itemnumber => $item->itemnumber,
386 biblionumber => $biblionumber,
393 $course_reserve_id = ModCourseReserve(
394 course_id => $course->{course_id},
395 ci_id => $course_item_id,
399 #Remove course reserve
400 DelCourseReserve( cr_id => $course_reserve_id );
401 $item_after = Koha::Items->find( $item->itemnumber );
402 is( $item->effective_itemtype, $item_after->itype, "Itemtype is unchanged after adding to and removing from course reserves for inactive course");
403 is( $item->location, $item_after->location, "Location is unchanged after adding to and removing from course reserves for inactive course");
404 is( $item->holdingbranch, $item_after->holdingbranch, "Holdingbranch is unchanged after adding to and removing from course reserves for inactive course");
405 is( $item->ccode, $item_after->ccode, "Collection is unchanged after adding to and removing from course reserves for inactive course");
413 $schema->storage->txn_rollback;
415 sub create_dependent_objects {
417 source => 'Itemtype',
419 itemtype => 'CD_foo',
420 description => 'Compact Disk'
425 source => 'Itemtype',
427 itemtype => 'BK_foo',
428 description => 'Book'
436 branchname => 'Branch 1'
444 branchname => 'Branch 2'
452 branchname => 'Branch 3'
457 source => 'AuthorisedValue',
460 authorised_value => 'BOOK',
466 source => 'AuthorisedValue',
469 authorised_value => 'DVD',
475 source => 'AuthorisedValue',
478 authorised_value => 'CD',
479 lib => 'Compact Disk'
484 source => 'AuthorisedValue',
487 authorised_value => 'HR',
493 source => 'AuthorisedValue',
496 authorised_value => 'TH',
502 sub create_bib_and_item {
503 my $item = $builder->build_sample_item(
511 return ($item->biblionumber, $item->itemnumber);