Bug 21468: (QA follow-up) Simplify payload
[koha.git] / t / db_dependent / Koha / Patron.t
1 #!/usr/bin/perl
2
3 # Copyright 2019 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 => 4;
23 use Test::Exception;
24
25 use Koha::Database;
26 use Koha::DateUtils qw(dt_from_string);
27 use Koha::Patrons;
28 use Koha::Patron::Relationships;
29
30 use t::lib::TestBuilder;
31 use t::lib::Mocks;
32
33 my $schema  = Koha::Database->new->schema;
34 my $builder = t::lib::TestBuilder->new;
35
36 subtest 'add_guarantor() tests' => sub {
37
38     plan tests => 6;
39
40     $schema->storage->txn_begin;
41
42     t::lib::Mocks::mock_preference( 'borrowerRelationship', 'father1|father2' );
43
44     my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
45     my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
46
47     throws_ok
48         { $patron_1->add_guarantor({ guarantor_id => $patron_2->borrowernumber }); }
49         'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
50         'Exception is thrown as no relationship passed';
51
52     is( $patron_1->guarantee_relationships->count, 0, 'No guarantors added' );
53
54     throws_ok
55         { $patron_1->add_guarantor({ guarantor_id => $patron_2->borrowernumber, relationship => 'father' }); }
56         'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
57         'Exception is thrown as a wrong relationship was passed';
58
59     is( $patron_1->guarantee_relationships->count, 0, 'No guarantors added' );
60
61     $patron_1->add_guarantor({ guarantor_id => $patron_2->borrowernumber, relationship => 'father1' });
62
63     my $guarantors = $patron_1->guarantor_relationships;
64
65     is( $guarantors->count, 1, 'No guarantors added' );
66
67     $SIG{__WARN__} = sub {}; # FIXME: PrintError = 0 not working!
68
69     throws_ok
70         { $patron_1->add_guarantor({ guarantor_id => $patron_2->borrowernumber, relationship => 'father2' }); }
71         'Koha::Exceptions::Patron::Relationship::DuplicateRelationship',
72         'Exception is thrown for duplicated relationship';
73
74     $schema->storage->txn_rollback;
75 };
76
77 subtest 'add_enrolment_fee_if_needed() tests' => sub {
78
79     plan tests => 2;
80
81     subtest 'category has enrolment fee' => sub {
82         plan tests => 7;
83
84         $schema->storage->txn_begin;
85
86         my $category = $builder->build_object(
87             {
88                 class => 'Koha::Patron::Categories',
89                 value => {
90                     enrolmentfee => 20
91                 }
92             }
93         );
94
95         my $patron = $builder->build_object(
96             {
97                 class => 'Koha::Patrons',
98                 value => {
99                     categorycode => $category->categorycode
100                 }
101             }
102         );
103
104         my $enrollment_fee = $patron->add_enrolment_fee_if_needed();
105         is( $enrollment_fee * 1, 20, 'Enrolment fee amount is correct' );
106         my $account = $patron->account;
107         is( $patron->account->balance * 1, 20, 'Patron charged the enrolment fee' );
108         # second enrolment fee, new
109         $enrollment_fee = $patron->add_enrolment_fee_if_needed(0);
110         # third enrolment fee, renewal
111         $enrollment_fee = $patron->add_enrolment_fee_if_needed(1);
112         is( $patron->account->balance * 1, 60, 'Patron charged the enrolment fees' );
113
114         my @debits = $account->outstanding_debits;
115         is( scalar @debits, 3, '3 enrolment fees' );
116         is( $debits[0]->debit_type_code, 'ACCOUNT', 'Account type set correctly' );
117         is( $debits[1]->debit_type_code, 'ACCOUNT', 'Account type set correctly' );
118         is( $debits[2]->debit_type_code, 'ACCOUNT_RENEW', 'Account type set correctly' );
119
120         $schema->storage->txn_rollback;
121     };
122
123     subtest 'no enrolment fee' => sub {
124
125         plan tests => 3;
126
127         $schema->storage->txn_begin;
128
129         my $category = $builder->build_object(
130             {
131                 class => 'Koha::Patron::Categories',
132                 value => {
133                     enrolmentfee => 0
134                 }
135             }
136         );
137
138         my $patron = $builder->build_object(
139             {
140                 class => 'Koha::Patrons',
141                 value => {
142                     categorycode => $category->categorycode
143                 }
144             }
145         );
146
147         my $enrollment_fee = $patron->add_enrolment_fee_if_needed();
148         is( $enrollment_fee * 1, 0, 'No enrolment fee' );
149         my $account = $patron->account;
150         is( $patron->account->balance, 0, 'Patron not charged anything' );
151
152         my @debits = $account->outstanding_debits;
153         is( scalar @debits, 0, 'no debits' );
154
155         $schema->storage->txn_rollback;
156     };
157 };
158
159 subtest 'to_api() tests' => sub {
160
161     plan tests => 6;
162
163     $schema->storage->txn_begin;
164
165     my $patron_class = Test::MockModule->new('Koha::Patron');
166     $patron_class->mock(
167         'algo',
168         sub { return 'algo' }
169     );
170
171     my $patron = $builder->build_object(
172         {
173             class => 'Koha::Patrons',
174             value => {
175                 debarred => undef
176             }
177         }
178     );
179
180     my $restricted = $patron->to_api->{restricted};
181     ok( defined $restricted, 'restricted is defined' );
182     ok( !$restricted, 'debarred is undef, restricted evaluates to false' );
183
184     $patron->debarred( dt_from_string->add( days => 1 ) )->store->discard_changes;
185     $restricted = $patron->to_api->{restricted};
186     ok( defined $restricted, 'restricted is defined' );
187     ok( $restricted, 'debarred is defined, restricted evaluates to true' );
188
189     my $patron_json = $patron->to_api({ embed => { algo => {} } });
190     ok( exists $patron_json->{algo} );
191     is( $patron_json->{algo}, 'algo' );
192
193     $schema->storage->txn_rollback;
194 };
195
196 subtest 'login_attempts tests' => sub {
197     plan tests => 1;
198
199     $schema->storage->txn_begin;
200
201     my $patron = $builder->build_object(
202         {
203             class => 'Koha::Patrons',
204         }
205     );
206     my $patron_info = $patron->unblessed;
207     $patron->delete;
208     delete $patron_info->{login_attempts};
209     my $new_patron = Koha::Patron->new($patron_info)->store;
210     is( $new_patron->discard_changes->login_attempts, 0, "login_attempts defaults to 0 as expected");
211
212     $schema->storage->txn_rollback;
213 };