3 # Tests for C4::Biblio::TransformMarcToKoha, TransformMarcToKohaOneField
5 # Copyright 2017 Rijksmuseum
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 3 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Test::More tests => 2;
26 use t::lib::TestBuilder;
30 use Koha::MarcSubfieldStructures;
33 my $schema = Koha::Database->new->schema;
34 $schema->storage->txn_begin;
36 # Create a new framework with a few mappings
37 # Note: TransformMarcToKoha wants a table name (biblio, biblioitems or items)
38 our $fwc = t::lib::TestBuilder->new->build_object({ class => 'Koha::BiblioFrameworks' })->frameworkcode;
39 Koha::MarcSubfieldStructure->new({ frameworkcode => $fwc, tagfield => '300', tagsubfield => 'a', kohafield => 'biblio.field1' })->store;
40 Koha::MarcSubfieldStructure->new({ frameworkcode => $fwc, tagfield => '300', tagsubfield => 'b', kohafield => 'biblio.field2' })->store;
41 Koha::MarcSubfieldStructure->new({ frameworkcode => $fwc, tagfield => '500', tagsubfield => 'a', kohafield => 'biblio.field3' })->store;
43 subtest 'Test a few mappings' => sub {
46 my $marc = MARC::Record->new;
48 MARC::Field->new( '300', '', '', a => 'a1', b => 'b1' ),
49 MARC::Field->new( '300', '', '', a => 'a2', b => 'b2' ),
50 MARC::Field->new( '500', '', '', a => 'note1', a => 'note2' ),
52 my $result = C4::Biblio::TransformMarcToKoha( $marc, $fwc );
53 # Note: TransformMarcToKoha stripped the table prefix biblio.
54 is( keys %{$result}, 3, 'Found all three mappings' );
55 is( $result->{field1}, 'a1 | a2', 'Check field1 results' );
56 is( $result->{field2}, 'b1 | b2', 'Check field2 results' );
57 is( $result->{field3}, 'note1 | note2', 'Check field3 results' );
59 is( C4::Biblio::TransformMarcToKohaOneField( 'biblio.field1', $marc, $fwc ),
60 $result->{field1}, 'TransformMarcToKohaOneField returns biblio.field1');
61 is( C4::Biblio::TransformMarcToKohaOneField( 'field4', $marc, $fwc ),
62 undef, 'TransformMarcToKohaOneField returns undef' );
65 subtest 'Multiple mappings for one kohafield' => sub {
68 # Add another mapping to field1
69 Koha::MarcSubfieldStructure->new({ frameworkcode => $fwc, tagfield => '510', tagsubfield => 'a', kohafield => 'biblio.field1' })->store;
70 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-$fwc" );
72 my $marc = MARC::Record->new;
73 $marc->append_fields( MARC::Field->new( '300', '', '', a => '3a' ) );
74 my $result = C4::Biblio::TransformMarcToKoha( $marc, $fwc );
75 is_deeply( $result, { field1 => '3a' }, 'Simple start' );
76 $marc->append_fields( MARC::Field->new( '510', '', '', a => '' ) );
77 $result = C4::Biblio::TransformMarcToKoha( $marc, $fwc );
78 is_deeply( $result, { field1 => '3a' }, 'An empty 510a makes no difference' );
79 $marc->append_fields( MARC::Field->new( '510', '', '', a => '51' ) );
80 $result = C4::Biblio::TransformMarcToKoha( $marc, $fwc );
81 is_deeply( $result, { field1 => '3a | 51' }, 'Got 300a and 510a' );
83 is( C4::Biblio::TransformMarcToKohaOneField( 'biblio.field1', $marc, $fwc ),
84 '3a | 51', 'TransformMarcToKohaOneField returns biblio.field1' );
88 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-$fwc" );
89 $schema->storage->txn_rollback;