3 # Copyright 2015 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 => 9;
33 use Koha::Virtualshelves;
35 use t::lib::TestBuilder;
38 my $schema = Koha::Database->new->schema;
39 $schema->storage->txn_begin;
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',
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',
64 C4::Context->_new_userenv('xxx');
65 C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, 'Midway Public Library', '', '', '');
67 is( Koha::Patrons->search->count, $nb_of_patrons + 2, 'The 2 patrons should have been added' );
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' );
72 subtest 'guarantees' => sub {
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' );
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 }});
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;
93 subtest 'siblings' => sub {
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;
114 subtest 'has_overdues' => sub {
117 my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
118 my $item_1 = $builder->build(
121 homebranch => $library->{branchcode},
122 holdingbranch => $library->{branchcode},
126 biblionumber => $biblioitem_1->{biblionumber}
130 my $retrieved_patron = Koha::Patrons->find( $new_patron_1->borrowernumber );
131 is( $retrieved_patron->has_overdues, 0, );
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, );
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, );
144 subtest 'update_password' => sub {
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' ) }
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' );
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' );
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' );
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' );
169 subtest 'renew_account' => sub {
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',
177 enrolmentperiod => 12,
178 enrolmentperioddate => undef,
182 my $patron = $builder->build(
183 { source => 'Borrower',
185 dateexpiry => $a_month_ago,
186 categorycode => $patron_category->{categorycode},
190 my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
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' );
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' );
210 $retrieved_patron->delete;
213 subtest "move_to_deleted" => sub {
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' } )
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
226 $retrieved_patron_1->delete;
227 is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' );
229 $schema->storage->txn_rollback;