From 8c571a7aa60d92a37dd0a04a90f08c895be4b4e5 Mon Sep 17 00:00:00 2001 From: Mark Tompsett Date: Wed, 14 Sep 2016 15:14:02 -0400 Subject: [PATCH] Bug 15690: Hardcoded 16 is uncool Signed-off-by: Mark Tompsett Followed test plan in comment #7, works as expectd. Signed-off-by: Marc Signed-off-by: Tomas Cohen Arazi Signed-off-by: Mason James --- C4/Members.pm | 7 ++++++- .../en/modules/admin/preferences/patrons.pref | 2 +- t/Members/cardnumber.t | 15 +++++++++++---- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/C4/Members.pm b/C4/Members.pm index 0615b7411b..774041c364 100644 --- a/C4/Members.pm +++ b/C4/Members.pm @@ -43,6 +43,9 @@ use Koha::AuthUtils qw(hash_password); use Koha::Database; use Koha::Holds; use Koha::List::Patron; +use Koha::Patrons; +use Koha::Patron::Categories; +use Koha::Schema; our (@ISA,@EXPORT,@EXPORT_OK,$debug); @@ -1265,7 +1268,9 @@ sub get_cardnumber_length { } } - $min = 16 if $min > 16; + my $borrower = Koha::Schema->resultset('Borrower'); + my $field_size = $borrower->result_source->column_info('cardnumber')->{size}; + $min = $field_size if $min > $field_size; return ( $min, $max ); } diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref index 1abf08945c..3c4d3ab568 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref @@ -146,7 +146,7 @@ Patrons: - Card numbers for patrons must be - pref: CardnumberLength - "characters long. The length can be a single number to specify an exact length, a range separated by a comma (i.e., 'Min,Max'), or a maximum with no minimum (i.e., ',Max')." - - "If 'cardnumber' is included in the BorrowerMandatoryField list, the minimum length, if not specified here, defaults to one. Maximum cannot be bigger than 16." + - "If 'cardnumber' is included in the BorrowerMandatoryField list, the minimum length, if not specified here, defaults to one. Maximum cannot be bigger than the database field size." - - pref: useDischarge choices: diff --git a/t/Members/cardnumber.t b/t/Members/cardnumber.t index deb76de879..af3a4bb759 100644 --- a/t/Members/cardnumber.t +++ b/t/Members/cardnumber.t @@ -5,6 +5,7 @@ use Test::More tests => 25; use t::lib::Mocks; +use Koha::Schema; use_ok('C4::Members'); BEGIN { @@ -14,6 +15,9 @@ BEGIN { my $dbh = C4::Context->dbh; my $rs = []; +my $borrower = Koha::Schema->resultset('Borrower'); +my $cardnumber_size = $borrower->result_source->column_info('cardnumber')->{size}; + t::lib::Mocks::mock_preference('BorrowerMandatoryField', ''); my $pref = "10"; t::lib::Mocks::mock_preference('CardnumberLength', $pref); @@ -49,7 +53,7 @@ is( C4::Members::checkcardnumber( q{1234567890} ), 0, "1234567890 matches $pref" $pref = q|8,|; # At least 8 chars t::lib::Mocks::mock_preference('CardnumberLength', $pref); -is_deeply( [ C4::Members::get_cardnumber_length() ], [ 8, 16 ], '8, => min=8 and max=16'); +is_deeply( [ C4::Members::get_cardnumber_length() ], [ 8, $cardnumber_size ], "8, => min=8 and max=$cardnumber_size"); $dbh->{mock_add_resultset} = $rs; is( C4::Members::checkcardnumber( q{1234567} ), 2, "1234567 is shorter than $pref"); $dbh->{mock_add_resultset} = $rs; @@ -67,11 +71,14 @@ is( C4::Members::checkcardnumber( q{1234567890123456} ), 2, "1234567890123456 is $dbh->{mock_add_resultset} = $rs; is( C4::Members::checkcardnumber( q{1234567890} ), 2, "1234567890 is longer than $pref"); -$pref = q|,40|; # max 40 chars, not allowed +$pref = sprintf(',%d', $cardnumber_size+1); t::lib::Mocks::mock_preference('CardnumberLength', $pref); -is_deeply( [ C4::Members::get_cardnumber_length() ], [ 0, 16 ], ',40 => min=0 and max=16'); +is_deeply( [ C4::Members::get_cardnumber_length() ], [ 0, $cardnumber_size ], + sprintf(",%d => min=0 and max=%d",$cardnumber_size+1,$cardnumber_size) ); $dbh->{mock_add_resultset} = $rs; -is( C4::Members::checkcardnumber( q{12345678901234567890} ), 2, "12345678901234567890 is longer than $pref => 16 is max!"); + +my $generated_cardnumber = sprintf("%s1234567890",q|9|x$cardnumber_size); +is( C4::Members::checkcardnumber( $generated_cardnumber ), 2, "$generated_cardnumber is longer than $pref => $cardnumber_size is max!"); $pref = q|,8|; # max 8 chars t::lib::Mocks::mock_preference('CardnumberLength', $pref); -- 2.39.5