Bug 12404: Allow equal sign '=' in the TT directive
[koha.git] / t / db_dependent / Record / marcrecord2csv.t
1 #!/usr/bin/perl;
2
3 use Modern::Perl;
4 use Test::More tests => 9;
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 my $dbh = C4::Context->dbh;
15 $dbh->{AutoCommit} = 0;
16 $dbh->{RaiseError} = 1;
17
18 my $module_biblio = Test::MockModule->new('C4::Biblio');
19
20 my $record = new_record();
21 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, q|| );
22 $module_biblio->mock( 'GetMarcBiblio', sub{ $record } );
23
24 my $csv_content = q(Title=245$a|Author=245$c|Subject=650$a);
25 my $csv_profile_id_1 = insert_csv_profile({ csv_content => $csv_content });
26 my $csv = Text::CSV::Encoded->new();
27
28 my $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_1, 1, $csv );
29
30 is( $csv_output, q[Title|Author|Subject
31 "The art of computer programming,The art of another title"|"Donald E. Knuth.,Donald E. Knuth. II"|"Computer programming.,Computer algorithms."
32 ], q|normal way: display headers and content| );
33
34 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_1, 0, $csv );
35 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."
36 ], q|normal way: don't display headers| );
37
38 $csv_content = q(245|650);
39 my $csv_profile_id_2 = insert_csv_profile({ csv_content => $csv_content });
40
41 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_2, 1, $csv );
42 is( $csv_output, q["TITLE STATEMENT"|"SUBJECT ADDED ENTRY--TOPICAL TERM"
43 "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"
44 ], q|normal way: headers retrieved from the DB| );
45
46 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_2, 0, $csv );
47 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"
48 ], q|normal way: headers are not display if not needed| );
49
50 $csv_content = q(Title and author=[% FOREACH field IN fields.245 %][% field.a.0 %] [% field.c.0 %][% END %]|Subject=650$a);
51 my $csv_profile_id_3 = insert_csv_profile({ csv_content => $csv_content });
52
53 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_3, 1, $csv );
54 is( $csv_output, q["Title and author"|Subject
55 "The art of computer programming Donald E. Knuth.The art of another title Donald E. Knuth. II"|"Computer programming.,Computer algorithms."
56 ], q|TT way: display all 245$a and 245$c| );
57
58 $csv_content = q(Subject=[% FOREACH field IN fields.650 %][% IF field.indicator.2 %][% field.a.0 %][% END %][% END %]);
59 my $csv_profile_id_4 = insert_csv_profile({ csv_content => $csv_content });
60
61 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_4, 1, $csv );
62 is( $csv_output, q[Subject
63 "Computer programming."
64 ], q|TT way: display 650$a if indicator 2 for 650 is set| );
65
66 $csv_content = q|Language=[% fields.008.0.substr( 28, 3 ) %]|;
67 my $csv_profile_id_5 = insert_csv_profile({ csv_content => $csv_content });
68
69 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_5, 1, $csv );
70 is( $csv_output, q[Language
71 eng
72 ], q|TT way: export language from the control field 008| );
73
74 $csv_content = q|Title=[% IF fields.100.0.indicator.1 %][% fields.245.0.a.0 %][% END %]|;
75 my $csv_profile_id_6 = insert_csv_profile({ csv_content => $csv_content });
76
77 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_6, 1, $csv );
78 is( $csv_output, q[Title
79 "The art of computer programming"
80 ], q|TT way: display first subfield a for first field 245 if indicator 1 for field 100 is set| );
81
82 $csv_content = q|Title=[% IF fields.100.0.indicator.1 == 1 %][% fields.245.0.a.0 %][% END %]|;
83 my $csv_profile_id_7 = insert_csv_profile({ csv_content => $csv_content });
84
85 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_7, 1, $csv );
86 is( $csv_output, q[Title
87 "The art of computer programming"
88 ], q|TT way: display first subfield a for first field 245 if indicator 1 == 1 for field 100 is set| );
89
90
91 sub insert_csv_profile {
92     my ( $params ) = @_;
93     my $csv_content = $params->{csv_content};
94     $dbh->do(q|
95         INSERT INTO export_format(profile, description, content, csv_separator, field_separator, subfield_separator, encoding, type) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
96         |, {}, ("TEST_PROFILE4", "my desc", $csv_content, '|', ';', ',', 'utf8', 'marc')
97     );
98     return $dbh->last_insert_id( undef, undef, 'export_format', undef );
99 }
100
101 sub new_record {
102     my $record = MARC::Record->new;
103     $record->leader('03174nam a2200445 a 4500');
104     my @fields = (
105         MARC::Field->new(
106             '008', "140211b xxu||||| |||| 00| 0 eng d"
107         ),
108         MARC::Field->new(
109             100, '1', ' ',
110             a => 'Knuth, Donald Ervin',
111             d => '1938',
112         ),
113         MARC::Field->new(
114             245, '1', '4',
115             a => 'The art of computer programming',
116             c => 'Donald E. Knuth.',
117             9 => '0',
118         ),
119         MARC::Field->new(
120             245, '1', '4',
121             a => 'The art of another title',
122             c => 'Donald E. Knuth. II',
123             9 => '1',
124         ),
125         MARC::Field->new(
126             650, ' ', '1',
127             a => 'Computer programming.',
128             9 => '462',
129         ),
130         MARC::Field->new(
131             650, ' ', '0',
132             a => 'Computer algorithms.',
133             9 => '499',
134         ),
135         MARC::Field->new(
136             952, ' ', ' ',
137             p => '3010023917',
138             y => 'BK',
139             c => 'GEN',
140             d => '2001-06-25',
141         ),
142     );
143     $record->append_fields(@fields);
144     return $record;
145 }