Bug 17600: Standardize our EXPORT_OK
[srvgit] / t / db_dependent / Biblio / TransformKohaToMarc.t
old mode 100644 (file)
new mode 100755 (executable)
index 978861f..d2efaa6
@@ -8,18 +8,17 @@ use t::lib::TestBuilder;
 use Koha::Database;
 use Koha::Caches;
 use Koha::MarcSubfieldStructures;
-use C4::Biblio;
+use C4::Biblio qw( TransformKohaToMarc );
 
 my $schema  = Koha::Database->new->schema;
 $schema->storage->txn_begin;
+our $builder = t::lib::TestBuilder->new;
 
 # Create/overwrite some Koha to MARC mappings in default framework
-my $mapping1 = Koha::MarcSubfieldStructures->find('','300','a') // Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '300', tagsubfield => 'a' });
-$mapping1->kohafield( "mytable.nicepages" );
-$mapping1->store;
-my $mapping2 = Koha::MarcSubfieldStructures->find('','300','b') // Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '300', tagsubfield => 'b' });
-$mapping2->kohafield( "mytable2.goodillustrations" );
-$mapping2->store;
+Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '300', tagsubfield => 'a' })->delete;
+Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '300', tagsubfield => 'a', kohafield => "mytable.nicepages" })->store;
+Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '300', tagsubfield => 'b' })->delete;
+Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '300', tagsubfield => 'b', kohafield => "mytable2.goodillustrations", repeatable => 1 })->store;
 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
 
 my $record = C4::Biblio::TransformKohaToMarc({
@@ -42,73 +41,75 @@ is_deeply( \@subfields, [
 
 # Now test multiple mappings per kohafield too
 subtest "Multiple Koha to MARC mappings (BZ 10306)" => sub {
-    plan tests => 4;
+    plan tests => 5;
 
-    # 300a and 260d mapped to mytable.nicepages
-    my $mapping3 = Koha::MarcSubfieldStructures->find('','260','d') // Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '260', tagsubfield => 'd' });
-    $mapping3->kohafield( "mytable.nicepages" );
-    $mapping3->store;
+    # Add 260d mapping so that 300a and 260d both map to mytable.nicepages
+    # Add 260e to test not-repeatable behavior
+    Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '260' })->delete;
+    Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '260', tagsubfield => 'd', kohafield => "mytable.nicepages" })->store;
+    Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '260', tagsubfield => 'e', kohafield => "mytable.unrepeatable", repeatable => 0 })->store;
     Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
 
     # Include two values in goodillustrations too: should result in two
-    # subfields.
+    # subfields. But unrepeatable should result in one field.
     my $record = C4::Biblio::TransformKohaToMarc({
         "mytable2.goodillustrations" => "good | better",
         "mytable.nicepages"          => "nice",
+        "mytable.unrepeatable"       => "A | B",
     });
     is( $record->subfield('260','d'), "nice", "Check 260d" );
+    is( $record->subfield('260','e'), "A | B", "Check 260e" );
     is( $record->subfield('300','a'), "nice", "Check 300a" );
     is( $record->subfield('300','b'), "good", "Check first 300b" );
     is( ($record->field('300')->subfield('b'))[1], "better",
         "Check second 300b" );
 };
 
-subtest "Working with control fields in another framework" => sub {
-    plan tests => 2;
-
-    # Add a new framework
-    my $fw = t::lib::TestBuilder->new->build_object({
-        class => 'Koha::BiblioFrameworks'
-    });
+subtest "Working with control fields" => sub {
+    plan tests => 1;
 
     # Map a controlfield to 'fullcontrol'
-    my $mapping = Koha::MarcSubfieldStructure->new({ frameworkcode => $fw->frameworkcode, tagfield => '001', tagsubfield => '@', kohafield => 'fullcontrol' });
-    $mapping->store;
+    Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '001', tagsubfield => '@' })->delete;
+    Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '001', tagsubfield => '@', kohafield => "fullcontrol" })->store;
+    Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
 
-    # First test in the wrong framework
     my @cols = ( notexist => 'i am not here', fullcontrol => 'all' );
     my $record = C4::Biblio::TransformKohaToMarc( { @cols } );
-    is( $record->field('001'), undef,
-        'With default framework we should not find a 001 controlfield' );
-    # Now include the framework parameter and test 001
-    $record = C4::Biblio::TransformKohaToMarc( { @cols }, $fw->frameworkcode );
-    is( $record->field('001')->data, "all", "Check controlfield 001 with right frameworkcode" );
-
-    # Remove from cache
-    Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-".$fw->frameworkcode );
+    is( $record->field('001')->data, 'all', 'Verify field 001' );
 };
 
-subtest "Add test for no_split option" => sub {
-    plan tests => 4;
+subtest "Add tests for _check_split" => sub {
+    plan tests => 8;
 
-    my $fwc = t::lib::TestBuilder->new->build({ source => 'MarcSubfieldStructure' })->{frameworkcode};
-    Koha::MarcSubfieldStructure->new({ frameworkcode => $fwc, tagfield => '952', tagsubfield => 'a', kohafield => 'items.fld1' })->store;
-    Koha::MarcSubfieldStructure->new({ frameworkcode => $fwc, tagfield => '952', tagsubfield => 'b', kohafield => 'items.fld1' })->store;
-    Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-$fwc" );
+    Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '952', tagsubfield => 'a' })->delete;
+    Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => 'a', kohafield => 'items.fld1' })->store;
+    Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '952', tagsubfield => 'b' })->delete;
+    Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => 'b', kohafield => 'items.fld1' })->store;
+    Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
+    # add 952a repeatable in another framework
+    my $fw =  $builder->build({ source => 'BiblioFramework' })->{frameworkcode};
+    Koha::MarcSubfieldStructure->new({ frameworkcode => $fw, tagfield => '952', tagsubfield => 'a', repeatable => 1, kohafield => 'items.fld1' })->store;
 
     # Test single value in fld1
     my @cols = ( 'items.fld1' => '01' );
-    my $record = C4::Biblio::TransformKohaToMarc( { @cols }, $fwc, { no_split => 1 } );
+    my $record = C4::Biblio::TransformKohaToMarc( { @cols }, { no_split => 1 } );
     is( $record->subfield( '952', 'a' ), '01', 'Check single in 952a' );
     is( $record->subfield( '952', 'b' ), '01', 'Check single in 952b' );
 
-    # Test glued (composite) value in fld1
+    # Test glued (composite) value in fld1 with no_split parameter
     @cols = ( 'items.fld1' => '01 | 02' );
-    $record = C4::Biblio::TransformKohaToMarc( { @cols }, $fwc, { no_split => 1 } );
+    $record = C4::Biblio::TransformKohaToMarc( { @cols }, { no_split => 1 } );
     is( $record->subfield( '952', 'a' ), '01 | 02', 'Check composite in 952a' );
     is( $record->subfield( '952', 'b' ), '01 | 02', 'Check composite in 952b' );
-
-    Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-$fwc" );
+    # Test without no_split (subfield is not repeatable)
+    $record = C4::Biblio::TransformKohaToMarc( { @cols } );
+    is( $record->subfield( '952', 'a' ), '01 | 02', 'Check composite in 952a' );
+    # Test with other framework (repeatable)
+    Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-". $fw );
+    $record =  C4::Biblio::TransformKohaToMarc( { @cols }, { framework => $fw } );
+    is( ($record->subfield( '952', 'a' ))[0], '01', "Framework $fw first 952a" );
+    is( ($record->subfield( '952', 'a' ))[1], '02', "Framework $fw second 952a" );
+    is( ref(Koha::Caches->get_instance->get_from_cache( "MarcSubfieldStructure-". $fw )), 'HASH', 'We did hit the cache' );
 };
 
 # Cleanup