Author: dom
Date: 2007-06-12 22:47:03 +0100 (Tue, 12 Jun 2007)
New Revision: 1070
Modified:
trunk/Changes
trunk/lib/OpenGuides.pm
trunk/lib/OpenGuides/Utils.pm
trunk/t/17_commit_node.t
trunk/templates/edit_form.tt
Log:
Add general validation framework, and add test for lat/long and os_x/os_y (closes #22)
Modified: trunk/Changes
===================================================================
--- trunk/Changes 2007-06-11 22:26:47 UTC (rev 1069)
+++ trunk/Changes 2007-06-12 21:47:03 UTC (rev 1070)
@@ -18,6 +18,7 @@
action=display (partial fix for #102)
Added new div#nonexistent_node_message for displaying message when
someone tries to view a nonexistent node.
+ Validate input geodata (#22)
0.60 13 May 2007
Removed footer search from edit page (shouldn't have been there).
Modified: trunk/lib/OpenGuides/Utils.pm
===================================================================
--- trunk/lib/OpenGuides/Utils.pm 2007-06-11 22:26:47 UTC (rev 1069)
+++ trunk/lib/OpenGuides/Utils.pm 2007-06-12 21:47:03 UTC (rev 1070)
@@ -355,6 +355,43 @@
}
}
+=item B<validate_edit>
+
+ my $fails = OpenGuides::Utils->validate_edit(
+ id => $node,
+ cgi_obj => $q
+ );
+
+Checks supplied content for general validity. If anything is invalid,
+returns an array ref of errors to report to the user.
+
+=cut
+
+sub validate_edit {
+ my ( $self, %args ) = @_;
+ my $q = $args{cgi_obj};
+ my @fails;
+ push @fails, "Content missing" unless $q;
+ return \@fails if @fails;
+
+ # Now do our real validation
+ # Numeric tests
+ foreach my $var (qw(os_x os_y)) {
+ if ($q->param($var) and $q->param($var) !~ /^-?\d+$/) {
+ push @fails, "$var must be integer, was: " . $q->param($var);
+ }
+ }
+
+ foreach my $var (qw(latitude longitude)) {
+ if ($q->param($var) and $q->param($var) !~ /^-?\d+\.?(\d+)?$/) {
+ push @fails, "$var must be numeric, was: " . $q->param($var);
+ }
+ }
+
+ return \@fails;
+
+};
+
=back
=head1 AUTHOR
Modified: trunk/lib/OpenGuides.pm
===================================================================
--- trunk/lib/OpenGuides.pm 2007-06-11 22:26:47 UTC (rev 1069)
+++ trunk/lib/OpenGuides.pm 2007-06-12 21:47:03 UTC (rev 1070)
@@ -459,12 +459,21 @@
$guide->display_edit_form(
id => "Vivat Bacchus",
+ vars => \%vars,
+ content => $content,
+ metadata => \%metadata,
+ checksum => $checksum
);
Display an edit form for the specified node. As with other methods, the
C<return_output> parameter can be used to return the output instead of
printing it to STDOUT.
+If this is to redisplay an existing edit, the content, metadata
+and checksum may be supplied in those arguments
+
+Extra template variables may be supplied in the vars argument
+
=cut
sub display_edit_form {
@@ -500,6 +509,22 @@
deter_robots => 1,
);
+ # Override some things if we were supplied with them
+ $tt_vars{content} = $args{content} if $args{content};
+ $tt_vars{checksum} = $args{checksum} if $args{checksum};
+ if (defined $args{vars}) {
+ my %supplied_vars = %{$args{vars}};
+ foreach my $key ( keys %supplied_vars ) {
+ $tt_vars{$key} = $supplied_vars{$key};
+ }
+ }
+ if (defined $args{metadata}) {
+ my %supplied_metadata = %{$args{metadata}};
+ foreach my $key ( keys %supplied_metadata ) {
+ $tt_vars{$key} = $supplied_metadata{$key};
+ }
+ }
+
my $output = $self->process_template(
id => $node,
template => "edit_form.tt",
@@ -1340,6 +1365,30 @@
? 1
: 0;
+ # General validation
+ my $fails = OpenGuides::Utils->validate_edit(
+ cgi_obj => $q
+ );
+
+ if ( scalar @{$fails} ) {
+ my %vars = (
+ validate_failed => $fails
+ );
+
+ my $output = $self->display_edit_form(
+ id => $node,
+ content => CGI->escapeHTML($content),
+ metadata => \%new_metadata,
+ vars => \%vars,
+ checksum => CGI->escapeHTML($checksum),
+ return_output => 1
+ );
+
+ return $output if $return_output;
+ print $output;
+ return;
+ }
+
# If we can, check to see if this edit looks like spam.
my $spam_detector = $config->spam_detector_module;
my $is_spam;
Modified: trunk/t/17_commit_node.t
===================================================================
--- trunk/t/17_commit_node.t 2007-06-11 22:26:47 UTC (rev 1069)
+++ trunk/t/17_commit_node.t 2007-06-12 21:47:03 UTC (rev 1070)
@@ -18,7 +18,7 @@
}
-plan tests => 5;
+plan tests => 7;
# Clear out the database from any previous runs.
unlink "t/node.db";
@@ -97,3 +97,34 @@
%node = $wiki->retrieve_node("Wombats");
is( $node{version}, 2, "First version" );
is( $node{metadata}->{edit_type}[0], "Normal edit", "Right edit type" );
+
+# Now try to commit some invalid data, and make sure we get an edit form back
+$q = CGI->new;
+$q->param( -name => "content", -value => "foo" );
+$q->param( -name => "categories", -value => "" );
+$q->param( -name => "locales", -value => "" );
+$q->param( -name => "phone", -value => "" );
+$q->param( -name => "fax", -value => "" );
+$q->param( -name => "website", -value => "" );
+$q->param( -name => "hours_text", -value => "" );
+$q->param( -name => "address", -value => "" );
+$q->param( -name => "postcode", -value => "" );
+$q->param( -name => "map_link", -value => "" );
+$q->param( -name => "os_x", -value => "fooooo" );
+$q->param( -name => "os_y", -value => "" );
+$q->param( -name => "username", -value => "bob" );
+$q->param( -name => "comment", -value => "foo" );
+$q->param( -name => "node_image", -value => "image" );
+$q->param( -name => "edit_type", -value => "Minor tidying" );
+
+$output = $guide->commit_node(
+ return_output => 1,
+ id => "Wombats again",
+ cgi_obj => $q,
+ );
+
+like( $output, qr/Your input was invalid/,
+ "Edit form displayed and invalid input message shown if invalid input" );
+
+like( $output, qr/os_x must be integer/,
+ "Edit form displayed and os_x integer message displayed" );
Modified: trunk/templates/edit_form.tt
===================================================================
--- trunk/templates/edit_form.tt 2007-06-11 22:26:47 UTC (rev 1069)
+++ trunk/templates/edit_form.tt 2007-06-12 21:47:03 UTC (rev 1070)
@@ -11,6 +11,21 @@
</div>
[% END %]
+ [% IF validate_failed %]
+ <div class="warning_text">
+ <p>
+ Your input was invalid:
+ </p>
+ <ul>
+ [% FOREACH error = validate_failed %]
+ <li>[% error %]</li>
+ [% END %]
+ </ul>
+ <p>
+ Please correct your input data and try again.
+ </p>
+ [% END %]
+
[% IF preview_html AND preview_above_edit_box %]
<div id="maincontent">
<h2>Preview</h2>