Bug 11046: Better handling of uncertain years for publicationyear
[koha.git] / 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 => 3;
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 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-" );
43
44 subtest 'Test a few mappings' => sub {
45     plan tests => 7;
46
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' ),
52     );
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' );
59
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' );
64
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' );
71 };
72
73 subtest 'Multiple mappings for one kohafield' => sub {
74     plan tests => 4;
75
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-" );
80
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' );
91
92     is( C4::Biblio::TransformMarcToKohaOneField( 'biblio.field1', $marc ),
93         '3a | 51', 'TransformMarcToKohaOneField returns biblio.field1' );
94 };
95
96 subtest 'Testing _adjust_pubyear' => sub {
97     plan tests => 8;
98
99     is( C4::Biblio::_adjust_pubyear('2004 c2000 2007'), 2000, 'First cYEAR' );
100     is( C4::Biblio::_adjust_pubyear('2004 2000 2007'), 2004, 'First year' );
101     is( C4::Biblio::_adjust_pubyear('18xx 1900'), 1900, '1900 has priority over 18xx' );
102     is( C4::Biblio::_adjust_pubyear('18xx'), 1800, '18xx on its own' );
103     is( C4::Biblio::_adjust_pubyear('197X'), 1970, '197X on its own' );
104     is( C4::Biblio::_adjust_pubyear('1...'), 1000, '1... on its own' );
105     is( C4::Biblio::_adjust_pubyear('12?? 13xx'), 1200, '12?? first' );
106     is( C4::Biblio::_adjust_pubyear('12? 1x'), '12? 1x', 'Too short' );
107 };
108
109 # Cleanup
110 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
111 $schema->storage->txn_rollback;