4 use Test::More tests => 13;
8 use Text::CSV::Encoded;
10 use C4::Biblio qw( AddBiblio );
17 use t::lib::TestBuilder;
19 my $schema = Koha::Database->new->schema;
20 $schema->storage->txn_begin;
21 my $dbh = C4::Context->dbh;
23 my $builder = t::lib::TestBuilder->new;
24 my $module_biblio = Test::MockModule->new('C4::Biblio');
26 my $record = new_record();
27 my $frameworkcode = q||;
28 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, $frameworkcode );
29 $module_biblio->mock( 'GetMarcBiblio', sub{ $record } );
31 my $csv_content = q(Title=245$a|Author=245$c|Subject=650$a);
32 my $csv_profile_id_1 = insert_csv_profile({ csv_content => $csv_content });
33 my $csv = Text::CSV::Encoded->new();
35 # Test bad biblionumber case
36 my $csv_output = C4::Record::marcrecord2csv( -1, $csv_profile_id_1, 1, $csv );
37 ok(! defined $csv_output, 'Bad biblionumber gives undef as expected.');
39 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_1, 1, $csv );
41 is( $csv_output, q[Title|Author|Subject
42 "The art of computer programming,The art of another title"|"Donald E. Knuth.,Donald E. Knuth. II"|"Computer programming.,Computer algorithms."
43 ], q|normal way: display headers and content| );
45 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_1, 0, $csv );
46 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."
47 ], q|normal way: don't display headers| );
49 $csv_content = q(245|650);
50 my $csv_profile_id_2 = insert_csv_profile({ csv_content => $csv_content });
52 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_2, 1, $csv );
53 is( $csv_output, q["TITLE STATEMENT"|"SUBJECT ADDED ENTRY--TOPICAL TERM"
54 "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"
55 ], q|normal way: headers retrieved from the DB| );
57 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_2, 0, $csv );
58 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"
59 ], q|normal way: headers are not display if not needed| );
61 $csv_content = q(Title and author=[% FOREACH field IN fields.245 %][% field.a.0 %] [% field.c.0 %][% END %]|Subject=650$a);
62 my $csv_profile_id_3 = insert_csv_profile({ csv_content => $csv_content });
64 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_3, 1, $csv );
65 is( $csv_output, q["Title and author"|Subject
66 "The art of computer programming Donald E. Knuth.The art of another title Donald E. Knuth. II"|"Computer programming.,Computer algorithms."
67 ], q|TT way: display all 245$a and 245$c| );
69 $csv_content = q(Subject=[% FOREACH field IN fields.650 %][% IF field.indicator.2 %][% field.a.0 %][% END %][% END %]);
70 my $csv_profile_id_4 = insert_csv_profile({ csv_content => $csv_content });
72 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_4, 1, $csv );
73 is( $csv_output, q[Subject
74 "Computer programming."
75 ], q|TT way: display 650$a if indicator 2 for 650 is set| );
77 $csv_content = q|Language=[% fields.008.0.substr( 28, 3 ) %]|;
78 my $csv_profile_id_5 = insert_csv_profile({ csv_content => $csv_content });
80 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_5, 1, $csv );
81 is( $csv_output, q[Language
83 ], q|TT way: export language from the control field 008| );
85 $csv_content = q|Title=[% IF fields.100.0.indicator.1 %][% fields.245.0.a.0 %][% END %]|;
86 my $csv_profile_id_6 = insert_csv_profile({ csv_content => $csv_content });
88 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_6, 1, $csv );
89 is( $csv_output, q[Title
90 "The art of computer programming"
91 ], q|TT way: display first subfield a for first field 245 if indicator 1 for field 100 is set| );
93 $csv_content = q|Title=[% IF fields.100.0.indicator.1 == 1 %][% fields.245.0.a.0 %][% END %]|;
94 my $csv_profile_id_7 = insert_csv_profile({ csv_content => $csv_content });
96 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_7, 1, $csv );
97 is( $csv_output, q[Title
98 "The art of computer programming"
99 ], q|TT way: display first subfield a for first field 245 if indicator 1 == 1 for field 100 is set| );
102 my $authorised_value_1 =
103 $builder->build( { source => 'AuthorisedValue', value => { category => 'MY_AV_1', authorised_value => 1, lib => 'This is an AV', lib_opac => 'This is an AV (opac)' } } );
104 my $authorised_value_2 = $builder->build(
105 { source => 'AuthorisedValue', value => { category => 'MY_AV_2', authorised_value => 2, lib => 'This is another AV', lib_opac => 'This is another AV (opac)' } } );
106 $dbh->do(q|DELETE FROM marc_subfield_structure WHERE tagfield='998' and ( tagsubfield='8' or tagsubfield='9')|);
108 { source => 'MarcSubfieldStructure', value => { authorised_value => $authorised_value_1->{category}, tagfield => 998, tagsubfield => '8', frameworkcode => $frameworkcode } }
111 { source => 'MarcSubfieldStructure', value => { authorised_value => $authorised_value_2->{category}, tagfield => 998, tagsubfield => '9', frameworkcode => $frameworkcode } }
113 $csv_content = q(Title=245$a|AV1=998$8|AV2=998$9);
114 my $csv_profile_id_8 = insert_csv_profile( { csv_content => $csv_content } );
115 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_8, 1, $csv );
116 is( $csv_output, q[Title|AV1|AV2
117 "The art of computer programming,The art of another title"|"This is an AV"|"This is another AV"
118 ], q|TT way: display first subfield a for first field 245 if indicator 1 == 1 for field 100 is set|
121 $csv_content = q(Title=245$a|AVs=998);
122 my $csv_profile_id_9 = insert_csv_profile( { csv_content => $csv_content } );
123 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_9, 1, $csv );
124 is( $csv_output, q[Title|AVs
125 "The art of computer programming,The art of another title"|"This is an AV,This is another AV"
126 ], q|TT way: display first subfield a for first field 245 if indicator 1 == 1 for field 100 is set|
129 subtest 'Test for subfields 0' => sub {
132 my $record2 = new_record();
133 my $frameworkcode2 = q||;
135 # We change the barcode of the second item record to prevent an error "duplicate entry"
136 my $field = $record->field('952');
137 my $new_field = MARC::Field->new(
142 $field->replace_with($new_field);
144 # We create another record
145 my ( $biblionumber2, $biblioitemnumber2 ) = AddBiblio( $record2, $frameworkcode2 );
147 # We add two item to two record to test fieldtag 952 and subfieldtag 9520
148 my ( undef, undef, $itemnumber2 ) = AddItemFromMarc( $record2, $biblionumber );
149 my ( undef, undef, $itemnumber ) = AddItemFromMarc( $record, $biblionumber );
150 $csv_content = q(Titre=245a|Nom de personne=100a|Statut « Élagué »=9520);
151 my $csv_profile_id_10 = insert_csv_profile( { csv_content => $csv_content } );
152 $csv_output = C4::Record::marcrecord2csv( $biblionumber, $csv_profile_id_10, 1, $csv );
154 [ split "\n", $csv_output ],
156 q[Titre|"Nom de personne"|"Statut « Élagué »"],
157 q["The art of computer programming,The art of another title"|"Knuth, Donald Ervin"|Withdrawn;Withdrawn],
159 q[subfieldtag 952\$0 is not working, should return 'Withdrawn']
163 sub insert_csv_profile {
165 my $csv_content = $params->{csv_content};
167 INSERT INTO export_format(profile, description, content, csv_separator, field_separator, subfield_separator, encoding, type) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
168 |, {}, ("TEST_PROFILE4", "my desc", $csv_content, '|', ';', ',', 'utf8', 'marc')
170 return $dbh->last_insert_id( undef, undef, 'export_format', undef );
174 my $record = MARC::Record->new;
175 $record->leader('03174nam a2200445 a 4500');
178 '008', "140211b xxu||||| |||| 00| 0 eng d"
182 a => 'Knuth, Donald Ervin',
187 a => 'The art of computer programming',
188 c => 'Donald E. Knuth.',
193 a => 'The art of another title',
194 c => 'Donald E. Knuth. II',
199 a => 'Computer programming.',
204 a => 'Computer algorithms.',
221 $record->append_fields(@fields);