4 use Test::More tests => 14;
6 use File::Temp qw/tempfile/;
9 use t::lib::TestBuilder;
15 use_ok('C4::ImportBatch');
19 my $schema = Koha::Database->new->schema;
20 $schema->storage->txn_begin;
21 my $builder = t::lib::TestBuilder->new;
22 my $dbh = C4::Context->dbh;
25 $dbh->do('DELETE FROM import_batches');
27 my $sample_import_batch1 = {
31 overlay_action => 'create_new',
32 nomatch_action => 'create_new',
33 item_action => 'always_add',
34 import_status => 'staged',
35 batch_type => 'z3950',
36 file_name => 'test.mrc',
38 record_type => 'auth',
41 my $sample_import_batch2 = {
45 overlay_action => 'create_new',
46 nomatch_action => 'create_new',
47 item_action => 'always_add',
48 import_status => 'staged',
49 batch_type => 'z3950',
50 file_name => 'test.mrc',
52 record_type => 'auth',
55 my $id_import_batch1 = C4::ImportBatch::AddImportBatch($sample_import_batch1);
56 my $id_import_batch2 = C4::ImportBatch::AddImportBatch($sample_import_batch2);
58 like( $id_import_batch1, '/^\d+$/', "AddImportBatch for sample_import_batch1 return an id" );
59 like( $id_import_batch2, '/^\d+$/', "AddImportBatch for sample_import_batch2 return an id" );
62 my $importbatch2 = C4::ImportBatch::GetImportBatch( $id_import_batch2 );
63 delete $importbatch2->{upload_timestamp};
64 delete $importbatch2->{import_batch_id};
65 delete $importbatch2->{num_records};
66 delete $importbatch2->{num_items};
68 is_deeply( $importbatch2, $sample_import_batch2,
69 "GetImportBatch returns the right informations about $sample_import_batch2" );
71 my $importbatch1 = C4::ImportBatch::GetImportBatch( $id_import_batch1 );
72 delete $importbatch1->{upload_timestamp};
73 delete $importbatch1->{import_batch_id};
74 delete $importbatch1->{num_records};
75 delete $importbatch1->{num_items};
77 is_deeply( $importbatch1, $sample_import_batch1,
78 "GetImportBatch returns the right informations about $sample_import_batch1" );
80 my $record = MARC::Record->new;
81 # FIXME Create another MARC::Record which won't be modified
82 # AddItemsToImportBiblio will remove the items field from the record passed in parameter.
83 my $original_record = MARC::Record->new;
84 $record->leader('03174nam a2200445 a 4500');
85 $original_record->leader('03174nam a2200445 a 4500');
86 my ($item_tag, $item_subfield) = C4::Biblio::GetMarcFromKohaField('items.itemnumber','');
90 a => 'Knuth, Donald Ervin',
95 a => 'The art of computer programming',
96 c => 'Donald E. Knuth.',
100 a => 'Computer programming.',
111 i => 'my item part 2',
114 $record->append_fields(@fields);
115 $original_record->append_fields(@fields);
116 my $import_record_id = AddBiblioToBatch( $id_import_batch1, 0, $record, 'utf8', int(rand(99999)), 0 );
117 AddItemsToImportBiblio( $id_import_batch1, $import_record_id, $record, 0 );
119 my $record_from_import_biblio_with_items = C4::ImportBatch::GetRecordFromImportBiblio( $import_record_id, 'embed_items' );
120 $original_record->leader($record_from_import_biblio_with_items->leader());
121 is_deeply( $record_from_import_biblio_with_items, $original_record, 'GetRecordFromImportBiblio should return the record with items if specified' );
122 $original_record->delete_fields($original_record->field($item_tag)); #Remove items fields
123 my $record_from_import_biblio_without_items = C4::ImportBatch::GetRecordFromImportBiblio( $import_record_id );
124 $original_record->leader($record_from_import_biblio_without_items->leader());
125 is_deeply( $record_from_import_biblio_without_items, $original_record, 'GetRecordFromImportBiblio should return the record without items by default' );
127 # Add a few tests for GetItemNumbersFromImportBatch
128 my @a = GetItemNumbersFromImportBatch( $id_import_batch1 );
129 is( @a, 0, 'No item numbers expected since we did not commit' );
130 my $itemno = $builder->build({ source => 'Item' })->{itemnumber};
131 # Link this item to the import item to fool GetItemNumbersFromImportBatch
132 my $sql = "UPDATE import_items SET itemnumber=? WHERE import_record_id=?";
133 $dbh->do( $sql, undef, $itemno, $import_record_id );
134 @a = GetItemNumbersFromImportBatch( $id_import_batch1 );
135 is( @a, 2, 'Expecting two items now' );
136 is( $a[0], $itemno, 'Check the first returned itemnumber' );
137 # Now delete the item and check again
138 $dbh->do( "DELETE FROM items WHERE itemnumber=?", undef, $itemno );
139 @a = GetItemNumbersFromImportBatch( $id_import_batch1 );
140 is( @a, 0, 'No item numbers expected since we deleted the item' );
141 $dbh->do( $sql, undef, undef, $import_record_id ); # remove link again
144 my $sample_import_batch3 = {
148 overlay_action => 'create_new',
149 nomatch_action => 'create_new',
150 item_action => 'always_add',
151 import_status => 'staged',
152 batch_type => 'z3950',
153 file_name => 'test.mrc',
155 record_type => 'auth',
158 my $id_import_batch3 = C4::ImportBatch::AddImportBatch($sample_import_batch3);
161 C4::ImportBatch::CleanBatch( $id_import_batch3 );
162 my $batch3_clean = $dbh->do('SELECT * FROM import_records WHERE import_batch_id = "$id_import_batch3"');
163 is( $batch3_clean, "0E0", "Batch 3 has been cleaned" );
166 C4::ImportBatch::DeleteBatch( $id_import_batch3 );
167 my $batch3_results = $dbh->do('SELECT * FROM import_batches WHERE import_batch_id = "$id_import_batch3"');
168 is( $batch3_results, "0E0", "Batch 3 has been deleted");
170 subtest "RecordsFromMarcPlugin" => sub {
174 my ( $fh, $name ) = tempfile();
179 245,a = Silence in the library
183 245,a = Noise in the library|;
185 t::lib::Mocks::mock_config( 'pluginsdir', dirname(__FILE__) . '/..' );
186 my ( $plugin ) = Koha::Plugins->new->GetPlugins({ metadata => { name => 'MarcFieldValues' } });
187 isnt( $plugin, undef, "Plugin found" );
188 my $records = C4::ImportBatch::RecordsFromMarcPlugin( $name, ref $plugin, 'UTF-8' );
189 is( @$records, 2, 'Two results returned' );
190 is( ref $records->[0], 'MARC::Record', 'Returned MARC::Record object' );
191 is( $records->[0]->subfield('245', 'a'), 'Silence in the library',
192 'Checked one field in first record' );
193 is( $records->[1]->subfield('100', 'a'), 'Another',
194 'Checked one field in second record' );
197 $schema->storage->txn_rollback;