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