From 951439a017b11d8c495067abc2661b0d0004bd75 Mon Sep 17 00:00:00 2001 From: Bart Jol Date: Fri, 23 Oct 2009 16:16:01 +0200 Subject: [PATCH] added picklanguage macro to international --- etc/WebGUI.conf.original | 1 + lib/WebGUI/Macro/PickLanguage.pm | 65 +++++++++++++++++++ t/Macro/PickLanguage.t | 104 +++++++++++++++++++++++++++++++ 3 files changed, 170 insertions(+) create mode 100644 lib/WebGUI/Macro/PickLanguage.pm create mode 100644 t/Macro/PickLanguage.t diff --git a/etc/WebGUI.conf.original b/etc/WebGUI.conf.original index ce4471029..b074415c1 100644 --- a/etc/WebGUI.conf.original +++ b/etc/WebGUI.conf.original @@ -811,6 +811,7 @@ "Page" : "Page", "PageTitle" : "PageTitle", "PageUrl" : "PageUrl", + "PickLanguage" : "PickLanguage", "RandomAssetProxy" : "RandomAssetProxy", "RandomThread" : "RandomThread", "RootTitle" : "RootTitle", diff --git a/lib/WebGUI/Macro/PickLanguage.pm b/lib/WebGUI/Macro/PickLanguage.pm new file mode 100644 index 000000000..60fc7c75d --- /dev/null +++ b/lib/WebGUI/Macro/PickLanguage.pm @@ -0,0 +1,65 @@ +package WebGUI::Macro::PickLanguage; # edit this line to match your own macro name + +#------------------------------------------------------------------- +# 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 strict; + +=head1 NAME + +Package WebGUI::Macro::PickLanguage + +=head1 DESCRIPTION + +This macro makes a link for each installed language so when clicked the SetLanguage contetntHandler is called and sets the language in the scratch. The link text is the label from the language. + +=head2 process( $session ) + +The main macro class, Macro.pm, will call this subroutine and pass it + +=over 4 + +=item * + +A session variable + +=item templateId + +This macro takes a templateId to show the links + +=back + +=cut + + +#------------------------------------------------------------------- +sub process { + my $session = shift; + my $templateId = shift; + my $template = WebGUI::Asset::Template->new($session, $templateId); + return "Could not instanciate template with id [$templateId]" unless $template; + my $i18n = WebGUI::International->new($session); + my $languages = $i18n->getLanguages(); + my $vars = {'lang_loop' => []}; + foreach my $language ( keys %$languages ) { + my $langVars = {}; + $langVars->{ 'language_url' } = '?op=setLanguage;language=' . $language ; + $langVars->{ 'language_lang' } = $i18n->getLanguage($language , 'label'); + $langVars->{ 'language_langAbbr' } = $i18n->getLanguage($language, 'languageAbbreviation'); + $langVars->{ 'language_langAbbrLoc' } = $i18n->getLanguage($language, 'locale'); + $langVars->{ 'language_langEng' } = $language; + push(@{$vars->{lang_loop}}, $langVars); + } + return $template->process($vars); +} + +1; + +#vim:ft=perl diff --git a/t/Macro/PickLanguage.t b/t/Macro/PickLanguage.t new file mode 100644 index 000000000..16e70fafc --- /dev/null +++ b/t/Macro/PickLanguage.t @@ -0,0 +1,104 @@ +#------------------------------------------------------------------- +# 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::Asset::Template; + +use Test::More; # increment this value for each test you create +use Test::Deep; +use Test::MockObject; +use Test::MockObject::Extends; + +my $session = WebGUI::Test->session; + +my $numTests = 3; + +$numTests += 1; #For the use_ok + +plan tests => $numTests; + +my $macro = 'WebGUI::Macro::PickLanguage'; +my $loaded = use_ok($macro); + +my $macroMock = Test::MockObject->new({}); +$macroMock->set_isa('WebGUI::Macro::PickLanguage'); +$macroMock->set_true('process'); + +#test for normal use + +my $templateId = 'PICKLANGUAGE_TEMPLATE_'; + +my $templateMock = Test::MockObject->new({}); +$templateMock->set_isa('WebGUI::Asset::Template'); +$templateMock->set_always('getId', $templateId); +my $templateVars; +$templateMock->mock('process', sub { $templateVars = $_[1]; } ); + + +{ + WebGUI::Test->mockAssetId($templateId, $templateMock); + WebGUI::Macro::PickLanguage::process($session,$templateMock->getId); + + cmp_deeply( + $templateVars, + { + lang_loop => [ + { 'language_url' => '?op=setLanguage;language=English', + 'language_lang' => 'English', + 'language_langAbbr' => 'en', + 'language_langAbbrLoc' => 'US', + 'language_langEng' => 'English' + }, + ], + }, + 'some template variables are created' + ); + WebGUI::Test->unmockAssetId($templateId); +} + +#test when template Id is left empty + +$templateId = ''; +my $templateNoId = $templateMock->mock('process',''); +$templateMock->set_always('getId', $templateId); +$templateMock->mock('process', sub { $templateVars = $_[1]; } ); +my $error; + + +{ + WebGUI::Test->mockAssetId($templateNoId, $templateMock); + $error = WebGUI::Macro::PickLanguage::process($session,$templateMock->getId); + + is($error,'Could not instanciate template with id []',"Empty template Id should return error"); + + WebGUI::Test->unmockAssetId($templateNoId); +} + +#test for an incorrect template Id + +$templateId = '1234567890123456789012'; +my $templateWrongId = $templateMock->mock('process',''); +$templateMock->set_always('getId', $templateId); +$templateMock->mock('process', sub { $templateVars = $_[1]; } ); +my $error; + + +{ + WebGUI::Test->mockAssetId($templateWrongId, $templateMock); + $error = WebGUI::Macro::PickLanguage::process($session,$templateMock->getId); + + is($error,'Could not instanciate template with id [1234567890123456789012]',"Template from the wrong namespace should not be initiated"); + WebGUI::Test->unmockAssetId($templateWrongId); +} +