Bug 14237: Add individual bibliographic records to course reserves
[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 => 35;
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     biblionumber          => $biblionumber,
43     itype_enabled         => 1,
44     ccode_enabled         => 1,
45     homebranch_enabled    => 1,
46     holdingbranch_enabled => 1,
47     location_enabled      => 1,
48     itype                 => 'BK_foo',
49     ccode                 => 'BOOK',
50     homebranch            => 'B2',
51     holdingbranch         => 'B2',
52     location              => 'TH',
53 );
54
55 my $course = $builder->build({
56     source => 'CourseReserve',
57     value => {
58         ci_id => $ci_id,
59         enabled => 'no',
60     }
61 });
62
63 my $cr_id = ModCourseReserve(
64     course_id   => $course->{course_id},
65     ci_id       => $ci_id,
66     staff_note  => '',
67     public_note => '',
68 );
69
70 my $course_item = GetCourseItem( ci_id => $ci_id );
71 is($course_item->{itype_storage}, 'CD_foo', 'Course item itype storage should be CD_foo');
72 is($course_item->{ccode_storage}, 'CD', 'Course item ccode storage should be CD');
73 is($course_item->{homebranch_storage}, 'B1', 'Course item holding branch storage should be B1');
74 is($course_item->{holdingbranch_storage}, 'B1', 'Course item holding branch storage should be B1');
75 is($course_item->{location_storage}, 'HR', 'Course item location storage should be HR');
76
77 my $item = Koha::Items->find($itemnumber);
78 is($item->effective_itemtype, 'BK_foo', 'Item type in course should be BK_foo');
79 is($item->ccode, 'BOOK', 'Item ccode in course should be BOOK');
80 is($item->homebranch, 'B2', 'Item home branch in course should be B2');
81 is($item->holdingbranch, 'B2', 'Item holding branch in course should be B2');
82 is($item->location, 'TH', 'Item location in course should be TH');
83
84 ModCourseItem(
85     itemnumber            => $itemnumber,
86     biblionumber          => $biblionumber,
87     itype_enabled         => 1,
88     ccode_enabled         => 1,
89     homebranch_enabled    => 1,
90     holdingbranch_enabled => 1,
91     location_enabled      => 1,
92     itype                 => 'BK_foo',
93     ccode                 => 'DVD',
94     homebranch            => 'B3',
95     holdingbranch         => 'B3',
96     location              => 'TH',
97 );
98
99 ModCourseReserve(
100     course_id   => $course->{course_id},
101     ci_id       => $ci_id,
102     staff_note  => '',
103     public_note => '',
104 );
105
106 $course_item = GetCourseItem( ci_id => $ci_id );
107 is($course_item->{itype_storage}, 'CD_foo', 'Course item itype storage should be CD_foo');
108 is($course_item->{ccode_storage}, 'CD', 'Course item ccode storage should be CD');
109 is($course_item->{homebranch_storage}, 'B1', 'Course item home branch storage should be B1');
110 is($course_item->{holdingbranch_storage}, 'B1', 'Course item holding branch storage should be B1');
111 is($course_item->{location_storage}, 'HR', 'Course item location storage should be HR');
112
113 $item = Koha::Items->find($itemnumber);
114 is($item->effective_itemtype, 'BK_foo', 'Item type in course should be BK_foo');
115 is($item->ccode, 'DVD', 'Item ccode in course should be DVD');
116 is($item->homebranch, 'B3', 'Item home branch in course should be B3');
117 is($item->holdingbranch, 'B3', 'Item holding branch in course should be B3');
118 is($item->location, 'TH', 'Item location in course should be TH');
119
120 DelCourseReserve( cr_id => $cr_id );
121 $item = Koha::Items->find($itemnumber);
122 is($item->effective_itemtype, 'CD_foo', 'Item type removed from course should be set back to CD_foo');
123 is($item->ccode, 'CD', 'Item ccode removed from course should be set back to CD');
124 is($item->homebranch, 'B1', 'Item home branch removed from course should be set back B1');
125 is($item->holdingbranch, 'B1', 'Item holding branch removed from course should be set back B1');
126 is($item->location, 'HR', 'Item location removed from course should be TH');
127
128 # Test the specific case described on bug #10382.
129 $item->ccode('')->store;
130
131 $item = Koha::Items->find($itemnumber);
132 is($item->ccode, '', 'Item ccode should be empty');
133
134 my $ci_id2 = ModCourseItem(
135     itemnumber            => $itemnumber,
136     itype_enabled         => 1,
137     ccode_enabled         => 1,
138     homebranch_enabled    => 1,
139     holdingbranch_enabled => 1,
140     location_enabled      => 1,
141     itype                 => 'CD_foo',
142     ccode                 => 'BOOK',
143     homebranch            => 'B1',
144     holdingbranch         => 'B1',
145     location              => 'HR',
146 );
147
148 my $cr_id2 = ModCourseReserve(
149     course_id   => $course->{course_id},
150     ci_id       => $ci_id2,
151     staff_note  => '',
152     public_note => '',
153 );
154
155 $item = Koha::Items->find($itemnumber);
156 is($item->ccode, 'BOOK', 'Item ccode should be BOOK');
157
158 my $course_item2 = GetCourseItem( ci_id => $ci_id2 );
159 is($course_item2->{ccode_storage}, '', 'Course item ccode storage should be empty');
160
161 ModCourseItem(
162     itemnumber            => $itemnumber,
163     biblionumber          => $biblionumber,
164     itype_enabled         => 1,
165     ccode_enabled         => 1,
166     homebranch_enabled    => 1,
167     holdingbranch_enabled => 1,
168     location_enabled      => 1,
169     itype                 => 'CD_foo',
170     ccode                 => 'DVD',
171     homebranch            => 'B1',
172     holdingbranch         => 'B1',
173     location              => 'HR',
174 );
175
176 ModCourseReserve(
177     course_id   => $course->{course_id},
178     ci_id       => $ci_id2,
179     staff_note  => '',
180     public_note => '',
181 );
182
183 $item = Koha::Items->find($itemnumber);
184 is($item->ccode, 'DVD', 'Item ccode should be DVD');
185
186 ModCourseItem(
187     itemnumber            => $itemnumber,
188     biblionumber          => $biblionumber,
189     itype_enabled         => 1,
190     ccode_enabled         => 1,
191     homebranch_enabled    => 0,             # LEAVE UNCHANGED
192     holdingbranch_enabled => 0,             # LEAVE UNCHANGED
193     location_enabled      => 1,
194     itype                 => 'BK',
195     ccode                 => 'BOOK',
196     holdingbranch         => undef,         # LEAVE UNCHANGED
197     location              => 'TH',
198 );
199 $item = Koha::Items->find($itemnumber);
200 is($item->ccode, 'BOOK', 'Item ccode should be BOOK');
201
202 $course_item2 = GetCourseItem( ci_id => $ci_id2 );
203 is($course_item2->{ccode_storage}, '', 'Course item ccode storage should be empty');
204
205 DelCourseReserve( cr_id => $cr_id2 );
206 $item = Koha::Items->find($itemnumber);
207 is($item->ccode, '', 'Item ccode should be set back to empty');
208
209 subtest 'Ensure modifying fields on existing course items updates the item and course item' => sub {
210     plan tests => 60;
211
212     my ($biblionumber, $itemnumber) = create_bib_and_item();
213     my $ci_id = ModCourseItem(
214         itemnumber            => $itemnumber,
215         biblionumber          => $biblionumber,
216         itype_enabled         => 0,
217         ccode_enabled         => 0,
218         homebranch_enabled    => 0,
219         holdingbranch_enabled => 0,
220         location_enabled      => 0,
221     );
222
223     my $course = $builder->build({
224         source => 'CourseReserve',
225         value => {
226             ci_id => $ci_id,
227             enabled => 'no',
228         }
229     });
230
231     my $cr_id = ModCourseReserve(
232         course_id   => $course->{course_id},
233         ci_id       => $ci_id,
234         staff_note  => '',
235         public_note => '',
236     );
237
238     my $course_item = GetCourseItem( ci_id => $ci_id );
239     is($course_item->{itype_storage}, undef, 'Course item itype storage should be undef');
240     is($course_item->{ccode_storage}, undef, 'Course item ccode storage should be undef');
241     is($course_item->{homebranch_storage}, undef, 'Course item holding branch storage should be undef');
242     is($course_item->{holdingbranch_storage}, undef, 'Course item holding branch storage should be undef');
243     is($course_item->{location_storage}, undef, 'Course item location storage should be undef');
244
245     is($course_item->{itype}, undef, 'Course item itype should be undef');
246     is($course_item->{ccode}, undef, 'Course item ccode should be undef');
247     is($course_item->{homebranch}, undef, 'Course item holding branch should be undef');
248     is($course_item->{holdingbranch}, undef, 'Course item holding branch should be undef');
249     is($course_item->{location}, undef, 'Course item location should be undef');
250
251     is($course_item->{itype_enabled}, 0, 'Course item itype enabled should be 0');
252     is($course_item->{ccode_enabled}, 0, 'Course item ccode enabled should be 0');
253     is($course_item->{homebranch_enabled}, 0, 'Course item holding branch enabled should be 0');
254     is($course_item->{holdingbranch_enabled}, 0, 'Course item holding branch enabled should be 0');
255     is($course_item->{location_enabled}, 0, 'Course item location enabled should be 0');
256
257     my $item = Koha::Items->find($itemnumber);
258     is($item->effective_itemtype, 'CD_foo', 'Item type in course should be CD_foo');
259     is($item->ccode, 'CD', 'Item ccode in course should be CD');
260     is($item->homebranch, 'B1', 'Item home branch in course should be B1');
261     is($item->holdingbranch, 'B1', 'Item holding branch in course should be B1');
262     is($item->location, 'HR', 'Item location in course should be HR');
263
264     ModCourseItem(
265         itemnumber            => $itemnumber,
266         itype_enabled         => 1,
267         ccode_enabled         => 1,
268         homebranch_enabled    => 1,
269         holdingbranch_enabled => 1,
270         location_enabled      => 1,
271         itype                 => 'BK_foo',
272         ccode                 => 'BOOK',
273         homebranch            => 'B2',
274         holdingbranch         => 'B2',
275         location              => 'TH',
276     );
277
278     $course_item = GetCourseItem( ci_id => $ci_id );
279     is($course_item->{itype_storage}, 'CD_foo', 'Course item itype storage should be CD_foo');
280     is($course_item->{ccode_storage}, 'CD', 'Course item ccode storage should be CD');
281     is($course_item->{homebranch_storage}, 'B1', 'Course item holding branch storage should be B1');
282     is($course_item->{holdingbranch_storage}, 'B1', 'Course item holding branch storage should be B1');
283     is($course_item->{location_storage}, 'HR', 'Course item location storage should be HR');
284
285     is($course_item->{itype}, 'BK_foo', 'Course item itype should be BK_foo');
286     is($course_item->{ccode}, 'BOOK', 'Course item ccode should be BOOK');
287     is($course_item->{homebranch}, 'B2', 'Course item holding branch should be B2');
288     is($course_item->{holdingbranch}, 'B2', 'Course item holding branch should be B2');
289     is($course_item->{location}, 'TH', 'Course item location should be TH');
290
291     is($course_item->{itype_enabled}, 1, 'Course item itype enabled should be 1');
292     is($course_item->{ccode_enabled}, 1, 'Course item ccode enabled should be 1');
293     is($course_item->{homebranch_enabled}, 1, 'Course item holding branch enabled should be 1');
294     is($course_item->{holdingbranch_enabled}, 1, 'Course item holding branch enabled should be 1');
295     is($course_item->{location_enabled}, 1, 'Course item location enabled should be 1');
296
297     $item = Koha::Items->find($itemnumber);
298     is($item->effective_itemtype, 'BK_foo', 'Item type in course should be BK_foo');
299     is($item->ccode, 'BOOK', 'Item ccode in course should be BOOK');
300     is($item->homebranch, 'B2', 'Item home branch in course should be B2');
301     is($item->holdingbranch, 'B2', 'Item holding branch in course should be B2');
302     is($item->location, 'TH', 'Item location in course should be TH');
303
304     # Test removing fields from an active course item
305     ModCourseItem(
306         itemnumber            => $itemnumber,
307         biblionumber          => $biblionumber,
308         itype_enabled         => 0,
309         ccode_enabled         => 0,
310         homebranch_enabled    => 0,
311         holdingbranch_enabled => 0,
312         location_enabled      => 0,
313     );
314
315     $course_item = GetCourseItem( ci_id => $ci_id );
316     is($course_item->{itype_storage}, undef, 'Course item itype storage should be undef');
317     is($course_item->{ccode_storage}, undef, 'Course item ccode storage should be undef');
318     is($course_item->{homebranch_storage}, undef, 'Course item holding branch storage should be undef');
319     is($course_item->{holdingbranch_storage}, undef, 'Course item holding branch storage should be undef');
320     is($course_item->{location_storage}, undef, 'Course item location storage should be undef');
321
322     is($course_item->{itype}, undef, 'Course item itype should be undef');
323     is($course_item->{ccode}, undef, 'Course item ccode should be undef');
324     is($course_item->{homebranch}, undef, 'Course item holding branch should be undef');
325     is($course_item->{holdingbranch}, undef, 'Course item holding branch should be undef');
326     is($course_item->{location}, undef, 'Course item location should be undef');
327
328     is($course_item->{itype_enabled}, 0, 'Course item itype enabled should be 0');
329     is($course_item->{ccode_enabled}, 0, 'Course item ccode enabled should be 0');
330     is($course_item->{homebranch_enabled}, 0, 'Course item holding branch enabled should be 0');
331     is($course_item->{holdingbranch_enabled}, 0, 'Course item holding branch enabled should be 0');
332     is($course_item->{location_enabled}, 0, 'Course item location enabled should be 0');
333
334     $item = Koha::Items->find($itemnumber);
335     is($item->effective_itemtype, 'CD_foo', 'Item type in course should be CD_foo');
336     is($item->ccode, 'CD', 'Item ccode in course should be CD');
337     is($item->homebranch, 'B1', 'Item home branch in course should be B1');
338     is($item->holdingbranch, 'B1', 'Item holding branch in course should be B1');
339     is($item->location, 'HR', 'Item location in course should be HR');
340 };
341
342 subtest 'Ensure item info is preserved' => sub {
343     plan tests => 8;
344
345     my $course = $builder->build({
346         source => 'Course',
347         value => {
348             enabled => 'no',
349         }
350     });
351     my $item = $builder->build_sample_item({ ccode=>"grasshopper", location=>"transylvania"});
352     #Add course item but change nothing
353     my $course_item_id = ModCourseItem(
354         itemnumber    => $item->itemnumber,
355         biblionumber  => $biblionumber,
356         itype         => '',
357         ccode         => '',
358         holdingbranch => '',
359         location      => '',
360     );
361     #Add course reserve
362     my $course_reserve_id = ModCourseReserve(
363         course_id   => $course->{course_id},
364         ci_id       => $course_item_id,
365         staff_note  => '',
366         public_note => '',
367     );
368     #Remove course reservei
369     DelCourseReserve( cr_id => $course_reserve_id );
370     my $item_after = Koha::Items->find( $item->itemnumber );
371     is( $item->effective_itemtype, $item_after->itype, "Itemtype is unchanged after adding to and removing from course reserves for inactive course");
372     is( $item->location, $item_after->location, "Location is unchanged after adding to and removing from course reserves for inactive course");
373     is( $item->holdingbranch, $item_after->holdingbranch, "Holdingbranch is unchanged after adding to and removing from course reserves for inactive course");
374     is( $item->ccode, $item_after->ccode, "Collection is unchanged after adding to and removing from course reserves for inactive course");
375
376     $course = $builder->build({
377         source => 'Course',
378         value => {
379             enabled => 'yes',
380         }
381     });
382     $item = $builder->build_sample_item({ ccode=>"grasshopper", location=>"transylvania"});
383     #Add course item but change nothing
384     $course_item_id = ModCourseItem(
385         itemnumber    => $item->itemnumber,
386         biblionumber  => $biblionumber,
387         itype         => '',
388         ccode         => '',
389         holdingbranch => '',
390         location      => '',
391     );
392     #Add course reserve
393     $course_reserve_id = ModCourseReserve(
394         course_id   => $course->{course_id},
395         ci_id       => $course_item_id,
396         staff_note  => '',
397         public_note => '',
398     );
399     #Remove course reserve
400     DelCourseReserve( cr_id => $course_reserve_id );
401     $item_after = Koha::Items->find( $item->itemnumber );
402     is( $item->effective_itemtype, $item_after->itype, "Itemtype is unchanged after adding to and removing from course reserves for inactive course");
403     is( $item->location, $item_after->location, "Location is unchanged after adding to and removing from course reserves for inactive course");
404     is( $item->holdingbranch, $item_after->holdingbranch, "Holdingbranch is unchanged after adding to and removing from course reserves for inactive course");
405     is( $item->ccode, $item_after->ccode, "Collection is unchanged after adding to and removing from course reserves for inactive course");
406
407 };
408
409
410
411
412
413 $schema->storage->txn_rollback;
414
415 sub create_dependent_objects {
416     $builder->build({
417         source => 'Itemtype',
418         value  => {
419             itemtype => 'CD_foo',
420             description => 'Compact Disk'
421         }
422     });
423
424     $builder->build({
425         source => 'Itemtype',
426         value  => {
427             itemtype => 'BK_foo',
428             description => 'Book'
429         }
430     });
431
432     $builder->build({
433         source => 'Branch',
434         value  => {
435             branchcode => 'B1',
436             branchname => 'Branch 1'
437         }
438     });
439
440     $builder->build({
441         source => 'Branch',
442         value  => {
443             branchcode => 'B2',
444             branchname => 'Branch 2'
445         }
446     });
447
448     $builder->build({
449         source => 'Branch',
450         value  => {
451             branchcode => 'B3',
452             branchname => 'Branch 3'
453         }
454     });
455
456     $builder->build({
457         source => 'AuthorisedValue',
458         value  => {
459             category => 'CCODE',
460             authorised_value => 'BOOK',
461             lib => 'Book'
462         }
463     });
464
465     $builder->build({
466         source => 'AuthorisedValue',
467         value  => {
468             category => 'CCODE',
469             authorised_value => 'DVD',
470             lib => 'Book'
471         }
472     });
473
474     $builder->build({
475         source => 'AuthorisedValue',
476         value  => {
477             category => 'CCODE',
478             authorised_value => 'CD',
479             lib => 'Compact Disk'
480         }
481     });
482
483     $builder->build({
484         source => 'AuthorisedValue',
485         value  => {
486             category => 'LOC',
487             authorised_value => 'HR',
488             lib => 'Here'
489         }
490     });
491
492     $builder->build({
493         source => 'AuthorisedValue',
494         value  => {
495             category => 'LOC',
496             authorised_value => 'TH',
497             lib => 'There'
498         }
499     });
500 }
501
502 sub create_bib_and_item {
503     my $item = $builder->build_sample_item(
504         {
505             itype    => 'CD_foo',
506             ccode    => 'CD',
507             location => 'HR',
508             library  => 'B1',
509         }
510     );
511     return ($item->biblionumber, $item->itemnumber);
512 }