Bug 20568: (QA follow-up) Get rid of the id column
[koha.git] / t / db_dependent / ImportBatch.t
blob788be3fc6d2776bd2de8b24f6f16a1a7148eaaa2
1 #!/usr/bin/perl
3 use Modern::Perl;
4 use Test::More tests => 14;
5 use Cwd qw(abs_path);
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 = abs_path(__FILE__);
17 $path =~ s/(\/[^\/]+){2}$//; # remove db_dependent and filename
18 t::lib::Mocks::mock_config( 'pluginsdir', $path );
19 use_ok('C4::ImportBatch');
22 # Start transaction
23 my $schema = Koha::Database->new->schema;
24 $schema->storage->txn_begin;
25 my $builder = t::lib::TestBuilder->new;
26 my $dbh = C4::Context->dbh;
28 # clear
29 $dbh->do('DELETE FROM import_batches');
31 my $sample_import_batch1 = {
32 matcher_id => 1,
33 template_id => 1,
34 branchcode => 'QRT',
35 overlay_action => 'create_new',
36 nomatch_action => 'create_new',
37 item_action => 'always_add',
38 import_status => 'staged',
39 batch_type => 'z3950',
40 file_name => 'test.mrc',
41 comments => 'test',
42 record_type => 'auth',
45 my $sample_import_batch2 = {
46 matcher_id => 2,
47 template_id => 2,
48 branchcode => 'QRZ',
49 overlay_action => 'create_new',
50 nomatch_action => 'create_new',
51 item_action => 'always_add',
52 import_status => 'staged',
53 batch_type => 'z3950',
54 file_name => 'test.mrc',
55 comments => 'test',
56 record_type => 'auth',
59 my $id_import_batch1 = C4::ImportBatch::AddImportBatch($sample_import_batch1);
60 my $id_import_batch2 = C4::ImportBatch::AddImportBatch($sample_import_batch2);
62 like( $id_import_batch1, '/^\d+$/', "AddImportBatch for sample_import_batch1 return an id" );
63 like( $id_import_batch2, '/^\d+$/', "AddImportBatch for sample_import_batch2 return an id" );
65 #Test GetImportBatch
66 my $importbatch2 = C4::ImportBatch::GetImportBatch( $id_import_batch2 );
67 delete $importbatch2->{upload_timestamp};
68 delete $importbatch2->{import_batch_id};
69 delete $importbatch2->{num_records};
70 delete $importbatch2->{num_items};
72 is_deeply( $importbatch2, $sample_import_batch2,
73 "GetImportBatch returns the right informations about $sample_import_batch2" );
75 my $importbatch1 = C4::ImportBatch::GetImportBatch( $id_import_batch1 );
76 delete $importbatch1->{upload_timestamp};
77 delete $importbatch1->{import_batch_id};
78 delete $importbatch1->{num_records};
79 delete $importbatch1->{num_items};
81 is_deeply( $importbatch1, $sample_import_batch1,
82 "GetImportBatch returns the right informations about $sample_import_batch1" );
84 my $record = MARC::Record->new;
85 # FIXME Create another MARC::Record which won't be modified
86 # AddItemsToImportBiblio will remove the items field from the record passed in parameter.
87 my $original_record = MARC::Record->new;
88 $record->leader('03174nam a2200445 a 4500');
89 $original_record->leader('03174nam a2200445 a 4500');
90 my ($item_tag, $item_subfield) = C4::Biblio::GetMarcFromKohaField('items.itemnumber','');
91 my @fields = (
92 MARC::Field->new(
93 100, '1', ' ',
94 a => 'Knuth, Donald Ervin',
95 d => '1938',
97 MARC::Field->new(
98 245, '1', '4',
99 a => 'The art of computer programming',
100 c => 'Donald E. Knuth.',
102 MARC::Field->new(
103 650, ' ', '0',
104 a => 'Computer programming.',
105 9 => '462',
107 MARC::Field->new(
108 $item_tag, ' ', ' ',
109 e => 'my edition',
110 i => 'my item part',
112 MARC::Field->new(
113 $item_tag, ' ', ' ',
114 e => 'my edition 2',
115 i => 'my item part 2',
118 $record->append_fields(@fields);
119 $original_record->append_fields(@fields);
120 my $import_record_id = AddBiblioToBatch( $id_import_batch1, 0, $record, 'utf8', int(rand(99999)), 0 );
121 AddItemsToImportBiblio( $id_import_batch1, $import_record_id, $record, 0 );
123 my $record_from_import_biblio_with_items = C4::ImportBatch::GetRecordFromImportBiblio( $import_record_id, 'embed_items' );
124 $original_record->leader($record_from_import_biblio_with_items->leader());
125 is_deeply( $record_from_import_biblio_with_items, $original_record, 'GetRecordFromImportBiblio should return the record with items if specified' );
126 $original_record->delete_fields($original_record->field($item_tag)); #Remove items fields
127 my $record_from_import_biblio_without_items = C4::ImportBatch::GetRecordFromImportBiblio( $import_record_id );
128 $original_record->leader($record_from_import_biblio_without_items->leader());
129 is_deeply( $record_from_import_biblio_without_items, $original_record, 'GetRecordFromImportBiblio should return the record without items by default' );
131 # Add a few tests for GetItemNumbersFromImportBatch
132 my @a = GetItemNumbersFromImportBatch( $id_import_batch1 );
133 is( @a, 0, 'No item numbers expected since we did not commit' );
134 my $itemno = $builder->build({ source => 'Item' })->{itemnumber};
135 # Link this item to the import item to fool GetItemNumbersFromImportBatch
136 my $sql = "UPDATE import_items SET itemnumber=? WHERE import_record_id=?";
137 $dbh->do( $sql, undef, $itemno, $import_record_id );
138 @a = GetItemNumbersFromImportBatch( $id_import_batch1 );
139 is( @a, 2, 'Expecting two items now' );
140 is( $a[0], $itemno, 'Check the first returned itemnumber' );
141 # Now delete the item and check again
142 $dbh->do( "DELETE FROM items WHERE itemnumber=?", undef, $itemno );
143 @a = GetItemNumbersFromImportBatch( $id_import_batch1 );
144 is( @a, 0, 'No item numbers expected since we deleted the item' );
145 $dbh->do( $sql, undef, undef, $import_record_id ); # remove link again
147 # fresh data
148 my $sample_import_batch3 = {
149 matcher_id => 3,
150 template_id => 3,
151 branchcode => 'QRT',
152 overlay_action => 'create_new',
153 nomatch_action => 'create_new',
154 item_action => 'always_add',
155 import_status => 'staged',
156 batch_type => 'z3950',
157 file_name => 'test.mrc',
158 comments => 'test',
159 record_type => 'auth',
162 my $id_import_batch3 = C4::ImportBatch::AddImportBatch($sample_import_batch3);
164 # Test CleanBatch
165 C4::ImportBatch::CleanBatch( $id_import_batch3 );
166 my $batch3_clean = $dbh->do('SELECT * FROM import_records WHERE import_batch_id = "$id_import_batch3"');
167 is( $batch3_clean, "0E0", "Batch 3 has been cleaned" );
169 # Test DeleteBatch
170 C4::ImportBatch::DeleteBatch( $id_import_batch3 );
171 my $batch3_results = $dbh->do('SELECT * FROM import_batches WHERE import_batch_id = "$id_import_batch3"');
172 is( $batch3_results, "0E0", "Batch 3 has been deleted");
174 subtest "RecordsFromMarcPlugin" => sub {
175 plan tests => 5;
177 # Create a test file
178 my ( $fh, $name ) = tempfile();
179 print $fh q|
180 003 = NLAmRIJ
181 100,a = Author
182 245,ind2 = 0
183 245,a = Silence in the library
184 500 , a= Some note
186 100,a = Another
187 245,a = Noise in the library|;
188 close $fh;
190 t::lib::Mocks::mock_config( 'enable_plugins', 1 );
191 my ( $plugin ) = Koha::Plugins->new->GetPlugins({ 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;