Bug 19036: (follow-up) Test credits should be credits
[koha.git] / t / Members / cardnumber.t
blob7f4e2b05354bd9679e5077cbd0bdccd28f84ec68
1 #!/usr/bin/env perl
3 use Modern::Perl;
4 use Module::Load::Conditional qw/check_install/;
5 use Test::More;
6 use Test::MockModule;
8 use t::lib::Mocks;
10 use_ok('C4::Members');
12 BEGIN {
13 if ( check_install( module => 'Test::DBIx::Class' ) ) {
14 plan tests => 25;
15 } else {
16 plan skip_all => "Need Test::DBIx::Class"
20 use Test::DBIx::Class;
22 my $db = Test::MockModule->new('Koha::Database');
23 $db->mock( _new_schema => sub { return Schema(); } );
25 my $dbh = C4::Context->dbh;
26 my $rs = [];
28 my $borrower = Koha::Schema->resultset('Borrower');
29 my $cardnumber_size = $borrower->result_source->column_info('cardnumber')->{size};
31 t::lib::Mocks::mock_preference('BorrowerMandatoryField', '');
32 my $pref = "10";
33 t::lib::Mocks::mock_preference('CardnumberLength', $pref);
34 is_deeply( [ C4::Members::get_cardnumber_length() ], [ 10, 10 ], '10 => min=10 and max=10');
35 $dbh->{mock_add_resultset} = $rs;
36 is( C4::Members::checkcardnumber( q{123456789} ), 2, "123456789 is shorter than $pref");
37 $dbh->{mock_add_resultset} = $rs;
38 is( C4::Members::checkcardnumber( q{1234567890123456} ), 2, "1234567890123456 is longer than $pref");
39 $dbh->{mock_add_resultset} = $rs;
40 is( C4::Members::checkcardnumber( q{1234567890} ), 0, "1234567890 is equal to $pref");
42 $pref = q|10,10|; # Same as before !
43 t::lib::Mocks::mock_preference('CardnumberLength', $pref);
44 is_deeply( [ C4::Members::get_cardnumber_length() ], [ 10, 10 ], '10,10 => min=10 and max=10');
45 $dbh->{mock_add_resultset} = $rs;
46 is( C4::Members::checkcardnumber( q{123456789} ), 2, "123456789 is shorter than $pref");
47 $dbh->{mock_add_resultset} = $rs;
48 is( C4::Members::checkcardnumber( q{1234567890123456} ), 2, "1234567890123456 is longer than $pref");
49 $dbh->{mock_add_resultset} = $rs;
50 is( C4::Members::checkcardnumber( q{1234567890} ), 0, "1234567890 is equal to $pref");
52 $pref = q|8,10|; # between 8 and 10 chars
53 t::lib::Mocks::mock_preference('CardnumberLength', $pref);
54 is_deeply( [ C4::Members::get_cardnumber_length() ], [ 8, 10 ], '8,10 => min=8 and max=10');
55 $dbh->{mock_add_resultset} = $rs;
56 is( C4::Members::checkcardnumber( q{12345678} ), 0, "12345678 matches $pref");
57 $dbh->{mock_add_resultset} = $rs;
58 is( C4::Members::checkcardnumber( q{1234567890123456} ), 2, "1234567890123456 is longer than $pref");
59 $dbh->{mock_add_resultset} = $rs;
60 is( C4::Members::checkcardnumber( q{1234567} ), 2, "1234567 is shorter than $pref");
61 $dbh->{mock_add_resultset} = $rs;
62 is( C4::Members::checkcardnumber( q{1234567890} ), 0, "1234567890 matches $pref");
64 $pref = q|8,|; # At least 8 chars
65 t::lib::Mocks::mock_preference('CardnumberLength', $pref);
66 is_deeply( [ C4::Members::get_cardnumber_length() ], [ 8, $cardnumber_size ], "8, => min=8 and max=$cardnumber_size");
67 $dbh->{mock_add_resultset} = $rs;
68 is( C4::Members::checkcardnumber( q{1234567} ), 2, "1234567 is shorter than $pref");
69 $dbh->{mock_add_resultset} = $rs;
70 is( C4::Members::checkcardnumber( q{1234567890123456} ), 0, "1234567890123456 matches $pref");
71 $dbh->{mock_add_resultset} = $rs;
72 is( C4::Members::checkcardnumber( q{1234567890} ), 0, "1234567890 matches $pref");
74 $pref = q|,8|; # max 8 chars
75 t::lib::Mocks::mock_preference('CardnumberLength', $pref);
76 is_deeply( [ C4::Members::get_cardnumber_length() ], [ 0, 8 ], ',8 => min=0 and max=8');
77 $dbh->{mock_add_resultset} = $rs;
78 is( C4::Members::checkcardnumber( q{1234567} ), 0, "1234567 matches $pref");
79 $dbh->{mock_add_resultset} = $rs;
80 is( C4::Members::checkcardnumber( q{1234567890123456} ), 2, "1234567890123456 is longer than $pref");
81 $dbh->{mock_add_resultset} = $rs;
82 is( C4::Members::checkcardnumber( q{1234567890} ), 2, "1234567890 is longer than $pref");
84 $pref = sprintf(',%d', $cardnumber_size+1);
85 t::lib::Mocks::mock_preference('CardnumberLength', $pref);
86 is_deeply( [ C4::Members::get_cardnumber_length() ], [ 0, $cardnumber_size ],
87 sprintf(",%d => min=0 and max=%d",$cardnumber_size+1,$cardnumber_size) );
88 $dbh->{mock_add_resultset} = $rs;
90 my $generated_cardnumber = sprintf("%s1234567890",q|9|x$cardnumber_size);
91 is( C4::Members::checkcardnumber( $generated_cardnumber ), 2, "$generated_cardnumber is longer than $pref => $cardnumber_size is max!");
93 $pref = q|,8|; # max 8 chars
94 t::lib::Mocks::mock_preference('CardnumberLength', $pref);
95 t::lib::Mocks::mock_preference('BorrowerMandatoryField', 'cardnumber');
96 is_deeply( [ C4::Members::get_cardnumber_length() ], [ 1, 8 ], ',8 => min=1 and max=8 if cardnumber is mandatory');