Bug 23727: Editing course reserve items is broken
[koha.git] / t / db_dependent / CourseReserves / CourseItems.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use t::lib::Mocks;
21 use t::lib::TestBuilder;
22 use C4::CourseReserves qw/ModCourseItem ModCourseReserve DelCourseReserve GetCourseItem/;
23 use C4::Context;
24 use Koha::Items;
25
26 use Test::More tests => 29;
27
28 BEGIN {
29     require_ok('C4::CourseReserves');
30 }
31
32 my $schema  = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
34
35 my $builder = t::lib::TestBuilder->new();
36
37 create_dependent_objects();
38 my ($biblionumber, $itemnumber) = create_bib_and_item();
39
40 my $ci_id = ModCourseItem(
41     itemnumber            => $itemnumber,
42     itype_enabled         => 1,
43     ccode_enabled         => 1,
44     holdingbranch_enabled => 1,
45     location_enabled      => 1,
46     itype                 => 'BK_foo',
47     ccode                 => 'BOOK',
48     holdingbranch         => 'B2',
49     location              => 'TH',
50 );
51
52 my $course = $builder->build({
53     source => 'CourseReserve',
54     value => {
55         ci_id => $ci_id,
56         enabled => 'no',
57     }
58 });
59
60 my $cr_id = ModCourseReserve(
61     course_id   => $course->{course_id},
62     ci_id       => $ci_id,
63     staff_note  => '',
64     public_note => '',
65 );
66
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');
72
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');
78
79 ModCourseItem(
80     itemnumber            => $itemnumber,
81     itype_enabled         => 1,
82     ccode_enabled         => 1,
83     holdingbranch_enabled => 1,
84     location_enabled      => 1,
85     itype                 => 'BK_foo',
86     ccode                 => 'DVD',
87     holdingbranch         => 'B3',
88     location              => 'TH',
89 );
90
91 ModCourseReserve(
92     course_id   => $course->{course_id},
93     ci_id       => $ci_id,
94     staff_note  => '',
95     public_note => '',
96 );
97
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');
103
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');
109
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');
116
117 # Test the specific case described on bug #10382.
118 $item->ccode('')->store;
119
120 $item = Koha::Items->find($itemnumber);
121 is($item->ccode, '', 'Item ccode should be empty');
122
123 my $ci_id2 = ModCourseItem(
124     itemnumber            => $itemnumber,
125     itype_enabled         => 1,
126     ccode_enabled         => 1,
127     holdingbranch_enabled => 1,
128     location_enabled      => 1,
129     itype                 => 'CD_foo',
130     ccode                 => 'BOOK',
131     holdingbranch         => 'B1',
132     location              => 'HR',
133 );
134
135 my $cr_id2 = ModCourseReserve(
136     course_id   => $course->{course_id},
137     ci_id       => $ci_id2,
138     staff_note  => '',
139     public_note => '',
140 );
141
142 $item = Koha::Items->find($itemnumber);
143 is($item->ccode, 'BOOK', 'Item ccode should be BOOK');
144
145 my $course_item2 = GetCourseItem( ci_id => $ci_id2 );
146 is($course_item2->{ccode_storage}, '', 'Course item ccode storage should be empty');
147
148 ModCourseItem(
149     itemnumber            => $itemnumber,
150     itype_enabled         => 1,
151     ccode_enabled         => 1,
152     holdingbranch_enabled => 1,
153     location_enabled      => 1,
154     itype                 => 'CD_foo',
155     ccode                 => 'DVD',
156     holdingbranch         => 'B1',
157     location              => 'HR',
158 );
159
160 ModCourseReserve(
161     course_id   => $course->{course_id},
162     ci_id       => $ci_id2,
163     staff_note  => '',
164     public_note => '',
165 );
166
167 $item = Koha::Items->find($itemnumber);
168 is($item->ccode, 'DVD', 'Item ccode should be DVD');
169
170 ModCourseItem(
171     itemnumber            => $itemnumber,
172     itype_enabled         => 1,
173     ccode_enabled         => 1,
174     holdingbranch_enabled => 0,             # LEAVE UNCHANGED
175     location_enabled      => 1,
176     itype                 => 'BK',
177     ccode                 => 'BOOK',
178     holdingbranch         => undef,         # LEAVE UNCHANGED
179     location              => 'TH',
180 );
181 $item = Koha::Items->find($itemnumber);
182 is($item->ccode, 'BOOK', 'Item ccode should be BOOK');
183
184 $course_item2 = GetCourseItem( ci_id => $ci_id2 );
185 is($course_item2->{ccode_storage}, '', 'Course item ccode storage should be empty');
186
187 DelCourseReserve( cr_id => $cr_id2 );
188 $item = Koha::Items->find($itemnumber);
189 is($item->ccode, '', 'Item ccode should be set back to empty');
190
191 subtest 'Ensure item info is preserved' => sub {
192     plan tests => 8;
193
194     my $course = $builder->build({
195         source => 'Course',
196         value => {
197             enabled => 'no',
198         }
199     });
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,
204         itype         => '',
205         ccode         => '',
206         holdingbranch => '',
207         location      => '',
208     );
209     #Add course reserve
210     my $course_reserve_id = ModCourseReserve(
211         course_id   => $course->{course_id},
212         ci_id       => $course_item_id,
213         staff_note  => '',
214         public_note => '',
215     );
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");
223
224     $course = $builder->build({
225         source => 'Course',
226         value => {
227             enabled => 'yes',
228         }
229     });
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,
234         itype         => '',
235         ccode         => '',
236         holdingbranch => '',
237         location      => '',
238     );
239     #Add course reserve
240     $course_reserve_id = ModCourseReserve(
241         course_id   => $course->{course_id},
242         ci_id       => $course_item_id,
243         staff_note  => '',
244         public_note => '',
245     );
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");
253
254 };
255
256
257
258
259
260 $schema->storage->txn_rollback;
261
262 sub create_dependent_objects {
263     $builder->build({
264         source => 'Itemtype',
265         value  => {
266             itemtype => 'CD_foo',
267             description => 'Compact Disk'
268         }
269     });
270
271     $builder->build({
272         source => 'Itemtype',
273         value  => {
274             itemtype => 'BK_foo',
275             description => 'Book'
276         }
277     });
278
279     $builder->build({
280         source => 'Branch',
281         value  => {
282             branchcode => 'B1',
283             branchname => 'Branch 1'
284         }
285     });
286
287     $builder->build({
288         source => 'Branch',
289         value  => {
290             branchcode => 'B2',
291             branchname => 'Branch 2'
292         }
293     });
294
295     $builder->build({
296         source => 'Branch',
297         value  => {
298             branchcode => 'B3',
299             branchname => 'Branch 3'
300         }
301     });
302
303     $builder->build({
304         source => 'AuthorisedValue',
305         value  => {
306             category => 'CCODE',
307             authorised_value => 'BOOK',
308             lib => 'Book'
309         }
310     });
311
312     $builder->build({
313         source => 'AuthorisedValue',
314         value  => {
315             category => 'CCODE',
316             authorised_value => 'DVD',
317             lib => 'Book'
318         }
319     });
320
321     $builder->build({
322         source => 'AuthorisedValue',
323         value  => {
324             category => 'CCODE',
325             authorised_value => 'CD',
326             lib => 'Compact Disk'
327         }
328     });
329
330     $builder->build({
331         source => 'AuthorisedValue',
332         value  => {
333             category => 'LOC',
334             authorised_value => 'HR',
335             lib => 'Here'
336         }
337     });
338
339     $builder->build({
340         source => 'AuthorisedValue',
341         value  => {
342             category => 'LOC',
343             authorised_value => 'TH',
344             lib => 'There'
345         }
346     });
347 }
348
349 sub create_bib_and_item {
350     my $biblio = $builder->build({
351         source => 'Biblio',
352         value  => {
353             title => 'Title',
354         }
355     });
356     my $item = $builder->build({
357         source => 'Item',
358         value  => {
359             biblionumber  => $biblio->{biblionumber},
360             itype         => 'CD_foo',
361             ccode         => 'CD',
362             location      => 'HR',
363             homebranch    => 'B1',
364             holdingbranch => 'B1',
365         }
366     });
367     return ($biblio->{biblionumber}, $item->{itemnumber});
368 }