4 use Test::More tests => 11;
8 use Text::CSV::Encoded;
10 use C4::Biblio qw( AddBiblio );
14 use t::lib::TestBuilder;
16 my $dbh = C4::Context->dbh;
17 $dbh->{AutoCommit} = 0;
18 $dbh->{RaiseError} = 1;
20 my $builder = t::lib::TestBuilder->new;
21 my $module_biblio = Test::MockModule->new('C4::Biblio');
23 my $record = new_record();
24 my $frameworkcode = q||;
25 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, $frameworkcode );
26 $module_biblio->mock( 'GetMarcBiblio', sub{ $record } );
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();
32 my $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_1, 1, $csv );
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| );
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| );
42 $csv_content = q(245|650);
43 my $csv_profile_id_2 = insert_csv_profile({ csv_content => $csv_content });
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| );
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| );
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 });
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| );
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 });
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| );
70 $csv_content = q|Language=[% fields.008.0.substr( 28, 3 ) %]|;
71 my $csv_profile_id_5 = insert_csv_profile({ csv_content => $csv_content });
73 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_5, 1, $csv );
74 is( $csv_output, q[Language
76 ], q|TT way: export language from the control field 008| );
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 });
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| );
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 });
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| );
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')|);
101 { source => 'MarcSubfieldStructure', value => { authorised_value => $authorised_value_1->{category}, tagfield => 998, tagsubfield => '8', frameworkcode => $frameworkcode } }
104 { source => 'MarcSubfieldStructure', value => { authorised_value => $authorised_value_2->{category}, tagfield => 998, tagsubfield => '9', frameworkcode => $frameworkcode } }
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|
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|
124 sub insert_csv_profile {
126 my $csv_content = $params->{csv_content};
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')
131 return $dbh->last_insert_id( undef, undef, 'export_format', undef );
135 my $record = MARC::Record->new;
136 $record->leader('03174nam a2200445 a 4500');
139 '008', "140211b xxu||||| |||| 00| 0 eng d"
143 a => 'Knuth, Donald Ervin',
148 a => 'The art of computer programming',
149 c => 'Donald E. Knuth.',
154 a => 'The art of another title',
155 c => 'Donald E. Knuth. II',
160 a => 'Computer programming.',
165 a => 'Computer algorithms.',
181 $record->append_fields(@fields);