4 use Test::More tests => 12;
8 use Text::CSV::Encoded;
10 use C4::Biblio qw( AddBiblio );
15 use t::lib::TestBuilder;
17 my $schema = Koha::Database->new->schema;
18 $schema->storage->txn_begin;
19 my $dbh = C4::Context->dbh;
21 my $builder = t::lib::TestBuilder->new;
22 my $module_biblio = Test::MockModule->new('C4::Biblio');
24 my $record = new_record();
25 my $frameworkcode = q||;
26 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, $frameworkcode );
27 $module_biblio->mock( 'GetMarcBiblio', sub{ $record } );
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();
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.');
37 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_1, 1, $csv );
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| );
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| );
47 $csv_content = q(245|650);
48 my $csv_profile_id_2 = insert_csv_profile({ csv_content => $csv_content });
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| );
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| );
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 });
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| );
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 });
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| );
75 $csv_content = q|Language=[% fields.008.0.substr( 28, 3 ) %]|;
76 my $csv_profile_id_5 = insert_csv_profile({ csv_content => $csv_content });
78 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_5, 1, $csv );
79 is( $csv_output, q[Language
81 ], q|TT way: export language from the control field 008| );
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 });
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| );
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 });
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| );
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')|);
106 { source => 'MarcSubfieldStructure', value => { authorised_value => $authorised_value_1->{category}, tagfield => 998, tagsubfield => '8', frameworkcode => $frameworkcode } }
109 { source => 'MarcSubfieldStructure', value => { authorised_value => $authorised_value_2->{category}, tagfield => 998, tagsubfield => '9', frameworkcode => $frameworkcode } }
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|
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|
129 sub insert_csv_profile {
131 my $csv_content = $params->{csv_content};
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')
136 return $dbh->last_insert_id( undef, undef, 'export_format', undef );
140 my $record = MARC::Record->new;
141 $record->leader('03174nam a2200445 a 4500');
144 '008', "140211b xxu||||| |||| 00| 0 eng d"
148 a => 'Knuth, Donald Ervin',
153 a => 'The art of computer programming',
154 c => 'Donald E. Knuth.',
159 a => 'The art of another title',
160 c => 'Donald E. Knuth. II',
165 a => 'Computer programming.',
170 a => 'Computer algorithms.',
186 $record->append_fields(@fields);