Bug 10306: Add tests for module changes
[srvgit] / t / db_dependent / Biblio / TransformMarcToKoha.t
1 #!/usr/bin/perl
2
3 # Tests for C4::Biblio::TransformMarcToKoha, TransformMarcToKohaOneField
4
5 # Copyright 2017 Rijksmuseum
6 #
7 # This file is part of Koha.
8 #
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.
13 #
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.
17 #
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>.
20
21 use Modern::Perl;
22 use Test::More tests => 2;
23 use MARC::Record;
24
25 use t::lib::Mocks;
26 use t::lib::TestBuilder;
27
28 use Koha::Database;
29 use Koha::Caches;
30 use Koha::MarcSubfieldStructures;
31 use C4::Biblio;
32
33 my $schema  = Koha::Database->new->schema;
34 $schema->storage->txn_begin;
35
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;
42
43 subtest 'Test a few mappings' => sub {
44     plan tests => 6;
45
46     my $marc = MARC::Record->new;
47     $marc->append_fields(
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' ),
51     );
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' );
58
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' );
63 };
64
65 subtest 'Multiple mappings for one kohafield' => sub {
66     plan tests => 4;
67
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" );
71
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' );
82
83     is( C4::Biblio::TransformMarcToKohaOneField( 'biblio.field1', $marc, $fwc ),
84         '3a | 51', 'TransformMarcToKohaOneField returns biblio.field1' );
85 };
86
87 # Cleanup
88 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-$fwc" );
89 $schema->storage->txn_rollback;