2 use Test::More tests => 4;
6 use t::lib::TestBuilder;
10 use Koha::MarcSubfieldStructures;
11 use C4::Biblio qw( TransformKohaToMarc );
13 my $schema = Koha::Database->new->schema;
14 $schema->storage->txn_begin;
15 our $builder = t::lib::TestBuilder->new;
17 # Create/overwrite some Koha to MARC mappings in default framework
18 Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '300', tagsubfield => 'a' })->delete;
19 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '300', tagsubfield => 'a', kohafield => "mytable.nicepages" })->store;
20 Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '300', tagsubfield => 'b' })->delete;
21 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '300', tagsubfield => 'b', kohafield => "mytable2.goodillustrations", repeatable => 1 })->store;
22 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
24 my $record = C4::Biblio::TransformKohaToMarc({
25 "mytable2.goodillustrations" => "Other physical details", # 300$b
26 "mytable.nicepages" => "Extent", # 300$a
28 my @subfields = $record->field('300')->subfields();
29 is_deeply( \@subfields, [
36 'Other physical details'
39 'TransformKohaToMarc should return sorted subfields (regression test for bug 12343)' );
42 # Now test multiple mappings per kohafield too
43 subtest "Multiple Koha to MARC mappings (BZ 10306)" => sub {
46 # Add 260d mapping so that 300a and 260d both map to mytable.nicepages
47 # Add 260e to test not-repeatable behavior
48 Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '260' })->delete;
49 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '260', tagsubfield => 'd', kohafield => "mytable.nicepages" })->store;
50 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '260', tagsubfield => 'e', kohafield => "mytable.unrepeatable", repeatable => 0 })->store;
51 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
53 # Include two values in goodillustrations too: should result in two
54 # subfields. But unrepeatable should result in one field.
55 my $record = C4::Biblio::TransformKohaToMarc({
56 "mytable2.goodillustrations" => "good | better",
57 "mytable.nicepages" => "nice",
58 "mytable.unrepeatable" => "A | B",
60 is( $record->subfield('260','d'), "nice", "Check 260d" );
61 is( $record->subfield('260','e'), "A | B", "Check 260e" );
62 is( $record->subfield('300','a'), "nice", "Check 300a" );
63 is( $record->subfield('300','b'), "good", "Check first 300b" );
64 is( ($record->field('300')->subfield('b'))[1], "better",
65 "Check second 300b" );
68 subtest "Working with control fields" => sub {
71 # Map a controlfield to 'fullcontrol'
72 Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '001', tagsubfield => '@' })->delete;
73 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '001', tagsubfield => '@', kohafield => "fullcontrol" })->store;
74 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
76 my @cols = ( notexist => 'i am not here', fullcontrol => 'all' );
77 my $record = C4::Biblio::TransformKohaToMarc( { @cols } );
78 is( $record->field('001')->data, 'all', 'Verify field 001' );
81 subtest "Add tests for _check_split" => sub {
84 Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '952', tagsubfield => 'a' })->delete;
85 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => 'a', kohafield => 'items.fld1' })->store;
86 Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '952', tagsubfield => 'b' })->delete;
87 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => 'b', kohafield => 'items.fld1' })->store;
88 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
89 # add 952a repeatable in another framework
90 my $fw = $builder->build({ source => 'BiblioFramework' })->{frameworkcode};
91 Koha::MarcSubfieldStructure->new({ frameworkcode => $fw, tagfield => '952', tagsubfield => 'a', repeatable => 1, kohafield => 'items.fld1' })->store;
93 # Test single value in fld1
94 my @cols = ( 'items.fld1' => '01' );
95 my $record = C4::Biblio::TransformKohaToMarc( { @cols }, { no_split => 1 } );
96 is( $record->subfield( '952', 'a' ), '01', 'Check single in 952a' );
97 is( $record->subfield( '952', 'b' ), '01', 'Check single in 952b' );
99 # Test glued (composite) value in fld1 with no_split parameter
100 @cols = ( 'items.fld1' => '01 | 02' );
101 $record = C4::Biblio::TransformKohaToMarc( { @cols }, { no_split => 1 } );
102 is( $record->subfield( '952', 'a' ), '01 | 02', 'Check composite in 952a' );
103 is( $record->subfield( '952', 'b' ), '01 | 02', 'Check composite in 952b' );
104 # Test without no_split (subfield is not repeatable)
105 $record = C4::Biblio::TransformKohaToMarc( { @cols } );
106 is( $record->subfield( '952', 'a' ), '01 | 02', 'Check composite in 952a' );
107 # Test with other framework (repeatable)
108 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-". $fw );
109 $record = C4::Biblio::TransformKohaToMarc( { @cols }, { framework => $fw } );
110 is( ($record->subfield( '952', 'a' ))[0], '01', "Framework $fw first 952a" );
111 is( ($record->subfield( '952', 'a' ))[1], '02', "Framework $fw second 952a" );
112 is( ref(Koha::Caches->get_instance->get_from_cache( "MarcSubfieldStructure-". $fw )), 'HASH', 'We did hit the cache' );
116 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
117 $schema->storage->txn_rollback;