Bug 19096: Adjusts unit tests
[koha.git] / t / db_dependent / Biblio / TransformMarcToKoha.t
blobf47632d7281451cc0d255ca33bef837bc22d30af
1 #!/usr/bin/perl
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
12 # version.
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>.
21 use Modern::Perl;
22 use Test::More tests => 2;
23 use MARC::Record;
25 use t::lib::Mocks;
26 use t::lib::TestBuilder;
28 use Koha::Database;
29 use Koha::Caches;
30 use Koha::MarcSubfieldStructures;
31 use C4::Biblio;
33 my $schema = Koha::Database->new->schema;
34 $schema->storage->txn_begin;
36 # Create a few mappings
37 # Note: TransformMarcToKoha wants a table name (biblio, biblioitems or items)
38 Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => [ '300', '500' ] })->delete;
39 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '300', tagsubfield => 'a', kohafield => 'biblio.field1' })->store;
40 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '300', tagsubfield => 'b', kohafield => 'biblio.field2' })->store;
41 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '500', tagsubfield => 'a', kohafield => 'biblio.field3' })->store;
42 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
44 subtest 'Test a few mappings' => sub {
45 plan tests => 7;
47 my $marc = MARC::Record->new;
48 $marc->append_fields(
49 MARC::Field->new( '300', '', '', a => 'a1', b => 'b1' ),
50 MARC::Field->new( '300', '', '', a => 'a2', b => 'b2' ),
51 MARC::Field->new( '500', '', '', a => 'note1', a => 'note2' ),
53 my $result = C4::Biblio::TransformMarcToKoha( $marc );
54 # Note: TransformMarcToKoha stripped the table prefix biblio.
55 is( keys %{$result}, 3, 'Found all three mappings' );
56 is( $result->{field1}, 'a1 | a2', 'Check field1 results' );
57 is( $result->{field2}, 'b1 | b2', 'Check field2 results' );
58 is( $result->{field3}, 'note1 | note2', 'Check field3 results' );
60 is( C4::Biblio::TransformMarcToKohaOneField( 'biblio.field1', $marc ),
61 $result->{field1}, 'TransformMarcToKohaOneField returns biblio.field1');
62 is( C4::Biblio::TransformMarcToKohaOneField( 'field4', $marc ),
63 undef, 'TransformMarcToKohaOneField returns undef' );
65 # Bug 19096 Default is authoritative now
66 # Test passing another framework
67 # CAUTION: This parameter of TransformMarcToKoha will be removed later
68 my $new_fw = t::lib::TestBuilder->new->build({source => 'BiblioFramework'});
69 $result = C4::Biblio::TransformMarcToKoha($marc, $new_fw->{frameworkcode});
70 is( keys %{$result}, 3, 'Still found all three mappings' );
73 subtest 'Multiple mappings for one kohafield' => sub {
74 plan tests => 4;
76 # Add another mapping to field1
77 Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '510' })->delete;
78 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '510', tagsubfield => 'a', kohafield => 'biblio.field1' })->store;
79 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
81 my $marc = MARC::Record->new;
82 $marc->append_fields( MARC::Field->new( '300', '', '', a => '3a' ) );
83 my $result = C4::Biblio::TransformMarcToKoha( $marc );
84 is_deeply( $result, { field1 => '3a' }, 'Simple start' );
85 $marc->append_fields( MARC::Field->new( '510', '', '', a => '' ) );
86 $result = C4::Biblio::TransformMarcToKoha( $marc );
87 is_deeply( $result, { field1 => '3a' }, 'An empty 510a makes no difference' );
88 $marc->append_fields( MARC::Field->new( '510', '', '', a => '51' ) );
89 $result = C4::Biblio::TransformMarcToKoha( $marc );
90 is_deeply( $result, { field1 => '3a | 51' }, 'Got 300a and 510a' );
92 is( C4::Biblio::TransformMarcToKohaOneField( 'biblio.field1', $marc ),
93 '3a | 51', 'TransformMarcToKohaOneField returns biblio.field1' );
96 # Cleanup
97 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
98 $schema->storage->txn_rollback;