Jonathan Druart
9d6d641d1f
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>
102 lines
4 KiB
Perl
Executable file
102 lines
4 KiB
Perl
Executable file
#!/usr/bin/perl
|
|
#
|
|
# This Koha test module is a stub!
|
|
# Add more tests here!!!
|
|
|
|
use Modern::Perl;
|
|
|
|
use Test::More tests => 18;
|
|
use List::Util qw(first);
|
|
use Data::Dumper;
|
|
use Test::Warn;
|
|
use t::lib::Mocks;
|
|
use Koha::Database;
|
|
|
|
BEGIN {
|
|
use_ok('C4::Languages', qw( accept_language getAllLanguages getLanguages getTranslatedLanguages get_rfc4646_from_iso639 ));
|
|
}
|
|
|
|
my $schema = Koha::Database->new->schema;
|
|
$schema->storage->txn_begin;
|
|
my $dbh = C4::Context->dbh;
|
|
|
|
isnt(C4::Languages::_get_themes(), undef, 'testing _get_themes doesnt return undef');
|
|
|
|
ok(C4::Languages::_get_language_dirs(), 'test getting _get_language_dirs');
|
|
|
|
my $result;
|
|
warning_is { $result = C4::Languages::accept_language(); }
|
|
q{accept_language(x,y) called with no clientPreferences (x).},
|
|
'accept_language() generated expected warning';
|
|
is($result,undef, 'test that accept_languages returns undef when nothing is entered');
|
|
|
|
ok(C4::Languages::getAllLanguages(), 'test get all languages');
|
|
|
|
t::lib::Mocks::mock_preference('AdvancedSearchLanguages', '');
|
|
my $all_languages = C4::Languages::getAllLanguages('eng');
|
|
ok(@$all_languages > 10, 'retrieved a bunch of languges');
|
|
|
|
my $languages = C4::Languages::getLanguages('eng');
|
|
is_deeply($languages, $all_languages, 'getLanguages() and getAllLanguages() return the same list');
|
|
|
|
$languages = C4::Languages::getLanguages('eng', 1);
|
|
is_deeply($languages, $all_languages, 'getLanguages() and getAllLanguages() with filtering selected but AdvancedSearchLanguages blank return the same list');
|
|
|
|
t::lib::Mocks::mock_preference('AdvancedSearchLanguages', 'ita|eng');
|
|
$languages = C4::Languages::getLanguages('eng', 1);
|
|
is(scalar(@$languages), 2, 'getLanguages() filtering using AdvancedSearchLanguages works');
|
|
|
|
my $translatedlanguages1;
|
|
warnings_are { $translatedlanguages1 = C4::Languages::getTranslatedLanguages('opac','prog',undef,'') }
|
|
[],
|
|
'no warnings for calling getTranslatedLanguages';
|
|
my @currentcheck1 = map { $_->{current} } @$translatedlanguages1;
|
|
my $onlyzeros = first { $_ != 0 } @currentcheck1;
|
|
ok(! $onlyzeros, "Everything was zeros.\n");
|
|
|
|
my $translatedlanguages2;
|
|
warnings_are { $translatedlanguages2 = C4::Languages::getTranslatedLanguages('opac','prog','en','') }
|
|
[],
|
|
'no warnings for calling getTranslatedLanguages';
|
|
my @currentcheck2 = map { $_->{current} } @$translatedlanguages2;
|
|
$onlyzeros = first { $_ != 0 } @currentcheck2;
|
|
ok($onlyzeros, "There is a $onlyzeros\n");
|
|
|
|
# Language Descriptions
|
|
my $sth = $dbh->prepare("SELECT DISTINCT subtag,type,lang,description from language_descriptions;");
|
|
$sth->execute();
|
|
my $DistinctLangDesc = $sth->fetchall_arrayref({});
|
|
|
|
$sth = $dbh->prepare("SELECT subtag,type,lang,description from language_descriptions;");
|
|
$sth->execute();
|
|
my $LangDesc = $sth->fetchall_arrayref({});
|
|
|
|
is(scalar(@$LangDesc),scalar(@$DistinctLangDesc),"No unexpected language_description duplicates.");
|
|
|
|
# Language_subtag_registry
|
|
$sth = $dbh->prepare("SELECT DISTINCT subtag,type,description,added FROM language_subtag_registry;");
|
|
$sth->execute();
|
|
my $DistinctLangReg = $sth->fetchall_arrayref({});
|
|
|
|
$sth = $dbh->prepare("SELECT subtag,type,description,added FROM language_subtag_registry;");
|
|
$sth->execute();
|
|
my $LangReg = $sth->fetchall_arrayref({});
|
|
|
|
is(scalar(@$LangReg),scalar(@$DistinctLangReg),"No unexpected language_subtag_registry duplicates.");
|
|
|
|
# Language RFC4646 to ISO639
|
|
$sth = $dbh->prepare("SELECT DISTINCT rfc4646_subtag,iso639_2_code FROM language_rfc4646_to_iso639;");
|
|
$sth->execute();
|
|
my $DistinctLangRfc4646 = $sth->fetchall_arrayref({});
|
|
|
|
$sth = $dbh->prepare("SELECT rfc4646_subtag,iso639_2_code FROM language_rfc4646_to_iso639;");
|
|
$sth->execute();
|
|
my $LangRfc4646 = $sth->fetchall_arrayref({});
|
|
|
|
is(scalar(@$LangRfc4646),scalar(@$DistinctLangRfc4646),"No unexpected language_rfc4646_to_iso639 duplicates.");
|
|
|
|
my $i = 0;
|
|
foreach my $pair (@$DistinctLangRfc4646){
|
|
$i++ if $pair->{rfc4646_subtag} eq C4::Languages::get_rfc4646_from_iso639( $pair->{iso639_2_code} );
|
|
}
|
|
is($i,scalar(@$DistinctLangRfc4646),"get_rfc4646_from_iso639 returns correct rfc for all iso values.");
|