4 use Test::More tests => 14;
6 use File::Temp qw/tempfile/;
9 use t::lib::TestBuilder;
15 # Mock pluginsdir before loading Plugins module
16 my $path = dirname(__FILE__) . '/../lib';
17 t::lib::Mocks::mock_config( 'pluginsdir', $path );
18 use_ok('C4::ImportBatch');
22 my $schema = Koha::Database->new->schema;
23 $schema->storage->txn_begin;
24 my $builder = t::lib::TestBuilder->new;
25 my $dbh = C4::Context->dbh;
28 $dbh->do('DELETE FROM import_batches');
30 my $sample_import_batch1 = {
34 overlay_action => 'create_new',
35 nomatch_action => 'create_new',
36 item_action => 'always_add',
37 import_status => 'staged',
38 batch_type => 'z3950',
39 file_name => 'test.mrc',
41 record_type => 'auth',
44 my $sample_import_batch2 = {
48 overlay_action => 'create_new',
49 nomatch_action => 'create_new',
50 item_action => 'always_add',
51 import_status => 'staged',
52 batch_type => 'z3950',
53 file_name => 'test.mrc',
55 record_type => 'auth',
58 my $id_import_batch1 = C4::ImportBatch::AddImportBatch($sample_import_batch1);
59 my $id_import_batch2 = C4::ImportBatch::AddImportBatch($sample_import_batch2);
61 like( $id_import_batch1, '/^\d+$/', "AddImportBatch for sample_import_batch1 return an id" );
62 like( $id_import_batch2, '/^\d+$/', "AddImportBatch for sample_import_batch2 return an id" );
65 my $importbatch2 = C4::ImportBatch::GetImportBatch( $id_import_batch2 );
66 delete $importbatch2->{upload_timestamp};
67 delete $importbatch2->{import_batch_id};
68 delete $importbatch2->{num_records};
69 delete $importbatch2->{num_items};
71 is_deeply( $importbatch2, $sample_import_batch2,
72 "GetImportBatch returns the right informations about $sample_import_batch2" );
74 my $importbatch1 = C4::ImportBatch::GetImportBatch( $id_import_batch1 );
75 delete $importbatch1->{upload_timestamp};
76 delete $importbatch1->{import_batch_id};
77 delete $importbatch1->{num_records};
78 delete $importbatch1->{num_items};
80 is_deeply( $importbatch1, $sample_import_batch1,
81 "GetImportBatch returns the right informations about $sample_import_batch1" );
83 my $record = MARC::Record->new;
84 # FIXME Create another MARC::Record which won't be modified
85 # AddItemsToImportBiblio will remove the items field from the record passed in parameter.
86 my $original_record = MARC::Record->new;
87 $record->leader('03174nam a2200445 a 4500');
88 $original_record->leader('03174nam a2200445 a 4500');
89 my ($item_tag, $item_subfield) = C4::Biblio::GetMarcFromKohaField('items.itemnumber','');
93 a => 'Knuth, Donald Ervin',
98 a => 'The art of computer programming',
99 c => 'Donald E. Knuth.',
103 a => 'Computer programming.',
114 i => 'my item part 2',
117 $record->append_fields(@fields);
118 $original_record->append_fields(@fields);
119 my $import_record_id = AddBiblioToBatch( $id_import_batch1, 0, $record, 'utf8', int(rand(99999)), 0 );
120 AddItemsToImportBiblio( $id_import_batch1, $import_record_id, $record, 0 );
122 my $record_from_import_biblio_with_items = C4::ImportBatch::GetRecordFromImportBiblio( $import_record_id, 'embed_items' );
123 $original_record->leader($record_from_import_biblio_with_items->leader());
124 is_deeply( $record_from_import_biblio_with_items, $original_record, 'GetRecordFromImportBiblio should return the record with items if specified' );
125 $original_record->delete_fields($original_record->field($item_tag)); #Remove items fields
126 my $record_from_import_biblio_without_items = C4::ImportBatch::GetRecordFromImportBiblio( $import_record_id );
127 $original_record->leader($record_from_import_biblio_without_items->leader());
128 is_deeply( $record_from_import_biblio_without_items, $original_record, 'GetRecordFromImportBiblio should return the record without items by default' );
130 # Add a few tests for GetItemNumbersFromImportBatch
131 my @a = GetItemNumbersFromImportBatch( $id_import_batch1 );
132 is( @a, 0, 'No item numbers expected since we did not commit' );
133 my $itemno = $builder->build({ source => 'Item' })->{itemnumber};
134 # Link this item to the import item to fool GetItemNumbersFromImportBatch
135 my $sql = "UPDATE import_items SET itemnumber=? WHERE import_record_id=?";
136 $dbh->do( $sql, undef, $itemno, $import_record_id );
137 @a = GetItemNumbersFromImportBatch( $id_import_batch1 );
138 is( @a, 2, 'Expecting two items now' );
139 is( $a[0], $itemno, 'Check the first returned itemnumber' );
140 # Now delete the item and check again
141 $dbh->do( "DELETE FROM items WHERE itemnumber=?", undef, $itemno );
142 @a = GetItemNumbersFromImportBatch( $id_import_batch1 );
143 is( @a, 0, 'No item numbers expected since we deleted the item' );
144 $dbh->do( $sql, undef, undef, $import_record_id ); # remove link again
147 my $sample_import_batch3 = {
151 overlay_action => 'create_new',
152 nomatch_action => 'create_new',
153 item_action => 'always_add',
154 import_status => 'staged',
155 batch_type => 'z3950',
156 file_name => 'test.mrc',
158 record_type => 'auth',
161 my $id_import_batch3 = C4::ImportBatch::AddImportBatch($sample_import_batch3);
164 C4::ImportBatch::CleanBatch( $id_import_batch3 );
165 my $batch3_clean = $dbh->do('SELECT * FROM import_records WHERE import_batch_id = "$id_import_batch3"');
166 is( $batch3_clean, "0E0", "Batch 3 has been cleaned" );
169 C4::ImportBatch::DeleteBatch( $id_import_batch3 );
170 my $batch3_results = $dbh->do('SELECT * FROM import_batches WHERE import_batch_id = "$id_import_batch3"');
171 is( $batch3_results, "0E0", "Batch 3 has been deleted");
173 subtest "RecordsFromMarcPlugin" => sub {
177 my ( $fh, $name ) = tempfile();
182 245,a = Silence in the library
186 245,a = Noise in the library|;
189 t::lib::Mocks::mock_config( 'enable_plugins', 1 );
190 t::lib::Mocks::mock_preference( 'UseKohaPlugins', 1 );
191 my ($plugin) = Koha::Plugins->new->GetPlugins({ all => 1, metadata => { name => 'MarcFieldValues' } });
192 isnt( $plugin, undef, "Plugin found" );
193 my $records = C4::ImportBatch::RecordsFromMarcPlugin( $name, ref $plugin, 'UTF-8' );
194 is( @$records, 2, 'Two results returned' );
195 is( ref $records->[0], 'MARC::Record', 'Returned MARC::Record object' );
196 is( $records->[0]->subfield('245', 'a'), 'Silence in the library',
197 'Checked one field in first record' );
198 is( $records->[1]->subfield('100', 'a'), 'Another',
199 'Checked one field in second record' );
202 $schema->storage->txn_rollback;