3 # Tests for C4::Biblio::TransformMarcToKoha
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 => 4;
26 use t::lib::TestBuilder;
30 use Koha::MarcSubfieldStructures;
31 use C4::Biblio qw( TransformMarcToKoha );
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 {
47 my $marc = MARC::Record->new;
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({ record => $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_deeply( C4::Biblio::TransformMarcToKoha({ record => $marc, kohafields => ['biblio.field1'] }),
61 {field1 => 'a1 | a2'}, 'TransformMarcToKoha returns biblio.field1 if kohafields specified');
62 is_deeply( C4::Biblio::TransformMarcToKoha({ record => $marc, kohafields => ['field4'] }),
63 {} , 'TransformMarcToKoha returns empty hashref on unknown kohafields' );
67 subtest 'Multiple mappings for one kohafield' => sub {
70 # Add another mapping to field1
71 Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '510' })->delete;
72 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '510', tagsubfield => 'a', kohafield => 'biblio.field1' })->store;
73 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
75 my $marc = MARC::Record->new;
76 $marc->append_fields( MARC::Field->new( '300', '', '', a => '3a' ) );
77 my $result = C4::Biblio::TransformMarcToKoha({ record => $marc });
78 is_deeply( $result, { field1 => '3a' }, 'Simple start' );
79 $marc->append_fields( MARC::Field->new( '510', '', '', a => '' ) );
80 $result = C4::Biblio::TransformMarcToKoha({ record => $marc });
81 is_deeply( $result, { field1 => '3a' }, 'An empty 510a makes no difference' );
82 $marc->append_fields( MARC::Field->new( '510', '', '', a => '51' ) );
83 $result = C4::Biblio::TransformMarcToKoha({ record => $marc });
84 is_deeply( $result, { field1 => '3a | 51' }, 'Got 300a and 510a' );
86 is_deeply( C4::Biblio::TransformMarcToKoha({ kohafields => ['biblio.field1'], record => $marc }),
87 { 'field1' => '3a | 51'}, 'TransformMarcToKoha returns biblio.field1 when kohafields specified' );
90 subtest 'Testing _adjust_pubyear' => sub {
93 is( C4::Biblio::_adjust_pubyear('2004 c2000 2007'), 2000, 'First cYEAR' );
94 is( C4::Biblio::_adjust_pubyear('2004 2000 2007'), 2004, 'First year' );
95 is( C4::Biblio::_adjust_pubyear('18xx 1900'), 1900, '1900 has priority over 18xx' );
96 is( C4::Biblio::_adjust_pubyear('18xx'), 1800, '18xx on its own' );
97 is( C4::Biblio::_adjust_pubyear('197X'), 1970, '197X on its own' );
98 is( C4::Biblio::_adjust_pubyear('1...'), 1000, '1... on its own' );
99 is( C4::Biblio::_adjust_pubyear('12?? 13xx'), 1200, '12?? first' );
100 is( C4::Biblio::_adjust_pubyear('12? 1x'), 1200, '12? first' );
101 is( C4::Biblio::_adjust_pubyear('198-'), 1980, '198-' );
102 is( C4::Biblio::_adjust_pubyear('19--'), 1900, '19--' );
103 is( C4::Biblio::_adjust_pubyear('19-'), 1900, '19-' );
104 is( C4::Biblio::_adjust_pubyear('1-'), 1000, '1-' );
105 is( C4::Biblio::_adjust_pubyear('2xxx'), 2000, '2xxx' );
106 is( C4::Biblio::_adjust_pubyear('2xx'), 2000, '2xx' );
107 is( C4::Biblio::_adjust_pubyear('2x'), 2000, '2x' );
108 is( C4::Biblio::_adjust_pubyear('198-?'), 1980, '198-?' );
109 is( C4::Biblio::_adjust_pubyear('1981-'), 1981, 'Date range returns first date' );
110 is( C4::Biblio::_adjust_pubyear('broken'), undef, 'Non-matching data' );
113 subtest 'Test repeatable subfields' => sub {
116 # Make 510x repeatable and 510y not
117 Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '510' })->delete;
118 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '510', tagsubfield => 'x', kohafield => 'items.test', repeatable => 1 })->store;
119 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '510', tagsubfield => 'y', kohafield => 'items.norepeat', repeatable => 0 })->store;
120 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
123 my $marc = MARC::Record->new;
124 $marc->append_fields( MARC::Field->new( '510', '', '', x => '1', x => '2', y => '3 | 4', y => '5' ) ); # actually, we should only have one $y (BZ 24652)
125 my $result = C4::Biblio::TransformMarcToKoha({ record => $marc });
126 is( $result->{test}, '1 | 2', 'Check 510x for two values' );
127 is( $result->{norepeat}, '3 | 4 | 5', 'Check 510y too' );
129 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '510', tagsubfield => 'a', kohafield => 'biblio.field1' })->store;
130 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
131 $marc->append_fields( MARC::Field->new( '510', '', '', a => '1' ) ); # actually, we should only have one $y (BZ 24652)
133 $result = C4::Biblio::TransformMarcToKoha({ record => $marc, limit_table => 'no_items' });
134 is( $result->{test}, undef, 'Item field not returned when "no_items" passed' );
135 is( $result->{norepeat}, undef, 'Item field not returned when "no_items" passed' );
136 is( $result->{field1}, 1, 'Biblio field returned when "no_items" passed' );
140 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
141 $schema->storage->txn_rollback;