Line 0
Link Here
|
|
|
1 |
#!/usr/bin/env perl |
2 |
|
3 |
use Modern::Perl; |
4 |
use Test::More tests =>14; |
5 |
|
6 |
use Test::MockModule; |
7 |
use DBD::Mock; |
8 |
|
9 |
use_ok('C4::Members'); |
10 |
|
11 |
my $module_context = new Test::MockModule('C4::Context'); |
12 |
$module_context->mock( |
13 |
'_new_dbh', |
14 |
sub { |
15 |
my $dbh = DBI->connect( 'DBI:Mock:', '', '' ) |
16 |
|| die "Cannot create handle: $DBI::errstr\n"; |
17 |
return $dbh; |
18 |
} |
19 |
); |
20 |
|
21 |
my $dbh = C4::Context->dbh; |
22 |
my $rs = []; |
23 |
|
24 |
my $pref = "10"; |
25 |
set_pref( $module_context, $pref ); |
26 |
|
27 |
$dbh->{mock_add_resultset} = $rs; |
28 |
is( C4::Members::checkcardnumber( q{123456789} ), 1, "123456789 is shorter than $pref"); |
29 |
$dbh->{mock_add_resultset} = $rs; |
30 |
is( C4::Members::checkcardnumber( q{12345678901234567890} ), 1, "12345678901234567890 is longer than $pref"); |
31 |
$dbh->{mock_add_resultset} = $rs; |
32 |
is( C4::Members::checkcardnumber( q{1234567890} ), 0, "1234567890 is equal to $pref"); |
33 |
|
34 |
$pref = q|10,10|; # Same as before ! |
35 |
set_pref( $module_context, $pref ); |
36 |
$dbh->{mock_add_resultset} = $rs; |
37 |
is( C4::Members::checkcardnumber( q{123456789} ), 1, "123456789 is shorter than $pref"); |
38 |
$dbh->{mock_add_resultset} = $rs; |
39 |
is( C4::Members::checkcardnumber( q{12345678901234567890} ), 1, "12345678901234567890 is longer than $pref"); |
40 |
$dbh->{mock_add_resultset} = $rs; |
41 |
is( C4::Members::checkcardnumber( q{1234567890} ), 0, "1234567890 is equal to $pref"); |
42 |
|
43 |
$pref = q|8,10|; # between 8 and 10 chars |
44 |
set_pref( $module_context, $pref ); |
45 |
$dbh->{mock_add_resultset} = $rs; |
46 |
is( C4::Members::checkcardnumber( q{12345678} ), 0, "12345678 matches $pref"); |
47 |
$dbh->{mock_add_resultset} = $rs; |
48 |
is( C4::Members::checkcardnumber( q{12345678901234567890} ), 1, "12345678901234567890 is longer than $pref"); |
49 |
$dbh->{mock_add_resultset} = $rs; |
50 |
is( C4::Members::checkcardnumber( q{1234567} ), 1, "1234567 is shorter than $pref"); |
51 |
$dbh->{mock_add_resultset} = $rs; |
52 |
is( C4::Members::checkcardnumber( q{1234567890} ), 0, "1234567890 matches $pref"); |
53 |
|
54 |
$pref = q|8,|; # At least 8 chars |
55 |
set_pref( $module_context, $pref ); |
56 |
$dbh->{mock_add_resultset} = $rs; |
57 |
is( C4::Members::checkcardnumber( q{1234567} ), 1, "1234567 is shorter than $pref"); |
58 |
$dbh->{mock_add_resultset} = $rs; |
59 |
is( C4::Members::checkcardnumber( q{12345678901234567890} ), 0, "12345678901234567890 matches $pref"); |
60 |
$dbh->{mock_add_resultset} = $rs; |
61 |
is( C4::Members::checkcardnumber( q{1234567890} ), 0, "12345678 matches $pref"); |
62 |
|
63 |
sub set_pref { |
64 |
my ( $module, $value ) = @_; |
65 |
$module->mock('preference', sub { return $value } ); |
66 |
} |