Bug 11319: Template modifications
[koha.git] / t / Members / cardnumber.t
blobbfad5cec8c9104d45126e46ff51be6ea28eb87cd
1 #!/usr/bin/env perl
3 use Modern::Perl;
4 use Test::More tests => 23;
6 use Test::MockModule;
7 use DBD::Mock;
8 use t::lib::Mocks;
10 use_ok('C4::Members');
12 my $module_context = new Test::MockModule('C4::Context');
13 $module_context->mock(
14 '_new_dbh',
15 sub {
16 my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
17 || die "Cannot create handle: $DBI::errstr\n";
18 return $dbh;
22 my $dbh = C4::Context->dbh;
23 my $rs = [];
25 t::lib::Mocks::mock_preference('BorrowerMandatoryField', '');
26 my $pref = "10";
27 t::lib::Mocks::mock_preference('CardnumberLength', $pref);
28 is_deeply( [ C4::Members::get_cardnumber_length() ], [ 10, 10 ], '10 => min=10 and max=10');
29 $dbh->{mock_add_resultset} = $rs;
30 is( C4::Members::checkcardnumber( q{123456789} ), 2, "123456789 is shorter than $pref");
31 $dbh->{mock_add_resultset} = $rs;
32 is( C4::Members::checkcardnumber( q{1234567890123456} ), 2, "1234567890123456 is longer than $pref");
33 $dbh->{mock_add_resultset} = $rs;
34 is( C4::Members::checkcardnumber( q{1234567890} ), 0, "1234567890 is equal to $pref");
36 $pref = q|10,10|; # Same as before !
37 t::lib::Mocks::mock_preference('CardnumberLength', $pref);
38 is_deeply( [ C4::Members::get_cardnumber_length() ], [ 10, 10 ], '10,10 => min=10 and max=10');
39 $dbh->{mock_add_resultset} = $rs;
40 is( C4::Members::checkcardnumber( q{123456789} ), 2, "123456789 is shorter than $pref");
41 $dbh->{mock_add_resultset} = $rs;
42 is( C4::Members::checkcardnumber( q{1234567890123456} ), 2, "1234567890123456 is longer than $pref");
43 $dbh->{mock_add_resultset} = $rs;
44 is( C4::Members::checkcardnumber( q{1234567890} ), 0, "1234567890 is equal to $pref");
46 $pref = q|8,10|; # between 8 and 10 chars
47 t::lib::Mocks::mock_preference('CardnumberLength', $pref);
48 is_deeply( [ C4::Members::get_cardnumber_length() ], [ 8, 10 ], '8,10 => min=8 and max=10');
49 $dbh->{mock_add_resultset} = $rs;
50 is( C4::Members::checkcardnumber( q{12345678} ), 0, "12345678 matches $pref");
51 $dbh->{mock_add_resultset} = $rs;
52 is( C4::Members::checkcardnumber( q{1234567890123456} ), 2, "1234567890123456 is longer than $pref");
53 $dbh->{mock_add_resultset} = $rs;
54 is( C4::Members::checkcardnumber( q{1234567} ), 2, "1234567 is shorter than $pref");
55 $dbh->{mock_add_resultset} = $rs;
56 is( C4::Members::checkcardnumber( q{1234567890} ), 0, "1234567890 matches $pref");
58 $pref = q|8,|; # At least 8 chars
59 t::lib::Mocks::mock_preference('CardnumberLength', $pref);
60 is_deeply( [ C4::Members::get_cardnumber_length() ], [ 8, 16 ], '8, => min=8 and max=16');
61 $dbh->{mock_add_resultset} = $rs;
62 is( C4::Members::checkcardnumber( q{1234567} ), 2, "1234567 is shorter than $pref");
63 $dbh->{mock_add_resultset} = $rs;
64 is( C4::Members::checkcardnumber( q{1234567890123456} ), 0, "1234567890123456 matches $pref");
65 $dbh->{mock_add_resultset} = $rs;
66 is( C4::Members::checkcardnumber( q{1234567890} ), 0, "1234567890 matches $pref");
68 $pref = q|,8|; # max 8 chars
69 t::lib::Mocks::mock_preference('CardnumberLength', $pref);
70 is_deeply( [ C4::Members::get_cardnumber_length() ], [ 0, 8 ], ',8 => min=0 and max=8');
71 $dbh->{mock_add_resultset} = $rs;
72 is( C4::Members::checkcardnumber( q{1234567} ), 0, "1234567 matches $pref");
73 $dbh->{mock_add_resultset} = $rs;
74 is( C4::Members::checkcardnumber( q{1234567890123456} ), 2, "1234567890123456 is longer than $pref");
75 $dbh->{mock_add_resultset} = $rs;
76 is( C4::Members::checkcardnumber( q{1234567890} ), 2, "1234567890 is longer than $pref");
78 t::lib::Mocks::mock_preference('BorrowerMandatoryField', 'cardnumber');
79 is_deeply( [ C4::Members::get_cardnumber_length() ], [ 1, 8 ], ',8 => min=1 and max=8 if cardnumber is mandatory');