1 package KohaTest::ImportBatch::BatchStageCommitRevert;
2 use base qw( KohaTest::ImportBatch );
13 # define test records for various batches
14 sub startup_60_make_test_records : Test( startup ) {
16 $self->{'batches'} = {
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' ] ],
27 overlay_action => 'create_new',
28 nomatch_action => 'create_new',
29 item_action => 'always_add',
39 num_items_errored => 0,
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'] ],
51 overlay_action => 'replace',
52 nomatch_action => 'create_new',
53 item_action => 'always_add',
63 num_items_errored => 0,
68 marc => _make_marc_batch([
69 ['isbn007', 'title 7', ['batch-item-11'] ],
70 ['isbn006', 'overlay title 6', ['batch-item-12'] ],
74 overlay_action => 'ignore',
75 nomatch_action => 'ignore',
76 item_action => 'always_add',
86 num_items_errored => 0,
91 marc => _make_marc_batch([
92 ['isbn008', 'title 8', ['batch-item-13'] ], # not loading this item
96 overlay_action => undef,
97 nomatch_action => 'create_new',
98 item_action => 'ignore',
107 num_items_added => 0,
108 num_items_errored => 0,
113 marc => _make_marc_batch([
114 ['isbn009', 'title 9', ['batch-item-1'] ], # trigger dup barcode error
115 'junkjunkjunkjunk', # trigger invalid bib
119 overlay_action => undef,
120 nomatch_action => undef,
121 item_action => undef,
130 num_items_added => 0,
131 num_items_errored => 1,
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'] ],
142 overlay_action => 'ignore',
143 nomatch_action => 'create_new',
144 item_action => 'always_add',
153 num_items_added => 4,
154 num_items_errored => 0,
162 sub _make_marc_batch {
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
182 push @marc, $bib->as_usmarc();
187 return join('', @marc);
190 sub stage_commit_batches : Test( 75 ) {
193 my $matcher = C4::Matcher->fetch($self->{'matcher_id'});
194 ok(ref($matcher) eq 'C4::Matcher', "retrieved matcher");
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");
208 my $num_matches = BatchFindBibDuplicates($batch_id, $matcher, 10);
209 cmp_ok($num_matches, "==", $results->{'num_matches'}, "$batch_key: correct number of bib matches");
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");
215 SetImportBatchOverlayAction($batch_id, $args->{'overlay_action'});
216 cmp_ok(GetImportBatchOverlayAction($batch_id), "eq", $args->{'overlay_action'},
217 "$batch_key: changed overlay action");
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");
224 SetImportBatchNoMatchAction($batch_id, $args->{'nomatch_action'});
225 cmp_ok(GetImportBatchNoMatchAction($batch_id), "eq", $args->{'nomatch_action'},
226 "$batch_key: changed nomatch action");
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");
233 SetImportBatchItemAction($batch_id, $args->{'item_action'});
234 cmp_ok(GetImportBatchItemAction($batch_id), "eq", $args->{'item_action'},
235 "$batch_key: changed item action");
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");
247 $self->reindex_marc();