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