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 my $mapping1 = Koha::MarcSubfieldStructures->find('','300','a') // Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '300', tagsubfield => 'a' });
18 $mapping1->kohafield( "mytable.nicepages" );
20 my $mapping2 = Koha::MarcSubfieldStructures->find('','300','b') // Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '300', tagsubfield => 'b' });
21 $mapping2->kohafield( "mytable2.goodillustrations" );
23 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
25 my $record = C4::Biblio::TransformKohaToMarc({
26 "mytable2.goodillustrations" => "Other physical details", # 300$b
27 "mytable.nicepages" => "Extent", # 300$a
29 my @subfields = $record->field('300')->subfields();
30 is_deeply( \@subfields, [
37 'Other physical details'
40 'TransformKohaToMarc should return sorted subfields (regression test for bug 12343)' );
43 # Now test multiple mappings per kohafield too
44 subtest "Multiple Koha to MARC mappings (BZ 10306)" => sub {
47 # 300a and 260d mapped to mytable.nicepages
48 my $mapping3 = Koha::MarcSubfieldStructures->find('','260','d') // Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '260', tagsubfield => 'd' });
49 $mapping3->kohafield( "mytable.nicepages" );
51 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
53 # Include two values in goodillustrations too: should result in two
55 my $record = C4::Biblio::TransformKohaToMarc({
56 "mytable2.goodillustrations" => "good | better",
57 "mytable.nicepages" => "nice",
59 is( $record->subfield('260','d'), "nice", "Check 260d" );
60 is( $record->subfield('300','a'), "nice", "Check 300a" );
61 is( $record->subfield('300','b'), "good", "Check first 300b" );
62 is( ($record->field('300')->subfield('b'))[1], "better",
63 "Check second 300b" );
66 subtest "Working with control fields in another framework" => sub {
70 my $fw = t::lib::TestBuilder->new->build_object({
71 class => 'Koha::BiblioFrameworks'
74 # Map a controlfield to 'fullcontrol'
75 my $mapping = Koha::MarcSubfieldStructure->new({ frameworkcode => $fw->frameworkcode, tagfield => '001', tagsubfield => '@', kohafield => 'fullcontrol' });
78 # First test in the wrong framework
79 my @cols = ( notexist => 'i am not here', fullcontrol => 'all' );
80 my $record = C4::Biblio::TransformKohaToMarc( { @cols } );
81 is( $record->field('001'), undef,
82 'With default framework we should not find a 001 controlfield' );
83 # Now include the framework parameter and test 001
84 $record = C4::Biblio::TransformKohaToMarc( { @cols }, $fw->frameworkcode );
85 is( $record->field('001')->data, "all", "Check controlfield 001 with right frameworkcode" );
88 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-".$fw->frameworkcode );
91 subtest "Add test for no_split option" => sub {
94 my $fwc = t::lib::TestBuilder->new->build({ source => 'BiblioFramework' })->{frameworkcode};
95 Koha::MarcSubfieldStructure->new({ frameworkcode => $fwc, tagfield => '952', tagsubfield => 'a', kohafield => 'items.fld1' })->store;
96 Koha::MarcSubfieldStructure->new({ frameworkcode => $fwc, tagfield => '952', tagsubfield => 'b', kohafield => 'items.fld1' })->store;
97 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-$fwc" );
99 # Test single value in fld1
100 my @cols = ( 'items.fld1' => '01' );
101 my $record = C4::Biblio::TransformKohaToMarc( { @cols }, $fwc, { no_split => 1 } );
102 is( $record->subfield( '952', 'a' ), '01', 'Check single in 952a' );
103 is( $record->subfield( '952', 'b' ), '01', 'Check single in 952b' );
105 # Test glued (composite) value in fld1
106 @cols = ( 'items.fld1' => '01 | 02' );
107 $record = C4::Biblio::TransformKohaToMarc( { @cols }, $fwc, { no_split => 1 } );
108 is( $record->subfield( '952', 'a' ), '01 | 02', 'Check composite in 952a' );
109 is( $record->subfield( '952', 'b' ), '01 | 02', 'Check composite in 952b' );
111 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-$fwc" );
115 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
116 $schema->storage->txn_rollback;