From 73c762d6f16a70db132aef86e66cd697e22bda49 Mon Sep 17 00:00:00 2001 From: martin Date: Fri, 4 Jun 2010 11:54:36 +0000 Subject: [PATCH] Fixed bug where Template form plugin would not show anything for getValueAsHtml. --- docs/changelog/7.x.x.txt | 1 + lib/WebGUI/Form/Template.pm | 99 ++++++++++++++++++++++++++----------- t/Form/Template.t | 72 +++++++++++++++++++++++++++ 3 files changed, 143 insertions(+), 29 deletions(-) create mode 100644 t/Form/Template.t diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index 32b8a5a2e..4525ed4ce 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -2,6 +2,7 @@ - fixed #11596: Calendar: all day events leaking - fixed #11604: scheduled workflows getting deleted - added API method commitAsUser allowing developers to commit version tags as other users + - fixed: The template form plugin would return an empty string when getValueAsHtml was called. ( Martin Kamerbeek / Oqapi ) 7.9.6 - new checkbox in the asset manager for clearing the package flag on import diff --git a/lib/WebGUI/Form/Template.pm b/lib/WebGUI/Form/Template.pm index e95bed1ef..d88d1a38c 100644 --- a/lib/WebGUI/Form/Template.pm +++ b/lib/WebGUI/Form/Template.pm @@ -78,23 +78,23 @@ If true, this will limit the list of template to only include templates that are =cut sub definition { - my $class = shift; - my $session = shift; - my $definition = shift || []; - my $i18n = WebGUI::International->new($session, 'Asset_Template'); - push(@{$definition}, { - label=>{ - defaultValue=>$i18n->get("assetName") - }, - name=>{ - defaultValue=>"templateId" - }, - namespace=>{ - defaultValue=>undef - }, - onlyCommitted=>{ - defaultValue=>'' - }, + my $class = shift; + my $session = shift; + my $definition = shift || []; + my $i18n = WebGUI::International->new($session, 'Asset_Template'); + push(@{$definition}, { + label=>{ + defaultValue=>$i18n->get("assetName") + }, + name=>{ + defaultValue=>"templateId" + }, + namespace=>{ + defaultValue=>undef + }, + onlyCommitted=>{ + defaultValue=>'' + }, }); return $class->SUPER::definition($session, $definition); } @@ -138,6 +138,54 @@ sub isDynamicCompatible { #------------------------------------------------------------------- +=head2 getValueAsHtml ( ) + +Returns the tempalte name of the selected template. + +=cut + +sub getValueAsHtml { + my $self = shift; + + $self->setOptions; + + return $self->SUPER::getValueAsHtml; +} + +#------------------------------------------------------------------- + +=head2 setOptions + +Fills the options of the select list with the appropriate templates. + +=cut + +sub setOptions { + my $self = shift; + my $session = $self->session; + my $userId = $session->user->userId; + + my $onlyCommitted = $self->get( 'onlyCommitted' ) + ? q{assetData.status='approved'} + : $self->get( 'onlyCommitted' ) + ; + my $templateList = WebGUI::Asset::Template->getList( $session, $self->get( 'namespace' ), $onlyCommitted ); + + #Remove entries from template list that the user does not have permission to view. + for my $assetId ( keys %{ $templateList } ) { + my $asset = WebGUI::Asset::Template->new( $session, $assetId ); + if ( $asset && !$asset->canView( $userId ) ) { + delete $templateList->{ $assetId }; + } + } + + $self->set( 'options', $templateList ); + + return; +} + +#------------------------------------------------------------------- + =head2 toHtml ( ) Renders a template picker control. @@ -145,18 +193,11 @@ Renders a template picker control. =cut sub toHtml { - my $self = shift; - my $onlyCommitted = $self->get('onlyCommitted') ? "assetData.status='approved'" : $self->get('onlyCommitted'); - my $templateList = WebGUI::Asset::Template->getList($self->session, $self->get("namespace"), $onlyCommitted); - #Remove entries from template list that the user does not have permission to view. - for my $assetId ( keys %{$templateList} ) { - my $asset = WebGUI::Asset::Template->new($self->session, $assetId); - if (!$asset->canView($self->session->user->userId)) { - delete $templateList->{$assetId}; - } - } - $self->set("options", $templateList); - return $self->SUPER::toHtml(); + my $self = shift; + + $self->setOptions; + + return $self->SUPER::toHtml(); } #------------------------------------------------------------------- diff --git a/t/Form/Template.t b/t/Form/Template.t new file mode 100644 index 000000000..a5729ff98 --- /dev/null +++ b/t/Form/Template.t @@ -0,0 +1,72 @@ +#------------------------------------------------------------------- +# 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::Form::Template; +use WebGUI::Session; + +use Test::Deep; +use Test::More; # increment this value for each test you create + +my $session = WebGUI::Test->session; + +plan tests => 4; + +my $versionTag = WebGUI::VersionTag->create( $session ); +$versionTag->setWorking; + +{ + my $templateList = WebGUI::Asset::Template->getList( $session, 'style' ); + my $elem = WebGUI::Form::Template->new( $session, { + namespace => 'style', + onlyCommitted => 0, + } ); + + $elem->setOptions; + cmp_deeply( + $templateList, + $elem->get('options'), + 'setOption sets correct templates' + ); + + my $newTemplate = WebGUI::Asset->getRoot( $session )->addChild( { + title => 'Klazam', + menuTitle => 'Klazam', + template => '', + namespace => 'style', + className => 'WebGUI::Asset::Template', + } ); + + $elem->setOptions; + cmp_deeply( + { %{$templateList}, $newTemplate->getId => 'Klazam' }, + $elem->get('options'), + 'setOption includes uncommitted templates when onlyCommitted is false' + ); + + $elem->set( onlyCommitted => 1 ); + $elem->setOptions; + cmp_deeply( + $templateList, + $elem->get('options'), + 'setOption excludes uncommitted templates when onlyCommitted is true' + ); + + my ( $id, $name ) = %{ $templateList }; + $elem->set( 'value', $id ); + is( $elem->getValueAsHtml, $name, 'getValueAsHtml return template name' ); + +} + +$versionTag->rollback;