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 => 27;
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 BOOK');
153 $course_item2 = GetCourseItem( ci_id => $ci_id2 );
154 is($course_item2->{ccode}, '', 'Course item ccode should be empty');
156 DelCourseReserve( cr_id => $cr_id2 );
157 $item = Koha::Items->find($itemnumber);
158 is($item->ccode, '', 'Item ccode should be set back to empty');
160 $schema->storage->txn_rollback;
162 sub create_dependent_objects {
164 source => 'Itemtype',
166 itemtype => 'CD_foo',
167 description => 'Compact Disk'
172 source => 'Itemtype',
174 itemtype => 'BK_foo',
175 description => 'Book'
183 branchname => 'Branch 1'
191 branchname => 'Branch 2'
199 branchname => 'Branch 3'
204 source => 'AuthorisedValue',
207 authorised_value => 'BOOK',
213 source => 'AuthorisedValue',
216 authorised_value => 'DVD',
222 source => 'AuthorisedValue',
225 authorised_value => 'CD',
226 lib => 'Compact Disk'
231 source => 'AuthorisedValue',
234 authorised_value => 'HR',
240 source => 'AuthorisedValue',
243 authorised_value => 'TH',
249 sub create_bib_and_item {
250 my $biblio = $builder->build({
256 my $item = $builder->build({
259 biblionumber => $biblio->{biblionumber},
264 holdingbranch => 'B1',
267 return ($biblio->{biblionumber}, $item->{itemnumber});