4 use Test
::More tests
=> 13;
7 use t
::lib
::TestBuilder
;
10 use_ok
('C4::ImportBatch');
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;
20 $dbh->do('DELETE FROM import_batches');
22 my $sample_import_batch1 = {
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',
33 record_type
=> 'auth',
36 my $sample_import_batch2 = {
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',
47 record_type
=> 'auth',
50 my $id_import_batch1 = C4
::ImportBatch
::AddImportBatch
($sample_import_batch1);
51 my $id_import_batch2 = C4
::ImportBatch
::AddImportBatch
($sample_import_batch2);
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" );
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
};
63 is_deeply
( $importbatch2, $sample_import_batch2,
64 "GetImportBatch returns the right informations about $sample_import_batch2" );
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
};
72 is_deeply
( $importbatch1, $sample_import_batch1,
73 "GetImportBatch returns the right informations about $sample_import_batch1" );
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','');
85 a
=> 'Knuth, Donald Ervin',
90 a
=> 'The art of computer programming',
91 c
=> 'Donald E. Knuth.',
95 a
=> 'Computer programming.',
106 i
=> 'my item part 2',
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 );
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' );
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
139 my $sample_import_batch3 = {
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',
150 record_type
=> 'auth',
153 my $id_import_batch3 = C4
::ImportBatch
::AddImportBatch
($sample_import_batch3);
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" );
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");
165 $schema->storage->txn_rollback;