3 # Copyright 2019 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 => 5;
26 use Koha::DateUtils qw(dt_from_string);
28 use Koha::Patron::Relationships;
30 use t::lib::TestBuilder;
33 my $schema = Koha::Database->new->schema;
34 my $builder = t::lib::TestBuilder->new;
36 subtest 'add_guarantor() tests' => sub {
40 $schema->storage->txn_begin;
42 t::lib::Mocks::mock_preference( 'borrowerRelationship', 'father1|father2' );
44 my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
45 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
48 { $patron_1->add_guarantor({ guarantor_id => $patron_2->borrowernumber }); }
49 'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
50 'Exception is thrown as no relationship passed';
52 is( $patron_1->guarantee_relationships->count, 0, 'No guarantors added' );
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';
59 is( $patron_1->guarantee_relationships->count, 0, 'No guarantors added' );
61 $patron_1->add_guarantor({ guarantor_id => $patron_2->borrowernumber, relationship => 'father1' });
63 my $guarantors = $patron_1->guarantor_relationships;
65 is( $guarantors->count, 1, 'No guarantors added' );
69 open STDERR, '>', '/dev/null';
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';
77 $schema->storage->txn_rollback;
80 subtest 'add_enrolment_fee_if_needed() tests' => sub {
84 subtest 'category has enrolment fee' => sub {
87 $schema->storage->txn_begin;
89 my $category = $builder->build_object(
91 class => 'Koha::Patron::Categories',
98 my $patron = $builder->build_object(
100 class => 'Koha::Patrons',
102 categorycode => $category->categorycode
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' );
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' );
123 $schema->storage->txn_rollback;
126 subtest 'no enrolment fee' => sub {
130 $schema->storage->txn_begin;
132 my $category = $builder->build_object(
134 class => 'Koha::Patron::Categories',
141 my $patron = $builder->build_object(
143 class => 'Koha::Patrons',
145 categorycode => $category->categorycode
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' );
155 my @debits = $account->outstanding_debits;
156 is( scalar @debits, 0, 'no debits' );
158 $schema->storage->txn_rollback;
162 subtest 'to_api() tests' => sub {
166 $schema->storage->txn_begin;
168 my $patron_class = Test::MockModule->new('Koha::Patron');
171 sub { return 'algo' }
174 my $patron = $builder->build_object(
176 class => 'Koha::Patrons',
183 my $restricted = $patron->to_api->{restricted};
184 ok( defined $restricted, 'restricted is defined' );
185 ok( !$restricted, 'debarred is undef, restricted evaluates to false' );
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' );
192 my $patron_json = $patron->to_api({ embed => { algo => {} } });
193 ok( exists $patron_json->{algo} );
194 is( $patron_json->{algo}, 'algo' );
196 $schema->storage->txn_rollback;
199 subtest 'login_attempts tests' => sub {
202 $schema->storage->txn_begin;
204 my $patron = $builder->build_object(
206 class => 'Koha::Patrons',
209 my $patron_info = $patron->unblessed;
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");
215 $schema->storage->txn_rollback;
218 subtest 'is_superlibrarian() tests' => sub {
222 $schema->storage->txn_begin;
224 my $patron = $builder->build_object(
226 class => 'Koha::Patrons',
234 is( $patron->is_superlibrarian, 0, 'Patron is not a superlibrarian and the method returns the correct value' );
236 $patron->flags(1)->store->discard_changes;
237 is( $patron->is_superlibrarian, 1, 'Patron is a superlibrarian and the method returns the correct value' );
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' );
242 $schema->storage->txn_rollback;