Changeset 1070


Ignore:
Timestamp:
Jun 12, 2007, 10:47:03 PM (14 years ago)
Author:
Dominic Hargreaves
Message:

Add general validation framework, and add test for lat/long and os_x/os_y (closes #22)

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/Changes

    r1065 r1070  
    1919        Added new div#nonexistent_node_message for displaying message when
    2020          someone tries to view a nonexistent node.
     21        Validate input geodata (#22)
    2122
    22230.60    13 May 2007
  • trunk/lib/OpenGuides.pm

    r1069 r1070  
    460460  $guide->display_edit_form(
    461461                             id => "Vivat Bacchus",
     462                             vars => \%vars,
     463                             content => $content,
     464                             metadata => \%metadata,
     465                             checksum => $checksum
    462466                           );
    463467
     
    465469C<return_output> parameter can be used to return the output instead of
    466470printing it to STDOUT.
     471
     472If this is to redisplay an existing edit, the content, metadata
     473and checksum may be supplied in those arguments
     474
     475Extra template variables may be supplied in the vars argument
    467476
    468477=cut
     
    500509                    deter_robots    => 1,
    501510    );
     511
     512    # Override some things if we were supplied with them
     513    $tt_vars{content} = $args{content} if $args{content};
     514    $tt_vars{checksum} = $args{checksum} if $args{checksum};
     515    if (defined $args{vars}) {
     516        my %supplied_vars = %{$args{vars}};
     517        foreach my $key ( keys %supplied_vars ) {
     518            $tt_vars{$key} = $supplied_vars{$key};
     519        }
     520    }
     521    if (defined $args{metadata}) {
     522        my %supplied_metadata = %{$args{metadata}};
     523        foreach my $key ( keys %supplied_metadata ) {
     524            $tt_vars{$key} = $supplied_metadata{$key};
     525        }
     526    }
    502527
    503528    my $output = $self->process_template(
     
    13411366                                    : 0;
    13421367
     1368    # General validation
     1369    my $fails = OpenGuides::Utils->validate_edit(
     1370        cgi_obj  => $q
     1371    );
     1372
     1373    if ( scalar @{$fails} ) {
     1374        my %vars = (
     1375            validate_failed => $fails
     1376        );
     1377
     1378        my $output = $self->display_edit_form(
     1379                           id            => $node,
     1380                           content       => CGI->escapeHTML($content),
     1381                           metadata      => \%new_metadata,
     1382                           vars          => \%vars,
     1383                           checksum      => CGI->escapeHTML($checksum),
     1384                           return_output => 1
     1385        );
     1386
     1387        return $output if $return_output;
     1388        print $output;
     1389        return;
     1390    }
     1391
    13431392    # If we can, check to see if this edit looks like spam.
    13441393    my $spam_detector = $config->spam_detector_module;
  • trunk/lib/OpenGuides/Utils.pm

    r1069 r1070  
    356356}
    357357
     358=item B<validate_edit>
     359
     360    my $fails = OpenGuides::Utils->validate_edit(
     361        id       => $node,
     362        cgi_obj  => $q
     363    );
     364
     365Checks supplied content for general validity. If anything is invalid,
     366returns an array ref of errors to report to the user.
     367
     368=cut
     369
     370sub validate_edit {
     371    my ( $self, %args ) = @_;
     372    my $q = $args{cgi_obj};
     373    my @fails;
     374    push @fails, "Content missing" unless $q;
     375    return \@fails if @fails;
     376
     377    # Now do our real validation
     378    # Numeric tests
     379    foreach my $var (qw(os_x os_y)) {
     380        if ($q->param($var) and $q->param($var) !~ /^-?\d+$/) {
     381            push @fails, "$var must be integer, was: " . $q->param($var);
     382        }
     383    }
     384
     385    foreach my $var (qw(latitude longitude)) {
     386        if ($q->param($var) and $q->param($var) !~ /^-?\d+\.?(\d+)?$/) {
     387            push @fails, "$var must be numeric, was: " . $q->param($var);
     388        }
     389    }
     390
     391    return \@fails;
     392
     393};
     394
    358395=back
    359396
  • trunk/t/17_commit_node.t

    r956 r1070  
    1919
    2020
    21 plan tests => 5;
     21plan tests => 7;
    2222
    2323# Clear out the database from any previous runs.
     
    9898is( $node{version}, 2, "First version" );
    9999is( $node{metadata}->{edit_type}[0], "Normal edit", "Right edit type" );
     100
     101# Now try to commit some invalid data, and make sure we get an edit form back
     102$q = CGI->new;
     103$q->param( -name => "content", -value => "foo" );
     104$q->param( -name => "categories", -value => "" );
     105$q->param( -name => "locales", -value => "" );
     106$q->param( -name => "phone", -value => "" );
     107$q->param( -name => "fax", -value => "" );
     108$q->param( -name => "website", -value => "" );
     109$q->param( -name => "hours_text", -value => "" );
     110$q->param( -name => "address", -value => "" );
     111$q->param( -name => "postcode", -value => "" );
     112$q->param( -name => "map_link", -value => "" );
     113$q->param( -name => "os_x", -value => "fooooo" );
     114$q->param( -name => "os_y", -value => "" );
     115$q->param( -name => "username", -value => "bob" );
     116$q->param( -name => "comment", -value => "foo" );
     117$q->param( -name => "node_image", -value => "image" );
     118$q->param( -name => "edit_type", -value => "Minor tidying" );
     119
     120$output = $guide->commit_node(
     121                                return_output => 1,
     122                                id => "Wombats again",
     123                                cgi_obj => $q,
     124                             );
     125
     126like( $output, qr/Your input was invalid/,
     127    "Edit form displayed and invalid input message shown if invalid input" );
     128
     129like( $output, qr/os_x must be integer/,
     130    "Edit form displayed and os_x integer message displayed" );
  • trunk/templates/edit_form.tt

    r1053 r1070  
    1010      You will need to manually merge your changes into the new version.
    1111    </div>
     12  [% END %]
     13
     14  [% IF validate_failed %]
     15    <div class="warning_text">
     16      <p>
     17        Your input was invalid:
     18      </p>
     19      <ul>
     20      [% FOREACH error = validate_failed %]
     21        <li>[% error %]</li>
     22      [% END %]
     23      </ul>
     24      <p>
     25        Please correct your input data and try again.
     26      </p>
    1227  [% END %]
    1328
Note: See TracChangeset for help on using the changeset viewer.