3 # Copyright 2023 Koha Development team
5 # This file is part of Koha
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Test::More tests => 3;
25 use Koha::Policy::Patrons::Cardnumber;
27 use t::lib::TestBuilder;
29 my $schema = Koha::Database->new->schema;
30 my $builder = t::lib::TestBuilder->new;
32 subtest 'is_valid' => sub {
36 $schema->storage->txn_begin;
38 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
40 t::lib::Mocks::mock_preference( 'CardnumberLength', '' );
42 my $policy = Koha::Policy::Patrons::Cardnumber->new;
44 my $is_valid = $policy->is_valid( $patron->cardnumber );
45 ok( !$is_valid, "Cardnumber in use, cannot be reused" );
47 $is_valid = $policy->is_valid( $patron->cardnumber, $patron );
48 ok( $is_valid, "Cardnumber in use but can be used by the same patron" );
50 my $tmp_patron = $builder->build_object( { class => 'Koha::Patrons' } );
51 my $available_cardnumber = $tmp_patron->cardnumber;
53 $is_valid = $policy->is_valid($available_cardnumber);
54 ok( $is_valid, "Cardnumber not in use" );
56 t::lib::Mocks::mock_preference( 'CardnumberLength', '4' );
58 $is_valid = $policy->is_valid("12345");
59 ok( !$is_valid, "Invalid cardnumber length" );
61 $is_valid = $policy->is_valid("123");
62 ok( !$is_valid, "Invalid cardnumber length" );
65 t::lib::Mocks::mock_preference( 'CardnumberLength', $pref );
66 ok( !$policy->is_valid(q{123456789}), "123456789 is shorter than $pref" );
67 ok( !$policy->is_valid(q{1234567890123456}), "1234567890123456 is longer than $pref" );
68 ok( $policy->is_valid(q{1234567890}), "1234567890 is equal to $pref" );
70 $pref = q|10,10|; # Same as before !
71 t::lib::Mocks::mock_preference( 'CardnumberLength', $pref );
72 ok( !$policy->is_valid(q{123456789}), "123456789 is shorter than $pref" );
73 ok( !$policy->is_valid(q{1234567890123456}), "1234567890123456 is longer than $pref" );
74 ok( $policy->is_valid(q{1234567890}), "1234567890 is equal to $pref" );
76 $pref = q|8,10|; # between 8 and 10 chars
77 t::lib::Mocks::mock_preference( 'CardnumberLength', $pref );
78 ok( $policy->is_valid(q{12345678}), "12345678 matches $pref" );
79 ok( !$policy->is_valid(q{1234567890123456}), "1234567890123456 is longer than $pref" );
80 ok( !$policy->is_valid(q{1234567}), "1234567 is shorter than $pref" );
81 ok( $policy->is_valid(q{1234567890}), "1234567890 matches $pref" );
83 $pref = q|8,|; # At least 8 chars
84 t::lib::Mocks::mock_preference( 'CardnumberLength', $pref );
85 ok( !$policy->is_valid(q{1234567}), "1234567 is shorter than $pref" );
86 ok( $policy->is_valid(q{1234567890123456}), "1234567890123456 matches $pref" );
87 ok( $policy->is_valid(q{1234567890}), "1234567890 matches $pref" );
89 $pref = q|,8|; # max 8 chars
90 t::lib::Mocks::mock_preference( 'CardnumberLength', $pref );
91 ok( $policy->is_valid(q{1234567}), "1234567 matches $pref" );
92 ok( !$policy->is_valid(q{1234567890123456}), "1234567890123456 is longer than $pref" );
93 ok( !$policy->is_valid(q{1234567890}), "1234567890 is longer than $pref" );
95 ok( $policy->is_valid(undef), "If cardnumber is null, we assume they're allowed" );
96 ok( !$policy->is_valid(""), "Empty string is not correct" );
98 $schema->storage->txn_rollback;
101 subtest 'get_valid_length' => sub {
105 $schema->storage->txn_begin;
107 my $policy = Koha::Policy::Patrons::Cardnumber->new;
109 t::lib::Mocks::mock_preference( 'BorrowerMandatoryField', '' );
112 t::lib::Mocks::mock_preference( 'CardnumberLength', $pref );
113 is_deeply( [ $policy->get_valid_length() ], [ 10, 10 ], '10 => min=10 and max=10' );
115 $pref = q|10,10|; # Same as before !
116 t::lib::Mocks::mock_preference( 'CardnumberLength', $pref );
117 is_deeply( [ $policy->get_valid_length() ], [ 10, 10 ], '10,10 => min=10 and max=10' );
119 $pref = q|8,10|; # between 8 and 10 chars
120 t::lib::Mocks::mock_preference( 'CardnumberLength', $pref );
121 is_deeply( [ $policy->get_valid_length() ], [ 8, 10 ], '8,10 => min=8 and max=10' );
123 $pref = q|,8|; # max 8 chars
124 t::lib::Mocks::mock_preference( 'CardnumberLength', $pref );
125 is_deeply( [ $policy->get_valid_length() ], [ 0, 8 ], ',8 => min=0 and max=8' );
127 $pref = q|,8|; # max 8 chars
128 t::lib::Mocks::mock_preference( 'CardnumberLength', $pref );
129 t::lib::Mocks::mock_preference( 'BorrowerMandatoryField', 'cardnumber' );
130 is_deeply( [ $policy->get_valid_length() ], [ 1, 8 ], ',8 => min=1 and max=8 if cardnumber is mandatory' );
132 $schema->storage->txn_rollback;
136 subtest 'compare with DB data size' => sub {
140 $schema->storage->txn_begin;
142 my $policy = Koha::Policy::Patrons::Cardnumber->new;
143 my $borrower = Koha::Schema->resultset('Borrower');
144 my $cardnumber_size = $borrower->result_source->column_info('cardnumber')->{size};
145 t::lib::Mocks::mock_preference( 'BorrowerMandatoryField', '' );
147 my $pref = q|8,|; # At least 8 chars
148 t::lib::Mocks::mock_preference( 'CardnumberLength', $pref );
149 is_deeply( [ $policy->get_valid_length() ], [ 8, $cardnumber_size ], "8, => min=8 and max=$cardnumber_size" );
151 $pref = sprintf( ',%d', $cardnumber_size + 1 );
152 t::lib::Mocks::mock_preference( 'CardnumberLength', $pref );
154 [ $policy->get_valid_length() ], [ 0, $cardnumber_size ],
155 sprintf( ",%d => min=0 and max=%d", $cardnumber_size + 1, $cardnumber_size )
158 my $generated_cardnumber = sprintf( "%s1234567890", q|9| x $cardnumber_size );
160 !$policy->is_valid($generated_cardnumber),
161 "$generated_cardnumber is longer than $pref => $cardnumber_size is max!"
164 $schema->storage->txn_rollback;