4d985f96d18577b8acb8ad9d729388d3173f42fc
[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 => 27;
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 BOOK');
152
153 $course_item2 = GetCourseItem( ci_id => $ci_id2 );
154 is($course_item2->{ccode}, '', 'Course item ccode should be empty');
155
156 DelCourseReserve( cr_id => $cr_id2 );
157 $item = Koha::Items->find($itemnumber);
158 is($item->ccode, '', 'Item ccode should be set back to empty');
159
160 $schema->storage->txn_rollback;
161
162 sub create_dependent_objects {
163     $builder->build({
164         source => 'Itemtype',
165         value  => {
166             itemtype => 'CD_foo',
167             description => 'Compact Disk'
168         }
169     });
170
171     $builder->build({
172         source => 'Itemtype',
173         value  => {
174             itemtype => 'BK_foo',
175             description => 'Book'
176         }
177     });
178
179     $builder->build({
180         source => 'Branch',
181         value  => {
182             branchcode => 'B1',
183             branchname => 'Branch 1'
184         }
185     });
186
187     $builder->build({
188         source => 'Branch',
189         value  => {
190             branchcode => 'B2',
191             branchname => 'Branch 2'
192         }
193     });
194
195     $builder->build({
196         source => 'Branch',
197         value  => {
198             branchcode => 'B3',
199             branchname => 'Branch 3'
200         }
201     });
202
203     $builder->build({
204         source => 'AuthorisedValue',
205         value  => {
206             category => 'CCODE',
207             authorised_value => 'BOOK',
208             lib => 'Book'
209         }
210     });
211
212     $builder->build({
213         source => 'AuthorisedValue',
214         value  => {
215             category => 'CCODE',
216             authorised_value => 'DVD',
217             lib => 'Book'
218         }
219     });
220
221     $builder->build({
222         source => 'AuthorisedValue',
223         value  => {
224             category => 'CCODE',
225             authorised_value => 'CD',
226             lib => 'Compact Disk'
227         }
228     });
229
230     $builder->build({
231         source => 'AuthorisedValue',
232         value  => {
233             category => 'LOC',
234             authorised_value => 'HR',
235             lib => 'Here'
236         }
237     });
238
239     $builder->build({
240         source => 'AuthorisedValue',
241         value  => {
242             category => 'LOC',
243             authorised_value => 'TH',
244             lib => 'There'
245         }
246     });
247 }
248
249 sub create_bib_and_item {
250     my $biblio = $builder->build({
251         source => 'Biblio',
252         value  => {
253             title => 'Title',
254         }
255     });
256     my $item = $builder->build({
257         source => 'Item',
258         value  => {
259             biblionumber  => $biblio->{biblionumber},
260             itype         => 'CD_foo',
261             ccode         => 'CD',
262             location      => 'HR',
263             homebranch    => 'B1',
264             holdingbranch => 'B1',
265         }
266     });
267     return ($biblio->{biblionumber}, $item->{itemnumber});
268 }