Lines 33-47
use C4::Biblio;
Link Here
|
33 |
my $schema = Koha::Database->new->schema; |
33 |
my $schema = Koha::Database->new->schema; |
34 |
$schema->storage->txn_begin; |
34 |
$schema->storage->txn_begin; |
35 |
|
35 |
|
36 |
# Create a new framework with a few mappings |
36 |
# Create a few mappings |
37 |
# Note: TransformMarcToKoha wants a table name (biblio, biblioitems or items) |
37 |
# Note: TransformMarcToKoha wants a table name (biblio, biblioitems or items) |
38 |
our $fwc = t::lib::TestBuilder->new->build_object({ class => 'Koha::BiblioFrameworks' })->frameworkcode; |
38 |
Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => [ '300', '500' ] })->delete; |
39 |
Koha::MarcSubfieldStructure->new({ frameworkcode => $fwc, tagfield => '300', tagsubfield => 'a', kohafield => 'biblio.field1' })->store; |
39 |
Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '300', tagsubfield => 'a', kohafield => 'biblio.field1' })->store; |
40 |
Koha::MarcSubfieldStructure->new({ frameworkcode => $fwc, tagfield => '300', tagsubfield => 'b', kohafield => 'biblio.field2' })->store; |
40 |
Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '300', tagsubfield => 'b', kohafield => 'biblio.field2' })->store; |
41 |
Koha::MarcSubfieldStructure->new({ frameworkcode => $fwc, tagfield => '500', tagsubfield => 'a', kohafield => 'biblio.field3' })->store; |
41 |
Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '500', tagsubfield => 'a', kohafield => 'biblio.field3' })->store; |
|
|
42 |
Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" ); |
42 |
|
43 |
|
43 |
subtest 'Test a few mappings' => sub { |
44 |
subtest 'Test a few mappings' => sub { |
44 |
plan tests => 6; |
45 |
plan tests => 7; |
45 |
|
46 |
|
46 |
my $marc = MARC::Record->new; |
47 |
my $marc = MARC::Record->new; |
47 |
$marc->append_fields( |
48 |
$marc->append_fields( |
Lines 49-89
subtest 'Test a few mappings' => sub {
Link Here
|
49 |
MARC::Field->new( '300', '', '', a => 'a2', b => 'b2' ), |
50 |
MARC::Field->new( '300', '', '', a => 'a2', b => 'b2' ), |
50 |
MARC::Field->new( '500', '', '', a => 'note1', a => 'note2' ), |
51 |
MARC::Field->new( '500', '', '', a => 'note1', a => 'note2' ), |
51 |
); |
52 |
); |
52 |
my $result = C4::Biblio::TransformMarcToKoha( $marc, $fwc ); |
53 |
my $result = C4::Biblio::TransformMarcToKoha( $marc ); |
53 |
# Note: TransformMarcToKoha stripped the table prefix biblio. |
54 |
# Note: TransformMarcToKoha stripped the table prefix biblio. |
54 |
is( keys %{$result}, 3, 'Found all three mappings' ); |
55 |
is( keys %{$result}, 3, 'Found all three mappings' ); |
55 |
is( $result->{field1}, 'a1 | a2', 'Check field1 results' ); |
56 |
is( $result->{field1}, 'a1 | a2', 'Check field1 results' ); |
56 |
is( $result->{field2}, 'b1 | b2', 'Check field2 results' ); |
57 |
is( $result->{field2}, 'b1 | b2', 'Check field2 results' ); |
57 |
is( $result->{field3}, 'note1 | note2', 'Check field3 results' ); |
58 |
is( $result->{field3}, 'note1 | note2', 'Check field3 results' ); |
58 |
|
59 |
|
59 |
is( C4::Biblio::TransformMarcToKohaOneField( 'biblio.field1', $marc, $fwc ), |
60 |
is( C4::Biblio::TransformMarcToKohaOneField( 'biblio.field1', $marc ), |
60 |
$result->{field1}, 'TransformMarcToKohaOneField returns biblio.field1'); |
61 |
$result->{field1}, 'TransformMarcToKohaOneField returns biblio.field1'); |
61 |
is( C4::Biblio::TransformMarcToKohaOneField( 'field4', $marc, $fwc ), |
62 |
is( C4::Biblio::TransformMarcToKohaOneField( 'field4', $marc ), |
62 |
undef, 'TransformMarcToKohaOneField returns undef' ); |
63 |
undef, 'TransformMarcToKohaOneField returns undef' ); |
|
|
64 |
|
65 |
# Bug 19096 Default is authoritative now |
66 |
# Test passing another framework |
67 |
# CAUTION: This parameter of TransformMarcToKoha will be removed later |
68 |
my $new_fw = t::lib::TestBuilder->new->build({source => 'BiblioFramework'}); |
69 |
$result = C4::Biblio::TransformMarcToKoha($marc, $new_fw->{frameworkcode}); |
70 |
is( keys %{$result}, 3, 'Still found all three mappings' ); |
63 |
}; |
71 |
}; |
64 |
|
72 |
|
65 |
subtest 'Multiple mappings for one kohafield' => sub { |
73 |
subtest 'Multiple mappings for one kohafield' => sub { |
66 |
plan tests => 4; |
74 |
plan tests => 4; |
67 |
|
75 |
|
68 |
# Add another mapping to field1 |
76 |
# Add another mapping to field1 |
69 |
Koha::MarcSubfieldStructure->new({ frameworkcode => $fwc, tagfield => '510', tagsubfield => 'a', kohafield => 'biblio.field1' })->store; |
77 |
Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '510' })->delete; |
70 |
Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-$fwc" ); |
78 |
Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '510', tagsubfield => 'a', kohafield => 'biblio.field1' })->store; |
|
|
79 |
Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" ); |
71 |
|
80 |
|
72 |
my $marc = MARC::Record->new; |
81 |
my $marc = MARC::Record->new; |
73 |
$marc->append_fields( MARC::Field->new( '300', '', '', a => '3a' ) ); |
82 |
$marc->append_fields( MARC::Field->new( '300', '', '', a => '3a' ) ); |
74 |
my $result = C4::Biblio::TransformMarcToKoha( $marc, $fwc ); |
83 |
my $result = C4::Biblio::TransformMarcToKoha( $marc ); |
75 |
is_deeply( $result, { field1 => '3a' }, 'Simple start' ); |
84 |
is_deeply( $result, { field1 => '3a' }, 'Simple start' ); |
76 |
$marc->append_fields( MARC::Field->new( '510', '', '', a => '' ) ); |
85 |
$marc->append_fields( MARC::Field->new( '510', '', '', a => '' ) ); |
77 |
$result = C4::Biblio::TransformMarcToKoha( $marc, $fwc ); |
86 |
$result = C4::Biblio::TransformMarcToKoha( $marc ); |
78 |
is_deeply( $result, { field1 => '3a' }, 'An empty 510a makes no difference' ); |
87 |
is_deeply( $result, { field1 => '3a' }, 'An empty 510a makes no difference' ); |
79 |
$marc->append_fields( MARC::Field->new( '510', '', '', a => '51' ) ); |
88 |
$marc->append_fields( MARC::Field->new( '510', '', '', a => '51' ) ); |
80 |
$result = C4::Biblio::TransformMarcToKoha( $marc, $fwc ); |
89 |
$result = C4::Biblio::TransformMarcToKoha( $marc ); |
81 |
is_deeply( $result, { field1 => '3a | 51' }, 'Got 300a and 510a' ); |
90 |
is_deeply( $result, { field1 => '3a | 51' }, 'Got 300a and 510a' ); |
82 |
|
91 |
|
83 |
is( C4::Biblio::TransformMarcToKohaOneField( 'biblio.field1', $marc, $fwc ), |
92 |
is( C4::Biblio::TransformMarcToKohaOneField( 'biblio.field1', $marc ), |
84 |
'3a | 51', 'TransformMarcToKohaOneField returns biblio.field1' ); |
93 |
'3a | 51', 'TransformMarcToKohaOneField returns biblio.field1' ); |
85 |
}; |
94 |
}; |
86 |
|
95 |
|
87 |
# Cleanup |
96 |
# Cleanup |
88 |
Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-$fwc" ); |
97 |
Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" ); |
89 |
$schema->storage->txn_rollback; |
98 |
$schema->storage->txn_rollback; |