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