377dbab6a52143aeba0f2d2954c2ab6e8ecb0349
[srvgit] / t / db_dependent / Biblio / TransformKohaToMarc.t
1 use Modern::Perl;
2 use Test::More tests => 4;
3 use MARC::Record;
4
5 use t::lib::Mocks;
6 use t::lib::TestBuilder;
7
8 use Koha::Database;
9 use Koha::Caches;
10 use Koha::MarcSubfieldStructures;
11 use C4::Biblio;
12
13 my $schema  = Koha::Database->new->schema;
14 $schema->storage->txn_begin;
15 our $builder = t::lib::TestBuilder->new;
16
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-" );
23
24 my $record = C4::Biblio::TransformKohaToMarc({
25     "mytable2.goodillustrations"   => "Other physical details", # 300$b
26     "mytable.nicepages"            => "Extent",                 # 300$a
27 });
28 my @subfields = $record->field('300')->subfields();
29 is_deeply( \@subfields, [
30           [
31             'a',
32             'Extent'
33           ],
34           [
35             'b',
36             'Other physical details'
37           ],
38         ],
39 'TransformKohaToMarc should return sorted subfields (regression test for bug 12343)' );
40
41
42 # Now test multiple mappings per kohafield too
43 subtest "Multiple Koha to MARC mappings (BZ 10306)" => sub {
44     plan tests => 5;
45
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-" );
52
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",
59     });
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" );
66 };
67
68 subtest "Working with control fields" => sub {
69     plan tests => 1;
70
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-" );
75
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' );
79 };
80
81 subtest "Add tests for _check_split" => sub {
82     plan tests => 8;
83
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;
92
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' );
98
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' );
113 };
114
115 # Cleanup
116 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
117 $schema->storage->txn_rollback;