2 use Test::More tests => 4;
6 use t::lib::TestBuilder;
10 use Koha::MarcSubfieldStructures;
13 my $schema = Koha::Database->new->schema;
14 $schema->storage->txn_begin;
16 # Create/overwrite some Koha to MARC mappings in default framework
17 Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '300', tagsubfield => 'a' })->delete;
18 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '300', tagsubfield => 'a', kohafield => "mytable.nicepages" })->store;
19 Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '300', tagsubfield => 'b' })->delete;
20 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '300', tagsubfield => 'b', kohafield => "mytable2.goodillustrations" })->store;
21 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
23 my $record = C4::Biblio::TransformKohaToMarc({
24 "mytable2.goodillustrations" => "Other physical details", # 300$b
25 "mytable.nicepages" => "Extent", # 300$a
27 my @subfields = $record->field('300')->subfields();
28 is_deeply( \@subfields, [
35 'Other physical details'
38 'TransformKohaToMarc should return sorted subfields (regression test for bug 12343)' );
41 # Now test multiple mappings per kohafield too
42 subtest "Multiple Koha to MARC mappings (BZ 10306)" => sub {
45 # Add260d mapping so that 300a and 260d both map to mytable.nicepages
46 Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '260', tagsubfield => 'd' })->delete;
47 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '260', tagsubfield => 'd', kohafield => "mytable.nicepages" })->store;
48 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
50 # Include two values in goodillustrations too: should result in two
52 my $record = C4::Biblio::TransformKohaToMarc({
53 "mytable2.goodillustrations" => "good | better",
54 "mytable.nicepages" => "nice",
56 is( $record->subfield('260','d'), "nice", "Check 260d" );
57 is( $record->subfield('300','a'), "nice", "Check 300a" );
58 is( $record->subfield('300','b'), "good", "Check first 300b" );
59 is( ($record->field('300')->subfield('b'))[1], "better",
60 "Check second 300b" );
63 subtest "Working with control fields" => sub {
66 # Map a controlfield to 'fullcontrol'
67 Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '001', tagsubfield => '@' })->delete;
68 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '001', tagsubfield => '@', kohafield => "fullcontrol" })->store;
69 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
71 my @cols = ( notexist => 'i am not here', fullcontrol => 'all' );
72 my $record = C4::Biblio::TransformKohaToMarc( { @cols } );
73 is( $record->field('001')->data, 'all', 'Verify field 001' );
76 subtest "Add test for no_split option" => sub {
79 Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '952', tagsubfield => 'a' })->delete;
80 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => 'a', kohafield => 'items.fld1' })->store;
81 Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '952', tagsubfield => 'b' })->delete;
82 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => 'b', kohafield => 'items.fld1' })->store;
83 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
85 # Test single value in fld1
86 my @cols = ( 'items.fld1' => '01' );
87 my $record = C4::Biblio::TransformKohaToMarc( { @cols }, { no_split => 1 } );
88 is( $record->subfield( '952', 'a' ), '01', 'Check single in 952a' );
89 is( $record->subfield( '952', 'b' ), '01', 'Check single in 952b' );
91 # Test glued (composite) value in fld1
92 @cols = ( 'items.fld1' => '01 | 02' );
93 $record = C4::Biblio::TransformKohaToMarc( { @cols }, { no_split => 1 } );
94 is( $record->subfield( '952', 'a' ), '01 | 02', 'Check composite in 952a' );
95 is( $record->subfield( '952', 'b' ), '01 | 02', 'Check composite in 952b' );
99 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
100 $schema->storage->txn_rollback;