Bug 18611: [QA Follow-up] Add unit test for GetItemNumbersFromImportBatch
[koha.git] / t / db_dependent / ImportBatch.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use Test::More tests => 13;
5
6 use Koha::Database;
7 use t::lib::TestBuilder;
8
9 BEGIN {
10         use_ok('C4::ImportBatch');
11 }
12
13 # Start transaction
14 my $schema  = Koha::Database->new->schema;
15 $schema->storage->txn_begin;
16 my $builder = t::lib::TestBuilder->new;
17 my $dbh = C4::Context->dbh;
18
19 # clear
20 $dbh->do('DELETE FROM import_batches');
21
22 my $sample_import_batch1 = {
23     matcher_id => 1,
24     template_id => 1,
25     branchcode => 'QRT',
26     overlay_action => 'create_new',
27     nomatch_action => 'create_new',
28     item_action => 'always_add',
29     import_status => 'staged',
30     batch_type => 'z3950',
31     file_name => 'test.mrc',
32     comments => 'test',
33     record_type => 'auth',
34 };
35
36 my $sample_import_batch2 = {
37     matcher_id => 2,
38     template_id => 2,
39     branchcode => 'QRZ',
40     overlay_action => 'create_new',
41     nomatch_action => 'create_new',
42     item_action => 'always_add',
43     import_status => 'staged',
44     batch_type => 'z3950',
45     file_name => 'test.mrc',
46     comments => 'test',
47     record_type => 'auth',
48 };
49
50 my $id_import_batch1 = C4::ImportBatch::AddImportBatch($sample_import_batch1);
51 my $id_import_batch2 = C4::ImportBatch::AddImportBatch($sample_import_batch2);
52
53 like( $id_import_batch1, '/^\d+$/', "AddImportBatch for sample_import_batch1 return an id" );
54 like( $id_import_batch2, '/^\d+$/', "AddImportBatch for sample_import_batch2 return an id" );
55
56 #Test GetImportBatch
57 my $importbatch2 = C4::ImportBatch::GetImportBatch( $id_import_batch2 );
58 delete $importbatch2->{upload_timestamp};
59 delete $importbatch2->{import_batch_id};
60 delete $importbatch2->{num_records};
61 delete $importbatch2->{num_items};
62
63 is_deeply( $importbatch2, $sample_import_batch2,
64     "GetImportBatch returns the right informations about $sample_import_batch2" );
65
66 my $importbatch1 = C4::ImportBatch::GetImportBatch( $id_import_batch1 );
67 delete $importbatch1->{upload_timestamp};
68 delete $importbatch1->{import_batch_id};
69 delete $importbatch1->{num_records};
70 delete $importbatch1->{num_items};
71
72 is_deeply( $importbatch1, $sample_import_batch1,
73     "GetImportBatch returns the right informations about $sample_import_batch1" );
74
75 my $record = MARC::Record->new;
76 # FIXME Create another MARC::Record which won't be modified
77 # AddItemsToImportBiblio will remove the items field from the record passed in parameter.
78 my $original_record = MARC::Record->new;
79 $record->leader('03174nam a2200445 a 4500');
80 $original_record->leader('03174nam a2200445 a 4500');
81 my ($item_tag, $item_subfield) = C4::Biblio::GetMarcFromKohaField('items.itemnumber','');
82 my @fields = (
83     MARC::Field->new(
84         100, '1', ' ',
85         a => 'Knuth, Donald Ervin',
86         d => '1938',
87     ),
88     MARC::Field->new(
89         245, '1', '4',
90         a => 'The art of computer programming',
91         c => 'Donald E. Knuth.',
92     ),
93     MARC::Field->new(
94         650, ' ', '0',
95         a => 'Computer programming.',
96         9 => '462',
97     ),
98     MARC::Field->new(
99         $item_tag, ' ', ' ',
100         e => 'my edition',
101         i => 'my item part',
102     ),
103     MARC::Field->new(
104         $item_tag, ' ', ' ',
105         e => 'my edition 2',
106         i => 'my item part 2',
107     ),
108 );
109 $record->append_fields(@fields);
110 $original_record->append_fields(@fields);
111 my $import_record_id = AddBiblioToBatch( $id_import_batch1, 0, $record, 'utf8', int(rand(99999)), 0 );
112 AddItemsToImportBiblio( $id_import_batch1, $import_record_id, $record, 0 );
113
114 my $record_from_import_biblio_with_items = C4::ImportBatch::GetRecordFromImportBiblio( $import_record_id, 'embed_items' );
115 $original_record->leader($record_from_import_biblio_with_items->leader());
116 is_deeply( $record_from_import_biblio_with_items, $original_record, 'GetRecordFromImportBiblio should return the record with items if specified' );
117 $original_record->delete_fields($original_record->field($item_tag)); #Remove items fields
118 my $record_from_import_biblio_without_items = C4::ImportBatch::GetRecordFromImportBiblio( $import_record_id );
119 $original_record->leader($record_from_import_biblio_without_items->leader());
120 is_deeply( $record_from_import_biblio_without_items, $original_record, 'GetRecordFromImportBiblio should return the record without items by default' );
121
122 # Add a few tests for GetItemNumbersFromImportBatch
123 my @a = GetItemNumbersFromImportBatch( $id_import_batch1 );
124 is( @a, 0, 'No item numbers expected since we did not commit' );
125 my $itemno = $builder->build({ source => 'Item' })->{itemnumber};
126 # Link this item to the import item to fool GetItemNumbersFromImportBatch
127 my $sql = "UPDATE import_items SET itemnumber=? WHERE import_record_id=?";
128 $dbh->do( $sql, undef, $itemno, $import_record_id );
129 @a = GetItemNumbersFromImportBatch( $id_import_batch1 );
130 is( @a, 2, 'Expecting two items now' );
131 is( $a[0], $itemno, 'Check the first returned itemnumber' );
132 # Now delete the item and check again
133 $dbh->do( "DELETE FROM items WHERE itemnumber=?", undef, $itemno );
134 @a = GetItemNumbersFromImportBatch( $id_import_batch1 );
135 is( @a, 0, 'No item numbers expected since we deleted the item' );
136 $dbh->do( $sql, undef, undef, $import_record_id ); # remove link again
137
138 # fresh data
139 my $sample_import_batch3 = {
140     matcher_id => 3,
141     template_id => 3,
142     branchcode => 'QRT',
143     overlay_action => 'create_new',
144     nomatch_action => 'create_new',
145     item_action => 'always_add',
146     import_status => 'staged',
147     batch_type => 'z3950',
148     file_name => 'test.mrc',
149     comments => 'test',
150     record_type => 'auth',
151 };
152
153 my $id_import_batch3 = C4::ImportBatch::AddImportBatch($sample_import_batch3);
154
155 # Test CleanBatch
156 C4::ImportBatch::CleanBatch( $id_import_batch3 );
157 my $batch3_clean = $dbh->do('SELECT * FROM import_records WHERE import_batch_id = "$id_import_batch3"');
158 is( $batch3_clean, "0E0", "Batch 3 has been cleaned" );
159
160 # Test DeleteBatch
161 C4::ImportBatch::DeleteBatch( $id_import_batch3 );
162 my $batch3_results = $dbh->do('SELECT * FROM import_batches WHERE import_batch_id = "$id_import_batch3"');
163 is( $batch3_results, "0E0", "Batch 3 has been deleted");
164
165 $schema->storage->txn_rollback;