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