bug 2466: regression test
[koha.git] / t / lib / KohaTest / Items / ModItemsFromMarc.pm
1 package KohaTest::Items::ModItemsFromMarc;
2 use base qw( KohaTest::Items );
3
4 use strict;
5 use warnings;
6
7 use Test::More;
8
9 use C4::Context;
10 use C4::Biblio;
11 use C4::Items;
12
13 =head2 STARTUP METHODS
14
15 These get run once, before the main test methods in this module
16
17 =cut
18
19 =head2 startup_90_add_item_get_callnumber
20
21 =cut
22
23 sub startup_90_add_item_get_callnumber : Test( startup => 13 ) {
24     my $self = shift;
25
26     $self->add_biblios( count => 1, add_items => 1 );
27
28     ok( $self->{'items'}, 'An item has been aded' )
29       or diag( Data::Dumper->Dump( [ $self->{'items'} ], ['items'] ) );
30
31     my @biblioitems = C4::Biblio::GetBiblioItemByBiblioNumber( $self->{'items'}[0]{'biblionumber'} );
32     ok( $biblioitems[0]->{'biblioitemnumber'}, '...and it has a biblioitemnumber' )
33       or diag( Data::Dumper->Dump( [ \@biblioitems ], ['biblioitems'] ) );
34
35     my $items_info = GetItemsByBiblioitemnumber( $biblioitems[0]->{'biblioitemnumber'} );
36     isa_ok( $items_info, 'ARRAY', '...and we can search with that biblioitemnumber' )
37       or diag( Data::Dumper->Dump( [$items_info], ['items_info'] ) );
38     cmp_ok( scalar @$items_info, '>', 0, '...and we can find at least one item with that biblioitemnumber' );
39
40     my $item_info = $items_info->[0];
41     ok( $item_info->{'itemcallnumber'}, '...and the item we found has a call number: ' . $item_info->{'itemcallnumber'} )
42       or diag( Data::Dumper->Dump( [$item_info], ['item_info'] ) );
43
44     $self->{itemnumber} = $item_info->{itemnumber};
45 }
46
47
48 =head2 TEST METHODS
49
50 standard test methods
51
52 =head3 bug2466
53
54 Regression test for bug 2466 (when clearing an item field
55 via the cataloging or serials item editor, corresponding
56 column is not cleared).
57
58 =cut
59
60 sub bug2466 : Test( 8 ) {
61     my $self = shift;
62
63     my $item = C4::Items::GetItem($self->{itemnumber});
64     isa_ok($item, 'HASH', "item $self->{itemnumber} exists");
65    
66     my $item_marc = C4::Items::GetMarcItem($item->{biblionumber}, $self->{itemnumber});
67     isa_ok($item_marc, 'MARC::Record', "retrieved item MARC");
68
69     cmp_ok($item->{itemcallnumber}, 'ne', '', "item call number is not blank");
70
71     my ($callnum_tag, $callnum_subfield) = C4::Biblio::GetMarcFromKohaField('items.itemcallnumber', '');
72     cmp_ok($callnum_tag, '>', 0, "found tag for itemcallnumber");
73
74     my $item_field = $item_marc->field($callnum_tag);
75     ok(defined($item_field), "retrieved MARC field for item");
76
77     $item_field->delete_subfield(code => $callnum_subfield);
78
79     my $dbh = C4::Context->dbh;
80     my $item_from_marc = C4::Biblio::TransformMarcToKoha($dbh, $item_marc, '', 'items');
81     ok(not(exists($item_from_marc->{itemcallnumber})), "itemcallnumber subfield removed");
82
83     C4::Items::ModItemFromMarc($item_marc, $item->{biblionumber}, $self->{itemnumber});
84
85     my $modified_item = C4::Items::GetItem($self->{itemnumber});
86     isa_ok($modified_item, 'HASH', "retrieved modified item");
87
88     ok(not(defined($modified_item->{itemcallnumber})), "itemcallnumber is now undef");
89 }
90
91 1;