Amon2-Plugin-Web-FormValidator-Simple

 view release on metacpan or  search on metacpan

t/01_validate.t  view on Meta::CPAN

use utf8;
use strict;
use warnings;
use Test::More;
use Test::Requires 'Test::WWW::Mechanize::PSGI';

use Encode;
use Plack::Middleware::Lint;
use Text::Xslate;

my $conf = +{ # app config {{{
    validator => +{
        messages => +{
            message_test => +{
                pass => +{
                    NOT_BLANK => 'パスワードを指定してください。',
                    ASCII => 'パスワードに不正な文字列があります。',
                    LENGTH => 'パスワードの長さは 16 文字以内にしてください。',
                },
                mail1 => +{
                    NOT_BLANK => 'メールアドレスを指定してください。',
                    ASCII => 'メールアドレスに不正な文字列があります。',
                },
                mail2 => +{
                    NOT_BLANK => 'メールアドレスを指定してください。',
                    ASCII => 'メールアドレスに不正な文字列があります。',
                },
                mails => +{
                    DUPLICATION => 'メールアドレスが一致しません。',
                },
            },
        },
        message_format => 'error: %s',
        message_decode_from => 'UTF-8',
    },
};
#}}}

# test app setting
{ #{{{
    package MyApp;
    use parent qw!Amon2!;

    sub load_config { $conf }

    package MyApp::Web;
    use parent -norequire, qw!MyApp!;
    use parent qw!Amon2::Web!;
    __PACKAGE__->load_plugins('Web::FormValidator::Simple');

    #{{{
    my $xslate = Text::Xslate->new(
        syntax => 'TTerse',
        function => +{c => sub {__PACKAGE__->context}},
        path => +{
            index => <<TT,
<!doctype html>
this is index.
TT
            error => <<TT,
<!doctype html>

t/01_validate.t  view on Meta::CPAN

SKIP: { # complex validation 2 - needs Date::Calc {{{
    eval { require Date::Calc; };
    skip 'Date::Calc not installed', 2 if $@;

    my $str = query_string(
        year => 2012,
        month => 9,
        day => -10,
    );

    $m->get_ok("/complex_validation2?$str");
    is n($m->content) => n(<<HTML);
<!doctype html>
<ul>
    <li>invalid: day - UINT
    <li>invalid: date - DATE
</ul>
HTML
} #}}}

{ # messages {{{
    my $str = query_string(
        mail1 => '',
        mail2 => 'あいうえお@どこか.com',
        pass => 'あいうえおあいうえおあいうえおあいうえお',
        action => 'message_test',
    );

    $m->get_ok("/complex_validation1?$str");
    is n($m->content) => n(<<HTML);
<!doctype html>
<ul>
    <li>invalid: pass - ASCII
    <li>invalid: pass - LENGTH
    <li>invalid: mail1 - NOT_BLANK
    <li>invalid: mail2 - ASCII
    <li>invalid: mails - DUPLICATION
</ul><ul>
    <li>error: パスワードに不正な文字列があります。
    <li>error: パスワードの長さは 16 文字以内にしてください。
    <li>error: メールアドレスを指定してください。
    <li>error: メールアドレスに不正な文字列があります。
    <li>error: メールアドレスが一致しません。
</ul>
HTML
} #}}}

done_testing;

# helper functions {{{
sub n {
    local $_ = shift;
    s/\n$//;
    $_;
}

sub query_string {
    my %param = ref $_[0] ? %{$_[0]} : @_;
    my $str = '';
    while (my ($k, $v) = each %param) {
        $v = encode(utf8 => $v);
        $v =~ s/([^a-zA-Z0-9_.!~*'()-])/'%' . unpack('H2', $1)/eg;
        $str .= "&$k=$v";
    }
    $str =~ s/^&//;

    return $str;
}
#}}}



( run in 0.795 second using v1.01-cache-2.11-cpan-39bf76dae61 )