enforce some restrictions for assets
This commit is contained in:
parent
9b31593dae
commit
d26c3c2ed3
2 changed files with 83 additions and 3 deletions
|
|
@ -18,7 +18,9 @@ use strict;
|
||||||
use warnings;
|
use warnings;
|
||||||
use 5.010;
|
use 5.010;
|
||||||
use base qw(WebGUI::Definition);
|
use base qw(WebGUI::Definition);
|
||||||
|
|
||||||
use WebGUI::International;
|
use WebGUI::International;
|
||||||
|
use WebGUI::Exception;
|
||||||
|
|
||||||
our $VERSION = '0.0.1';
|
our $VERSION = '0.0.1';
|
||||||
|
|
||||||
|
|
@ -28,10 +30,23 @@ sub import {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
my $definition = (@_ == 1 && ref $_[0]) ? $_[0] : { @_ };
|
my $definition = (@_ == 1 && ref $_[0]) ? $_[0] : { @_ };
|
||||||
|
|
||||||
|
my $table = $definition->{tableName}
|
||||||
|
|| WebGUI::Error::InvalidParam->throw(param => 'tableName');
|
||||||
|
|
||||||
if ( my $properties = $definition->{properties} ) {
|
if ( my $properties = $definition->{properties} ) {
|
||||||
my $table = $definition->{tableName};
|
for ( my $i = 0; $i < $#{ $properties }; $i += 2) {
|
||||||
for ( my $i = 1; $i < @{ $properties }; $i += 2) {
|
my ($name, $value) = @{ $properties }[$i, $i + 1];
|
||||||
$properties->[$i]{tableName} ||= $table;
|
$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');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ use FindBin;
|
||||||
use lib "$FindBin::Bin/../lib";
|
use lib "$FindBin::Bin/../lib";
|
||||||
|
|
||||||
use Test::More 'no_plan'; #tests => 1;
|
use Test::More 'no_plan'; #tests => 1;
|
||||||
|
use Test::Exception;
|
||||||
use WebGUI::Test;
|
use WebGUI::Test;
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
@ -25,13 +26,16 @@ use WebGUI::Test;
|
||||||
tableName => 'mytable',
|
tableName => 'mytable',
|
||||||
properties => [
|
properties => [
|
||||||
showInForms => {
|
showInForms => {
|
||||||
|
fieldType => 'Text',
|
||||||
label => ['show in forms'],
|
label => ['show in forms'],
|
||||||
},
|
},
|
||||||
confirmChange => {
|
confirmChange => {
|
||||||
|
fieldType => 'Text',
|
||||||
label => ['confirm change', 'Asset'],
|
label => ['confirm change', 'Asset'],
|
||||||
tableName => 'othertable',
|
tableName => 'othertable',
|
||||||
},
|
},
|
||||||
noTrans => {
|
noTrans => {
|
||||||
|
fieldType => 'Text',
|
||||||
label => 'this label will not be translated',
|
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',
|
is $object->getProperty('noTrans')->{label}, 'this label will not be translated',
|
||||||
q{getProperty doesn't internationalize plain scalars};
|
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';
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue