Bug 17600: Standardize our EXPORT_OK
[srvgit] / t / db_dependent / Biblio / TransformMarcToKoha.t
old mode 100644 (file)
new mode 100755 (executable)
index f47632d..5062ed7
@@ -19,7 +19,7 @@
 # with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
-use Test::More tests => 2;
+use Test::More tests => 4;
 use MARC::Record;
 
 use t::lib::Mocks;
@@ -28,7 +28,7 @@ use t::lib::TestBuilder;
 use Koha::Database;
 use Koha::Caches;
 use Koha::MarcSubfieldStructures;
-use C4::Biblio;
+use C4::Biblio qw( TransformMarcToKoha TransformMarcToKohaOneField );
 
 my $schema  = Koha::Database->new->schema;
 $schema->storage->txn_begin;
@@ -93,6 +93,49 @@ subtest 'Multiple mappings for one kohafield' => sub {
         '3a | 51', 'TransformMarcToKohaOneField returns biblio.field1' );
 };
 
+subtest 'Testing _adjust_pubyear' => sub {
+    plan tests => 12;
+
+    is( C4::Biblio::_adjust_pubyear('2004 c2000 2007'), 2000, 'First cYEAR' );
+    is( C4::Biblio::_adjust_pubyear('2004 2000 2007'), 2004, 'First year' );
+    is( C4::Biblio::_adjust_pubyear('18xx 1900'), 1900, '1900 has priority over 18xx' );
+    is( C4::Biblio::_adjust_pubyear('18xx'), 1800, '18xx on its own' );
+    is( C4::Biblio::_adjust_pubyear('197X'), 1970, '197X on its own' );
+    is( C4::Biblio::_adjust_pubyear('1...'), 1000, '1... on its own' );
+    is( C4::Biblio::_adjust_pubyear('12?? 13xx'), 1200, '12?? first' );
+    is( C4::Biblio::_adjust_pubyear('12? 1x'), undef, 'Too short return nothing as data must be int' );
+    is( C4::Biblio::_adjust_pubyear('198-'), undef, 'Missing question mark, nothing is returned as data must be int' );
+    is( C4::Biblio::_adjust_pubyear('198-?'), '1980', '198-?' );
+    is( C4::Biblio::_adjust_pubyear('1981-'), '1981', 'Date range returns first date' );
+    is( C4::Biblio::_adjust_pubyear('broken'), undef, 'Non-matchign data returns nothing as the field must be int' );
+};
+
+subtest 'Test repeatable subfields' => sub {
+    plan tests => 5;
+
+    # Make 510x repeatable and 510y not
+    Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '510' })->delete;
+    Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '510', tagsubfield => 'x', kohafield => 'items.test', repeatable => 1 })->store;
+    Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '510', tagsubfield => 'y', kohafield => 'items.norepeat', repeatable => 0 })->store;
+    Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
+
+
+    my $marc = MARC::Record->new;
+    $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)
+    my $result = C4::Biblio::TransformMarcToKoha( $marc );
+    is( $result->{test}, '1 | 2', 'Check 510x for two values' );
+    is( $result->{norepeat}, '3 | 4 | 5', 'Check 510y too' );
+
+    Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '510', tagsubfield => 'a', kohafield => 'biblio.field1' })->store;
+    Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
+    $marc->append_fields( MARC::Field->new( '510', '', '', a => '1' ) ); # actually, we should only have one $y (BZ 24652)
+
+    $result = C4::Biblio::TransformMarcToKoha( $marc, '', 'no_items' );
+    is( $result->{test}, undef, 'Item field not returned when "no_items" passed' );
+    is( $result->{norepeat}, undef, 'Item field not returned when "no_items" passed' );
+    is( $result->{field1}, 1, 'Biblio field returned when "no_items" passed' );
+};
+
 # Cleanup
 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
 $schema->storage->txn_rollback;