4 use Test::More tests => 9;
8 use Text::CSV::Encoded;
10 use C4::Biblio qw( AddBiblio );
14 my $dbh = C4::Context->dbh;
15 $dbh->{AutoCommit} = 0;
16 $dbh->{RaiseError} = 1;
18 my $module_biblio = Test::MockModule->new('C4::Biblio');
20 my $record = new_record();
21 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, q|| );
22 $module_biblio->mock( 'GetMarcBiblio', sub{ $record } );
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();
28 my $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_1, 1, $csv );
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| );
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| );
38 $csv_content = q(245|650);
39 my $csv_profile_id_2 = insert_csv_profile({ csv_content => $csv_content });
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| );
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| );
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 });
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| );
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 });
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| );
66 $csv_content = q|Language=[% fields.008.0.substr( 28, 3 ) %]|;
67 my $csv_profile_id_5 = insert_csv_profile({ csv_content => $csv_content });
69 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_5, 1, $csv );
70 is( $csv_output, q[Language
72 ], q|TT way: export language from the control field 008| );
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 });
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| );
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 });
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| );
91 sub insert_csv_profile {
93 my $csv_content = $params->{csv_content};
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')
98 return $dbh->last_insert_id( undef, undef, 'export_format', undef );
102 my $record = MARC::Record->new;
103 $record->leader('03174nam a2200445 a 4500');
106 '008', "140211b xxu||||| |||| 00| 0 eng d"
110 a => 'Knuth, Donald Ervin',
115 a => 'The art of computer programming',
116 c => 'Donald E. Knuth.',
121 a => 'The art of another title',
122 c => 'Donald E. Knuth. II',
127 a => 'Computer programming.',
132 a => 'Computer algorithms.',
143 $record->append_fields(@fields);