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 => 28;
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 => 'B2',
48 my $course = $builder->build({
49 source => 'CourseReserve',
55 my $cr_id = ModCourseReserve(
56 course_id => $course->{course_id},
62 my $course_item = GetCourseItem( ci_id => $ci_id );
63 is($course_item->{itype}, 'CD_foo', 'Course item itype should be CD_foo');
64 is($course_item->{ccode}, 'CD', 'Course item ccode should be CD');
65 is($course_item->{holdingbranch}, 'B1', 'Course item holding branch should be B1');
66 is($course_item->{location}, 'HR', 'Course item location should be HR');
68 my $item = Koha::Items->find($itemnumber);
69 is($item->itype, 'BK_foo', 'Item type in course should be BK_foo');
70 is($item->ccode, 'BOOK', 'Item ccode in course should be BOOK');
71 is($item->holdingbranch, 'B2', 'Item holding branch in course should be B2');
72 is($item->location, 'TH', 'Item location in course should be TH');
75 itemnumber => $itemnumber,
78 holdingbranch => 'B3',
83 course_id => $course->{course_id},
89 $course_item = GetCourseItem( ci_id => $ci_id );
90 is($course_item->{itype}, 'CD_foo', 'Course item itype should be CD_foo');
91 is($course_item->{ccode}, 'CD', 'Course item ccode should be CD');
92 is($course_item->{holdingbranch}, 'B1', 'Course item holding branch should be B1');
93 is($course_item->{location}, 'HR', 'Course item location should be HR');
95 $item = Koha::Items->find($itemnumber);
96 is($item->itype, 'BK_foo', 'Item type in course should be BK_foo');
97 is($item->ccode, 'DVD', 'Item ccode in course should be DVD');
98 is($item->holdingbranch, 'B3', 'Item holding branch in course should be B3');
99 is($item->location, 'TH', 'Item location in course should be TH');
101 DelCourseReserve( cr_id => $cr_id );
102 $item = Koha::Items->find($itemnumber);
103 is($item->itype, 'CD_foo', 'Item type removed from course should be set back to CD_foo');
104 is($item->ccode, 'CD', 'Item ccode removed from course should be set back to CD');
105 is($item->holdingbranch, 'B1', 'Item holding branch removed from course should be set back B1');
106 is($item->location, 'HR', 'Item location removed from course should be TH');
108 # Test the specific case described on bug #10382.
109 $item->ccode('')->store;
111 $item = Koha::Items->find($itemnumber);
112 is($item->ccode, '', 'Item ccode should be empty');
114 my $ci_id2 = ModCourseItem(
115 itemnumber => $itemnumber,
118 holdingbranch => 'B1',
122 my $cr_id2 = ModCourseReserve(
123 course_id => $course->{course_id},
129 $item = Koha::Items->find($itemnumber);
130 is($item->ccode, 'BOOK', 'Item ccode should be BOOK');
132 my $course_item2 = GetCourseItem( ci_id => $ci_id2 );
133 is($course_item2->{ccode}, '', 'Course item ccode should be empty');
136 itemnumber => $itemnumber,
139 holdingbranch => 'B1',
144 course_id => $course->{course_id},
150 $item = Koha::Items->find($itemnumber);
151 is($item->ccode, 'DVD', 'Item ccode should be DVD');
154 itemnumber => $itemnumber,
157 holdingbranch => '', # LEAVE UNCHANGED
160 $item = Koha::Items->find($itemnumber);
161 is($item->ccode, 'BOOK', 'Item ccode should be BOOK');
163 $course_item2 = GetCourseItem( ci_id => $ci_id2 );
164 is($course_item2->{ccode}, '', 'Course item ccode should be empty');
166 DelCourseReserve( cr_id => $cr_id2 );
167 $item = Koha::Items->find($itemnumber);
168 is($item->ccode, '', 'Item ccode should be set back to empty');
170 subtest 'Ensure item info is preserved' => sub {
173 my $course = $builder->build({
179 my $item = $builder->build_sample_item({ ccode=>"grasshopper", location=>"transylvania"});
180 #Add course item but change nothing
181 my $course_item_id = ModCourseItem(
182 itemnumber => $item->itemnumber,
189 my $course_reserve_id = ModCourseReserve(
190 course_id => $course->{course_id},
191 ci_id => $course_item_id,
195 #Remove course reservei
196 DelCourseReserve( cr_id => $course_reserve_id );
197 my $item_after = Koha::Items->find( $item->itemnumber );
198 is( $item->itype, $item_after->itype, "Itemtype is unchanged after adding to and removing from course reserves for inactive course");
199 is( $item->location, $item_after->location, "Location is unchanged after adding to and removing from course reserves for inactive course");
200 is( $item->holdingbranch, $item_after->holdingbranch, "Holdingbranch is unchanged after adding to and removing from course reserves for inactive course");
201 is( $item->ccode, $item_after->ccode, "Collection is unchanged after adding to and removing from course reserves for inactive course");
203 $course = $builder->build({
209 $item = $builder->build_sample_item({ ccode=>"grasshopper", location=>"transylvania"});
210 #Add course item but change nothing
211 $course_item_id = ModCourseItem(
212 itemnumber => $item->itemnumber,
219 $course_reserve_id = ModCourseReserve(
220 course_id => $course->{course_id},
221 ci_id => $course_item_id,
225 #Remove course reserve
226 DelCourseReserve( cr_id => $course_reserve_id );
227 $item_after = Koha::Items->find( $item->itemnumber );
228 is( $item->itype, $item_after->itype, "Itemtype is unchanged after adding to and removing from course reserves for inactive course");
229 is( $item->location, $item_after->location, "Location is unchanged after adding to and removing from course reserves for inactive course");
230 is( $item->holdingbranch, $item_after->holdingbranch, "Holdingbranch is unchanged after adding to and removing from course reserves for inactive course");
231 is( $item->ccode, $item_after->ccode, "Collection is unchanged after adding to and removing from course reserves for inactive course");
239 $schema->storage->txn_rollback;
241 sub create_dependent_objects {
243 source => 'Itemtype',
245 itemtype => 'CD_foo',
246 description => 'Compact Disk'
251 source => 'Itemtype',
253 itemtype => 'BK_foo',
254 description => 'Book'
262 branchname => 'Branch 1'
270 branchname => 'Branch 2'
278 branchname => 'Branch 3'
283 source => 'AuthorisedValue',
286 authorised_value => 'BOOK',
292 source => 'AuthorisedValue',
295 authorised_value => 'DVD',
301 source => 'AuthorisedValue',
304 authorised_value => 'CD',
305 lib => 'Compact Disk'
310 source => 'AuthorisedValue',
313 authorised_value => 'HR',
319 source => 'AuthorisedValue',
322 authorised_value => 'TH',
328 sub create_bib_and_item {
329 my $biblio = $builder->build({
335 my $item = $builder->build({
338 biblionumber => $biblio->{biblionumber},
343 holdingbranch => 'B1',
346 return ($biblio->{biblionumber}, $item->{itemnumber});