Bug 26384: Fix executable flags
[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 => 5;
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     {
68         local *STDERR;
69         open STDERR, '>', '/dev/null';
70         throws_ok
71             { $patron_1->add_guarantor({ guarantor_id => $patron_2->borrowernumber, relationship => 'father2' }); }
72             'Koha::Exceptions::Patron::Relationship::DuplicateRelationship',
73             'Exception is thrown for duplicated relationship';
74         close STDERR;
75     }
76
77     $schema->storage->txn_rollback;
78 };
79
80 subtest 'add_enrolment_fee_if_needed() tests' => sub {
81
82     plan tests => 2;
83
84     subtest 'category has enrolment fee' => sub {
85         plan tests => 7;
86
87         $schema->storage->txn_begin;
88
89         my $category = $builder->build_object(
90             {
91                 class => 'Koha::Patron::Categories',
92                 value => {
93                     enrolmentfee => 20
94                 }
95             }
96         );
97
98         my $patron = $builder->build_object(
99             {
100                 class => 'Koha::Patrons',
101                 value => {
102                     categorycode => $category->categorycode
103                 }
104             }
105         );
106
107         my $enrollment_fee = $patron->add_enrolment_fee_if_needed();
108         is( $enrollment_fee * 1, 20, 'Enrolment fee amount is correct' );
109         my $account = $patron->account;
110         is( $patron->account->balance * 1, 20, 'Patron charged the enrolment fee' );
111         # second enrolment fee, new
112         $enrollment_fee = $patron->add_enrolment_fee_if_needed(0);
113         # third enrolment fee, renewal
114         $enrollment_fee = $patron->add_enrolment_fee_if_needed(1);
115         is( $patron->account->balance * 1, 60, 'Patron charged the enrolment fees' );
116
117         my @debits = $account->outstanding_debits;
118         is( scalar @debits, 3, '3 enrolment fees' );
119         is( $debits[0]->debit_type_code, 'ACCOUNT', 'Account type set correctly' );
120         is( $debits[1]->debit_type_code, 'ACCOUNT', 'Account type set correctly' );
121         is( $debits[2]->debit_type_code, 'ACCOUNT_RENEW', 'Account type set correctly' );
122
123         $schema->storage->txn_rollback;
124     };
125
126     subtest 'no enrolment fee' => sub {
127
128         plan tests => 3;
129
130         $schema->storage->txn_begin;
131
132         my $category = $builder->build_object(
133             {
134                 class => 'Koha::Patron::Categories',
135                 value => {
136                     enrolmentfee => 0
137                 }
138             }
139         );
140
141         my $patron = $builder->build_object(
142             {
143                 class => 'Koha::Patrons',
144                 value => {
145                     categorycode => $category->categorycode
146                 }
147             }
148         );
149
150         my $enrollment_fee = $patron->add_enrolment_fee_if_needed();
151         is( $enrollment_fee * 1, 0, 'No enrolment fee' );
152         my $account = $patron->account;
153         is( $patron->account->balance, 0, 'Patron not charged anything' );
154
155         my @debits = $account->outstanding_debits;
156         is( scalar @debits, 0, 'no debits' );
157
158         $schema->storage->txn_rollback;
159     };
160 };
161
162 subtest 'to_api() tests' => sub {
163
164     plan tests => 6;
165
166     $schema->storage->txn_begin;
167
168     my $patron_class = Test::MockModule->new('Koha::Patron');
169     $patron_class->mock(
170         'algo',
171         sub { return 'algo' }
172     );
173
174     my $patron = $builder->build_object(
175         {
176             class => 'Koha::Patrons',
177             value => {
178                 debarred => undef
179             }
180         }
181     );
182
183     my $restricted = $patron->to_api->{restricted};
184     ok( defined $restricted, 'restricted is defined' );
185     ok( !$restricted, 'debarred is undef, restricted evaluates to false' );
186
187     $patron->debarred( dt_from_string->add( days => 1 ) )->store->discard_changes;
188     $restricted = $patron->to_api->{restricted};
189     ok( defined $restricted, 'restricted is defined' );
190     ok( $restricted, 'debarred is defined, restricted evaluates to true' );
191
192     my $patron_json = $patron->to_api({ embed => { algo => {} } });
193     ok( exists $patron_json->{algo} );
194     is( $patron_json->{algo}, 'algo' );
195
196     $schema->storage->txn_rollback;
197 };
198
199 subtest 'login_attempts tests' => sub {
200     plan tests => 1;
201
202     $schema->storage->txn_begin;
203
204     my $patron = $builder->build_object(
205         {
206             class => 'Koha::Patrons',
207         }
208     );
209     my $patron_info = $patron->unblessed;
210     $patron->delete;
211     delete $patron_info->{login_attempts};
212     my $new_patron = Koha::Patron->new($patron_info)->store;
213     is( $new_patron->discard_changes->login_attempts, 0, "login_attempts defaults to 0 as expected");
214
215     $schema->storage->txn_rollback;
216 };
217
218 subtest 'is_superlibrarian() tests' => sub {
219
220     plan tests => 3;
221
222     $schema->storage->txn_begin;
223
224     my $patron = $builder->build_object(
225         {
226             class => 'Koha::Patrons',
227
228             value => {
229                 flags => 16
230             }
231         }
232     );
233
234     is( $patron->is_superlibrarian, 0, 'Patron is not a superlibrarian and the method returns the correct value' );
235
236     $patron->flags(1)->store->discard_changes;
237     is( $patron->is_superlibrarian, 1, 'Patron is a superlibrarian and the method returns the correct value' );
238
239     $patron->flags(0)->store->discard_changes;
240     is( $patron->is_superlibrarian, 0, 'Patron is not a superlibrarian and the method returns the correct value' );
241
242     $schema->storage->txn_rollback;
243 };