Koha/t/db_dependent/Biblio/TransformKohaToMarc.t
Jonathan Druart 9d6d641d1f Bug 17600: Standardize our EXPORT_OK
On bug 17591 we discovered that there was something weird going on with
the way we export and use subroutines/modules.
This patch tries to standardize our EXPORT to use EXPORT_OK only.

That way we will need to explicitely define the subroutine we want to
use from a module.

This patch is a squashed version of:
Bug 17600: After export.pl
Bug 17600: After perlimport
Bug 17600: Manual changes
Bug 17600: Other manual changes after second perlimports run
Bug 17600: Fix tests

And a lot of other manual changes.

export.pl is a dirty script that can be found on bug 17600.

"perlimport" is:
git clone https://github.com/oalders/App-perlimports.git
cd App-perlimports/
cpanm --installdeps .
export PERL5LIB="$PERL5LIB:/kohadevbox/koha/App-perlimports/lib"
find . \( -name "*.pl" -o -name "*.pm" \) -exec perl App-perlimports/script/perlimports --inplace-edit --no-preserve-unused --filename {} \;

The ideas of this patch are to:
* use EXPORT_OK instead of EXPORT
* perltidy the EXPORT_OK list
* remove '&' before the subroutine names
* remove some uneeded use statements
* explicitely import the subroutines we need within the controllers or
modules

Note that the private subroutines (starting with _) should not be
exported (and not used from outside of the module except from tests).

EXPORT vs EXPORT_OK (from
https://www.thegeekstuff.com/2010/06/perl-exporter-examples/)
"""
Export allows to export the functions and variables of modules to user’s namespace using the standard import method. This way, we don’t need to create the objects for the modules to access it’s members.

@EXPORT and @EXPORT_OK are the two main variables used during export operation.

@EXPORT contains list of symbols (subroutines and variables) of the module to be exported into the caller namespace.

@EXPORT_OK does export of symbols on demand basis.
"""

If this patch caused a conflict with a patch you wrote prior to its
push:
* Make sure you are not reintroducing a "use" statement that has been
removed
* "$subroutine" is not exported by the C4::$MODULE module
means that you need to add the subroutine to the @EXPORT_OK list
* Bareword "$subroutine" not allowed while "strict subs"
means that you didn't imported the subroutine from the module:
  - use $MODULE qw( $subroutine list );
You can also use the fully qualified namespace: C4::$MODULE::$subroutine

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
2021-07-16 08:58:47 +02:00

117 lines
5.9 KiB
Perl
Executable file

use Modern::Perl;
use Test::More tests => 4;
use MARC::Record;
use t::lib::Mocks;
use t::lib::TestBuilder;
use Koha::Database;
use Koha::Caches;
use Koha::MarcSubfieldStructures;
use C4::Biblio qw( TransformKohaToMarc );
my $schema = Koha::Database->new->schema;
$schema->storage->txn_begin;
our $builder = t::lib::TestBuilder->new;
# Create/overwrite some Koha to MARC mappings in default framework
Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '300', tagsubfield => 'a' })->delete;
Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '300', tagsubfield => 'a', kohafield => "mytable.nicepages" })->store;
Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '300', tagsubfield => 'b' })->delete;
Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '300', tagsubfield => 'b', kohafield => "mytable2.goodillustrations", repeatable => 1 })->store;
Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
my $record = C4::Biblio::TransformKohaToMarc({
"mytable2.goodillustrations" => "Other physical details", # 300$b
"mytable.nicepages" => "Extent", # 300$a
});
my @subfields = $record->field('300')->subfields();
is_deeply( \@subfields, [
[
'a',
'Extent'
],
[
'b',
'Other physical details'
],
],
'TransformKohaToMarc should return sorted subfields (regression test for bug 12343)' );
# Now test multiple mappings per kohafield too
subtest "Multiple Koha to MARC mappings (BZ 10306)" => sub {
plan tests => 5;
# Add 260d mapping so that 300a and 260d both map to mytable.nicepages
# Add 260e to test not-repeatable behavior
Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '260' })->delete;
Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '260', tagsubfield => 'd', kohafield => "mytable.nicepages" })->store;
Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '260', tagsubfield => 'e', kohafield => "mytable.unrepeatable", repeatable => 0 })->store;
Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
# Include two values in goodillustrations too: should result in two
# subfields. But unrepeatable should result in one field.
my $record = C4::Biblio::TransformKohaToMarc({
"mytable2.goodillustrations" => "good | better",
"mytable.nicepages" => "nice",
"mytable.unrepeatable" => "A | B",
});
is( $record->subfield('260','d'), "nice", "Check 260d" );
is( $record->subfield('260','e'), "A | B", "Check 260e" );
is( $record->subfield('300','a'), "nice", "Check 300a" );
is( $record->subfield('300','b'), "good", "Check first 300b" );
is( ($record->field('300')->subfield('b'))[1], "better",
"Check second 300b" );
};
subtest "Working with control fields" => sub {
plan tests => 1;
# Map a controlfield to 'fullcontrol'
Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '001', tagsubfield => '@' })->delete;
Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '001', tagsubfield => '@', kohafield => "fullcontrol" })->store;
Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
my @cols = ( notexist => 'i am not here', fullcontrol => 'all' );
my $record = C4::Biblio::TransformKohaToMarc( { @cols } );
is( $record->field('001')->data, 'all', 'Verify field 001' );
};
subtest "Add tests for _check_split" => sub {
plan tests => 8;
Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '952', tagsubfield => 'a' })->delete;
Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => 'a', kohafield => 'items.fld1' })->store;
Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '952', tagsubfield => 'b' })->delete;
Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => 'b', kohafield => 'items.fld1' })->store;
Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
# add 952a repeatable in another framework
my $fw = $builder->build({ source => 'BiblioFramework' })->{frameworkcode};
Koha::MarcSubfieldStructure->new({ frameworkcode => $fw, tagfield => '952', tagsubfield => 'a', repeatable => 1, kohafield => 'items.fld1' })->store;
# Test single value in fld1
my @cols = ( 'items.fld1' => '01' );
my $record = C4::Biblio::TransformKohaToMarc( { @cols }, { no_split => 1 } );
is( $record->subfield( '952', 'a' ), '01', 'Check single in 952a' );
is( $record->subfield( '952', 'b' ), '01', 'Check single in 952b' );
# Test glued (composite) value in fld1 with no_split parameter
@cols = ( 'items.fld1' => '01 | 02' );
$record = C4::Biblio::TransformKohaToMarc( { @cols }, { no_split => 1 } );
is( $record->subfield( '952', 'a' ), '01 | 02', 'Check composite in 952a' );
is( $record->subfield( '952', 'b' ), '01 | 02', 'Check composite in 952b' );
# Test without no_split (subfield is not repeatable)
$record = C4::Biblio::TransformKohaToMarc( { @cols } );
is( $record->subfield( '952', 'a' ), '01 | 02', 'Check composite in 952a' );
# Test with other framework (repeatable)
Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-". $fw );
$record = C4::Biblio::TransformKohaToMarc( { @cols }, { framework => $fw } );
is( ($record->subfield( '952', 'a' ))[0], '01', "Framework $fw first 952a" );
is( ($record->subfield( '952', 'a' ))[1], '02', "Framework $fw second 952a" );
is( ref(Koha::Caches->get_instance->get_from_cache( "MarcSubfieldStructure-". $fw )), 'HASH', 'We did hit the cache' );
};
# Cleanup
Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
$schema->storage->txn_rollback;