da0c1899b21c359de71f3644df3eef55b7523dbe
[srvgit] / t / db_dependent / ImportBatch.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use Test::More tests => 16;
5 use utf8;
6 use File::Basename;
7 use File::Temp qw/tempfile/;
8
9 use t::lib::Mocks;
10 use t::lib::TestBuilder;
11
12 use Koha::Database;
13 use Koha::Plugins;
14
15 BEGIN {
16     # Mock pluginsdir before loading Plugins module
17     my $path = dirname(__FILE__) . '/../lib';
18     t::lib::Mocks::mock_config( 'pluginsdir', $path );
19     use_ok('C4::ImportBatch');
20 }
21
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;
27
28 # clear
29 $dbh->do('DELETE FROM import_batches');
30
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',
43 };
44
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',
57 };
58
59 my $id_import_batch1 = C4::ImportBatch::AddImportBatch($sample_import_batch1);
60 my $id_import_batch2 = C4::ImportBatch::AddImportBatch($sample_import_batch2);
61
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" );
64
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};
71 delete $importbatch2->{profile_id};
72 delete $importbatch2->{profile};
73
74 is_deeply( $importbatch2, $sample_import_batch2,
75     "GetImportBatch returns the right informations about $sample_import_batch2" );
76
77 my $importbatch1 = C4::ImportBatch::GetImportBatch( $id_import_batch1 );
78 delete $importbatch1->{upload_timestamp};
79 delete $importbatch1->{import_batch_id};
80 delete $importbatch1->{num_records};
81 delete $importbatch1->{num_items};
82 delete $importbatch1->{profile_id};
83 delete $importbatch1->{profile};
84
85 is_deeply( $importbatch1, $sample_import_batch1,
86     "GetImportBatch returns the right informations about $sample_import_batch1" );
87
88 my $record = MARC::Record->new;
89 # FIXME Create another MARC::Record which won't be modified
90 # AddItemsToImportBiblio will remove the items field from the record passed in parameter.
91 my $original_record = MARC::Record->new;
92 $record->leader('03174nam a2200445 a 4500');
93 $original_record->leader('03174nam a2200445 a 4500');
94 my ($item_tag, $item_subfield) = C4::Biblio::GetMarcFromKohaField( 'items.itemnumber' );
95 my @fields = (
96     MARC::Field->new(
97         100, '1', ' ',
98         a => 'Knuth, Donald Ervin',
99         d => '1938',
100     ),
101     MARC::Field->new(
102         245, '1', '4',
103         a => 'The art of computer programming',
104         c => 'Donald E. Knuth.',
105     ),
106     MARC::Field->new(
107         650, ' ', '0',
108         a => 'Computer programming.',
109         9 => '462',
110     ),
111     MARC::Field->new(
112         $item_tag, ' ', ' ',
113         e => 'my edition ❤',
114         i => 'my item part',
115     ),
116     MARC::Field->new(
117         $item_tag, ' ', ' ',
118         e => 'my edition 2',
119         i => 'my item part 2',
120     ),
121 );
122 $record->append_fields(@fields);
123 $original_record->append_fields(@fields);
124 my $import_record_id = AddBiblioToBatch( $id_import_batch1, 0, $record, 'utf8', int(rand(99999)), 0 );
125 AddItemsToImportBiblio( $id_import_batch1, $import_record_id, $record, 0 );
126
127 my $record_from_import_biblio_with_items = C4::ImportBatch::GetRecordFromImportBiblio( $import_record_id, 'embed_items' );
128 $original_record->leader($record_from_import_biblio_with_items->leader());
129 is_deeply( $record_from_import_biblio_with_items, $original_record, 'GetRecordFromImportBiblio should return the record with items if specified' );
130 my $utf8_field = $record_from_import_biblio_with_items->subfield($item_tag, 'e');
131 is($utf8_field, 'my edition ❤');
132 $original_record->delete_fields($original_record->field($item_tag)); #Remove items fields
133 my $record_from_import_biblio_without_items = C4::ImportBatch::GetRecordFromImportBiblio( $import_record_id );
134 $original_record->leader($record_from_import_biblio_without_items->leader());
135 is_deeply( $record_from_import_biblio_without_items, $original_record, 'GetRecordFromImportBiblio should return the record without items by default' );
136
137 my $another_biblio = $builder->build_sample_biblio;
138 C4::ImportBatch::SetMatchedBiblionumber( $import_record_id, $another_biblio->biblionumber );
139 my $import_biblios = GetImportBiblios( $import_record_id );
140 is( $import_biblios->[0]->{matched_biblionumber}, $another_biblio->biblionumber, 'SetMatchedBiblionumber  should set the correct biblionumber' );
141
142 # Add a few tests for GetItemNumbersFromImportBatch
143 my @a = GetItemNumbersFromImportBatch( $id_import_batch1 );
144 is( @a, 0, 'No item numbers expected since we did not commit' );
145 my $itemno = $builder->build_sample_item->itemnumber;
146 # Link this item to the import item to fool GetItemNumbersFromImportBatch
147 my $sql = "UPDATE import_items SET itemnumber=? WHERE import_record_id=?";
148 $dbh->do( $sql, undef, $itemno, $import_record_id );
149 @a = GetItemNumbersFromImportBatch( $id_import_batch1 );
150 is( @a, 2, 'Expecting two items now' );
151 is( $a[0], $itemno, 'Check the first returned itemnumber' );
152 # Now delete the item and check again
153 $dbh->do( "DELETE FROM items WHERE itemnumber=?", undef, $itemno );
154 @a = GetItemNumbersFromImportBatch( $id_import_batch1 );
155 is( @a, 0, 'No item numbers expected since we deleted the item' );
156 $dbh->do( $sql, undef, undef, $import_record_id ); # remove link again
157
158 # fresh data
159 my $sample_import_batch3 = {
160     matcher_id => 3,
161     template_id => 3,
162     branchcode => 'QRT',
163     overlay_action => 'create_new',
164     nomatch_action => 'create_new',
165     item_action => 'always_add',
166     import_status => 'staged',
167     batch_type => 'z3950',
168     file_name => 'test.mrc',
169     comments => 'test',
170     record_type => 'auth',
171 };
172
173 my $id_import_batch3 = C4::ImportBatch::AddImportBatch($sample_import_batch3);
174
175 # Test CleanBatch
176 C4::ImportBatch::CleanBatch( $id_import_batch3 );
177 my $batch3_clean = $dbh->do('SELECT * FROM import_records WHERE import_batch_id = "$id_import_batch3"');
178 is( $batch3_clean, "0E0", "Batch 3 has been cleaned" );
179
180 # Test DeleteBatch
181 C4::ImportBatch::DeleteBatch( $id_import_batch3 );
182 my $batch3_results = $dbh->do('SELECT * FROM import_batches WHERE import_batch_id = "$id_import_batch3"');
183 is( $batch3_results, "0E0", "Batch 3 has been deleted");
184
185 subtest "RecordsFromMarcPlugin" => sub {
186     plan tests => 5;
187
188     # Create a test file
189     my ( $fh, $name ) = tempfile();
190     print $fh q|
191 003 = NLAmRIJ
192 100,a = Author
193 245,ind2 = 0
194 245,a = Silence in the library
195 500 , a= Some note
196
197 100,a = Another
198 245,a = Noise in the library|;
199     close $fh;
200
201     t::lib::Mocks::mock_config( 'enable_plugins', 1 );
202
203     my $plugins = Koha::Plugins->new;
204     $plugins->InstallPlugins;
205     my ($plugin) = $plugins->GetPlugins({ all => 1, metadata => { name => 'MarcFieldValues' } });
206     isnt( $plugin, undef, "Plugin found" );
207     my $records = C4::ImportBatch::RecordsFromMarcPlugin( $name, ref $plugin, 'UTF-8' );
208     is( @$records, 2, 'Two results returned' );
209     is( ref $records->[0], 'MARC::Record', 'Returned MARC::Record object' );
210     is( $records->[0]->subfield('245', 'a'), 'Silence in the library',
211         'Checked one field in first record' );
212     is( $records->[1]->subfield('100', 'a'), 'Another',
213         'Checked one field in second record' );
214 };
215
216 $schema->storage->txn_rollback;