Bug 21832: add unit test
[koha.git] / 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
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-" );
22
23 my $record = C4::Biblio::TransformKohaToMarc({
24     "mytable2.goodillustrations"   => "Other physical details", # 300$b
25     "mytable.nicepages"            => "Extent",                 # 300$a
26 });
27 my @subfields = $record->field('300')->subfields();
28 is_deeply( \@subfields, [
29           [
30             'a',
31             'Extent'
32           ],
33           [
34             'b',
35             'Other physical details'
36           ],
37         ],
38 'TransformKohaToMarc should return sorted subfields (regression test for bug 12343)' );
39
40
41 # Now test multiple mappings per kohafield too
42 subtest "Multiple Koha to MARC mappings (BZ 10306)" => sub {
43     plan tests => 4;
44
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-" );
49
50     # Include two values in goodillustrations too: should result in two
51     # subfields.
52     my $record = C4::Biblio::TransformKohaToMarc({
53         "mytable2.goodillustrations" => "good | better",
54         "mytable.nicepages"          => "nice",
55     });
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" );
61 };
62
63 subtest "Working with control fields" => sub {
64     plan tests => 1;
65
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-" );
70
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' );
74 };
75
76 subtest "Add test for no_split option" => sub {
77     plan tests => 4;
78
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-" );
84
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' );
90
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' );
96 };
97
98 # Cleanup
99 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
100 $schema->storage->txn_rollback;