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 => 2;
27 use Koha::Patron::Relationships;
29 use t::lib::TestBuilder;
32 my $schema = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new;
35 subtest 'add_guarantor() tests' => sub {
39 $schema->storage->txn_begin;
41 t::lib::Mocks::mock_preference( 'borrowerRelationship', 'father1|father2' );
43 my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
44 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
47 { $patron_1->add_guarantor({ guarantor_id => $patron_2->borrowernumber }); }
48 'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
49 'Exception is thrown as no relationship passed';
51 is( $patron_1->guarantee_relationships->count, 0, 'No guarantors added' );
54 { $patron_1->add_guarantor({ guarantor_id => $patron_2->borrowernumber, relationship => 'father' }); }
55 'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
56 'Exception is thrown as a wrong relationship was passed';
58 is( $patron_1->guarantee_relationships->count, 0, 'No guarantors added' );
60 $patron_1->add_guarantor({ guarantor_id => $patron_2->borrowernumber, relationship => 'father1' });
62 my $guarantors = $patron_1->guarantor_relationships;
64 is( $guarantors->count, 1, 'No guarantors added' );
66 $SIG{__WARN__} = sub {}; # FIXME: PrintError = 0 not working!
69 { $patron_1->add_guarantor({ guarantor_id => $patron_2->borrowernumber, relationship => 'father2' }); }
70 'Koha::Exceptions::Patron::Relationship::DuplicateRelationship',
71 'Exception is thrown for duplicated relationship';
73 $schema->storage->txn_rollback;
76 subtest 'add_enrolment_fee_if_needed() tests' => sub {
80 subtest 'category has enrolment fee' => sub {
83 $schema->storage->txn_begin;
85 my $category = $builder->build_object(
87 class => 'Koha::Patron::Categories',
94 my $patron = $builder->build_object(
96 class => 'Koha::Patrons',
98 categorycode => $category->categorycode
103 my $enrollment_fee = $patron->add_enrolment_fee_if_needed();
104 is( $enrollment_fee * 1, 20, 'Enrolment fee amount is correct' );
105 my $account = $patron->account;
106 is( $patron->account->balance * 1, 20, 'Patron charged the enrolment fee' );
107 # second enrolment fee, new
108 $enrollment_fee = $patron->add_enrolment_fee_if_needed(0);
109 # third enrolment fee, renewal
110 $enrollment_fee = $patron->add_enrolment_fee_if_needed(1);
111 is( $patron->account->balance * 1, 60, 'Patron charged the enrolment fees' );
113 my @debits = $account->outstanding_debits;
114 is( scalar @debits, 3, '3 enrolment fees' );
115 is( $debits[0]->debit_type_code, 'ACCOUNT', 'Account type set correctly' );
116 is( $debits[1]->debit_type_code, 'ACCOUNT', 'Account type set correctly' );
117 is( $debits[2]->debit_type_code, 'ACCOUNT_RENEW', 'Account type set correctly' );
119 $schema->storage->txn_rollback;
122 subtest 'no enrolment fee' => sub {
126 $schema->storage->txn_begin;
128 my $category = $builder->build_object(
130 class => 'Koha::Patron::Categories',
137 my $patron = $builder->build_object(
139 class => 'Koha::Patrons',
141 categorycode => $category->categorycode
146 my $enrollment_fee = $patron->add_enrolment_fee_if_needed();
147 is( $enrollment_fee * 1, 0, 'No enrolment fee' );
148 my $account = $patron->account;
149 is( $patron->account->balance, 0, 'Patron not charged anything' );
151 my @debits = $account->outstanding_debits;
152 is( scalar @debits, 0, 'no debits' );
154 $schema->storage->txn_rollback;