|
Line 0
Link Here
|
|
|
1 |
use Modern::Perl; |
| 2 |
use Test::More tests => 4; |
| 3 |
|
| 4 |
use Test::MockModule; |
| 5 |
use MARC::Record; |
| 6 |
use MARC::Field; |
| 7 |
|
| 8 |
use C4::Biblio; |
| 9 |
use C4::Context; |
| 10 |
use C4::Items; |
| 11 |
use C4::Record; |
| 12 |
use Koha::DateUtils; |
| 13 |
use Koha::Item; |
| 14 |
|
| 15 |
use t::lib::TestBuilder; |
| 16 |
|
| 17 |
my $schema = Koha::Database->new()->schema(); |
| 18 |
$schema->storage->txn_begin(); |
| 19 |
|
| 20 |
my $builder = t::lib::TestBuilder->new(); |
| 21 |
|
| 22 |
my $title = q|The art of computer programming|; |
| 23 |
my $author = 'Knuth, Donald Ervin'; |
| 24 |
my $biblio = $builder->build_sample_biblio({ title => $title, author => $author }); |
| 25 |
my $record = $biblio->metadata->record; |
| 26 |
if (C4::Context->preference('marcflavour') eq 'UNIMARC') { |
| 27 |
$record->append_fields( |
| 28 |
MARC::Field->new(200, ' ', ' ', a => 'The art of another title', f => 'Donald E. Knuth. II'), |
| 29 |
MARC::Field->new(700, '0', '0', a => 'Knuth,Donald Ervin'), |
| 30 |
); |
| 31 |
} else { |
| 32 |
$record->field(245)->update(c => $author); |
| 33 |
$record->append_fields( |
| 34 |
MARC::Field->new(245, ' ', ' ', a => 'The art of another title', f => 'Donald E. Knuth. II'), |
| 35 |
); |
| 36 |
} |
| 37 |
$record->append_fields( |
| 38 |
MARC::Field->new( |
| 39 |
600, ' ', '1', |
| 40 |
a => 'Computer programming.', |
| 41 |
9 => '462', |
| 42 |
), |
| 43 |
MARC::Field->new( |
| 44 |
600, ' ', '0', |
| 45 |
a => 'Computer algorithms.', |
| 46 |
9 => '499', |
| 47 |
), |
| 48 |
); |
| 49 |
$biblio->metadata->update({ metadata => $record->as_xml() }); |
| 50 |
my $biblionumber = $biblio->biblionumber; |
| 51 |
my $item = $builder->build_sample_item({ biblionumber => $biblionumber, library => 'CPL', itype => 'CANNOT', barcode => 'my new barcode' }); |
| 52 |
|
| 53 |
subtest 'csv' => sub { |
| 54 |
plan tests => 1; |
| 55 |
my $csv_content = C4::Context->preference('marcflavour') eq 'UNIMARC' |
| 56 |
? 'Title=200$a|Subject=600$a' |
| 57 |
: 'Title=245$a|Subject=600$a'; |
| 58 |
my $csv_profile_id = insert_csv_profile({ csv_content => $csv_content }); |
| 59 |
|
| 60 |
my $csv_data = C4::Biblio::BuildBiblioDataForExport({ |
| 61 |
biblionumbers => [ $biblionumber ], |
| 62 |
format => 'csv', |
| 63 |
csv_profile_id => $csv_profile_id, |
| 64 |
}); |
| 65 |
|
| 66 |
is_deeply( $csv_data, { records_data => [], records_file => qq{Title|Subject |
| 67 |
"$title,The art of another title"|"Computer programming.,Computer algorithms." |
| 68 |
}}, 'BuildBiblioDataForExport should return correct values in csv' ); |
| 69 |
}; |
| 70 |
|
| 71 |
subtest 'iso2709' => sub { |
| 72 |
plan tests => 7; |
| 73 |
my $iso2709_data = C4::Biblio::BuildBiblioDataForExport({ |
| 74 |
biblionumbers => [ $biblionumber ], |
| 75 |
format => 'iso2709', |
| 76 |
}); |
| 77 |
|
| 78 |
my $today = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1}); |
| 79 |
|
| 80 |
like( $iso2709_data->{records_file}, qr{00467nam a2200121 a \d+140211b xxu||||| |||| 00| 0 eng d1 aKnuth, Donald Ervind193814a$titlecDonald E. Knuth.9014aThe art of another titlecDonald E. Knuth. II91 1aComputer programming.9462 0aComputer algorithms.9499 c57d57 00104070910aCPLbCPLd$todaypmy new barcoder$todayw$todayyCANNOT}, 'BuildBiblioDataForExport should return a correct records file in iso2709' ); |
| 81 |
|
| 82 |
my $first_biblio_data = @{ $iso2709_data->{records_data} }[0]; |
| 83 |
is( $first_biblio_data->{HASAUTHORS}, 1 ); |
| 84 |
is( scalar( @{ $first_biblio_data->{MARCSUBJCTS} } ), 2, '499 and 462' ); |
| 85 |
is( $first_biblio_data->{title}, $title ); |
| 86 |
is( scalar( @{ $first_biblio_data->{ITEM_RESULTS} } ), 1, '1 item' ); |
| 87 |
is( scalar( @{ $first_biblio_data->{MARCAUTHORS} } ), 1, '1 author' ); |
| 88 |
is( $first_biblio_data->{biblionumber}, $biblionumber, 'bibionumber is correct' ); |
| 89 |
}; |
| 90 |
|
| 91 |
subtest 'ris' => sub { |
| 92 |
plan tests => 1; |
| 93 |
my $ris_data = C4::Biblio::BuildBiblioDataForExport({ |
| 94 |
biblionumbers => [ $biblionumber ], |
| 95 |
format => 'ris', |
| 96 |
}); |
| 97 |
is( $ris_data->{records_file}, qq|TY - GEN\r\nAU - Knuth,Donald Ervin\r\nTI - $title\r\nKW - Computer programming.\r\nKW - Computer algorithms.\r\nER - \r\n|,); |
| 98 |
}; |
| 99 |
|
| 100 |
subtest 'bibtex' => sub { |
| 101 |
plan tests => 1; |
| 102 |
my $bibtex_data = C4::Biblio::BuildBiblioDataForExport({ |
| 103 |
biblionumbers => [ $biblionumber ], |
| 104 |
format => 'bibtex', |
| 105 |
}); |
| 106 |
my $expected = qq|\@book{$biblionumber,\n\tauthor = {Knuth,Donald Ervin},\n\ttitle = {The art of computer programming}\n}\n|; |
| 107 |
is( $bibtex_data->{records_file}, $expected); |
| 108 |
}; |
| 109 |
|
| 110 |
sub insert_csv_profile { |
| 111 |
my ( $params ) = @_; |
| 112 |
my $dbh = C4::Context->dbh; |
| 113 |
$dbh->do(q| |
| 114 |
INSERT INTO export_format(profile, description, content, csv_separator, field_separator, subfield_separator, encoding, type) VALUES (?, ?, ?, ?, ?, ?, ?, ?) |
| 115 |
|, {}, ("TEST_PROFILE4", "my desc", $params->{csv_content}, '|', ';', ',', 'utf8', 'marc') |
| 116 |
); |
| 117 |
return $dbh->last_insert_id( undef, undef, 'export_format', undef ); |
| 118 |
} |
| 119 |
|
| 120 |
$schema->storage->txn_rollback(); |