Bug 22884: Show separator between 440 and 490 fields
[koha.git] / t / db_dependent / ImportBatch.t
blob66ffcf9aafa9e625fa7d8d74f0fae0519a9a4a34
1 #!/usr/bin/perl
3 use Modern::Perl;
4 use Test::More tests => 14;
5 use File::Basename;
6 use File::Temp qw/tempfile/;
8 use t::lib::Mocks;
9 use t::lib::TestBuilder;
11 use Koha::Database;
12 use Koha::Plugins;
14 BEGIN {
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');
21 # Start transaction
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;
27 # clear
28 $dbh->do('DELETE FROM import_batches');
30 my $sample_import_batch1 = {
31 matcher_id => 1,
32 template_id => 1,
33 branchcode => 'QRT',
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',
40 comments => 'test',
41 record_type => 'auth',
44 my $sample_import_batch2 = {
45 matcher_id => 2,
46 template_id => 2,
47 branchcode => 'QRZ',
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',
54 comments => 'test',
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" );
64 #Test GetImportBatch
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' );
90 my @fields = (
91 MARC::Field->new(
92 100, '1', ' ',
93 a => 'Knuth, Donald Ervin',
94 d => '1938',
96 MARC::Field->new(
97 245, '1', '4',
98 a => 'The art of computer programming',
99 c => 'Donald E. Knuth.',
101 MARC::Field->new(
102 650, ' ', '0',
103 a => 'Computer programming.',
104 9 => '462',
106 MARC::Field->new(
107 $item_tag, ' ', ' ',
108 e => 'my edition',
109 i => 'my item part',
111 MARC::Field->new(
112 $item_tag, ' ', ' ',
113 e => 'my edition 2',
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
146 # fresh data
147 my $sample_import_batch3 = {
148 matcher_id => 3,
149 template_id => 3,
150 branchcode => 'QRT',
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',
157 comments => 'test',
158 record_type => 'auth',
161 my $id_import_batch3 = C4::ImportBatch::AddImportBatch($sample_import_batch3);
163 # Test CleanBatch
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" );
168 # Test DeleteBatch
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 {
174 plan tests => 5;
176 # Create a test file
177 my ( $fh, $name ) = tempfile();
178 print $fh q|
179 003 = NLAmRIJ
180 100,a = Author
181 245,ind2 = 0
182 245,a = Silence in the library
183 500 , a= Some note
185 100,a = Another
186 245,a = Noise in the library|;
187 close $fh;
189 t::lib::Mocks::mock_config( 'enable_plugins', 1 );
190 t::lib::Mocks::mock_preference( 'UseKohaPlugins', 1 );
192 my $plugins = Koha::Plugins->new;
193 $plugins->InstallPlugins;
194 my ($plugin) = $plugins->GetPlugins({ all => 1, metadata => { name => 'MarcFieldValues' } });
195 isnt( $plugin, undef, "Plugin found" );
196 my $records = C4::ImportBatch::RecordsFromMarcPlugin( $name, ref $plugin, 'UTF-8' );
197 is( @$records, 2, 'Two results returned' );
198 is( ref $records->[0], 'MARC::Record', 'Returned MARC::Record object' );
199 is( $records->[0]->subfield('245', 'a'), 'Silence in the library',
200 'Checked one field in first record' );
201 is( $records->[1]->subfield('100', 'a'), 'Another',
202 'Checked one field in second record' );
205 $schema->storage->txn_rollback;