File: credit_card.t

package info (click to toggle)
libdata-formvalidator-perl 4.66-1%2Bsqueeze1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 588 kB
  • ctags: 127
  • sloc: perl: 2,756; makefile: 2
file content (98 lines) | stat: -rw-r--r-- 2,693 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use strict;
# check credit card number validation (the cc_number constraint).
# note: this constraint is checked directly in 11_procedural_match.t and
# procedural_valid.t, but here we will test it indirectly through a profile

$^W = 1;

use Data::FormValidator;
use Data::FormValidator::Constraints qw(:closures);
use Test::More tests => 25;

my $dfv_profile_old = {
  required    => [qw(credit_card_type credit_card_number)],
  constraints => {
    credit_card_number => {
      constraint => 'cc_number',
      params     => [ qw(credit_card_number credit_card_type) ],
    },
  },
};

# numbers from
# http://www.verisign.com/support/payflow/manager/selfHelp/testCardNum.html
# maps type  => [ [ invalids ... ], [ valids ... ] ]
my %cc_numbers = (
  Visa       => [ [ '4000111122223333', ],
                  [ '4111111111111111', '4012888888881881', ] ],

  Mastercard => [ [ '5424111122223333', ],
                  [ '5105105105105100', '5555555555554444', ] ],

  Discover   => [ [ '6000111122223333', ],
                  [ '6011111111111117', '6011000990139424', ] ],

  Amex       => [ [ '371500001111222', ],
                  [ '378282246310005',  '371449635398431', ] ],
);

while ( my ($card_type, $numbers) = each %cc_numbers ) {
  foreach my $is_valid ( 0..1 ) {
    foreach my $n ( @{ $numbers->[$is_valid] } ) {
      my $msg = ($is_valid ? "Valid" : "Invalid") . ": $card_type/$n";
      my $input = {
        credit_card_type   => $card_type,
        credit_card_number => $n,
      };

      is( validate_q( $input, $dfv_profile_old ), $is_valid, "$msg (old)" );
    }
  }
}

my $dfv_profile_new = eval {
  { 
    required           => [qw(credit_card_type credit_card_number)],
    constraint_methods => {
      credit_card_number => cc_number({fields => ['credit_card_type']}),
    },
  }
};

ok( ! $@, "cc_number subroutine runs without error" );

# broken cc_number subroutine in older dfv
SKIP: {
  skip "(Older DFV has broken cc_number subroutine)", 12 if $@;

  while ( my ($card_type, $numbers) = each %cc_numbers ) {
    foreach my $is_valid ( 0..1 ) {
      foreach my $n ( @{ $numbers->[$is_valid] } ) {
        my $msg = ($is_valid ? "Valid" : "Invalid") . ": $card_type/$n";
        my $input = {
          credit_card_type   => $card_type,
          credit_card_number => $n,
        };

        is( validate_q( $input, $dfv_profile_new ), $is_valid, "$msg (new)" );
      }
    }
  }
}

##

sub validate_q {
  my ( $data, $profile ) = @_;

  my $dfv_result = eval {
    Data::FormValidator->check($data, $profile);
  };

  if( $@ ) {
    diag "Failed check [$@]";
    return;
  }

  return ($dfv_result->has_invalid || $dfv_result->has_missing) ? 0 : 1;
}