Language override file changes

This commit is contained in:
Bart Jol 2009-10-02 15:55:02 +02:00 committed by Graham Knop
parent 7a439fd702
commit bb4d14d944
6 changed files with 230 additions and 3 deletions

View file

@ -0,0 +1,83 @@
package WebGUI::Content::SetLanguage;
=head1 LEGAL
-------------------------------------------------------------------
WebGUI is Copyright 2001-2009 Plain Black Corporation.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
http://www.plainblack.com info@plainblack.com
-------------------------------------------------------------------
=cut
use strict;
use WebGUI::Session;
use WebGUI::International;
=head1 NAME
Package WebGUI::Content::SetLanguage
=head1 DESCRIPTION
Sets or delete an scratch variable that overrides the profile field language
=head1 SYNOPSIS
use WebGUI::Content::SetLanguage;
WebGUI::Content::SetLanguage::handler();
=head1 SUBROUTINES
These subroutines are available from this package:
handler
=cut
#-------------------------------------------------------------
=head2 handler ( session, op, setLanguage )
sets or delete scratch variable in a session and returns undef
=head3 session
The current WebGUI::Session object.
=head3 op
op should be setLanguage to call the handler
=head3 language
language should be an installed language or delete
=cut
sub handler {
my ($session) = @_;
return undef unless $session->form->get('op') eq 'setLanguage';
my $language = $session->form->get('language');
#check whether a language has been given in the url
if (!$language) {
$session->log->error('There is no language given to this method');
return undef;
}
#make it possible to delete the language scratch variable from the session
if ($language eq 'delete' ) {
$session->scratch->removeLanguageOverride;
return undef;
}
#set a scratch variable language or throw error if language is not installed
else {
return $session->scratch->setLanguageOverride($language);
}
}
1;

View file

@ -284,7 +284,7 @@ Specify a default language. Defaults to user preference or "English".
sub new {
my ($class, $session, $namespace, $language) = @_;
$namespace ||= 'WebGUI';
$language ||= $session->user->profileField('language');
$language ||= $session->scratch->getLanguageOverride() || $session->user->profileField('language');
my $self =
bless {
_session => $session,

View file

@ -15,6 +15,7 @@ package WebGUI::Session::Scratch;
=cut
use strict;
use WebGUI::International;
=head1 NAME
@ -160,6 +161,19 @@ sub get {
return $self->{_data}{$var};
}
#-------------------------------------------------------------------
=head2 getLanguageOverride ()
Retrieves the language of the session scratch
=cut
sub getLanguageOverride {
my $self = shift;
my $languageOverride = $self->session->scratch->get('language');
return $languageOverride;
}
#-------------------------------------------------------------------
@ -180,7 +194,18 @@ sub new {
bless {_session=>$session, _data=>$data}, $class;
}
#-------------------------------------------------------------------
=head2 removeLanguageOverride()
Removes the language scratch variable from the session
=cut
sub removeLanguageOverride {
my $self = shift;
$self->session->scratch->delete('language');
}
#-------------------------------------------------------------------
=head2 session ( )
@ -220,5 +245,30 @@ sub set {
$self->session->db->write("insert into userSessionScratch (sessionId, name, value) values (?,?,?) on duplicate key update value=VALUES(value)", [$self->session->getId, $name, $value]);
}
#----------------------------------------------------------------------
=head2 setLanguageOverride ( language )
Sets a scratch variable language in the session if the language is installed
=head3 language
The language that should be set into the session
=cut
sub setLanguageOverride {
my $self = shift;
my $language = shift;
my $i18n = WebGUI::International->new($self->session);
if($i18n->getLanguages()->{$language}) {
$self->session->scratch->set("language",$language);
return undef;
}
else {
$self->session->log->error("Language $language is not installed in this site");
return undef;
}
}
1;

55
t/Content/SetLanguage.t Normal file
View file

@ -0,0 +1,55 @@
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2009 Plain Black Corporation.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#-------------------------------------------------------------------
# http://www.plainblack.com info@plainblack.com
#-------------------------------------------------------------------
use FindBin;
use strict;
use lib "$FindBin::Bin/../lib";
use WebGUI::Test;
use WebGUI::Session;
use WebGUI::Content::SetLanguage;
# load your modules here
use Test::More tests => 5; # increment this value for each test you create
my $session = WebGUI::Test->session;
# put your tests here
my $formvariables = {
'op' =>'setLanguage',
'language' => 'English'
};
#test 1
$session->request->setup_body($formvariables);
WebGUI::Content::SetLanguage::handler($session);
is($session->scratch->getLanguageOverride, 'English', 'the language was not set');
#test2
$formvariables->{'language'} = 'delete';
$session->request->setup_body($formvariables);
WebGUI::Content::SetLanguage::handler($session);
is($session->scratch->getLanguageOverride, undef, 'language delete should remove the scratch variable');
#test3
$formvariables->{'op'} = 'SetLanguage';
$formvariables->{'language'} = 'English';
$session->request->setup_body($formvariables);
WebGUI::Content::SetLanguage::handler($session);
is($session->scratch->getLanguageOverride, undef, 'Naming the method wrongly should not change anything');
#test4
$formvariables->{'op'} = 'setLanguage';
$formvariables->{'language'} = 'MyImaginaryLanguageThatIsNotInstalled';
$session->request->setup_body($formvariables);
WebGUI::Content::SetLanguage::handler($session);
is($session->scratch->getLanguageOverride, undef, 'Giving a non installed language should not change anything');
#test5
$formvariables->{'language'} = undef;
$session->request->setup_body($formvariables);
WebGUI::Content::SetLanguage::handler($session);
is($session->scratch->getLanguageOverride, undef, 'Passing an empty language variable should return undef');

View file

@ -16,12 +16,13 @@ use WebGUI::Session;
use Test::More; # increment this value for each test you create
use File::Copy;
use File::Spec;
use WebGUI::Content::SetLanguage;
my $session = WebGUI::Test->session;
my $numTests = 1; ##For conditional load check
my $langTests = 4; ##For language look-up tests
$numTests += 12 + $langTests;
$numTests += 20 + $langTests;
plan tests => $numTests;
@ -50,6 +51,16 @@ is($i18n->get('topicName', 'WebGUI'), 'WebGUI', 'get: test manual namespace over
installPigLatin();
#tests for sub new
my $i18nNew1 = WebGUI::International->new($session);
is($i18nNew1->{_language}, 'English', 'Calling new without parameters should return object with language English');
is($i18nNew1->{_namespace}, 'WebGUI', 'Calling without parameters should give namespace WebgUI');
my $i18nNew2 = WebGUI::International->new($session, 'WebGUI::Asset');
is($i18nNew2->{_language}, 'English', 'Calling new with only namespace parameter should return object with language English');
is($i18nNew2->{_namespace}, 'WebGUI::Asset', 'Calling with only parameter namespace should give requested namespace');
my $i18nNew3 = WebGUI::International->new($session, undef , 'PigLatin');
is($i18nNew3->{_language}, 'PigLatin', 'Calling new with only language parameter should return object with language PigLatin');
is($i18nNew3->{_namespace}, 'WebGUI', 'Calling with only parameter namespace should give WebGUI ');
my $languages = $i18n->getLanguages();
my $gotPigLatin = exists $languages->{PigLatin};
@ -102,6 +113,24 @@ sub installPigLatin {
);
}
#test for sub new with language overridden by scratch
my $formvariables = {
'op' =>'setLanguage',
'language' => 'PigLatin'
};
$session->request->setup_body($formvariables);
WebGUI::Content::SetLanguage::handler($session);
my $newi18n = WebGUI::International->new($session);
is(
$newi18n->get('webgui','WebGUI','PigLatin'),
'ebGUIWay',
'if the piglatin language is in the scratch that messages should be retrieved'
);
is(
$newi18n->get('104','Asset','PigLatin'),
$newi18n->get('104', 'WebGUI', 'English'),
'Language check after SetLanguage contentHandler : key from missing file return English key'
);
END {
unlink File::Spec->catfile(WebGUI::Test->lib, qw/WebGUI i18n PigLatin WebGUI.pm/);
unlink File::Spec->catfile(WebGUI::Test->lib, qw/WebGUI i18n PigLatin.pm/);

View file

@ -15,7 +15,7 @@ use lib "$FindBin::Bin/../lib";
use WebGUI::Test;
use WebGUI::Session;
use Test::More tests => 58; # increment this value for each test you create
use Test::More tests => 62; # increment this value for each test you create
use Test::Deep;
my $session = WebGUI::Test->session;
@ -117,6 +117,16 @@ is($sessionBank[0]->scratch->deleteNameByValue('',''), undef, 'deleteNameByValue
is($sessionBank[3]->scratch->deleteNameByValue('falseValue','0'), 1, 'deleteNameByValue will delete values that are false (0)');
is($sessionBank[2]->scratch->deleteNameByValue('falseValue',''), 1, "deleteNameByValue will delete values that are false ('')");
$scratch->setLanguageOverride('English');
is($scratch->getLanguageOverride, 'English', 'session scratch language is not correctly set');
$scratch->removeLanguageOverride;
is($scratch->getLanguageOverride, undef, 'The session scratch variable language is not removed');
$scratch->setLanguageOverride('myimmaginarylanguagethatisnotinstalled');
is($scratch->getLanguageOverride, undef, 'A non-existing language is set');
$scratch->setLanguageOverride('English');
$scratch->setLanguageOverride();
is($scratch->getLanguageOverride, 'English', 'A empty string is falsely recognised as a language');
END {
$session->scratch->deleteAll;
foreach my $wgSess ($newSession, @sessionBank) {