Bug 26384: Fix executable flags
[koha.git] / t / db_dependent / Biblio / TransformMarcToKoha.t
blob8d1e94c1eb9b91cae86e617e33bebf7eec1a1d9b
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 => 4;
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 subtest 'Testing _adjust_pubyear' => sub {
97 plan tests => 10;
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 is( C4::Biblio::_adjust_pubyear('198-'), '198-', 'Missing question mark' );
108 is( C4::Biblio::_adjust_pubyear('198-?'), '1980', '198-?' );
111 subtest 'Test repeatable subfields' => sub {
112 plan tests => 2;
114 # Make 510x repeatable and 510y not
115 Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '510' })->delete;
116 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '510', tagsubfield => 'x', kohafield => 'items.test', repeatable => 1 })->store;
117 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '510', tagsubfield => 'y', kohafield => 'items.norepeat', repeatable => 0 })->store;
118 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
121 my $marc = MARC::Record->new;
122 $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)
123 my $result = C4::Biblio::TransformMarcToKoha( $marc );
124 is( $result->{test}, '1 | 2', 'Check 510x for two values' );
125 is( $result->{norepeat}, '3 | 4 | 5', 'Check 510y too' );
128 # Cleanup
129 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
130 $schema->storage->txn_rollback;