Bug 25793: OAI 'Set' and 'Metadata' dropdowns broken by OPAC jQuery upgrade
[koha.git] / t / db_dependent / CourseReserves / CourseItems.t
blobdf179a922c813643ca4e14bfada7a818acba2449
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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>.
18 use Modern::Perl;
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;
26 use Test::More tests => 35;
28 BEGIN {
29 require_ok('C4::CourseReserves');
32 my $schema = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
35 my $builder = t::lib::TestBuilder->new();
37 create_dependent_objects();
38 my ($biblionumber, $itemnumber) = create_bib_and_item();
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',
54 my $course = $builder->build({
55 source => 'CourseReserve',
56 value => {
57 ci_id => $ci_id,
58 enabled => 'no',
60 });
62 my $cr_id = ModCourseReserve(
63 course_id => $course->{course_id},
64 ci_id => $ci_id,
65 staff_note => '',
66 public_note => '',
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');
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');
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',
97 ModCourseReserve(
98 course_id => $course->{course_id},
99 ci_id => $ci_id,
100 staff_note => '',
101 public_note => '',
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');
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');
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');
126 # Test the specific case described on bug #10382.
127 $item->ccode('')->store;
129 $item = Koha::Items->find($itemnumber);
130 is($item->ccode, '', 'Item ccode should be empty');
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',
146 my $cr_id2 = ModCourseReserve(
147 course_id => $course->{course_id},
148 ci_id => $ci_id2,
149 staff_note => '',
150 public_note => '',
153 $item = Koha::Items->find($itemnumber);
154 is($item->ccode, 'BOOK', 'Item ccode should be BOOK');
156 my $course_item2 = GetCourseItem( ci_id => $ci_id2 );
157 is($course_item2->{ccode_storage}, '', 'Course item ccode storage should be empty');
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',
173 ModCourseReserve(
174 course_id => $course->{course_id},
175 ci_id => $ci_id2,
176 staff_note => '',
177 public_note => '',
180 $item = Koha::Items->find($itemnumber);
181 is($item->ccode, 'DVD', 'Item ccode should be DVD');
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',
195 $item = Koha::Items->find($itemnumber);
196 is($item->ccode, 'BOOK', 'Item ccode should be BOOK');
198 $course_item2 = GetCourseItem( ci_id => $ci_id2 );
199 is($course_item2->{ccode_storage}, '', 'Course item ccode storage should be empty');
201 DelCourseReserve( cr_id => $cr_id2 );
202 $item = Koha::Items->find($itemnumber);
203 is($item->ccode, '', 'Item ccode should be set back to empty');
205 subtest 'Ensure modifying fields on existing course items updates the item and course item' => sub {
206 plan tests => 60;
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,
218 my $course = $builder->build({
219 source => 'CourseReserve',
220 value => {
221 ci_id => $ci_id,
222 enabled => 'no',
226 my $cr_id = ModCourseReserve(
227 course_id => $course->{course_id},
228 ci_id => $ci_id,
229 staff_note => '',
230 public_note => '',
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');
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');
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');
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');
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',
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');
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');
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');
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');
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,
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');
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');
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');
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');
336 subtest 'Ensure item info is preserved' => sub {
337 plan tests => 8;
339 my $course = $builder->build({
340 source => 'Course',
341 value => {
342 enabled => 'no',
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 => '',
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 => '',
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");
369 $course = $builder->build({
370 source => 'Course',
371 value => {
372 enabled => 'yes',
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 => '',
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 => '',
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");
405 $schema->storage->txn_rollback;
407 sub create_dependent_objects {
408 $builder->build({
409 source => 'Itemtype',
410 value => {
411 itemtype => 'CD_foo',
412 description => 'Compact Disk'
416 $builder->build({
417 source => 'Itemtype',
418 value => {
419 itemtype => 'BK_foo',
420 description => 'Book'
424 $builder->build({
425 source => 'Branch',
426 value => {
427 branchcode => 'B1',
428 branchname => 'Branch 1'
432 $builder->build({
433 source => 'Branch',
434 value => {
435 branchcode => 'B2',
436 branchname => 'Branch 2'
440 $builder->build({
441 source => 'Branch',
442 value => {
443 branchcode => 'B3',
444 branchname => 'Branch 3'
448 $builder->build({
449 source => 'AuthorisedValue',
450 value => {
451 category => 'CCODE',
452 authorised_value => 'BOOK',
453 lib => 'Book'
457 $builder->build({
458 source => 'AuthorisedValue',
459 value => {
460 category => 'CCODE',
461 authorised_value => 'DVD',
462 lib => 'Book'
466 $builder->build({
467 source => 'AuthorisedValue',
468 value => {
469 category => 'CCODE',
470 authorised_value => 'CD',
471 lib => 'Compact Disk'
475 $builder->build({
476 source => 'AuthorisedValue',
477 value => {
478 category => 'LOC',
479 authorised_value => 'HR',
480 lib => 'Here'
484 $builder->build({
485 source => 'AuthorisedValue',
486 value => {
487 category => 'LOC',
488 authorised_value => 'TH',
489 lib => 'There'
494 sub create_bib_and_item {
495 my $biblio = $builder->build({
496 source => 'Biblio',
497 value => {
498 title => 'Title',
501 my $item = $builder->build({
502 source => 'Item',
503 value => {
504 biblionumber => $biblio->{biblionumber},
505 itype => 'CD_foo',
506 ccode => 'CD',
507 location => 'HR',
508 homebranch => 'B1',
509 holdingbranch => 'B1',
512 return ($biblio->{biblionumber}, $item->{itemnumber});