Bug 5327 shifting database dependent modules and scripts to t/db_dependent
[koha.git] / t / db_dependent / lib / KohaTest / Biblio / ModBiblio.pm
1 package KohaTest::Biblio::ModBiblio;
2 use base qw( KohaTest::Biblio );
3
4 use strict;
5 use warnings;
6
7 use Test::More;
8
9 use C4::Biblio;
10 use C4::Items;
11
12 =head2 STARTUP METHODS
13
14 These get run once, before the main test methods in this module
15
16 =head3 add_bib_to_modify
17
18 =cut
19
20 sub add_bib_to_modify : Test( startup => 3 ) {
21     my $self = shift;
22
23     my $bib = MARC::Record->new();
24     $bib->leader('     ngm a22     7a 4500');   
25     $bib->append_fields(
26         MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
27         MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
28     );
29     
30     my ($bibnum, $bibitemnum) = AddBiblio($bib, '');
31     $self->{'bib_to_modify'} = $bibnum;
32
33     # add an item
34     my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => 'CPL', holdingbranch => 'CPL' } , $bibnum);
35
36     cmp_ok($item_bibnum, '==', $bibnum, "new item is linked to correct biblionumber"); 
37     cmp_ok($item_bibitemnum, '==', $bibitemnum, "new item is linked to correct biblioitemnumber"); 
38
39     $self->reindex_marc(); 
40
41     my $marc = $self->fetch_bib($bibnum);
42     $self->sort_item_and_bibnumber_fields($marc);
43     $self->{'bib_to_modify_formatted'} = $marc->as_formatted(); # simple way to compare later
44 }
45
46 =head2 TEST METHODS
47
48 standard test methods
49
50 =head3 bug_2297
51
52 Regression test for bug 2297 (saving a subscription duplicates MARC  item fields)
53
54 =cut
55
56 sub bug_2297 : Test( 5 ) {
57     my $self = shift;
58
59     my $bibnum = $self->{'bib_to_modify'};
60     my $marc = $self->fetch_bib($bibnum);
61     $self->check_item_count($marc, 1);
62
63     ModBiblio($marc, $bibnum, ''); # no change made to bib
64
65     my $modified_marc = $self->fetch_bib($bibnum);
66     diag "checking item field count after null modification";
67     $self->check_item_count($modified_marc, 1);
68
69     $self->sort_item_and_bibnumber_fields($modified_marc);
70     is($modified_marc->as_formatted(), $self->{'bib_to_modify_formatted'}, "no change to bib after null modification");
71 }
72
73 =head2 HELPER METHODS
74
75 These methods are used by other test methods, but
76 are not meant to be called directly.
77
78 =cut
79
80 =head3 fetch_bib
81
82 =cut
83
84 sub fetch_bib { # +1 to test count per call
85     my $self = shift;
86     my $bibnum = shift;
87
88     my $marc = GetMarcBiblio($bibnum);
89     ok(defined($marc), "retrieved bib record $bibnum");
90
91     return $marc;
92 }
93
94 =head3 check_item_count
95
96 =cut
97
98 sub check_item_count { # +1 to test count per call
99     my $self = shift;
100     my $marc = shift;
101     my $expected_items = shift;
102
103     my ($itemtag, $itemsubfield) = GetMarcFromKohaField("items.itemnumber", '');
104     my @item_fields = $marc->field($itemtag);
105     cmp_ok(scalar(@item_fields), "==", $expected_items, "exactly one item field");
106 }
107
108 =head3 sort_item_and_bibnumber_fields
109
110 This method sorts the field containing the embedded item data
111 and the bibnumber - ModBiblio(), AddBiblio(), and ModItem() do
112 not guarantee that these fields will be sorted in tag order.
113
114 =cut
115
116 sub sort_item_and_bibnumber_fields {
117     my $self = shift;
118     my $marc = shift;
119
120     my ($itemtag, $itemsubfield)     = GetMarcFromKohaField("items.itemnumber", '');
121     my ($bibnumtag, $bibnumsubfield) = GetMarcFromKohaField("biblio.biblionumber", '');
122
123     my @item_fields = ();
124     foreach my $field ($marc->field($itemtag)) {
125         push @item_fields, $field;
126         $marc->delete_field($field);
127     }
128     $marc->insert_fields_ordered(@item_fields) if scalar(@item_fields);;
129    
130     my @bibnum_fields = (); 
131     foreach my $field ($marc->field($bibnumtag)) {
132         push @bibnum_fields, $field;
133         $marc->delete_field($field);
134     }
135     $marc->insert_fields_ordered(@bibnum_fields) if scalar(@bibnum_fields);
136
137 }
138
139 =head2 SHUTDOWN METHODS
140
141 These get run once, after the main test methods in this module
142
143 =head3 shutdown_clean_object
144
145 =cut
146
147 sub shutdown_clean_object : Test( shutdown => 0 ) {
148     my $self = shift;
149
150     delete $self->{'bib_to_modify'};
151     delete $self->{'bib_to_modify_formatted'};
152 }
153
154 1;