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,
44 homebranch_enabled => 1,
45 holdingbranch_enabled => 1,
46 location_enabled => 1,
50 holdingbranch => 'B2',
54 my $course = $builder->build({
55 source => 'CourseReserve',
62 my $cr_id = ModCourseReserve(
63 course_id => $course->{course_id},
69 my $course_item = GetCourseItem( ci_id => $ci_id );
70 is($course_item->{itype_storage}, 'CD_foo', 'Course item itype storage should be CD_foo');
71 is($course_item->{ccode_storage}, 'CD', 'Course item ccode storage should be CD');
72 is($course_item->{homebranch_storage}, 'B1', 'Course item holding branch storage should be B1');
73 is($course_item->{holdingbranch_storage}, 'B1', 'Course item holding branch storage should be B1');
74 is($course_item->{location_storage}, 'HR', 'Course item location storage should be HR');
76 my $item = Koha::Items->find($itemnumber);
77 is($item->effective_itemtype, 'BK_foo', 'Item type in course should be BK_foo');
78 is($item->ccode, 'BOOK', 'Item ccode in course should be BOOK');
79 is($item->homebranch, 'B2', 'Item home branch in course should be B2');
80 is($item->holdingbranch, 'B2', 'Item holding branch in course should be B2');
81 is($item->location, 'TH', 'Item location in course should be TH');
84 itemnumber => $itemnumber,
87 homebranch_enabled => 1,
88 holdingbranch_enabled => 1,
89 location_enabled => 1,
93 holdingbranch => 'B3',
98 course_id => $course->{course_id},
104 $course_item = GetCourseItem( ci_id => $ci_id );
105 is($course_item->{itype_storage}, 'CD_foo', 'Course item itype storage should be CD_foo');
106 is($course_item->{ccode_storage}, 'CD', 'Course item ccode storage should be CD');
107 is($course_item->{homebranch_storage}, 'B1', 'Course item home branch storage should be B1');
108 is($course_item->{holdingbranch_storage}, 'B1', 'Course item holding branch storage should be B1');
109 is($course_item->{location_storage}, 'HR', 'Course item location storage should be HR');
111 $item = Koha::Items->find($itemnumber);
112 is($item->effective_itemtype, 'BK_foo', 'Item type in course should be BK_foo');
113 is($item->ccode, 'DVD', 'Item ccode in course should be DVD');
114 is($item->homebranch, 'B3', 'Item home branch in course should be B3');
115 is($item->holdingbranch, 'B3', 'Item holding branch in course should be B3');
116 is($item->location, 'TH', 'Item location in course should be TH');
118 DelCourseReserve( cr_id => $cr_id );
119 $item = Koha::Items->find($itemnumber);
120 is($item->effective_itemtype, 'CD_foo', 'Item type removed from course should be set back to CD_foo');
121 is($item->ccode, 'CD', 'Item ccode removed from course should be set back to CD');
122 is($item->homebranch, 'B1', 'Item home branch removed from course should be set back B1');
123 is($item->holdingbranch, 'B1', 'Item holding branch removed from course should be set back B1');
124 is($item->location, 'HR', 'Item location removed from course should be TH');
126 # Test the specific case described on bug #10382.
127 $item->ccode('')->store;
129 $item = Koha::Items->find($itemnumber);
130 is($item->ccode, '', 'Item ccode should be empty');
132 my $ci_id2 = ModCourseItem(
133 itemnumber => $itemnumber,
136 homebranch_enabled => 1,
137 holdingbranch_enabled => 1,
138 location_enabled => 1,
142 holdingbranch => 'B1',
146 my $cr_id2 = ModCourseReserve(
147 course_id => $course->{course_id},
153 $item = Koha::Items->find($itemnumber);
154 is($item->ccode, 'BOOK', 'Item ccode should be BOOK');
156 my $course_item2 = GetCourseItem( ci_id => $ci_id2 );
157 is($course_item2->{ccode_storage}, '', 'Course item ccode storage should be empty');
160 itemnumber => $itemnumber,
163 homebranch_enabled => 1,
164 holdingbranch_enabled => 1,
165 location_enabled => 1,
169 holdingbranch => 'B1',
174 course_id => $course->{course_id},
180 $item = Koha::Items->find($itemnumber);
181 is($item->ccode, 'DVD', 'Item ccode should be DVD');
184 itemnumber => $itemnumber,
187 homebranch_enabled => 0, # LEAVE UNCHANGED
188 holdingbranch_enabled => 0, # LEAVE UNCHANGED
189 location_enabled => 1,
192 holdingbranch => undef, # LEAVE UNCHANGED
195 $item = Koha::Items->find($itemnumber);
196 is($item->ccode, 'BOOK', 'Item ccode should be BOOK');
198 $course_item2 = GetCourseItem( ci_id => $ci_id2 );
199 is($course_item2->{ccode_storage}, '', 'Course item ccode storage should be empty');
201 DelCourseReserve( cr_id => $cr_id2 );
202 $item = Koha::Items->find($itemnumber);
203 is($item->ccode, '', 'Item ccode should be set back to empty');
205 subtest 'Ensure modifying fields on existing course items updates the item and course item' => sub {
208 my ($biblionumber, $itemnumber) = create_bib_and_item();
209 my $ci_id = ModCourseItem(
210 itemnumber => $itemnumber,
213 homebranch_enabled => 0,
214 holdingbranch_enabled => 0,
215 location_enabled => 0,
218 my $course = $builder->build({
219 source => 'CourseReserve',
226 my $cr_id = ModCourseReserve(
227 course_id => $course->{course_id},
233 my $course_item = GetCourseItem( ci_id => $ci_id );
234 is($course_item->{itype_storage}, undef, 'Course item itype storage should be undef');
235 is($course_item->{ccode_storage}, undef, 'Course item ccode storage should be undef');
236 is($course_item->{homebranch_storage}, undef, 'Course item holding branch storage should be undef');
237 is($course_item->{holdingbranch_storage}, undef, 'Course item holding branch storage should be undef');
238 is($course_item->{location_storage}, undef, 'Course item location storage should be undef');
240 is($course_item->{itype}, undef, 'Course item itype should be undef');
241 is($course_item->{ccode}, undef, 'Course item ccode should be undef');
242 is($course_item->{homebranch}, undef, 'Course item holding branch should be undef');
243 is($course_item->{holdingbranch}, undef, 'Course item holding branch should be undef');
244 is($course_item->{location}, undef, 'Course item location should be undef');
246 is($course_item->{itype_enabled}, 0, 'Course item itype enabled should be 0');
247 is($course_item->{ccode_enabled}, 0, 'Course item ccode enabled should be 0');
248 is($course_item->{homebranch_enabled}, 0, 'Course item holding branch enabled should be 0');
249 is($course_item->{holdingbranch_enabled}, 0, 'Course item holding branch enabled should be 0');
250 is($course_item->{location_enabled}, 0, 'Course item location enabled should be 0');
252 my $item = Koha::Items->find($itemnumber);
253 is($item->effective_itemtype, 'CD_foo', 'Item type in course should be CD_foo');
254 is($item->ccode, 'CD', 'Item ccode in course should be CD');
255 is($item->homebranch, 'B1', 'Item home branch in course should be B1');
256 is($item->holdingbranch, 'B1', 'Item holding branch in course should be B1');
257 is($item->location, 'HR', 'Item location in course should be HR');
260 itemnumber => $itemnumber,
263 homebranch_enabled => 1,
264 holdingbranch_enabled => 1,
265 location_enabled => 1,
269 holdingbranch => 'B2',
273 $course_item = GetCourseItem( ci_id => $ci_id );
274 is($course_item->{itype_storage}, 'CD_foo', 'Course item itype storage should be CD_foo');
275 is($course_item->{ccode_storage}, 'CD', 'Course item ccode storage should be CD');
276 is($course_item->{homebranch_storage}, 'B1', 'Course item holding branch storage should be B1');
277 is($course_item->{holdingbranch_storage}, 'B1', 'Course item holding branch storage should be B1');
278 is($course_item->{location_storage}, 'HR', 'Course item location storage should be HR');
280 is($course_item->{itype}, 'BK_foo', 'Course item itype should be BK_foo');
281 is($course_item->{ccode}, 'BOOK', 'Course item ccode should be BOOK');
282 is($course_item->{homebranch}, 'B2', 'Course item holding branch should be B2');
283 is($course_item->{holdingbranch}, 'B2', 'Course item holding branch should be B2');
284 is($course_item->{location}, 'TH', 'Course item location should be TH');
286 is($course_item->{itype_enabled}, 1, 'Course item itype enabled should be 1');
287 is($course_item->{ccode_enabled}, 1, 'Course item ccode enabled should be 1');
288 is($course_item->{homebranch_enabled}, 1, 'Course item holding branch enabled should be 1');
289 is($course_item->{holdingbranch_enabled}, 1, 'Course item holding branch enabled should be 1');
290 is($course_item->{location_enabled}, 1, 'Course item location enabled should be 1');
292 $item = Koha::Items->find($itemnumber);
293 is($item->effective_itemtype, 'BK_foo', 'Item type in course should be BK_foo');
294 is($item->ccode, 'BOOK', 'Item ccode in course should be BOOK');
295 is($item->homebranch, 'B2', 'Item home branch in course should be B2');
296 is($item->holdingbranch, 'B2', 'Item holding branch in course should be B2');
297 is($item->location, 'TH', 'Item location in course should be TH');
299 # Test removing fields from an active course item
301 itemnumber => $itemnumber,
304 homebranch_enabled => 0,
305 holdingbranch_enabled => 0,
306 location_enabled => 0,
309 $course_item = GetCourseItem( ci_id => $ci_id );
310 is($course_item->{itype_storage}, undef, 'Course item itype storage should be undef');
311 is($course_item->{ccode_storage}, undef, 'Course item ccode storage should be undef');
312 is($course_item->{homebranch_storage}, undef, 'Course item holding branch storage should be undef');
313 is($course_item->{holdingbranch_storage}, undef, 'Course item holding branch storage should be undef');
314 is($course_item->{location_storage}, undef, 'Course item location storage should be undef');
316 is($course_item->{itype}, undef, 'Course item itype should be undef');
317 is($course_item->{ccode}, undef, 'Course item ccode should be undef');
318 is($course_item->{homebranch}, undef, 'Course item holding branch should be undef');
319 is($course_item->{holdingbranch}, undef, 'Course item holding branch should be undef');
320 is($course_item->{location}, undef, 'Course item location should be undef');
322 is($course_item->{itype_enabled}, 0, 'Course item itype enabled should be 0');
323 is($course_item->{ccode_enabled}, 0, 'Course item ccode enabled should be 0');
324 is($course_item->{homebranch_enabled}, 0, 'Course item holding branch enabled should be 0');
325 is($course_item->{holdingbranch_enabled}, 0, 'Course item holding branch enabled should be 0');
326 is($course_item->{location_enabled}, 0, 'Course item location enabled should be 0');
328 $item = Koha::Items->find($itemnumber);
329 is($item->effective_itemtype, 'CD_foo', 'Item type in course should be CD_foo');
330 is($item->ccode, 'CD', 'Item ccode in course should be CD');
331 is($item->homebranch, 'B1', 'Item home branch in course should be B1');
332 is($item->holdingbranch, 'B1', 'Item holding branch in course should be B1');
333 is($item->location, 'HR', 'Item location in course should be HR');
336 subtest 'Ensure item info is preserved' => sub {
339 my $course = $builder->build({
345 my $item = $builder->build_sample_item({ ccode=>"grasshopper", location=>"transylvania"});
346 #Add course item but change nothing
347 my $course_item_id = ModCourseItem(
348 itemnumber => $item->itemnumber,
355 my $course_reserve_id = ModCourseReserve(
356 course_id => $course->{course_id},
357 ci_id => $course_item_id,
361 #Remove course reservei
362 DelCourseReserve( cr_id => $course_reserve_id );
363 my $item_after = Koha::Items->find( $item->itemnumber );
364 is( $item->effective_itemtype, $item_after->itype, "Itemtype is unchanged after adding to and removing from course reserves for inactive course");
365 is( $item->location, $item_after->location, "Location is unchanged after adding to and removing from course reserves for inactive course");
366 is( $item->holdingbranch, $item_after->holdingbranch, "Holdingbranch is unchanged after adding to and removing from course reserves for inactive course");
367 is( $item->ccode, $item_after->ccode, "Collection is unchanged after adding to and removing from course reserves for inactive course");
369 $course = $builder->build({
375 $item = $builder->build_sample_item({ ccode=>"grasshopper", location=>"transylvania"});
376 #Add course item but change nothing
377 $course_item_id = ModCourseItem(
378 itemnumber => $item->itemnumber,
385 $course_reserve_id = ModCourseReserve(
386 course_id => $course->{course_id},
387 ci_id => $course_item_id,
391 #Remove course reserve
392 DelCourseReserve( cr_id => $course_reserve_id );
393 $item_after = Koha::Items->find( $item->itemnumber );
394 is( $item->effective_itemtype, $item_after->itype, "Itemtype is unchanged after adding to and removing from course reserves for inactive course");
395 is( $item->location, $item_after->location, "Location is unchanged after adding to and removing from course reserves for inactive course");
396 is( $item->holdingbranch, $item_after->holdingbranch, "Holdingbranch is unchanged after adding to and removing from course reserves for inactive course");
397 is( $item->ccode, $item_after->ccode, "Collection is unchanged after adding to and removing from course reserves for inactive course");
405 $schema->storage->txn_rollback;
407 sub create_dependent_objects {
409 source => 'Itemtype',
411 itemtype => 'CD_foo',
412 description => 'Compact Disk'
417 source => 'Itemtype',
419 itemtype => 'BK_foo',
420 description => 'Book'
428 branchname => 'Branch 1'
436 branchname => 'Branch 2'
444 branchname => 'Branch 3'
449 source => 'AuthorisedValue',
452 authorised_value => 'BOOK',
458 source => 'AuthorisedValue',
461 authorised_value => 'DVD',
467 source => 'AuthorisedValue',
470 authorised_value => 'CD',
471 lib => 'Compact Disk'
476 source => 'AuthorisedValue',
479 authorised_value => 'HR',
485 source => 'AuthorisedValue',
488 authorised_value => 'TH',
494 sub create_bib_and_item {
495 my $biblio = $builder->build({
501 my $item = $builder->build({
504 biblionumber => $biblio->{biblionumber},
509 holdingbranch => 'B1',
512 return ($biblio->{biblionumber}, $item->{itemnumber});