From bd81357f09aa514f081fe33951841c709feeaa6b Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Fri, 15 Oct 2021 08:27:30 +0000 Subject: [PATCH] Bug 29245: Prepare Languages.t for changes Content-Type: text/plain; charset=utf-8 Add license. Reorganize in subtests. Cosmetic changes: whitespace. Add rollback. Test plan: Run the test again. Confirm that it still passes. Signed-off-by: Marcel de Rooy --- t/db_dependent/Languages.t | 161 ++++++++++++++++++++++--------------- 1 file changed, 95 insertions(+), 66 deletions(-) diff --git a/t/db_dependent/Languages.t b/t/db_dependent/Languages.t index f9e50aade8..f87c45795b 100755 --- a/t/db_dependent/Languages.t +++ b/t/db_dependent/Languages.t @@ -1,14 +1,27 @@ #!/usr/bin/perl + +# Copyright 2021 Koha Development team +# +# This file is part of Koha +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. # -# This Koha test module is a stub! -# Add more tests here!!! +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . use Modern::Perl; - -use Test::More tests => 18; use List::Util qw(first); -use Data::Dumper; +use Test::More tests => 5; use Test::Warn; + use t::lib::Mocks; use Koha::Database; @@ -20,83 +33,99 @@ 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'); +subtest 'get_themes, get_language_dirs' => sub { + plan tests => 4; + isnt( C4::Languages::_get_themes(), undef, 'testing _get_themes doesnt return undef' ); + ok( C4::Languages::_get_language_dirs(), 'test getting _get_language_dirs' ); -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' ); +}; -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'); +subtest 'getAllLanguages' => sub { + plan tests => 5; -ok(C4::Languages::getAllLanguages(), 'test get all languages'); + 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'); + t::lib::Mocks::mock_preference('AdvancedSearchLanguages', q{} ); + my $all_languages = C4::Languages::getAllLanguages('eng'); + ok( @$all_languages > 10, 'retrieved a bunch of languages' ); -my $languages = C4::Languages::getLanguages('eng'); -is_deeply($languages, $all_languages, 'getLanguages() and getAllLanguages() return the same list'); + 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'); + $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'); + 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"); +subtest 'getTranslatedLanguages' => sub { + plan tests => 4; -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"); + my $translatedlanguages1; + warnings_are { $translatedlanguages1 = C4::Languages::getTranslatedLanguages( 'opac', 'prog' ) } + [], + 'no warnings for calling getTranslatedLanguages'; -# Language Descriptions -my $sth = $dbh->prepare("SELECT DISTINCT subtag,type,lang,description from language_descriptions;"); -$sth->execute(); -my $DistinctLangDesc = $sth->fetchall_arrayref({}); + my @currentcheck1 = map { $_->{current} } @$translatedlanguages1; + my $onlyzeros = first { $_ != 0 } @currentcheck1; + ok(! $onlyzeros, "Everything was zeros."); -$sth = $dbh->prepare("SELECT subtag,type,lang,description from language_descriptions;"); -$sth->execute(); -my $LangDesc = $sth->fetchall_arrayref({}); + 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"); +}; -is(scalar(@$LangDesc),scalar(@$DistinctLangDesc),"No unexpected language_description duplicates."); +subtest 'Language descriptions, Language_subtag_registry, RFC4646 to ISO639' => sub { + plan tests => 4; -# Language_subtag_registry -$sth = $dbh->prepare("SELECT DISTINCT subtag,type,description,added FROM language_subtag_registry;"); -$sth->execute(); -my $DistinctLangReg = $sth->fetchall_arrayref({}); + 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,description,added FROM language_subtag_registry;"); -$sth->execute(); -my $LangReg = $sth->fetchall_arrayref({}); + $sth = $dbh->prepare("SELECT subtag,type,lang,description from language_descriptions;"); + $sth->execute(); + my $LangDesc = $sth->fetchall_arrayref({}); -is(scalar(@$LangReg),scalar(@$DistinctLangReg),"No unexpected language_subtag_registry duplicates."); + is( scalar(@$LangDesc), scalar(@$DistinctLangDesc), "No unexpected language_description 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({}); + # 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 rfc4646_subtag,iso639_2_code FROM language_rfc4646_to_iso639;"); -$sth->execute(); -my $LangRfc4646 = $sth->fetchall_arrayref({}); + $sth = $dbh->prepare("SELECT subtag,type,description,added FROM language_subtag_registry;"); + $sth->execute(); + my $LangReg = $sth->fetchall_arrayref({}); -is(scalar(@$LangRfc4646),scalar(@$DistinctLangRfc4646),"No unexpected language_rfc4646_to_iso639 duplicates."); + is( scalar(@$LangReg), scalar(@$DistinctLangReg), "No unexpected language_subtag_registry 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."); + # 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." ); +}; + +$schema->storage->txn_rollback; -- 2.20.1