From 9b31593dae9c4190ca42a517949cecc7b951a38b Mon Sep 17 00:00:00 2001 From: Graham Knop Date: Thu, 22 Oct 2009 10:20:34 -0500 Subject: [PATCH 1/2] return undef for ->get() with invalid property --- lib/WebGUI/Definition.pm | 5 ++++- t/Definition.t | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/WebGUI/Definition.pm b/lib/WebGUI/Definition.pm index 117997b34..62fada36e 100644 --- a/lib/WebGUI/Definition.pm +++ b/lib/WebGUI/Definition.pm @@ -166,7 +166,10 @@ sub _gen_get { my $self = shift; if (@_) { my $prop = shift; - return $self->$prop; + if ($self->can($prop)) { + return $self->$prop; + } + return undef; } my @all_properties = $self->getProperties; my %props; diff --git a/t/Definition.t b/t/Definition.t index d12d511eb..10a875997 100644 --- a/t/Definition.t +++ b/t/Definition.t @@ -13,6 +13,8 @@ use warnings; no warnings qw(uninitialized); use Test::More 'no_plan'; #tests => 1; +use Test::Exception; + my $called_getProperties; { package WGT::Class; @@ -97,6 +99,12 @@ is_deeply $object->get, { property1 => 'property 1 value' }, is_deeply $subclass_object->get, { property1 => undef, a_property => ' - BLAH', property2 => 'property 2 value' }, 'get returns hash with correct properties'; +is $object->get('property1'), 'property 1 value', + 'get with parameter returns value from accessor'; + +is $object->get('nonExistantProperty'), undef, + 'get with non-existant parameter returns undef'; + is_deeply $object->getProperty('property1'), { label => 'property1 label', defaultValue => $object }, 'getProperty returns correct hash for object'; is_deeply $subclass_object->getProperty('property2'), { label => 'property2 label', defaultValue => 'dynamic value' }, From d26c3c2ed378869111522797e7cbde8566e9dccc Mon Sep 17 00:00:00 2001 From: Graham Knop Date: Thu, 22 Oct 2009 10:21:10 -0500 Subject: [PATCH 2/2] enforce some restrictions for assets --- lib/WebGUI/Definition/Asset.pm | 21 +++++++++-- t/Definition/Asset.t | 65 ++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 3 deletions(-) diff --git a/lib/WebGUI/Definition/Asset.pm b/lib/WebGUI/Definition/Asset.pm index 1f532aec1..86aa0dedf 100644 --- a/lib/WebGUI/Definition/Asset.pm +++ b/lib/WebGUI/Definition/Asset.pm @@ -18,7 +18,9 @@ use strict; use warnings; use 5.010; use base qw(WebGUI::Definition); + use WebGUI::International; +use WebGUI::Exception; our $VERSION = '0.0.1'; @@ -28,10 +30,23 @@ sub import { return; } my $definition = (@_ == 1 && ref $_[0]) ? $_[0] : { @_ }; + + my $table = $definition->{tableName} + || WebGUI::Error::InvalidParam->throw(param => 'tableName'); + if ( my $properties = $definition->{properties} ) { - my $table = $definition->{tableName}; - for ( my $i = 1; $i < @{ $properties }; $i += 2) { - $properties->[$i]{tableName} ||= $table; + for ( my $i = 0; $i < $#{ $properties }; $i += 2) { + my ($name, $value) = @{ $properties }[$i, $i + 1]; + $value->{tableName} ||= $table; + if ( ! $value->{tableName}|| ref $value->{tableName}) { + WebGUI::Error::InvalidParam->throw(param => 'tableName'); + } + elsif ( ! $value->{fieldType} || ref $value->{fieldType}) { + WebGUI::Error::InvalidParam->throw(param => 'fieldType'); + } + elsif ( ( ! $value->{noFormPost} || ref $value->{noFormPost} ) && ! $value->{label}) { + WebGUI::Error::InvalidParam->throw(param => 'label'); + } } } diff --git a/t/Definition/Asset.t b/t/Definition/Asset.t index c61dcf528..cbe8637da 100644 --- a/t/Definition/Asset.t +++ b/t/Definition/Asset.t @@ -16,6 +16,7 @@ use FindBin; use lib "$FindBin::Bin/../lib"; use Test::More 'no_plan'; #tests => 1; +use Test::Exception; use WebGUI::Test; { @@ -25,13 +26,16 @@ use WebGUI::Test; tableName => 'mytable', properties => [ showInForms => { + fieldType => 'Text', label => ['show in forms'], }, confirmChange => { + fieldType => 'Text', label => ['confirm change', 'Asset'], tableName => 'othertable', }, noTrans => { + fieldType => 'Text', label => 'this label will not be translated', }, ], @@ -56,3 +60,64 @@ is $object->getProperty('confirmChange')->{label}, 'Are you sure?', is $object->getProperty('noTrans')->{label}, 'this label will not be translated', q{getProperty doesn't internationalize plain scalars}; +{ + package WGT::Class2; + use Test::Exception; + throws_ok { WebGUI::Definition::Asset->import( + properties => [], + ) } 'WebGUI::Error::InvalidParam', 'Exception thrown when no tableName specified'; + throws_ok { WebGUI::Definition::Asset->import( + tableName => 'mytable', + properties => [ + 'property1' => { + label => 'label', + }, + ], + ) } 'WebGUI::Error::InvalidParam', 'Exception thrown when no fieldType specified'; + throws_ok { WebGUI::Definition::Asset->import( + tableName => 'mytable', + properties => [ + 'property1' => { + fieldType => sub { return 'Text' }, + label => 'label', + }, + ], + ) } 'WebGUI::Error::InvalidParam', 'Exception thrown when dynamic fieldType specified'; + throws_ok { WebGUI::Definition::Asset->import( + tableName => 'mytable', + properties => [ + 'property1' => { + tableName => sub { return 'othertable' }, + fieldType => 'Text', + label => 'label', + }, + ], + ) } 'WebGUI::Error::InvalidParam', 'Exception thrown when dynamic tableName specified'; + throws_ok { WebGUI::Definition::Asset->import( + tableName => 'mytable', + properties => [ + 'property1' => { + fieldType => 'Text', + }, + ], + ) } 'WebGUI::Error::InvalidParam', 'Exception thrown when no label specified'; + throws_ok { WebGUI::Definition::Asset->import( + tableName => 'mytable', + properties => [ + 'property1' => { + fieldType => 'Text', + noFormPost => sub { 1 }, + }, + ], + ) } 'WebGUI::Error::InvalidParam', 'Exception thrown when no label and noFormPost is dynamic'; + lives_ok { WebGUI::Definition::Asset->import( + tableName => 'mytable', + properties => [ + 'property1' => { + fieldType => 'Text', + noFormPost => 1, + }, + ], + ) } 'Allows no label when noFormPost specified'; +} +