Bug 24029: Remove "Truncated incorrect DOUBLE value: '01e'" from marcrecord2csv.t
[koha.git] / t / db_dependent / Record / marcrecord2csv.t
1 #!/usr/bin/perl;
2
3 use Modern::Perl;
4 use Test::More tests => 12;
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 t::lib::TestBuilder;
16
17 my $schema = Koha::Database->new->schema;
18 $schema->storage->txn_begin;
19 my $dbh = C4::Context->dbh;
20
21 my $builder = t::lib::TestBuilder->new;
22 my $module_biblio = Test::MockModule->new('C4::Biblio');
23
24 my $record = new_record();
25 my $frameworkcode = q||;
26 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, $frameworkcode );
27 $module_biblio->mock( 'GetMarcBiblio', sub{ $record } );
28
29 my $csv_content = q(Title=245$a|Author=245$c|Subject=650$a);
30 my $csv_profile_id_1 = insert_csv_profile({ csv_content => $csv_content });
31 my $csv = Text::CSV::Encoded->new();
32
33 # Test bad biblionumber case
34 my $csv_output = C4::Record::marcrecord2csv( -1, $csv_profile_id_1, 1, $csv );
35 ok(! defined $csv_output, 'Bad biblionumber gives undef as expected.');
36
37 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_1, 1, $csv );
38
39 is( $csv_output, q[Title|Author|Subject
40 "The art of computer programming,The art of another title"|"Donald E. Knuth.,Donald E. Knuth. II"|"Computer programming.,Computer algorithms."
41 ], q|normal way: display headers and content| );
42
43 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_1, 0, $csv );
44 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."
45 ], q|normal way: don't display headers| );
46
47 $csv_content = q(245|650);
48 my $csv_profile_id_2 = insert_csv_profile({ csv_content => $csv_content });
49
50 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_2, 1, $csv );
51 is( $csv_output, q["TITLE STATEMENT"|"SUBJECT ADDED ENTRY--TOPICAL TERM"
52 "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"
53 ], q|normal way: headers retrieved from the DB| );
54
55 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_2, 0, $csv );
56 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"
57 ], q|normal way: headers are not display if not needed| );
58
59 $csv_content = q(Title and author=[% FOREACH field IN fields.245 %][% field.a.0 %] [% field.c.0 %][% END %]|Subject=650$a);
60 my $csv_profile_id_3 = insert_csv_profile({ csv_content => $csv_content });
61
62 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_3, 1, $csv );
63 is( $csv_output, q["Title and author"|Subject
64 "The art of computer programming Donald E. Knuth.The art of another title Donald E. Knuth. II"|"Computer programming.,Computer algorithms."
65 ], q|TT way: display all 245$a and 245$c| );
66
67 $csv_content = q(Subject=[% FOREACH field IN fields.650 %][% IF field.indicator.2 %][% field.a.0 %][% END %][% END %]);
68 my $csv_profile_id_4 = insert_csv_profile({ csv_content => $csv_content });
69
70 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_4, 1, $csv );
71 is( $csv_output, q[Subject
72 "Computer programming."
73 ], q|TT way: display 650$a if indicator 2 for 650 is set| );
74
75 $csv_content = q|Language=[% fields.008.0.substr( 28, 3 ) %]|;
76 my $csv_profile_id_5 = insert_csv_profile({ csv_content => $csv_content });
77
78 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_5, 1, $csv );
79 is( $csv_output, q[Language
80 eng
81 ], q|TT way: export language from the control field 008| );
82
83 $csv_content = q|Title=[% IF fields.100.0.indicator.1 %][% fields.245.0.a.0 %][% END %]|;
84 my $csv_profile_id_6 = insert_csv_profile({ csv_content => $csv_content });
85
86 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_6, 1, $csv );
87 is( $csv_output, q[Title
88 "The art of computer programming"
89 ], q|TT way: display first subfield a for first field 245 if indicator 1 for field 100 is set| );
90
91 $csv_content = q|Title=[% IF fields.100.0.indicator.1 == 1 %][% fields.245.0.a.0 %][% END %]|;
92 my $csv_profile_id_7 = insert_csv_profile({ csv_content => $csv_content });
93
94 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_7, 1, $csv );
95 is( $csv_output, q[Title
96 "The art of computer programming"
97 ], q|TT way: display first subfield a for first field 245 if indicator 1 == 1 for field 100 is set| );
98
99
100 my $authorised_value_1 =
101   $builder->build( { source => 'AuthorisedValue', value => { category => 'MY_AV_1', authorised_value => 1, lib => 'This is an AV', lib_opac => 'This is an AV (opac)' } } );
102 my $authorised_value_2 = $builder->build(
103     { source => 'AuthorisedValue', value => { category => 'MY_AV_2', authorised_value => 2, lib => 'This is another AV', lib_opac => 'This is another AV (opac)' } } );
104 $dbh->do(q|DELETE FROM marc_subfield_structure WHERE tagfield='998' and ( tagsubfield='8' or tagsubfield='9')|);
105 $builder->build(
106     { source => 'MarcSubfieldStructure', value => { authorised_value => $authorised_value_1->{category}, tagfield => 998, tagsubfield => '8', frameworkcode => $frameworkcode } }
107 );
108 $builder->build(
109     { source => 'MarcSubfieldStructure', value => { authorised_value => $authorised_value_2->{category}, tagfield => 998, tagsubfield => '9', frameworkcode => $frameworkcode } }
110 );
111 $csv_content = q(Title=245$a|AV1=998$8|AV2=998$9);
112 my $csv_profile_id_8 = insert_csv_profile( { csv_content => $csv_content } );
113 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_8, 1, $csv );
114 is( $csv_output, q[Title|AV1|AV2
115 "The art of computer programming,The art of another title"|"This is an AV"|"This is another AV"
116 ], q|TT way: display first subfield a for first field 245 if indicator 1 == 1 for field 100 is set|
117 );
118
119 $csv_content = q(Title=245$a|AVs=998);
120 my $csv_profile_id_9 = insert_csv_profile( { csv_content => $csv_content } );
121 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_9, 1, $csv );
122 is( $csv_output, q[Title|AVs
123 "The art of computer programming,The art of another title"|"This is an AV,This is another AV"
124 ], q|TT way: display first subfield a for first field 245 if indicator 1 == 1 for field 100 is set|
125 );
126
127
128
129 sub insert_csv_profile {
130     my ( $params ) = @_;
131     my $csv_content = $params->{csv_content};
132     $dbh->do(q|
133         INSERT INTO export_format(profile, description, content, csv_separator, field_separator, subfield_separator, encoding, type) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
134         |, {}, ("TEST_PROFILE4", "my desc", $csv_content, '|', ';', ',', 'utf8', 'marc')
135     );
136     return $dbh->last_insert_id( undef, undef, 'export_format', undef );
137 }
138
139 sub new_record {
140     my $record = MARC::Record->new;
141     $record->leader('03174nam a2200445 a 4500');
142     my @fields = (
143         MARC::Field->new(
144             '008', "140211b xxu||||| |||| 00| 0 eng d"
145         ),
146         MARC::Field->new(
147             100, '1', ' ',
148             a => 'Knuth, Donald Ervin',
149             d => '1938',
150         ),
151         MARC::Field->new(
152             245, '1', '4',
153             a => 'The art of computer programming',
154             c => 'Donald E. Knuth.',
155             9 => '0',
156         ),
157         MARC::Field->new(
158             245, '1', '4',
159             a => 'The art of another title',
160             c => 'Donald E. Knuth. II',
161             9 => '1',
162         ),
163         MARC::Field->new(
164             650, ' ', '1',
165             a => 'Computer programming.',
166             9 => '462',
167         ),
168         MARC::Field->new(
169             650, ' ', '0',
170             a => 'Computer algorithms.',
171             9 => '499',
172         ),
173         MARC::Field->new(
174             952, ' ', ' ',
175             p => '3010023917',
176             y => 'BK',
177             c => 'GEN',
178             d => '2001-06-25',
179         ),
180         MARC::Field->new(
181             998, ' ', ' ',
182             8 => 1,
183             9 => 2,
184         ),
185     );
186     $record->append_fields(@fields);
187     return $record;
188 }