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