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 => 29;
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 holdingbranch_enabled => 1,
45 location_enabled => 1,
48 holdingbranch => 'B2',
52 my $course = $builder->build({
53 source => 'CourseReserve',
60 my $cr_id = ModCourseReserve(
61 course_id => $course->{course_id},
67 my $course_item = GetCourseItem( ci_id => $ci_id );
68 is($course_item->{itype_storage}, 'CD_foo', 'Course item itype storage should be CD_foo');
69 is($course_item->{ccode_storage}, 'CD', 'Course item ccode storage should be CD');
70 is($course_item->{holdingbranch_storage}, 'B1', 'Course item holding branch storage should be B1');
71 is($course_item->{location_storage}, 'HR', 'Course item location storage should be HR');
73 my $item = Koha::Items->find($itemnumber);
74 is($item->effective_itemtype, 'BK_foo', 'Item type in course should be BK_foo');
75 is($item->ccode, 'BOOK', 'Item ccode in course should be BOOK');
76 is($item->holdingbranch, 'B2', 'Item holding branch in course should be B2');
77 is($item->location, 'TH', 'Item location in course should be TH');
80 itemnumber => $itemnumber,
83 holdingbranch_enabled => 1,
84 location_enabled => 1,
87 holdingbranch => 'B3',
92 course_id => $course->{course_id},
98 $course_item = GetCourseItem( ci_id => $ci_id );
99 is($course_item->{itype_storage}, 'CD_foo', 'Course item itype storage should be CD_foo');
100 is($course_item->{ccode_storage}, 'CD', 'Course item ccode storage should be CD');
101 is($course_item->{holdingbranch_storage}, 'B1', 'Course item holding branch storage should be B1');
102 is($course_item->{location_storage}, 'HR', 'Course item location storage should be HR');
104 $item = Koha::Items->find($itemnumber);
105 is($item->effective_itemtype, 'BK_foo', 'Item type in course should be BK_foo');
106 is($item->ccode, 'DVD', 'Item ccode in course should be DVD');
107 is($item->holdingbranch, 'B3', 'Item holding branch in course should be B3');
108 is($item->location, 'TH', 'Item location in course should be TH');
110 DelCourseReserve( cr_id => $cr_id );
111 $item = Koha::Items->find($itemnumber);
112 is($item->effective_itemtype, 'CD_foo', 'Item type removed from course should be set back to CD_foo');
113 is($item->ccode, 'CD', 'Item ccode removed from course should be set back to CD');
114 is($item->holdingbranch, 'B1', 'Item holding branch removed from course should be set back B1');
115 is($item->location, 'HR', 'Item location removed from course should be TH');
117 # Test the specific case described on bug #10382.
118 $item->ccode('')->store;
120 $item = Koha::Items->find($itemnumber);
121 is($item->ccode, '', 'Item ccode should be empty');
123 my $ci_id2 = ModCourseItem(
124 itemnumber => $itemnumber,
127 holdingbranch_enabled => 1,
128 location_enabled => 1,
131 holdingbranch => 'B1',
135 my $cr_id2 = ModCourseReserve(
136 course_id => $course->{course_id},
142 $item = Koha::Items->find($itemnumber);
143 is($item->ccode, 'BOOK', 'Item ccode should be BOOK');
145 my $course_item2 = GetCourseItem( ci_id => $ci_id2 );
146 is($course_item2->{ccode_storage}, '', 'Course item ccode storage should be empty');
149 itemnumber => $itemnumber,
152 holdingbranch_enabled => 1,
153 location_enabled => 1,
156 holdingbranch => 'B1',
161 course_id => $course->{course_id},
167 $item = Koha::Items->find($itemnumber);
168 is($item->ccode, 'DVD', 'Item ccode should be DVD');
171 itemnumber => $itemnumber,
174 holdingbranch_enabled => 0, # LEAVE UNCHANGED
175 location_enabled => 1,
178 holdingbranch => undef, # LEAVE UNCHANGED
181 $item = Koha::Items->find($itemnumber);
182 is($item->ccode, 'BOOK', 'Item ccode should be BOOK');
184 $course_item2 = GetCourseItem( ci_id => $ci_id2 );
185 is($course_item2->{ccode_storage}, '', 'Course item ccode storage should be empty');
187 DelCourseReserve( cr_id => $cr_id2 );
188 $item = Koha::Items->find($itemnumber);
189 is($item->ccode, '', 'Item ccode should be set back to empty');
191 subtest 'Ensure item info is preserved' => sub {
194 my $course = $builder->build({
200 my $item = $builder->build_sample_item({ ccode=>"grasshopper", location=>"transylvania"});
201 #Add course item but change nothing
202 my $course_item_id = ModCourseItem(
203 itemnumber => $item->itemnumber,
210 my $course_reserve_id = ModCourseReserve(
211 course_id => $course->{course_id},
212 ci_id => $course_item_id,
216 #Remove course reservei
217 DelCourseReserve( cr_id => $course_reserve_id );
218 my $item_after = Koha::Items->find( $item->itemnumber );
219 is( $item->effective_itemtype, $item_after->itype, "Itemtype is unchanged after adding to and removing from course reserves for inactive course");
220 is( $item->location, $item_after->location, "Location is unchanged after adding to and removing from course reserves for inactive course");
221 is( $item->holdingbranch, $item_after->holdingbranch, "Holdingbranch is unchanged after adding to and removing from course reserves for inactive course");
222 is( $item->ccode, $item_after->ccode, "Collection is unchanged after adding to and removing from course reserves for inactive course");
224 $course = $builder->build({
230 $item = $builder->build_sample_item({ ccode=>"grasshopper", location=>"transylvania"});
231 #Add course item but change nothing
232 $course_item_id = ModCourseItem(
233 itemnumber => $item->itemnumber,
240 $course_reserve_id = ModCourseReserve(
241 course_id => $course->{course_id},
242 ci_id => $course_item_id,
246 #Remove course reserve
247 DelCourseReserve( cr_id => $course_reserve_id );
248 $item_after = Koha::Items->find( $item->itemnumber );
249 is( $item->effective_itemtype, $item_after->itype, "Itemtype is unchanged after adding to and removing from course reserves for inactive course");
250 is( $item->location, $item_after->location, "Location is unchanged after adding to and removing from course reserves for inactive course");
251 is( $item->holdingbranch, $item_after->holdingbranch, "Holdingbranch is unchanged after adding to and removing from course reserves for inactive course");
252 is( $item->ccode, $item_after->ccode, "Collection is unchanged after adding to and removing from course reserves for inactive course");
260 $schema->storage->txn_rollback;
262 sub create_dependent_objects {
264 source => 'Itemtype',
266 itemtype => 'CD_foo',
267 description => 'Compact Disk'
272 source => 'Itemtype',
274 itemtype => 'BK_foo',
275 description => 'Book'
283 branchname => 'Branch 1'
291 branchname => 'Branch 2'
299 branchname => 'Branch 3'
304 source => 'AuthorisedValue',
307 authorised_value => 'BOOK',
313 source => 'AuthorisedValue',
316 authorised_value => 'DVD',
322 source => 'AuthorisedValue',
325 authorised_value => 'CD',
326 lib => 'Compact Disk'
331 source => 'AuthorisedValue',
334 authorised_value => 'HR',
340 source => 'AuthorisedValue',
343 authorised_value => 'TH',
349 sub create_bib_and_item {
350 my $biblio = $builder->build({
356 my $item = $builder->build({
359 biblionumber => $biblio->{biblionumber},
364 holdingbranch => 'B1',
367 return ($biblio->{biblionumber}, $item->{itemnumber});