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( ModCourse ModCourseItem ModCourseReserve GetCourse GetCourseItem DelCourse DelCourseReserve );
26 use Test::More tests => 36;
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,
44 homebranch_enabled => 1,
45 holdingbranch_enabled => 1,
46 location_enabled => 1,
50 holdingbranch => 'B2',
54 my $course = $builder->build({
55 source => 'CourseReserve',
61 my $cr_id = ModCourseReserve(
62 course_id => $course->{course_id},
68 my $course_item = GetCourseItem( ci_id => $ci_id );
69 is($course_item->{itype_storage}, 'CD_foo', 'Course item itype storage should be CD_foo');
70 is($course_item->{ccode_storage}, 'CD', 'Course item ccode storage should be CD');
71 is($course_item->{homebranch_storage}, 'B1', 'Course item holding branch storage should be B1');
72 is($course_item->{holdingbranch_storage}, 'B1', 'Course item holding branch storage should be B1');
73 is($course_item->{location_storage}, 'HR', 'Course item location storage should be HR');
75 my $item = Koha::Items->find($itemnumber);
76 is($item->effective_itemtype, 'BK_foo', 'Item type in course should be BK_foo');
77 is($item->ccode, 'BOOK', 'Item ccode in course should be BOOK');
78 is($item->homebranch, 'B2', 'Item home branch in course should be B2');
79 is($item->holdingbranch, 'B2', 'Item holding branch in course should be B2');
80 is($item->location, 'TH', 'Item location in course should be TH');
83 itemnumber => $itemnumber,
86 homebranch_enabled => 1,
87 holdingbranch_enabled => 1,
88 location_enabled => 1,
92 holdingbranch => 'B3',
97 course_id => $course->{course_id},
103 $course_item = GetCourseItem( ci_id => $ci_id );
104 is($course_item->{itype_storage}, 'CD_foo', 'Course item itype storage should be CD_foo');
105 is($course_item->{ccode_storage}, 'CD', 'Course item ccode storage should be CD');
106 is($course_item->{homebranch_storage}, 'B1', 'Course item home branch storage should be B1');
107 is($course_item->{holdingbranch_storage}, 'B1', 'Course item holding branch storage should be B1');
108 is($course_item->{location_storage}, 'HR', 'Course item location storage should be HR');
110 $item = Koha::Items->find($itemnumber);
111 is($item->effective_itemtype, 'BK_foo', 'Item type in course should be BK_foo');
112 is($item->ccode, 'DVD', 'Item ccode in course should be DVD');
113 is($item->homebranch, 'B3', 'Item home branch in course should be B3');
114 is($item->holdingbranch, 'B3', 'Item holding branch in course should be B3');
115 is($item->location, 'TH', 'Item location in course should be TH');
117 DelCourseReserve( cr_id => $cr_id );
118 $item = Koha::Items->find($itemnumber);
119 is($item->effective_itemtype, 'CD_foo', 'Item type removed from course should be set back to CD_foo');
120 is($item->ccode, 'CD', 'Item ccode removed from course should be set back to CD');
121 is($item->homebranch, 'B1', 'Item home branch removed from course should be set back B1');
122 is($item->holdingbranch, 'B1', 'Item holding branch removed from course should be set back B1');
123 is($item->location, 'HR', 'Item location removed from course should be TH');
125 # Test the specific case described on bug #10382.
126 $item->ccode('')->store;
128 $item = Koha::Items->find($itemnumber);
129 is($item->ccode, '', 'Item ccode should be empty');
131 my $ci_id2 = ModCourseItem(
132 itemnumber => $itemnumber,
135 homebranch_enabled => 1,
136 holdingbranch_enabled => 1,
137 location_enabled => 1,
141 holdingbranch => 'B1',
145 my $cr_id2 = ModCourseReserve(
146 course_id => $course->{course_id},
152 $item = Koha::Items->find($itemnumber);
153 is($item->ccode, 'BOOK', 'Item ccode should be BOOK');
155 my $course_item2 = GetCourseItem( ci_id => $ci_id2 );
156 is($course_item2->{ccode_storage}, '', 'Course item ccode storage should be empty');
159 itemnumber => $itemnumber,
162 homebranch_enabled => 1,
163 holdingbranch_enabled => 1,
164 location_enabled => 1,
168 holdingbranch => 'B1',
173 course_id => $course->{course_id},
179 $item = Koha::Items->find($itemnumber);
180 is($item->ccode, 'DVD', 'Item ccode should be DVD');
183 itemnumber => $itemnumber,
186 homebranch_enabled => 0, # LEAVE UNCHANGED
187 holdingbranch_enabled => 0, # LEAVE UNCHANGED
188 location_enabled => 1,
191 holdingbranch => undef, # LEAVE UNCHANGED
194 $item = Koha::Items->find($itemnumber);
195 is($item->ccode, 'BOOK', 'Item ccode should be BOOK');
197 $course_item2 = GetCourseItem( ci_id => $ci_id2 );
198 is($course_item2->{ccode_storage}, '', 'Course item ccode storage should be empty');
200 DelCourseReserve( cr_id => $cr_id2 );
201 $item = Koha::Items->find($itemnumber);
202 is($item->ccode, '', 'Item ccode should be set back to empty');
204 subtest 'Ensure modifying fields on existing course items updates the item and course item' => sub {
207 my ($biblionumber, $itemnumber) = create_bib_and_item();
208 my $ci_id = ModCourseItem(
209 itemnumber => $itemnumber,
212 homebranch_enabled => 0,
213 holdingbranch_enabled => 0,
214 location_enabled => 0,
217 my $course = $builder->build({
218 source => 'CourseReserve',
224 my $cr_id = ModCourseReserve(
225 course_id => $course->{course_id},
231 my $course_item = GetCourseItem( ci_id => $ci_id );
232 is($course_item->{itype_storage}, undef, 'Course item itype storage should be undef');
233 is($course_item->{ccode_storage}, undef, 'Course item ccode storage should be undef');
234 is($course_item->{homebranch_storage}, undef, 'Course item holding branch storage should be undef');
235 is($course_item->{holdingbranch_storage}, undef, 'Course item holding branch storage should be undef');
236 is($course_item->{location_storage}, undef, 'Course item location storage should be undef');
238 is($course_item->{itype}, undef, 'Course item itype should be undef');
239 is($course_item->{ccode}, undef, 'Course item ccode should be undef');
240 is($course_item->{homebranch}, undef, 'Course item holding branch should be undef');
241 is($course_item->{holdingbranch}, undef, 'Course item holding branch should be undef');
242 is($course_item->{location}, undef, 'Course item location should be undef');
244 is($course_item->{itype_enabled}, 0, 'Course item itype enabled should be 0');
245 is($course_item->{ccode_enabled}, 0, 'Course item ccode enabled should be 0');
246 is($course_item->{homebranch_enabled}, 0, 'Course item holding branch enabled should be 0');
247 is($course_item->{holdingbranch_enabled}, 0, 'Course item holding branch enabled should be 0');
248 is($course_item->{location_enabled}, 0, 'Course item location enabled should be 0');
250 my $item = Koha::Items->find($itemnumber);
251 is($item->effective_itemtype, 'CD_foo', 'Item type in course should be CD_foo');
252 is($item->ccode, 'CD', 'Item ccode in course should be CD');
253 is($item->homebranch, 'B1', 'Item home branch in course should be B1');
254 is($item->holdingbranch, 'B1', 'Item holding branch in course should be B1');
255 is($item->location, 'HR', 'Item location in course should be HR');
258 itemnumber => $itemnumber,
261 homebranch_enabled => 1,
262 holdingbranch_enabled => 1,
263 location_enabled => 1,
267 holdingbranch => 'B2',
271 $course_item = GetCourseItem( ci_id => $ci_id );
272 is($course_item->{itype_storage}, 'CD_foo', 'Course item itype storage should be CD_foo');
273 is($course_item->{ccode_storage}, 'CD', 'Course item ccode storage should be CD');
274 is($course_item->{homebranch_storage}, 'B1', 'Course item holding branch storage should be B1');
275 is($course_item->{holdingbranch_storage}, 'B1', 'Course item holding branch storage should be B1');
276 is($course_item->{location_storage}, 'HR', 'Course item location storage should be HR');
278 is($course_item->{itype}, 'BK_foo', 'Course item itype should be BK_foo');
279 is($course_item->{ccode}, 'BOOK', 'Course item ccode should be BOOK');
280 is($course_item->{homebranch}, 'B2', 'Course item holding branch should be B2');
281 is($course_item->{holdingbranch}, 'B2', 'Course item holding branch should be B2');
282 is($course_item->{location}, 'TH', 'Course item location should be TH');
284 is($course_item->{itype_enabled}, 1, 'Course item itype enabled should be 1');
285 is($course_item->{ccode_enabled}, 1, 'Course item ccode enabled should be 1');
286 is($course_item->{homebranch_enabled}, 1, 'Course item holding branch enabled should be 1');
287 is($course_item->{holdingbranch_enabled}, 1, 'Course item holding branch enabled should be 1');
288 is($course_item->{location_enabled}, 1, 'Course item location enabled should be 1');
290 $item = Koha::Items->find($itemnumber);
291 is($item->effective_itemtype, 'BK_foo', 'Item type in course should be BK_foo');
292 is($item->ccode, 'BOOK', 'Item ccode in course should be BOOK');
293 is($item->homebranch, 'B2', 'Item home branch in course should be B2');
294 is($item->holdingbranch, 'B2', 'Item holding branch in course should be B2');
295 is($item->location, 'TH', 'Item location in course should be TH');
297 # Test removing fields from an active course item
299 itemnumber => $itemnumber,
302 homebranch_enabled => 0,
303 holdingbranch_enabled => 0,
304 location_enabled => 0,
307 $course_item = GetCourseItem( ci_id => $ci_id );
308 is($course_item->{itype_storage}, undef, 'Course item itype storage should be undef');
309 is($course_item->{ccode_storage}, undef, 'Course item ccode storage should be undef');
310 is($course_item->{homebranch_storage}, undef, 'Course item holding branch storage should be undef');
311 is($course_item->{holdingbranch_storage}, undef, 'Course item holding branch storage should be undef');
312 is($course_item->{location_storage}, undef, 'Course item location storage should be undef');
314 is($course_item->{itype}, undef, 'Course item itype should be undef');
315 is($course_item->{ccode}, undef, 'Course item ccode should be undef');
316 is($course_item->{homebranch}, undef, 'Course item holding branch should be undef');
317 is($course_item->{holdingbranch}, undef, 'Course item holding branch should be undef');
318 is($course_item->{location}, undef, 'Course item location should be undef');
320 is($course_item->{itype_enabled}, 0, 'Course item itype enabled should be 0');
321 is($course_item->{ccode_enabled}, 0, 'Course item ccode enabled should be 0');
322 is($course_item->{homebranch_enabled}, 0, 'Course item holding branch enabled should be 0');
323 is($course_item->{holdingbranch_enabled}, 0, 'Course item holding branch enabled should be 0');
324 is($course_item->{location_enabled}, 0, 'Course item location enabled should be 0');
326 $item = Koha::Items->find($itemnumber);
327 is($item->effective_itemtype, 'CD_foo', 'Item type in course should be CD_foo');
328 is($item->ccode, 'CD', 'Item ccode in course should be CD');
329 is($item->homebranch, 'B1', 'Item home branch in course should be B1');
330 is($item->holdingbranch, 'B1', 'Item holding branch in course should be B1');
331 is($item->location, 'HR', 'Item location in course should be HR');
334 subtest 'Ensure item info is preserved' => sub {
337 my $course = $builder->build({ source => 'Course' });
338 my $item = $builder->build_sample_item({ ccode=>"grasshopper", location=>"transylvania"});
339 #Add course item but change nothing
340 my $course_item_id = ModCourseItem(
341 itemnumber => $item->itemnumber,
348 my $course_reserve_id = ModCourseReserve(
349 course_id => $course->{course_id},
350 ci_id => $course_item_id,
354 #Remove course reservei
355 DelCourseReserve( cr_id => $course_reserve_id );
356 my $item_after = Koha::Items->find( $item->itemnumber );
357 is( $item->effective_itemtype, $item_after->itype, "Itemtype is unchanged after adding to and removing from course reserves for inactive course");
358 is( $item->location, $item_after->location, "Location is unchanged after adding to and removing from course reserves for inactive course");
359 is( $item->holdingbranch, $item_after->holdingbranch, "Holdingbranch is unchanged after adding to and removing from course reserves for inactive course");
360 is( $item->ccode, $item_after->ccode, "Collection is unchanged after adding to and removing from course reserves for inactive course");
362 $course = $builder->build({ source => 'Course' });
363 $item = $builder->build_sample_item({ ccode=>"grasshopper", location=>"transylvania"});
364 #Add course item but change nothing
365 $course_item_id = ModCourseItem(
366 itemnumber => $item->itemnumber,
373 $course_reserve_id = ModCourseReserve(
374 course_id => $course->{course_id},
375 ci_id => $course_item_id,
379 #Remove course reserve
380 DelCourseReserve( cr_id => $course_reserve_id );
381 $item_after = Koha::Items->find( $item->itemnumber );
382 is( $item->effective_itemtype, $item_after->itype, "Itemtype is unchanged after adding to and removing from course reserves for inactive course");
383 is( $item->location, $item_after->location, "Location is unchanged after adding to and removing from course reserves for inactive course");
384 is( $item->holdingbranch, $item_after->holdingbranch, "Holdingbranch is unchanged after adding to and removing from course reserves for inactive course");
385 is( $item->ccode, $item_after->ccode, "Collection is unchanged after adding to and removing from course reserves for inactive course");
389 subtest 'biblio added to course without items' => sub {
392 my $course = $builder->build({ source => 'Course' });
393 #Add course item but change nothing
394 my $course_item_id = ModCourseItem(
396 biblionumber => $biblionumber,
403 my $course_reserve_id = ModCourseReserve(
404 course_id => $course->{course_id},
405 ci_id => $course_item_id,
406 staff_note => 'staff note',
410 my $course_item = GetCourseItem( ci_id => $course_item_id );
411 is( $course_item->{itemnumber}, undef, "Course reserve with no item correctly added" );
416 $schema->storage->txn_rollback;
418 sub create_dependent_objects {
420 source => 'Itemtype',
422 itemtype => 'CD_foo',
423 description => 'Compact Disk'
428 source => 'Itemtype',
430 itemtype => 'BK_foo',
431 description => 'Book'
439 branchname => 'Branch 1'
447 branchname => 'Branch 2'
455 branchname => 'Branch 3'
460 source => 'AuthorisedValue',
463 authorised_value => 'BOOK',
469 source => 'AuthorisedValue',
472 authorised_value => 'DVD',
478 source => 'AuthorisedValue',
481 authorised_value => 'CD',
482 lib => 'Compact Disk'
487 source => 'AuthorisedValue',
490 authorised_value => 'HR',
496 source => 'AuthorisedValue',
499 authorised_value => 'TH',
505 sub create_bib_and_item {
506 my $item = $builder->build_sample_item(
514 return ($item->biblionumber, $item->itemnumber);