37b6e305834ba56ab658321b41d60fbba9a09dd5
[srvgit] / t / db_dependent / Record / marcrecord2csv.t
1 #!/usr/bin/perl;
2
3 use Modern::Perl;
4 use Test::More tests => 13;
5 use Test::MockModule;
6 use MARC::Record;
7 use MARC::Field;
8 use Text::CSV::Encoded;
9
10 use C4::Biblio qw( AddBiblio );
11 use C4::Context;
12 use C4::Record;
13 use Koha::Database;
14
15 use C4::Items;
16
17 use t::lib::TestBuilder;
18
19 my $schema = Koha::Database->new->schema;
20 $schema->storage->txn_begin;
21 my $dbh = C4::Context->dbh;
22
23 my $builder = t::lib::TestBuilder->new;
24 my $module_biblio = Test::MockModule->new('C4::Biblio');
25
26 my $record = new_record();
27 my $frameworkcode = q||;
28 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, $frameworkcode );
29 $module_biblio->mock( 'GetMarcBiblio', sub{ $record } );
30
31 my $csv_content = q(Title=245$a|Author=245$c|Subject=650$a);
32 my $csv_profile_id_1 = insert_csv_profile({ csv_content => $csv_content });
33 my $csv = Text::CSV::Encoded->new();
34
35 # Test bad biblionumber case
36 my $csv_output = C4::Record::marcrecord2csv( -1, $csv_profile_id_1, 1, $csv );
37 ok(! defined $csv_output, 'Bad biblionumber gives undef as expected.');
38
39 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_1, 1, $csv );
40
41 is( $csv_output, q[Title|Author|Subject
42 "The art of computer programming,The art of another title"|"Donald E. Knuth.,Donald E. Knuth. II"|"Computer programming.,Computer algorithms."
43 ], q|normal way: display headers and content| );
44
45 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_1, 0, $csv );
46 is( $csv_output, q["The art of computer programming,The art of another title"|"Donald E. Knuth.,Donald E. Knuth. II"|"Computer programming.,Computer algorithms."
47 ], q|normal way: don't display headers| );
48
49 $csv_content = q(245|650);
50 my $csv_profile_id_2 = insert_csv_profile({ csv_content => $csv_content });
51
52 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_2, 1, $csv );
53 is( $csv_output, q["TITLE STATEMENT"|"SUBJECT ADDED ENTRY--TOPICAL TERM"
54 "The art of computer programming,Donald E. Knuth.,0;The art of another title,Donald E. Knuth. II,1"|"Computer programming.,462;Computer algorithms.,499"
55 ], q|normal way: headers retrieved from the DB| );
56
57 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_2, 0, $csv );
58 is( $csv_output, q["The art of computer programming,Donald E. Knuth.,0;The art of another title,Donald E. Knuth. II,1"|"Computer programming.,462;Computer algorithms.,499"
59 ], q|normal way: headers are not display if not needed| );
60
61 $csv_content = q(Title and author=[% FOREACH field IN fields.245 %][% field.a.0 %] [% field.c.0 %][% END %]|Subject=650$a);
62 my $csv_profile_id_3 = insert_csv_profile({ csv_content => $csv_content });
63
64 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_3, 1, $csv );
65 is( $csv_output, q["Title and author"|Subject
66 "The art of computer programming Donald E. Knuth.The art of another title Donald E. Knuth. II"|"Computer programming.,Computer algorithms."
67 ], q|TT way: display all 245$a and 245$c| );
68
69 $csv_content = q(Subject=[% FOREACH field IN fields.650 %][% IF field.indicator.2 %][% field.a.0 %][% END %][% END %]);
70 my $csv_profile_id_4 = insert_csv_profile({ csv_content => $csv_content });
71
72 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_4, 1, $csv );
73 is( $csv_output, q[Subject
74 "Computer programming."
75 ], q|TT way: display 650$a if indicator 2 for 650 is set| );
76
77 $csv_content = q|Language=[% fields.008.0.substr( 28, 3 ) %]|;
78 my $csv_profile_id_5 = insert_csv_profile({ csv_content => $csv_content });
79
80 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_5, 1, $csv );
81 is( $csv_output, q[Language
82 eng
83 ], q|TT way: export language from the control field 008| );
84
85 $csv_content = q|Title=[% IF fields.100.0.indicator.1 %][% fields.245.0.a.0 %][% END %]|;
86 my $csv_profile_id_6 = insert_csv_profile({ csv_content => $csv_content });
87
88 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_6, 1, $csv );
89 is( $csv_output, q[Title
90 "The art of computer programming"
91 ], q|TT way: display first subfield a for first field 245 if indicator 1 for field 100 is set| );
92
93 $csv_content = q|Title=[% IF fields.100.0.indicator.1 == 1 %][% fields.245.0.a.0 %][% END %]|;
94 my $csv_profile_id_7 = insert_csv_profile({ csv_content => $csv_content });
95
96 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_7, 1, $csv );
97 is( $csv_output, q[Title
98 "The art of computer programming"
99 ], q|TT way: display first subfield a for first field 245 if indicator 1 == 1 for field 100 is set| );
100
101
102 my $authorised_value_1 =
103   $builder->build( { source => 'AuthorisedValue', value => { category => 'MY_AV_1', authorised_value => 1, lib => 'This is an AV', lib_opac => 'This is an AV (opac)' } } );
104 my $authorised_value_2 = $builder->build(
105     { source => 'AuthorisedValue', value => { category => 'MY_AV_2', authorised_value => 2, lib => 'This is another AV', lib_opac => 'This is another AV (opac)' } } );
106 $dbh->do(q|DELETE FROM marc_subfield_structure WHERE tagfield='998' and ( tagsubfield='8' or tagsubfield='9')|);
107 $builder->build(
108     { source => 'MarcSubfieldStructure', value => { authorised_value => $authorised_value_1->{category}, tagfield => 998, tagsubfield => '8', frameworkcode => $frameworkcode } }
109 );
110 $builder->build(
111     { source => 'MarcSubfieldStructure', value => { authorised_value => $authorised_value_2->{category}, tagfield => 998, tagsubfield => '9', frameworkcode => $frameworkcode } }
112 );
113 $csv_content = q(Title=245$a|AV1=998$8|AV2=998$9);
114 my $csv_profile_id_8 = insert_csv_profile( { csv_content => $csv_content } );
115 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_8, 1, $csv );
116 is( $csv_output, q[Title|AV1|AV2
117 "The art of computer programming,The art of another title"|"This is an AV"|"This is another AV"
118 ], q|TT way: display first subfield a for first field 245 if indicator 1 == 1 for field 100 is set|
119 );
120
121 $csv_content = q(Title=245$a|AVs=998);
122 my $csv_profile_id_9 = insert_csv_profile( { csv_content => $csv_content } );
123 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_9, 1, $csv );
124 is( $csv_output, q[Title|AVs
125 "The art of computer programming,The art of another title"|"This is an AV,This is another AV"
126 ], q|TT way: display first subfield a for first field 245 if indicator 1 == 1 for field 100 is set|
127 );
128
129 subtest 'Test for subfields 0' => sub {
130     plan tests => 1;
131
132     my $record2        = new_record();
133     my $frameworkcode2 = q||;
134
135     # We change the barcode of the second item record to prevent an error "duplicate entry"
136     my $field     = $record->field('952');
137     my $new_field = MARC::Field->new(
138         '952', ' ', ' ',
139         0 => '1',
140         p => '3010023918',
141     );
142     $field->replace_with($new_field);
143
144     # We create another record
145     my ( $biblionumber2, $biblioitemnumber2 ) = AddBiblio( $record2, $frameworkcode2 );
146
147     # We add two item to two record to test fieldtag 952 and subfieldtag 9520
148     my ( undef, undef, $itemnumber2 ) = AddItemFromMarc( $record2, $biblionumber );
149     my ( undef, undef, $itemnumber ) = AddItemFromMarc( $record, $biblionumber );
150     $csv_content = q(Titre=245a|Nom de personne=100a|Statut « Élagué »=9520);
151     my $csv_profile_id_10 = insert_csv_profile( { csv_content => $csv_content } );
152     $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_10, 1, $csv );
153     is_deeply(
154         [ split "\n", $csv_output ],
155         [
156             q[Titre|"Nom de personne"|"Statut « Élagué »"],
157             q["The art of computer programming,The art of another title"|"Knuth, Donald Ervin"|Withdrawn;Withdrawn],
158         ],
159         q[subfieldtag 952\$0 is not working, should return 'Withdrawn']
160     );
161 };
162
163 sub insert_csv_profile {
164     my ( $params ) = @_;
165     my $csv_content = $params->{csv_content};
166     $dbh->do(q|
167         INSERT INTO export_format(profile, description, content, csv_separator, field_separator, subfield_separator, encoding, type) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
168         |, {}, ("TEST_PROFILE4", "my desc", $csv_content, '|', ';', ',', 'utf8', 'marc')
169     );
170     return $dbh->last_insert_id( undef, undef, 'export_format', undef );
171 }
172
173 sub new_record {
174     my $record = MARC::Record->new;
175     $record->leader('03174nam a2200445 a 4500');
176     my @fields = (
177         MARC::Field->new(
178             '008', "140211b xxu||||| |||| 00| 0 eng d"
179         ),
180         MARC::Field->new(
181             100, '1', ' ',
182             a => 'Knuth, Donald Ervin',
183             d => '1938',
184         ),
185         MARC::Field->new(
186             245, '1', '4',
187             a => 'The art of computer programming',
188             c => 'Donald E. Knuth.',
189             9 => '0',
190         ),
191         MARC::Field->new(
192             245, '1', '4',
193             a => 'The art of another title',
194             c => 'Donald E. Knuth. II',
195             9 => '1',
196         ),
197         MARC::Field->new(
198             650, ' ', '1',
199             a => 'Computer programming.',
200             9 => '462',
201         ),
202         MARC::Field->new(
203             650, ' ', '0',
204             a => 'Computer algorithms.',
205             9 => '499',
206         ),
207         MARC::Field->new(
208             952, ' ', ' ',
209             0 => '1',
210             p => '3010023917',
211             y => 'BK',
212             c => 'GEN',
213             d => '2001-06-25',
214         ),
215         MARC::Field->new(
216             998, ' ', ' ',
217             8 => 1,
218             9 => 2,
219         ),
220     );
221     $record->append_fields(@fields);
222     return $record;
223 }