Line 0
Link Here
|
|
|
1 |
#!/usr/bin/perl |
2 |
|
3 |
# Copyright 2023 Koha Development team |
4 |
# |
5 |
# This file is part of Koha |
6 |
# |
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. |
11 |
# |
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. |
16 |
# |
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>. |
19 |
|
20 |
use Modern::Perl; |
21 |
|
22 |
use Test::More tests => 3; |
23 |
|
24 |
use Koha::Database; |
25 |
use Koha::Policy::Patrons::Cardnumber; |
26 |
use t::lib::Mocks; |
27 |
use t::lib::TestBuilder; |
28 |
|
29 |
my $schema = Koha::Database->new->schema; |
30 |
my $builder = t::lib::TestBuilder->new; |
31 |
|
32 |
subtest 'is_valid' => sub { |
33 |
|
34 |
plan tests => 21; |
35 |
|
36 |
$schema->storage->txn_begin; |
37 |
|
38 |
my $patron = $builder->build_object({ class => 'Koha::Patrons' }); |
39 |
|
40 |
t::lib::Mocks::mock_preference( 'CardnumberLength', '' ); |
41 |
|
42 |
my $policy = Koha::Policy::Patrons::Cardnumber->new; |
43 |
|
44 |
my $is_valid = $policy->is_valid( $patron->cardnumber ); |
45 |
ok( !$is_valid, "Cardnumber in use, cannot be reused"); |
46 |
|
47 |
$is_valid = $policy->is_valid( $patron->cardnumber, $patron ); |
48 |
ok( $is_valid, "Cardnumber in use but can be used by the same patron"); |
49 |
|
50 |
my $tmp_patron = $builder->build_object({ class => 'Koha::Patrons' }); |
51 |
my $available_cardnumber = $tmp_patron->cardnumber; |
52 |
$tmp_patron->delete; |
53 |
$is_valid = $policy->is_valid( $available_cardnumber ); |
54 |
ok( $is_valid, "Cardnumber not in use"); |
55 |
|
56 |
t::lib::Mocks::mock_preference( 'CardnumberLength', '4' ); |
57 |
|
58 |
$is_valid = $policy->is_valid( "12345" ); |
59 |
ok( !$is_valid, "Invalid cardnumber length"); |
60 |
|
61 |
$is_valid = $policy->is_valid( "123" ); |
62 |
ok( !$is_valid, "Invalid cardnumber length"); |
63 |
|
64 |
my $pref = "10"; |
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"); |
69 |
|
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"); |
75 |
|
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"); |
82 |
|
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"); |
88 |
|
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"); |
94 |
|
95 |
$schema->storage->txn_rollback; |
96 |
}; |
97 |
|
98 |
subtest 'get_valid_length' => sub { |
99 |
|
100 |
plan tests => 5; |
101 |
|
102 |
$schema->storage->txn_begin; |
103 |
|
104 |
my $policy = Koha::Policy::Patrons::Cardnumber->new; |
105 |
|
106 |
t::lib::Mocks::mock_preference('BorrowerMandatoryField', ''); |
107 |
|
108 |
my $pref = "10"; |
109 |
t::lib::Mocks::mock_preference( 'CardnumberLength', $pref ); |
110 |
is_deeply( [ $policy->get_valid_length() ], [ 10, 10 ], '10 => min=10 and max=10' ); |
111 |
|
112 |
$pref = q|10,10|; # Same as before ! |
113 |
t::lib::Mocks::mock_preference( 'CardnumberLength', $pref ); |
114 |
is_deeply( [ $policy->get_valid_length() ], [ 10, 10 ], '10,10 => min=10 and max=10' ); |
115 |
|
116 |
$pref = q|8,10|; # between 8 and 10 chars |
117 |
t::lib::Mocks::mock_preference( 'CardnumberLength', $pref ); |
118 |
is_deeply( [ $policy->get_valid_length() ], [ 8, 10 ], '8,10 => min=8 and max=10' ); |
119 |
|
120 |
$pref = q|,8|; # max 8 chars |
121 |
t::lib::Mocks::mock_preference( 'CardnumberLength', $pref ); |
122 |
is_deeply( [ $policy->get_valid_length() ], [ 0, 8 ], ',8 => min=0 and max=8' ); |
123 |
|
124 |
$pref = q|,8|; # max 8 chars |
125 |
t::lib::Mocks::mock_preference('CardnumberLength', $pref); |
126 |
t::lib::Mocks::mock_preference('BorrowerMandatoryField', 'cardnumber'); |
127 |
is_deeply( [ $policy->get_valid_length() ], [ 1, 8 ], ',8 => min=1 and max=8 if cardnumber is mandatory'); |
128 |
|
129 |
$schema->storage->txn_rollback; |
130 |
|
131 |
}; |
132 |
|
133 |
subtest 'compare with DB data size' => sub { |
134 |
|
135 |
plan tests => 3; |
136 |
|
137 |
$schema->storage->txn_begin; |
138 |
|
139 |
my $policy = Koha::Policy::Patrons::Cardnumber->new; |
140 |
my $borrower = Koha::Schema->resultset('Borrower'); |
141 |
my $cardnumber_size = $borrower->result_source->column_info('cardnumber')->{size}; |
142 |
t::lib::Mocks::mock_preference('BorrowerMandatoryField', ''); |
143 |
|
144 |
my $pref = q|8,|; # At least 8 chars |
145 |
t::lib::Mocks::mock_preference( 'CardnumberLength', $pref ); |
146 |
is_deeply( [ $policy->get_valid_length() ], [ 8, $cardnumber_size ], "8, => min=8 and max=$cardnumber_size" ); |
147 |
|
148 |
$pref = sprintf( ',%d', $cardnumber_size + 1 ); |
149 |
t::lib::Mocks::mock_preference( 'CardnumberLength', $pref ); |
150 |
is_deeply( |
151 |
[ $policy->get_valid_length() ], [ 0, $cardnumber_size ], |
152 |
sprintf( ",%d => min=0 and max=%d", $cardnumber_size + 1, $cardnumber_size ) |
153 |
); |
154 |
|
155 |
my $generated_cardnumber = sprintf("%s1234567890",q|9|x$cardnumber_size); |
156 |
ok( !$policy->is_valid( $generated_cardnumber ), "$generated_cardnumber is longer than $pref => $cardnumber_size is max!"); |
157 |
|
158 |
$schema->storage->txn_rollback; |
159 |
|
160 |
}; |