Bug 24295: Finally remove GetTransfers from C4/Circulation
[koha.git] / t / db_dependent / Biblio / TransformMarcToKoha.t
1 #!/usr/bin/perl
2
3 # Tests for C4::Biblio::TransformMarcToKoha
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 => 4;
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 qw( TransformMarcToKoha );
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 => 6;
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({ 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' );
59
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' );
64
65 };
66
67 subtest 'Multiple mappings for one kohafield' => sub {
68     plan tests => 4;
69
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-" );
74
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' );
85
86     is_deeply( C4::Biblio::TransformMarcToKoha({ kohafields => ['biblio.field1'], record => $marc }),
87         { 'field1' => '3a | 51'}, 'TransformMarcToKoha returns biblio.field1 when kohafields specified' );
88 };
89
90 subtest 'Testing _adjust_pubyear' => sub {
91     plan tests => 18;
92
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' );
111 };
112
113 subtest 'Test repeatable subfields' => sub {
114     plan tests => 5;
115
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-" );
121
122
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' );
128
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)
132
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' );
137 };
138
139 # Cleanup
140 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
141 $schema->storage->txn_rollback;