Bug 10306: (QA follow-up) Correct a TestBuilder call
[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 my $mapping1 = Koha::MarcSubfieldStructures->find('','300','a') // Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '300', tagsubfield => 'a' });
18 $mapping1->kohafield( "mytable.nicepages" );
19 $mapping1->store;
20 my $mapping2 = Koha::MarcSubfieldStructures->find('','300','b') // Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '300', tagsubfield => 'b' });
21 $mapping2->kohafield( "mytable2.goodillustrations" );
22 $mapping2->store;
23 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
24
25 my $record = C4::Biblio::TransformKohaToMarc({
26     "mytable2.goodillustrations"   => "Other physical details", # 300$b
27     "mytable.nicepages"            => "Extent",                 # 300$a
28 });
29 my @subfields = $record->field('300')->subfields();
30 is_deeply( \@subfields, [
31           [
32             'a',
33             'Extent'
34           ],
35           [
36             'b',
37             'Other physical details'
38           ],
39         ],
40 'TransformKohaToMarc should return sorted subfields (regression test for bug 12343)' );
41
42
43 # Now test multiple mappings per kohafield too
44 subtest "Multiple Koha to MARC mappings (BZ 10306)" => sub {
45     plan tests => 4;
46
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" );
50     $mapping3->store;
51     Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
52
53     # Include two values in goodillustrations too: should result in two
54     # subfields.
55     my $record = C4::Biblio::TransformKohaToMarc({
56         "mytable2.goodillustrations" => "good | better",
57         "mytable.nicepages"          => "nice",
58     });
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" );
64 };
65
66 subtest "Working with control fields in another framework" => sub {
67     plan tests => 2;
68
69     # Add a new framework
70     my $fw = t::lib::TestBuilder->new->build_object({
71         class => 'Koha::BiblioFrameworks'
72     });
73
74     # Map a controlfield to 'fullcontrol'
75     my $mapping = Koha::MarcSubfieldStructure->new({ frameworkcode => $fw->frameworkcode, tagfield => '001', tagsubfield => '@', kohafield => 'fullcontrol' });
76     $mapping->store;
77
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" );
86
87     # Remove from cache
88     Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-".$fw->frameworkcode );
89 };
90
91 subtest "Add test for no_split option" => sub {
92     plan tests => 4;
93
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" );
98
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' );
104
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' );
110
111     Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-$fwc" );
112 };
113
114 # Cleanup
115 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
116 $schema->storage->txn_rollback;