Bug 16851: Move HasOverdues to Koha::Patron->has_overdues
[koha.git] / t / db_dependent / Koha / Patrons.t
1 #!/usr/bin/perl
2
3 # Copyright 2015 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 => 7;
23 use Test::Warn;
24
25 use C4::Circulation;
26 use Koha::Patron;
27 use Koha::Patrons;
28 use Koha::Database;
29 use Koha::DateUtils;
30 use Koha::Virtualshelves;
31
32 use t::lib::TestBuilder;
33 use t::lib::Mocks;
34
35 my $schema = Koha::Database->new->schema;
36 $schema->storage->txn_begin;
37
38 my $builder       = t::lib::TestBuilder->new;
39 my $library = $builder->build({source => 'Branch' });
40 my $category = $builder->build({source => 'Category' });
41 my $nb_of_patrons = Koha::Patrons->search->count;
42 my $new_patron_1  = Koha::Patron->new(
43     {   cardnumber => 'test_cn_1',
44         branchcode => $library->{branchcode},
45         categorycode => $category->{categorycode},
46         surname => 'surname for patron1',
47         firstname => 'firstname for patron1',
48         userid => 'a_nonexistent_userid_1',
49     }
50 )->store;
51 my $new_patron_2  = Koha::Patron->new(
52     {   cardnumber => 'test_cn_2',
53         branchcode => $library->{branchcode},
54         categorycode => $category->{categorycode},
55         surname => 'surname for patron2',
56         firstname => 'firstname for patron2',
57         userid => 'a_nonexistent_userid_2',
58     }
59 )->store;
60
61 C4::Context->_new_userenv('xxx');
62 C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, 'Midway Public Library', '', '', '');
63
64 is( Koha::Patrons->search->count, $nb_of_patrons + 2, 'The 2 patrons should have been added' );
65
66 my $retrieved_patron_1 = Koha::Patrons->find( $new_patron_1->borrowernumber );
67 is( $retrieved_patron_1->cardnumber, $new_patron_1->cardnumber, 'Find a patron by borrowernumber should return the correct patron' );
68
69 subtest 'guarantees' => sub {
70     plan tests => 8;
71     my $guarantees = $new_patron_1->guarantees;
72     is( ref($guarantees), 'Koha::Patrons', 'Koha::Patron->guarantees should return a Koha::Patrons result set in a scalar context' );
73     is( $guarantees->count, 0, 'new_patron_1 should have 0 guarantee' );
74     my @guarantees = $new_patron_1->guarantees;
75     is( ref(\@guarantees), 'ARRAY', 'Koha::Patron->guarantees should return an array in a list context' );
76     is( scalar(@guarantees), 0, 'new_patron_1 should have 0 guarantee' );
77
78     my $guarantee_1 = $builder->build({ source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber }});
79     my $guarantee_2 = $builder->build({ source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber }});
80
81     $guarantees = $new_patron_1->guarantees;
82     is( ref($guarantees), 'Koha::Patrons', 'Koha::Patron->guarantees should return a Koha::Patrons result set in a scalar context' );
83     is( $guarantees->count, 2, 'new_patron_1 should have 2 guarantees' );
84     @guarantees = $new_patron_1->guarantees;
85     is( ref(\@guarantees), 'ARRAY', 'Koha::Patron->guarantees should return an array in a list context' );
86     is( scalar(@guarantees), 2, 'new_patron_1 should have 2 guarantees' );
87     $_->delete for @guarantees;
88 };
89
90 subtest 'siblings' => sub {
91     plan tests => 7;
92     my $siblings = $new_patron_1->siblings;
93     is( $siblings, undef, 'Koha::Patron->siblings should not crashed if the patron has no guarantor' );
94     my $guarantee_1 = $builder->build( { source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber } } );
95     my $retrieved_guarantee_1 = Koha::Patrons->find($guarantee_1);
96     $siblings = $retrieved_guarantee_1->siblings;
97     is( ref($siblings), 'Koha::Patrons', 'Koha::Patron->siblings should return a Koha::Patrons result set in a scalar context' );
98     my @siblings = $retrieved_guarantee_1->siblings;
99     is( ref( \@siblings ), 'ARRAY', 'Koha::Patron->siblings should return an array in a list context' );
100     is( $siblings->count,  0,       'guarantee_1 should not have siblings yet' );
101     my $guarantee_2 = $builder->build( { source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber } } );
102     my $guarantee_3 = $builder->build( { source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber } } );
103     $siblings = $retrieved_guarantee_1->siblings;
104     is( $siblings->count,               2,                               'guarantee_1 should have 2 siblings' );
105     is( $guarantee_2->{borrowernumber}, $siblings->next->borrowernumber, 'guarantee_2 should exist in the guarantees' );
106     is( $guarantee_3->{borrowernumber}, $siblings->next->borrowernumber, 'guarantee_3 should exist in the guarantees' );
107     $_->delete for $retrieved_guarantee_1->siblings;
108     $retrieved_guarantee_1->delete;
109 };
110
111 subtest 'has_overdues' => sub {
112     plan tests => 3;
113
114     my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
115     my $item_1 = $builder->build(
116         {   source => 'Item',
117             value  => {
118                 homebranch    => $library->{branchcode},
119                 holdingbranch => $library->{branchcode},
120                 notforloan    => 0,
121                 itemlost      => 0,
122                 withdrawn     => 0,
123                 biblionumber  => $biblioitem_1->{biblionumber}
124             }
125         }
126     );
127     my $retrieved_patron = Koha::Patrons->find( $new_patron_1->borrowernumber );
128     is( $retrieved_patron->has_overdues, 0, );
129
130     my $tomorrow = DateTime->today( time_zone => C4::Context->tz() )->add( days => 1 );
131     my $issue = AddIssue( $new_patron_1->unblessed, $item_1->{barcode} );
132     is( $retrieved_patron->has_overdues, 0, );
133     AddReturn( $item_1->{barcode} );
134     my $yesterday = DateTime->today(time_zone => C4::Context->tz())->add( days => -1 );
135     $issue = AddIssue( $new_patron_1->unblessed, $item_1->{barcode}, $yesterday );
136     $retrieved_patron = Koha::Patrons->find( $new_patron_1->borrowernumber );
137     is( $retrieved_patron->has_overdues, 1, );
138     AddReturn( $item_1->{barcode} );
139 };
140
141 subtest 'update_password' => sub {
142     plan tests => 7;
143
144     t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
145     my $original_userid   = $new_patron_1->userid;
146     my $original_password = $new_patron_1->password;
147     warning_like { $retrieved_patron_1->update_password( $new_patron_2->userid, 'another_password' ) }
148     qr{Duplicate entry},
149       'Koha::Patron->update_password should warn if the userid is already used by another patron';
150     is( Koha::Patrons->find( $new_patron_1->borrowernumber )->userid,   $original_userid,   'Koha::Patron->update_password should not have updated the userid' );
151     is( Koha::Patrons->find( $new_patron_1->borrowernumber )->password, $original_password, 'Koha::Patron->update_password should not have updated the userid' );
152
153     $retrieved_patron_1->update_password( 'another_nonexistent_userid_1', 'another_password' );
154     is( Koha::Patrons->find( $new_patron_1->borrowernumber )->userid,   'another_nonexistent_userid_1', 'Koha::Patron->update_password should have updated the userid' );
155     is( Koha::Patrons->find( $new_patron_1->borrowernumber )->password, 'another_password',             'Koha::Patron->update_password should have updated the password' );
156
157     my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'CHANGE PASS', object => $new_patron_1->borrowernumber } )->count;
158     is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->update_password should have logged' );
159
160     t::lib::Mocks::mock_preference( 'BorrowersLog', 0 );
161     $retrieved_patron_1->update_password( 'yet_another_nonexistent_userid_1', 'another_password' );
162     $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'CHANGE PASS', object => $new_patron_1->borrowernumber } )->count;
163     is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->update_password should not have logged' );
164 };
165
166 subtest 'renew_account' => sub {
167     plan tests => 6;
168     my $a_month_ago                = dt_from_string->add( months => -1 )->truncate( to => 'day' );
169     my $a_year_later               = dt_from_string->add( months => 12 )->truncate( to => 'day' );
170     my $a_year_later_minus_a_month = dt_from_string->add( months => 11 )->truncate( to => 'day' );
171     my $patron_category = $builder->build(
172         {   source => 'Category',
173             value  => {
174                 enrolmentperiod     => 12,
175                 enrolmentperioddate => undef,
176             }
177         }
178     );
179     my $patron = $builder->build(
180         {   source => 'Borrower',
181             value  => {
182                 dateexpiry   => $a_month_ago,
183                 categorycode => $patron_category->{categorycode},
184             }
185         }
186     );
187     my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
188
189     t::lib::Mocks::mock_preference( 'BorrowerRenewalPeriodBase', 'dateexpiry' );
190     t::lib::Mocks::mock_preference( 'BorrowersLog',              1 );
191     my $expiry_date = $retrieved_patron->renew_account;
192     is( $expiry_date, $a_year_later_minus_a_month, );
193     my $retrieved_expiry_date = Koha::Patrons->find( $patron->{borrowernumber} )->dateexpiry;
194     is( dt_from_string($retrieved_expiry_date), $a_year_later_minus_a_month );
195     my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'RENEW', object => $retrieved_patron->borrowernumber } )->count;
196     is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->renew_account should have logged' );
197
198     t::lib::Mocks::mock_preference( 'BorrowerRenewalPeriodBase', 'now' );
199     t::lib::Mocks::mock_preference( 'BorrowersLog',              0 );
200     $expiry_date = $retrieved_patron->renew_account;
201     is( $expiry_date, $a_year_later, );
202     $retrieved_expiry_date = Koha::Patrons->find( $patron->{borrowernumber} )->dateexpiry;
203     is( dt_from_string($retrieved_expiry_date), $a_year_later );
204     $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'RENEW', object => $retrieved_patron->borrowernumber } )->count;
205     is( $number_of_logs, 1, 'Without BorrowerLogs, Koha::Patron->renew_account should not have logged' );
206
207     $retrieved_patron->delete;
208 };
209
210 $retrieved_patron_1->delete;
211 is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' );
212
213 $schema->storage->txn_rollback;
214
215 1;