Koha/t/db_dependent/Biblio/TransformHtmlToMarc.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

132 lines
8.9 KiB
Perl
Executable file

#!/usr/bin/perl
use Modern::Perl;
use CGI;
use Encode qw( encode );
use Test::More tests => 2;
use Koha::Caches;
use Koha::Database;
use Koha::MarcSubfieldStructures;
use C4::Biblio qw( GetMarcFromKohaField TransformHtmlToMarc );
our ( $biblionumbertagfield, $biblionumbertagsubfield );
my $schema = Koha::Database->new->schema;
$schema->storage->txn_begin;
# Move field for biblionumber to imaginary 399
Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'biblio.biblionumber' })->delete;
Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '399', tagsubfield => 'a' })->delete;
Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '399', tagsubfield => 'a', kohafield => "biblio.biblionumber" })->store;
Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
( $biblionumbertagfield, $biblionumbertagsubfield ) = C4::Biblio::GetMarcFromKohaField( "biblio.biblionumber" );
subtest 'Biblio record' => sub {
plan tests => 17;
my $leader = '00203nam a2200097 4500';
my $input = CGI->new;
$input->param( -name => 'biblionumber', -value => '42' );
$input->param( -name => 'tag_000_indicator1_570367553534', -value => '' );
$input->param( -name => 'tag_000_indicator2_570367553534', -value => '' );
$input->param( -name => 'tag_000_code_00_570367_810561', -value => '' );
$input->param( -name => 'tag_000_subfield_00_570367_810561', -value => $leader );
$input->param( -name => 'tag_010_indicator1_493056', -value => '' );
$input->param( -name => 'tag_010_indicator2_493056', -value => '' );
$input->param( -name => 'tag_010_code_a_493056_296409', -value => 'a' );
$input->param( -name => 'tag_010_subfield_a_493056_296409', -value => Encode::encode( 'utf-8', "first isbn é" ) );
$input->param( -name => 'tag_010_indicator1_49305613979', -value => '' );
$input->param( -name => 'tag_010_indicator2_49305613979', -value => '' );
$input->param( -name => 'tag_010_code_a_493056_29640913979', -value => 'a' );
$input->param( -name => 'tag_010_subfield_a_493056_29640913979', -value => Encode::encode( 'utf-8', "second isbn à" ) ); # 2 010 fields
$input->param( -name => 'tag_100_indicator1_588794844868', -value => '' );
$input->param( -name => 'tag_100_indicator2_588794844868', -value => '' );
$input->param( -name => 'tag_100_code_a_588794_15537', -value => 'a' );
$input->param( -name => 'tag_100_subfield_a_588794_15537', -value => '20160112d u||y0frey5050 ba' );
$input->param( -name => 'tag_200_indicator1_593269251146', -value => '' );
$input->param( -name => 'tag_200_indicator2_593269251146', -value => '' );
$input->param( -name => 'tag_200_code_a_593269_944056', -value => 'a' );
$input->param( -name => 'tag_200_subfield_a_593269_944056', -value => 'first title' ); # 2 200$a in the same field
$input->param( -name => 'tag_200_code_a_593269_94405618065', -value => 'a' );
$input->param( -name => 'tag_200_subfield_a_593269_94405618065', -value => 'second title' );
$input->param( -name => 'tag_200_code_b_593269_250538', -value => 'b' );
$input->param( -name => 'tag_200_subfield_b_593269_250538', -value => 'DVD' );
$input->param( -name => 'tag_200_code_f_593269_445603', -value => 'f' );
$input->param( -name => 'tag_200_subfield_f_593269_445603', -value => 'author' );
$input->param( -name => 'tag_200_code_h_593269_616594', -value => 'h' ); # Empty field
$input->param( -name => 'tag_200_subfield_h_593269_616594', -value => '' );
# Add a field 390 before our 399
$input->param( -name => "tag_390_indicator1_123", -value => "" );
$input->param( -name => "tag_390_indicator2_123", -value => "" );
$input->param( -name => "tag_390_code_a_123", -value => 'a' );
$input->param( -name => "tag_390_subfield_a_123", -value => '390a' );
# Our imaginary biblionumber field in 399
$input->param( -name => "tag_${biblionumbertagfield}_indicator1_588794844868", -value => "" );
$input->param( -name => "tag_${biblionumbertagfield}_indicator2_588794844868", -value => "" );
$input->param( -name => "tag_${biblionumbertagfield}_code_${biblionumbertagsubfield}_588794_784323", -value => $biblionumbertagsubfield );
$input->param( -name => "tag_${biblionumbertagfield}_subfield_${biblionumbertagsubfield}_588794_784323", -value => $biblionumbertagfield );
# A field (490) after 399
$input->param( -name => "tag_490_indicator1_1123", -value => "" );
$input->param( -name => "tag_490_indicator2_1123", -value => "" );
$input->param( -name => "tag_490_code_b_1123", -value => 'b' );
$input->param( -name => "tag_490_subfield_b_1123", -value => '490b' );
my $record = C4::Biblio::TransformHtmlToMarc($input, 1);
my @all_fields = $record->fields;
is( @all_fields, 7, 'The record should have been created with 7 fields' );
# biblionumber + 2x010 + 100 + 200 + 390 + 490
my @fields_010 = $record->field('010');
is( @fields_010, 2, 'The record should have been created with 2 010' );
my @fields_100 = $record->field('100');
is( @fields_100, 1, 'The record should have been created with 1 100' );
my @fields_200 = $record->field('200');
is( @fields_200, 1, 'The record should have been created with 1 200' );
is_deeply( $fields_010[0]->subfields(), [ 'a', 'first isbn é' ], 'The first isbn should be correct' );
is_deeply( $fields_010[1]->subfields(), [ 'a', 'second isbn à' ], 'The second isbn should be correct' );
my @subfields_200_a = $record->subfield( 200, 'a' );
is( @subfields_200_a, 2, 'The record should have been created with 2 200$a' );
is_deeply( \@subfields_200_a, [ 'first title', 'second title' ], 'The 2 titles should have been kept in the correct order' );
my @subfields_biblionumber = $record->subfield( $biblionumbertagfield, $biblionumbertagsubfield );
is( @subfields_biblionumber, 1, 'The record should contain only one biblionumber field' );
is( $record->leader, $leader, 'The leader should have been kept' );
# Check the order of some fields
is( $all_fields[0]->tag, '010', 'First field expected 010' );
is( $all_fields[1]->tag, '010', 'Second field also 010' );
is( $all_fields[2]->tag, '100', 'Third field is 100' );
is( $all_fields[3]->tag, '200', 'Fourth field is 200' );
is( $all_fields[4]->tag, '390', 'Fifth field is 390' );
is( $all_fields[5]->subfield('a'), 42, 'Sixth field contains bibnumber' );
is( $all_fields[6]->tag, '490', 'Last field is 490' );
};
subtest 'Add authority record' => sub {
plan tests => 1;
my $input = CGI->new;
$input->param( -name => 'tag_200_indicator1_906288', -value => '' );
$input->param( -name => 'tag_200_indicator2_906288', -value => '' );
$input->param( -name => 'tag_200_code_a_906288_722171', -value => 'a' );
$input->param( -name => 'tag_200_subfield_a_906288_722171', -value => 'a 200$a' );
$input->param( -name => 'tag_200_code_b_906288_611549', -value => 'b' );
$input->param( -name => 'tag_200_subfield_b_906288_611549', -value => 'a 200$b' );
$input->param( -name => "tag_${biblionumbertagfield}_indicator1_198510", -value => "" );
$input->param( -name => "tag_${biblionumbertagfield}_indicator2_198510", -value => "" );
$input->param( -name => "tag_${biblionumbertagfield}_code_${biblionumbertagsubfield}_198510_886205", -value => $biblionumbertagsubfield );
$input->param( -name => "tag_${biblionumbertagfield}_subfield_${biblionumbertagsubfield}_198510_886205", -value => "a biblionumber which is not a biblionumber" );
my $record = C4::Biblio::TransformHtmlToMarc($input, 0);
my @subfields_biblionumber = $record->subfield( $biblionumbertagfield, $biblionumbertagsubfield );
is( @subfields_biblionumber, 1, 'The record should contain the field which are mapped to biblio.biblionumber' );
};
Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
$schema->storage->txn_rollback;