Bug 24459: Regression tests
[koha.git] / t / db_dependent / Koha / Patron.t
blob63b77c1573a0a2c00a0b8f1e21f23a575fdf202b
1 #!/usr/bin/perl
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>.
20 use Modern::Perl;
22 use Test::More tests => 3;
23 use Test::Exception;
25 use Koha::Database;
26 use Koha::DateUtils qw(dt_from_string);
27 use Koha::Patrons;
28 use Koha::Patron::Relationships;
30 use t::lib::TestBuilder;
31 use t::lib::Mocks;
33 my $schema = Koha::Database->new->schema;
34 my $builder = t::lib::TestBuilder->new;
36 subtest 'add_guarantor() tests' => sub {
38 plan tests => 6;
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' });
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';
52 is( $patron_1->guarantee_relationships->count, 0, 'No guarantors added' );
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';
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' );
67 $SIG{__WARN__} = sub {}; # FIXME: PrintError = 0 not working!
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';
74 $schema->storage->txn_rollback;
77 subtest 'add_enrolment_fee_if_needed() tests' => sub {
79 plan tests => 2;
81 subtest 'category has enrolment fee' => sub {
82 plan tests => 7;
84 $schema->storage->txn_begin;
86 my $category = $builder->build_object(
88 class => 'Koha::Patron::Categories',
89 value => {
90 enrolmentfee => 20
95 my $patron = $builder->build_object(
97 class => 'Koha::Patrons',
98 value => {
99 categorycode => $category->categorycode
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' );
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' );
120 $schema->storage->txn_rollback;
123 subtest 'no enrolment fee' => sub {
125 plan tests => 3;
127 $schema->storage->txn_begin;
129 my $category = $builder->build_object(
131 class => 'Koha::Patron::Categories',
132 value => {
133 enrolmentfee => 0
138 my $patron = $builder->build_object(
140 class => 'Koha::Patrons',
141 value => {
142 categorycode => $category->categorycode
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' );
152 my @debits = $account->outstanding_debits;
153 is( scalar @debits, 0, 'no debits' );
155 $schema->storage->txn_rollback;
159 subtest 'to_api() tests' => sub {
161 plan tests => 6;
163 $schema->storage->txn_begin;
165 my $patron_class = Test::MockModule->new('Koha::Patron');
166 $patron_class->mock(
167 'algo',
168 sub { return 'algo' }
171 my $patron = $builder->build_object(
173 class => 'Koha::Patrons',
174 value => {
175 debarred => undef
180 my $restricted = $patron->to_api->{restricted};
181 ok( defined $restricted, 'restricted is defined' );
182 ok( !$restricted, 'debarred is undef, restricted evaluates to false' );
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' );
189 my $patron_json = $patron->to_api({ embed => { algo => {} } });
190 ok( exists $patron_json->{algo} );
191 is( $patron_json->{algo}, 'algo' );
193 $schema->storage->txn_rollback;