Bug 24146: Increment existing fine
[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         => 'BK_foo',
43     ccode         => 'BOOK',
44     holdingbranch => 'B2',
45     location      => 'TH',
46 );
47
48 my $course = $builder->build({
49     source => 'CourseReserve',
50     value => {
51         ci_id => $ci_id,
52     }
53 });
54
55 my $cr_id = ModCourseReserve(
56     course_id   => $course->{course_id},
57     ci_id       => $ci_id,
58     staff_note  => '',
59     public_note => '',
60 );
61
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');
67
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');
73
74 ModCourseItem(
75     itemnumber    => $itemnumber,
76     itype         => 'BK_foo',
77     ccode         => 'DVD',
78     holdingbranch => 'B3',
79     location      => 'TH',
80 );
81
82 ModCourseReserve(
83     course_id   => $course->{course_id},
84     ci_id       => $ci_id,
85     staff_note  => '',
86     public_note => '',
87 );
88
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');
94
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');
100
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');
107
108 # Test the specific case described on bug #10382.
109 $item->ccode('')->store;
110
111 $item = Koha::Items->find($itemnumber);
112 is($item->ccode, '', 'Item ccode should be empty');
113
114 my $ci_id2 = ModCourseItem(
115     itemnumber    => $itemnumber,
116     itype         => 'CD_foo',
117     ccode         => 'BOOK',
118     holdingbranch => 'B1',
119     location      => 'HR',
120 );
121
122 my $cr_id2 = ModCourseReserve(
123     course_id   => $course->{course_id},
124     ci_id       => $ci_id2,
125     staff_note  => '',
126     public_note => '',
127 );
128
129 $item = Koha::Items->find($itemnumber);
130 is($item->ccode, 'BOOK', 'Item ccode should be BOOK');
131
132 my $course_item2 = GetCourseItem( ci_id => $ci_id2 );
133 is($course_item2->{ccode}, '', 'Course item ccode should be empty');
134
135 ModCourseItem(
136     itemnumber    => $itemnumber,
137     itype         => 'CD_foo',
138     ccode         => 'DVD',
139     holdingbranch => 'B1',
140     location      => 'HR',
141 );
142
143 ModCourseReserve(
144     course_id   => $course->{course_id},
145     ci_id       => $ci_id2,
146     staff_note  => '',
147     public_note => '',
148 );
149
150 $item = Koha::Items->find($itemnumber);
151 is($item->ccode, 'DVD', 'Item ccode should be DVD');
152
153 ModCourseItem(
154     itemnumber    => $itemnumber,
155     itype         => 'BK',
156     ccode         => 'BOOK',
157     holdingbranch => '', # LEAVE UNCHANGED
158     location      => 'TH',
159 );
160 $item = Koha::Items->find($itemnumber);
161 is($item->ccode, 'BOOK', 'Item ccode should be BOOK');
162
163 $course_item2 = GetCourseItem( ci_id => $ci_id2 );
164 is($course_item2->{ccode}, '', 'Course item ccode should be empty');
165
166 DelCourseReserve( cr_id => $cr_id2 );
167 $item = Koha::Items->find($itemnumber);
168 is($item->ccode, '', 'Item ccode should be set back to empty');
169
170 subtest 'Ensure item info is preserved' => sub {
171     plan tests => 8;
172
173     my $course = $builder->build({
174         source => 'Course',
175         value => {
176             enabled => 'no',
177         }
178     });
179     my $item = $builder->build_sample_item({ ccode=>"grasshopper", location=>"transylvania"});
180     #Add course item but change nothing
181     my $course_item_id = ModCourseItem(
182         itemnumber    => $item->itemnumber,
183         itype         => '',
184         ccode         => '',
185         holdingbranch => '',
186         location      => '',
187     );
188     #Add course reserve
189     my $course_reserve_id = ModCourseReserve(
190         course_id   => $course->{course_id},
191         ci_id       => $course_item_id,
192         staff_note  => '',
193         public_note => '',
194     );
195     #Remove course reservei
196     DelCourseReserve( cr_id => $course_reserve_id );
197     my $item_after = Koha::Items->find( $item->itemnumber );
198     is( $item->itype, $item_after->itype, "Itemtype is unchanged after adding to and removing from course reserves for inactive course");
199     is( $item->location, $item_after->location, "Location is unchanged after adding to and removing from course reserves for inactive course");
200     is( $item->holdingbranch, $item_after->holdingbranch, "Holdingbranch is unchanged after adding to and removing from course reserves for inactive course");
201     is( $item->ccode, $item_after->ccode, "Collection is unchanged after adding to and removing from course reserves for inactive course");
202
203     $course = $builder->build({
204         source => 'Course',
205         value => {
206             enabled => 'yes',
207         }
208     });
209     $item = $builder->build_sample_item({ ccode=>"grasshopper", location=>"transylvania"});
210     #Add course item but change nothing
211     $course_item_id = ModCourseItem(
212         itemnumber    => $item->itemnumber,
213         itype         => '',
214         ccode         => '',
215         holdingbranch => '',
216         location      => '',
217     );
218     #Add course reserve
219     $course_reserve_id = ModCourseReserve(
220         course_id   => $course->{course_id},
221         ci_id       => $course_item_id,
222         staff_note  => '',
223         public_note => '',
224     );
225     #Remove course reserve
226     DelCourseReserve( cr_id => $course_reserve_id );
227     $item_after = Koha::Items->find( $item->itemnumber );
228     is( $item->itype, $item_after->itype, "Itemtype is unchanged after adding to and removing from course reserves for inactive course");
229     is( $item->location, $item_after->location, "Location is unchanged after adding to and removing from course reserves for inactive course");
230     is( $item->holdingbranch, $item_after->holdingbranch, "Holdingbranch is unchanged after adding to and removing from course reserves for inactive course");
231     is( $item->ccode, $item_after->ccode, "Collection is unchanged after adding to and removing from course reserves for inactive course");
232
233 };
234
235
236
237
238
239 $schema->storage->txn_rollback;
240
241 sub create_dependent_objects {
242     $builder->build({
243         source => 'Itemtype',
244         value  => {
245             itemtype => 'CD_foo',
246             description => 'Compact Disk'
247         }
248     });
249
250     $builder->build({
251         source => 'Itemtype',
252         value  => {
253             itemtype => 'BK_foo',
254             description => 'Book'
255         }
256     });
257
258     $builder->build({
259         source => 'Branch',
260         value  => {
261             branchcode => 'B1',
262             branchname => 'Branch 1'
263         }
264     });
265
266     $builder->build({
267         source => 'Branch',
268         value  => {
269             branchcode => 'B2',
270             branchname => 'Branch 2'
271         }
272     });
273
274     $builder->build({
275         source => 'Branch',
276         value  => {
277             branchcode => 'B3',
278             branchname => 'Branch 3'
279         }
280     });
281
282     $builder->build({
283         source => 'AuthorisedValue',
284         value  => {
285             category => 'CCODE',
286             authorised_value => 'BOOK',
287             lib => 'Book'
288         }
289     });
290
291     $builder->build({
292         source => 'AuthorisedValue',
293         value  => {
294             category => 'CCODE',
295             authorised_value => 'DVD',
296             lib => 'Book'
297         }
298     });
299
300     $builder->build({
301         source => 'AuthorisedValue',
302         value  => {
303             category => 'CCODE',
304             authorised_value => 'CD',
305             lib => 'Compact Disk'
306         }
307     });
308
309     $builder->build({
310         source => 'AuthorisedValue',
311         value  => {
312             category => 'LOC',
313             authorised_value => 'HR',
314             lib => 'Here'
315         }
316     });
317
318     $builder->build({
319         source => 'AuthorisedValue',
320         value  => {
321             category => 'LOC',
322             authorised_value => 'TH',
323             lib => 'There'
324         }
325     });
326 }
327
328 sub create_bib_and_item {
329     my $biblio = $builder->build({
330         source => 'Biblio',
331         value  => {
332             title => 'Title',
333         }
334     });
335     my $item = $builder->build({
336         source => 'Item',
337         value  => {
338             biblionumber  => $biblio->{biblionumber},
339             itype         => 'CD_foo',
340             ccode         => 'CD',
341             location      => 'HR',
342             homebranch    => 'B1',
343             holdingbranch => 'B1',
344         }
345     });
346     return ($biblio->{biblionumber}, $item->{itemnumber});
347 }