]> git.koha-community.org Git - koha.git/blob - t/db_dependent/Koha/Policy/Patrons/Cardnumber.t
Bug 33940: Consider NULL as valid
[koha.git] / t / db_dependent / Koha / Policy / Patrons / Cardnumber.t
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 => 23;
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     ok( $policy->is_valid(undef), "If cardnumber is null, we assume they're allowed" );
96     ok( !$policy->is_valid(""),   "Empty string is not correct" );
97
98     $schema->storage->txn_rollback;
99 };
100
101 subtest 'get_valid_length' => sub {
102
103     plan tests => 5;
104
105     $schema->storage->txn_begin;
106
107     my $policy = Koha::Policy::Patrons::Cardnumber->new;
108
109     t::lib::Mocks::mock_preference( 'BorrowerMandatoryField', '' );
110
111     my $pref = "10";
112     t::lib::Mocks::mock_preference( 'CardnumberLength', $pref );
113     is_deeply( [ $policy->get_valid_length() ], [ 10, 10 ], '10 => min=10 and max=10' );
114
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' );
118
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' );
122
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' );
126
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' );
131
132     $schema->storage->txn_rollback;
133
134 };
135
136 subtest 'compare with DB data size' => sub {
137
138     plan tests => 3;
139
140     $schema->storage->txn_begin;
141
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', '' );
146
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" );
150
151     $pref = sprintf( ',%d', $cardnumber_size + 1 );
152     t::lib::Mocks::mock_preference( 'CardnumberLength', $pref );
153     is_deeply(
154         [ $policy->get_valid_length() ], [ 0, $cardnumber_size ],
155         sprintf( ",%d => min=0 and max=%d", $cardnumber_size + 1, $cardnumber_size )
156     );
157
158     my $generated_cardnumber = sprintf( "%s1234567890", q|9| x $cardnumber_size );
159     ok(
160         !$policy->is_valid($generated_cardnumber),
161         "$generated_cardnumber is longer than $pref => $cardnumber_size is max!"
162     );
163
164     $schema->storage->txn_rollback;
165
166 };