Fix the TimeField form control, and add lots of tests.

This commit is contained in:
Colin Kuskie 2008-12-09 16:43:39 +00:00
parent 1a67df91bc
commit 869aa3e64d
3 changed files with 162 additions and 35 deletions

View file

@ -9,6 +9,7 @@
- fixed #9244: i18n for colin
- fixed #9252: Thingy defaultValue length causes input to be truncated (SDH Consulting Group)
- fixed #8937: Feeds in Calendar strip information
- fixed #9063: Thingy - Time Field editing errors
7.6.5
- security: A problem was discovered and fixed in which users could email executable attachments to a collaboration system and then when viewed online, could execute them.

View file

@ -115,51 +115,44 @@ sub getName {
=head2 getValue ( [ value ] )
If the defaultValue is a MySQL time, the value returned by this form element
will be a MySQL time. Note: Will not be adjusted for the user's time zone.
If the defaultValue is a MySQL time, or the format property = 'mysql',
the value returned by this form element will be a MySQL time.
Note: Will not be adjusted for the user's time zone.
Otherwise, the value returned by this form element will be a number of seconds,
adjusted for the user's time zone..
Otherwise, the value returned by this form element will be a number of seconds.
=head3 value
An optional value to process, instead of POST input. This should be in the form of an integer of seconds, 'HH:MM', or 'HH:MM:SS'.
An optional value to process, instead of POST input. This should be
in the form of an integer of seconds, 'HH:MM', or 'HH:MM:SS'.
=cut
my $mysqlFormattedDate = qr/^\d{2}\D\d{2}(?:\D\d{2})?$/;
sub getValue {
my $self = shift;
my $value = $self->SUPER::getValue(@_);
if (@_) {
if ($self->get('format') ne 'mysql' && (
!$self->get("defaultValue")
|| $self->get("defaultValue") =~ m/^\d+$/
|| !$value
|| $value =~ m/^\d+$/)) {
return $self->session->datetime->timeToSeconds($value)-($self->session->user->profileField("timeOffset")*3600);
}
elsif ($value =~ /^\d{2}\D\d{2}(\D\d{2})?$/) {
return $value
}
else {
return undef;
}
}
# This should probably be rewritten as a cascading ternary
if ($self->get('format') ne 'mysql' && (
!$self->get("defaultValue")
|| $self->get("defaultValue") =~ m/^\d+$/
|| !$self->get("value")
|| $self->get("value") =~ m/^\d+$/)) {
# epoch format
return $self->session->datetime->timeToSeconds($value)-($self->session->user->profileField("timeOffset")*3600);
}
else {
# Mysql format
return undef unless $value =~ /^\d{2}\D\d{2}(\D\d{2})?$/;
return $value;
}
my $mysqlMode = $self->get('format') eq 'mysql'
|| $self->getDefaultValue =~ $mysqlFormattedDate;
my $mysqlDate = ($value =~ $mysqlFormattedDate);
my $digits = ($value =~ /^\d+/);
##Format is fine
if ( ( $mysqlMode && $mysqlDate)
||(!$mysqlMode && !$mysqlDate) && $digits) {
return $value;
}
##Convert to mysql format
elsif ($mysqlMode && $digits) {
return $self->session->datetime->secondsToTime($value);
}
##Convert to seconds.
elsif ($mysqlDate) {
return $self->session->datetime->timeToSeconds($value);
}
else { ##Bad stuff, maynard
return undef;
}
}
#-------------------------------------------------------------------

133
t/Form/TimeField.pm Normal file
View file

@ -0,0 +1,133 @@
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2008 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;
use WebGUI::Form::TimeField;
use WebGUI::Session;
use HTML::Form;
use WebGUI::Form_Checking;
#The goal of this test is to verify that Text form elements work
use Test::More; # increment this value for each test you create
my $session = WebGUI::Test->session;
# put your tests here
my $testBlock = [
{
key => 'Time1',
testValue => '00:00:10',
expected => '10',
comment => 'Send it mysql format data, seconds',
},
{
key => 'Time2',
testValue => '00:10:00',
expected => '600',
comment => 'Send it mysql format data, minutes',
},
{
key => 'Time3',
testValue => '10',
expected => '10',
comment => 'Send it seconds format data',
},
];
my $formType = 'text'; ##timeField is a text subclass
my $numTests = 37 + scalar @{ $testBlock };
plan tests => $numTests;
my ($header, $footer) = (WebGUI::Form::formHeader($session), WebGUI::Form::formFooter($session));
my $html = join "\n",
$header,
WebGUI::Form::TimeField->new($session, {
name => 'TestTime',
value => '00:00:10',
})->toHtml,
$footer;
my @forms = HTML::Form->parse($html, 'http://www.webgui.org');
##Test Form Generation
is(scalar @forms, 1, '1 form was parsed');
my @inputs = $forms[0]->inputs;
is(scalar @inputs, 2, 'The form has 2 inputs. One for the field, another for the JS pop-up button.');
#Basic tests
my $input = $inputs[0];
is($input->name, 'TestTime', 'Checking input name');
is($input->type, $formType, 'Checking input type');
is($input->value, '00:00:10', 'Checking default value');
WebGUI::Form_Checking::auto_check($session, 'TimeField', $testBlock);
# test that we can process non-POST values correctly
my $cntl;
$cntl = WebGUI::Form::TimeField->new($session,{ });
is($cntl->getValue('10'), '10', 'no default, not mysql mode, all digits');
is($cntl->getValue('00:00:10'), '10', 'no default, not mysql mode, mysql formatted data, 3 pairs');
is($cntl->getValue('00:10'), '600', 'no default, not mysql mode, mysql formatted data, 2 pairs');
is($cntl->getValue('00:10:00'), '600', 'no default, not mysql mode, mysql formatted data, 3 pairs');
is($cntl->getValue('innocent'), undef, 'no default, not mysql mode, wrong data');
$cntl = WebGUI::Form::TimeField->new($session,{ format => 'mysql' });
is($cntl->getValue('10'), '00:00:10', 'no default, mysql mode, all digits');
is($cntl->getValue('00:00:10'), '00:00:10', 'no default, mysql mode, mysql formatted data, 3 pairs');
is($cntl->getValue('00:10'), '00:10', 'no default, mysql mode, mysql formatted data, 2 pairs');
is($cntl->getValue('00:10:00'), '00:10:00', 'no default, mysql mode, mysql formatted data, 3 pairs');
is($cntl->getValue('innocent'), undef, 'no default, mysql mode, wrong data');
$cntl = WebGUI::Form::TimeField->new($session,{ defaultValue => 0, });
is($cntl->getValue('10'), '10', '0 default, not mysql mode, all digits');
is($cntl->getValue('00:00:10'), '10', '0 default, not mysql mode, mysql formatted data, 3 pairs');
is($cntl->getValue('00:10'), '600', '0 default, not mysql mode, mysql formatted data, 2 pairs');
is($cntl->getValue('00:10:00'), '600', '0 default, not mysql mode, mysql formatted data, 3 pairs');
$cntl = WebGUI::Form::TimeField->new($session,{ defaultValue => 1, });
is($cntl->getValue('10'), '10', '1 default, not mysql mode, all digits');
is($cntl->getValue('00:00:10'), '10', '1 default, not mysql mode, mysql formatted data, 3 pairs');
is($cntl->getValue('00:10'), '600', '1 default, not mysql mode, mysql formatted data, 2 pairs');
is($cntl->getValue('00:10:00'), '600', '1 default, not mysql mode, mysql formatted data, 3 pairs');
$cntl = WebGUI::Form::TimeField->new($session,{ defaultValue => '55:55:55', });
is($cntl->getValue('10'), '00:00:10', 'mysql defaultValue, all digits');
is($cntl->getValue('00:00:10'), '00:00:10', 'mysql defaultValue, mysql formatted data, 3 pairs');
is($cntl->getValue('00:10'), '00:10', 'mysql defaultValue, mysql formatted data, 2 pairs');
is($cntl->getValue('00:10:00'), '00:10:00', 'mysql defaultValue, mysql formatted data, 3 pairs');
$cntl = WebGUI::Form::TimeField->new($session,{ defaultValue => 0, format => 'mysql', });
is($cntl->getValue('10'), '00:00:10', '0 default, mysql mode, all digits');
is($cntl->getValue('00:00:10'), '00:00:10', '0 default, mysql mode, mysql formatted data, 3 pairs');
is($cntl->getValue('00:10'), '00:10', '0 default, mysql mode, mysql formatted data, 2 pairs');
is($cntl->getValue('00:10:00'), '00:10:00', '0 default, mysql mode, mysql formatted data, 3 pairs');
is($cntl->getValue('high noon'), undef, '0 default, mysql mode, mysql formatted data, bad data');
$cntl = WebGUI::Form::TimeField->new($session,{ defaultValue => 1, format => 'mysql', });
is($cntl->getValue('10'), '00:00:10', '1 default, mysql mode, all digits');
is($cntl->getValue('00:00:10'), '00:00:10', '1 default, mysql mode, mysql formatted data, 3 pairs');
is($cntl->getValue('00:10'), '00:10', '1 default, mysql mode, mysql formatted data, 2 pairs');
is($cntl->getValue('00:10:00'), '00:10:00', '1 default, mysql mode, mysql formatted data, 3 pairs');
__END__