staging import - enhance record overlay behavior
[koha.git] / t / lib / KohaTest / ImportBatch / BatchStageCommitRevert.pm
1 package KohaTest::ImportBatch::BatchStageCommitRevert;
2 use base qw( KohaTest::ImportBatch );
3
4 use strict;
5 use warnings;
6
7 use Test::More;
8
9 use C4::ImportBatch;
10 use C4::Matcher;
11 use C4::Biblio;
12
13 # define test records for various batches
14 sub startup_60_make_test_records : Test( startup ) {
15     my $self = shift;
16     $self->{'batches'} = {
17         'batch1' => { 
18                         marc => _make_marc_batch([
19                             ['isbn001', 'title 1', ['batch-item-1'] ],
20                             ['isbn002', 'title 2', [] ],
21                             ['isbn003', 'title 3', ['batch-item-2','batch-item-3'] ],
22                             ['isbn004', 'title 4', [ 'batch-item-4' ] ],
23                             ['isbn005', 'title 5', [ 'batch-item-5', 'batch-item-6', 'batch-item-7' ] ],
24                         ]),
25                         args => {
26                             parse_items => 1,
27                             overlay_action => 'create_new',
28                             nomatch_action => 'create_new',
29                             item_action => 'always_add',
30                         },
31                         results => {
32                             num_bibs  => 5,
33                             num_items => 7,
34                             num_invalid => 0,
35                             num_matches => 0,
36                             num_added => 5,
37                             num_updated => 0,
38                             num_items_added => 7,
39                             num_items_errored => 0,
40                             num_ignored => 0,
41                         },
42                     },
43         'batch2' => {
44                         marc => _make_marc_batch([
45                             ['isbn001', 'overlay title 1', ['batch-item-8'] ],
46                             ['isbn002', 'overlay title 2', ['batch-item-9'] ],
47                             ['isbn006', 'title 6', ['batch-item-10'] ],
48                         ]),
49                         args => {
50                             parse_items => 1,
51                             overlay_action => 'replace',
52                             nomatch_action => 'create_new',
53                             item_action => 'always_add',
54                         },
55                         results => {
56                             num_bibs  => 3,
57                             num_items => 3,
58                             num_invalid => 0,
59                             num_matches => 2,
60                             num_added => 1,
61                             num_updated => 2,
62                             num_items_added => 3,
63                             num_items_errored => 0,
64                             num_ignored => 0,
65                         },
66                     },
67         'batch3' => {
68                         marc => _make_marc_batch([ 
69                             ['isbn007', 'title 7', ['batch-item-11'] ],
70                             ['isbn006', 'overlay title 6', ['batch-item-12'] ],
71                         ]),
72                         args => {
73                             parse_items => 1,
74                             overlay_action => 'ignore',
75                             nomatch_action => 'ignore',
76                             item_action => 'always_add',
77                         },
78                         results => {
79                             num_bibs  => 2,
80                             num_items => 2,
81                             num_invalid => 0,
82                             num_matches => 1,
83                             num_added => 0,
84                             num_updated => 0,
85                             num_items_added => 1,
86                             num_items_errored => 0,
87                             num_ignored => 2,
88                         },
89                     },
90         'batch4' => {
91                         marc => _make_marc_batch([ 
92                             ['isbn008', 'title 8', ['batch-item-13'] ], # not loading this item
93                         ]),
94                         args => {
95                             parse_items => 0,
96                             overlay_action => undef,
97                             nomatch_action => 'create_new',
98                             item_action => 'ignore',
99                         },
100                         results => {
101                             num_bibs  => 1,
102                             num_items => 0,
103                             num_invalid => 0,
104                             num_matches => 0,
105                             num_added => 1,
106                             num_updated => 0,
107                             num_items_added => 0,
108                             num_items_errored => 0,
109                             num_ignored => 0,
110                         },
111                     },
112         'batch5' => {
113                         marc => _make_marc_batch([ 
114                             ['isbn009', 'title 9', ['batch-item-1'] ], # trigger dup barcode error
115                             'junkjunkjunkjunk', # trigger invalid bib
116                         ]),
117                         args => {
118                             parse_items => 1,
119                             overlay_action => undef,
120                             nomatch_action => undef,
121                             item_action => undef,
122                         },
123                         results => {
124                             num_bibs  => 1,
125                             num_items => 1,
126                             num_invalid => 1,
127                             num_matches => 0,
128                             num_added => 1,
129                             num_updated => 0,
130                             num_items_added => 0,
131                             num_items_errored => 1,
132                             num_ignored => 0,
133                         },
134                     },
135         'batch6' => {
136                         marc => _make_marc_batch([ 
137                             ['isbn001', 'match title 1', ['batch-item-14', 'batch-item-15'] ],
138                             ['isbn010', 'title 10', ['batch-item-16', 'batch-item-17'] ],
139                         ]),
140                         args => {
141                             parse_items => 1,
142                             overlay_action => 'ignore',
143                             nomatch_action => 'create_new',
144                             item_action => 'always_add',
145                         },
146                         results => {
147                             num_bibs  => 2,
148                             num_items => 4,
149                             num_invalid => 0,
150                             num_matches => 1,
151                             num_added => 1,
152                             num_updated => 0,
153                             num_items_added => 4,
154                             num_items_errored => 0,
155                             num_ignored => 1,
156                         },
157                     },
158     };
159     
160 }
161
162 sub _make_marc_batch {
163     my $defs = shift;
164     my @marc = ();
165     foreach my $rec (@$defs) {
166         if (ref($rec) eq 'ARRAY') {
167             my $isbn = $rec->[0];
168             my $title = $rec->[1];
169             my $items = $rec->[2];
170             my $bib = MARC::Record->new();
171             $bib->leader('     nam a22     7a 4500');
172             $bib->append_fields(MARC::Field->new('020', ' ', ' ', a => $isbn),
173                                 MARC::Field->new('245', ' ', ' ', a => $title));
174             foreach my $barcode (@$items) {
175                 my ($itemtag, $toss, $barcodesf, $branchsf);
176                 ($itemtag, $toss)   = GetMarcFromKohaField('items.itemnumber', '');
177                 ($toss, $barcodesf) = GetMarcFromKohaField('items.barcode', '');
178                 ($toss, $branchsf)  = GetMarcFromKohaField('items.homebranch', '');
179                 $bib->append_fields(MARC::Field->new($itemtag, ' ', ' ', $barcodesf => $barcode, $branchsf => 'CPL')); 
180                         # FIXME: define branch in KohaTest
181             }
182             push @marc, $bib->as_usmarc();
183         } else {
184             push @marc, $rec;
185         }
186     }
187     return join('', @marc);
188 }
189
190 sub stage_commit_batches : Test( 75 ) {
191     my $self = shift;
192
193     my $matcher = C4::Matcher->fetch($self->{'matcher_id'});
194     ok(ref($matcher) eq 'C4::Matcher', "retrieved matcher");
195
196     for my $batch_key (sort keys %{ $self->{'batches'} }) {
197         my $batch = $self->{'batches'}->{$batch_key};
198         my $args = $batch->{'args'};
199         my $results = $batch->{'results'};
200         my ($batch_id, $num_bibs, $num_items, @invalid) =
201             BatchStageMarcRecords('MARC21', $batch->{marc}, "$batch_key.mrc", "$batch_key comments", 
202                                   '', $args->{'parse_items'}, 0);
203         like($batch_id, qr/^\d+$/, "staged $batch_key");
204         cmp_ok($num_bibs, "==", $results->{'num_bibs'}, "$batch_key: correct number of bibs");
205         cmp_ok($num_items, "==", $results->{'num_items'}, "$batch_key: correct number of items");
206         cmp_ok(scalar(@invalid), "==", $results->{'num_invalid'}, "$batch_key: correct number of invalid bibs");
207
208         my $num_matches = BatchFindBibDuplicates($batch_id, $matcher, 10);
209         cmp_ok($num_matches, "==", $results->{'num_matches'}, "$batch_key: correct number of bib matches");
210
211         if (defined $args->{'overlay_action'}) {
212             if ($args->{'overlay_action'} eq 'create_new') {
213                 cmp_ok(GetImportBatchOverlayAction($batch_id), "eq", 'create_new', "$batch_key: verify default overlay action");
214             } else {
215                 SetImportBatchOverlayAction($batch_id, $args->{'overlay_action'});
216                 cmp_ok(GetImportBatchOverlayAction($batch_id), "eq", $args->{'overlay_action'}, 
217                                                    "$batch_key: changed overlay action");
218             }
219         }
220         if (defined $args->{'nomatch_action'}) {
221             if ($args->{'nomatch_action'} eq 'create_new') {
222                 cmp_ok(GetImportBatchNoMatchAction($batch_id), "eq", 'create_new', "$batch_key: verify default nomatch action");
223             } else {
224                 SetImportBatchNoMatchAction($batch_id, $args->{'nomatch_action'});
225                 cmp_ok(GetImportBatchNoMatchAction($batch_id), "eq", $args->{'nomatch_action'}, 
226                                                    "$batch_key: changed nomatch action");
227             }
228         }
229         if (defined $args->{'item_action'}) {
230             if ($args->{'item_action'} eq 'create_new') {
231                 cmp_ok(GetImportBatchItemAction($batch_id), "eq", 'always_add', "$batch_key: verify default item action");
232             } else {
233                 SetImportBatchItemAction($batch_id, $args->{'item_action'});
234                 cmp_ok(GetImportBatchItemAction($batch_id), "eq", $args->{'item_action'}, 
235                                                    "$batch_key: changed item action");
236             }
237         }
238
239         my ($num_added, $num_updated, $num_items_added, 
240             $num_items_errored, $num_ignored) = BatchCommitBibRecords($batch_id);
241         cmp_ok($num_added,         "==", $results->{'num_added'},         "$batch_key: added correct number of bibs");
242         cmp_ok($num_updated,       "==", $results->{'num_updated'},       "$batch_key: updated correct number of bibs");
243         cmp_ok($num_items_added,   "==", $results->{'num_items_added'},   "$batch_key: added correct number of items");
244         cmp_ok($num_items_errored, "==", $results->{'num_items_errored'}, "$batch_key: correct number of item add errors");
245         cmp_ok($num_ignored,       "==", $results->{'num_ignored'},       "$batch_key: ignored correct number of bibs");
246
247         $self->reindex_marc();
248     }
249      
250 }
251
252 1;