From e959966cdc350d679664ed4ab7fce41553a8d317 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Fri, 15 Oct 2021 10:04:46 +0000 Subject: [PATCH] Bug 29245: Extend tests for getTranslatedLanguages Content-Type: text/plain; charset=utf-8 Test plan: Run test again. Signed-off-by: Marcel de Rooy --- t/db_dependent/Languages.t | 49 +++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/t/db_dependent/Languages.t b/t/db_dependent/Languages.t index f87c45795b..3fe3bcab98 100755 --- a/t/db_dependent/Languages.t +++ b/t/db_dependent/Languages.t @@ -21,17 +21,21 @@ use Modern::Perl; use List::Util qw(first); use Test::More tests => 5; use Test::Warn; +use Test::MockModule; use t::lib::Mocks; use Koha::Database; +use Koha::Caches; BEGIN { use_ok('C4::Languages', qw( accept_language getAllLanguages getLanguages getTranslatedLanguages get_rfc4646_from_iso639 )); } -my $schema = Koha::Database->new->schema; +our $schema = Koha::Database->new->schema; $schema->storage->txn_begin; -my $dbh = C4::Context->dbh; +our $dbh = C4::Context->dbh; +our $lang_module = Test::MockModule->new( 'C4::Languages' ); +our $cache = Koha::Caches->get_instance; subtest 'get_themes, get_language_dirs' => sub { plan tests => 4; @@ -66,24 +70,53 @@ subtest 'getAllLanguages' => sub { }; subtest 'getTranslatedLanguages' => sub { - plan tests => 4; + plan tests => 9; + + # Obvious assumptions + t::lib::Mocks::mock_preference( 'opacthemes', 'bootstrap' ); + t::lib::Mocks::mock_preference( 'template', 'prog' ); my $translatedlanguages1; - warnings_are { $translatedlanguages1 = C4::Languages::getTranslatedLanguages( 'opac', 'prog' ) } + $cache->clear_from_cache( "languages_opac_bootstrap" ); + warnings_are { $translatedlanguages1 = C4::Languages::getTranslatedLanguages( 'opac', 'bootstrap' ) } [], 'no warnings for calling getTranslatedLanguages'; - my @currentcheck1 = map { $_->{current} } @$translatedlanguages1; my $onlyzeros = first { $_ != 0 } @currentcheck1; - ok(! $onlyzeros, "Everything was zeros."); + is( $onlyzeros, undef, "No language was marked as current"); my $translatedlanguages2; - warnings_are { $translatedlanguages2 = C4::Languages::getTranslatedLanguages( 'opac', 'prog', 'en' ) } + $cache->clear_from_cache( "languages_opac_bootstrap" ); + warnings_are { $translatedlanguages2 = C4::Languages::getTranslatedLanguages( 'opac', 'bootstrap', 'en' ) } [], 'no warnings for calling getTranslatedLanguages'; my @currentcheck2 = map { $_->{current} } @$translatedlanguages2; $onlyzeros = first { $_ != 0 } @currentcheck2; - ok($onlyzeros, "There is a $onlyzeros"); + is( $onlyzeros, 1, "en is marked as current now" ); + + # mock opac languages, test leaving out opac/bootstrap + my @language_dirs; + $lang_module->mock( '_get_language_dirs', sub { return @language_dirs; } ); + t::lib::Mocks::mock_preference( 'OPACLanguages', 'ita,eng,nl-NL' ); + @language_dirs = ( 'eng', 'ita', 'nl-NL' ); + $translatedlanguages1 = C4::Languages::getTranslatedLanguages(); + is( @$translatedlanguages1, 1, 'Only one language since we didnt clear the cache' ); + $cache->clear_from_cache( "languages_opac_bootstrap" ); + $translatedlanguages1 = C4::Languages::getTranslatedLanguages(); + is( @$translatedlanguages1, 3, 'Check if we get back three languages now' ); + + # mock staff languages + @language_dirs = ( 'eng', 'fr-CA', 'fr-FR', 'fr-NL', 'zh-NL' ); + $cache->clear_from_cache( "languages_staff_prog" ); + t::lib::Mocks::mock_preference( 'language', 'zh,fr-NL,fr-CA,eng,fr-FR,de-DE' ); # de-DE missing + # TODO Should getTranslatedLanguages sort? + $translatedlanguages1 = [ sort + { $a->{language} cmp $b->{language} } + @{ C4::Languages::getTranslatedLanguages( 'staff' ) }]; # forgot prog + is( @$translatedlanguages1, 3, 'Three staff languages' ); + is( @{$translatedlanguages1->[1]->{sublanguages_loop}}, 3, 'Three french variants' ); + is( $translatedlanguages1->[2]->{language}, 'zh', 'Found zh too' ); + $lang_module->unmock( '_get_language_dirs' ); }; subtest 'Language descriptions, Language_subtag_registry, RFC4646 to ISO639' => sub { -- 2.20.1