From f9c917e4263ed840b914977608705328e5f14d79 Mon Sep 17 00:00:00 2001 From: Patrick Donelan Date: Sat, 18 Jul 2009 03:03:22 +0000 Subject: [PATCH] Fixes #10660 Crud doesn't like 0 --- docs/changelog/7.x.x.txt | 1 + lib/WebGUI/Crud.pm | 5 ++- t/Crud/Subclass.t | 66 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 t/Crud/Subclass.t diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index 05694e278..2db2bf1d7 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -3,6 +3,7 @@ - fixed #10665: Navigations with old templates still in 7.7.16 - fixed #10662: Manage Clipboard / delete item - fixed #10637: shelf displays assets it shouldn't + - fixed #10660: Crud doesn't like 0 7.7.15 - fixed #10629: WebGUI::ProfileField create new field bug diff --git a/lib/WebGUI/Crud.pm b/lib/WebGUI/Crud.pm index 68dce87df..305980f7a 100644 --- a/lib/WebGUI/Crud.pm +++ b/lib/WebGUI/Crud.pm @@ -169,7 +169,10 @@ sub create { # add defaults my $properties = $class->crud_getProperties($session); foreach my $property (keys %{$properties}) { - $data->{$property} ||= $properties->{$property}{defaultValue}; + # set a default value if it's empty or undef (as per L) + if ($data->{$property} eq "") { + $data->{$property} = $properties->{$property}{defaultValue}; + } } # determine sequence diff --git a/t/Crud/Subclass.t b/t/Crud/Subclass.t new file mode 100644 index 000000000..a747f4d8f --- /dev/null +++ b/t/Crud/Subclass.t @@ -0,0 +1,66 @@ +package WebGUI::Crud::Subclass; + +use strict; +use base 'WebGUI::Crud'; +sub crud_definition { + my ($class, $session) = @_; + my $definition = $class->SUPER::crud_definition($session); + $definition->{tableName} = 'crudSubclass'; + $definition->{tableKey} = 'crudSubclassId'; + $definition->{sequenceKey} = ''; + my $properties = $definition->{properties}; + $properties->{field1} = { + fieldType => 'integer', + defaultValue => 5, + }; + return $definition; +} + +#package main; +# vim:syntax=perl +#------------------------------------------------------------------- +# 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 +#------------------------------------------------------------------ + +# Tests a subclass of WebGUI::Crud +use FindBin; +use strict; +use lib "$FindBin::Bin/../lib"; +use Test::More; +use WebGUI::Test; # Must use this before any other WebGUI modules + +#---------------------------------------------------------------------------- +# Init +my $session = WebGUI::Test->session; + +#---------------------------------------------------------------------------- +# Tests + +plan tests => 4; # Increment this number for each test you create + +#---------------------------------------------------------------------------- + +# Create +WebGUI::Crud::Subclass->crud_createTable($session); +my $record1 = WebGUI::Crud::Subclass->create($session, { field1 => 10 }); +isa_ok($record1, "WebGUI::Crud", "isa WebGUI::Crud"); +is($record1->get('field1'), 10, "got back correct field1 value"); + +# bug #10660 (zero should not trigger defaultValue) +is(WebGUI::Crud::Subclass->create($session, { field1 => 0 })->get('field1'), 0, 'zero does not trigger default'); +is(WebGUI::Crud::Subclass->create($session, { field1 => '' })->get('field1'), 5, '..but empty string intentionally triggers default'); + +#---------------------------------------------------------------------------- +# Cleanup +END { + +WebGUI::Crud::Subclass->crud_dropTable($session); + +} +#vim:ft=perl