Bug 19049: Testing RecordsFromMarcPlugin with a to_marc plugin
[koha.git] / t / db_dependent / ImportBatch.t
blob607c71ea1d26e717e00837f2a98278949a4ccd70
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 use_ok('C4::ImportBatch');
18 # Start transaction
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;
24 # clear
25 $dbh->do('DELETE FROM import_batches');
27 my $sample_import_batch1 = {
28 matcher_id => 1,
29 template_id => 1,
30 branchcode => 'QRT',
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',
37 comments => 'test',
38 record_type => 'auth',
41 my $sample_import_batch2 = {
42 matcher_id => 2,
43 template_id => 2,
44 branchcode => 'QRZ',
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',
51 comments => 'test',
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" );
61 #Test GetImportBatch
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','');
87 my @fields = (
88 MARC::Field->new(
89 100, '1', ' ',
90 a => 'Knuth, Donald Ervin',
91 d => '1938',
93 MARC::Field->new(
94 245, '1', '4',
95 a => 'The art of computer programming',
96 c => 'Donald E. Knuth.',
98 MARC::Field->new(
99 650, ' ', '0',
100 a => 'Computer programming.',
101 9 => '462',
103 MARC::Field->new(
104 $item_tag, ' ', ' ',
105 e => 'my edition',
106 i => 'my item part',
108 MARC::Field->new(
109 $item_tag, ' ', ' ',
110 e => 'my edition 2',
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
143 # fresh data
144 my $sample_import_batch3 = {
145 matcher_id => 3,
146 template_id => 3,
147 branchcode => 'QRT',
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',
154 comments => 'test',
155 record_type => 'auth',
158 my $id_import_batch3 = C4::ImportBatch::AddImportBatch($sample_import_batch3);
160 # Test CleanBatch
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" );
165 # Test DeleteBatch
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 {
171 plan tests => 5;
173 # Create a test file
174 my ( $fh, $name ) = tempfile();
175 print $fh q|
176 003 = NLAmRIJ
177 100,a = Author
178 245,ind2 = 0
179 245,a = Silence in the library
180 500 , a= Some note
182 100,a = Another
183 245,a = Noise in the library|;
184 close $fh;
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;