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 => 7;
29 use Koha::Virtualshelves;
31 use t::lib::TestBuilder;
34 my $schema = Koha::Database->new->schema;
35 $schema->storage->txn_begin;
37 my $builder = t::lib::TestBuilder->new;
38 my $library = $builder->build({source => 'Branch' });
39 my $category = $builder->build({source => 'Category' });
40 my $nb_of_patrons = Koha::Patrons->search->count;
41 my $new_patron_1 = Koha::Patron->new(
42 { cardnumber => 'test_cn_1',
43 branchcode => $library->{branchcode},
44 categorycode => $category->{categorycode},
45 surname => 'surname for patron1',
46 firstname => 'firstname for patron1',
47 userid => 'a_nonexistent_userid_1',
50 my $new_patron_2 = Koha::Patron->new(
51 { cardnumber => 'test_cn_2',
52 branchcode => $library->{branchcode},
53 categorycode => $category->{categorycode},
54 surname => 'surname for patron2',
55 firstname => 'firstname for patron2',
56 userid => 'a_nonexistent_userid_2',
60 is( Koha::Patrons->search->count, $nb_of_patrons + 2, 'The 2 patrons should have been added' );
62 my $retrieved_patron_1 = Koha::Patrons->find( $new_patron_1->borrowernumber );
63 is( $retrieved_patron_1->cardnumber, $new_patron_1->cardnumber, 'Find a patron by borrowernumber should return the correct patron' );
65 subtest 'guarantees' => sub {
67 my $guarantees = $new_patron_1->guarantees;
68 is( ref($guarantees), 'Koha::Patrons', 'Koha::Patron->guarantees should return a Koha::Patrons result set in a scalar context' );
69 is( $guarantees->count, 0, 'new_patron_1 should have 0 guarantee' );
70 my @guarantees = $new_patron_1->guarantees;
71 is( ref(\@guarantees), 'ARRAY', 'Koha::Patron->guarantees should return an array in a list context' );
72 is( scalar(@guarantees), 0, 'new_patron_1 should have 0 guarantee' );
74 my $guarantee_1 = $builder->build({ source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber }});
75 my $guarantee_2 = $builder->build({ source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber }});
77 $guarantees = $new_patron_1->guarantees;
78 is( ref($guarantees), 'Koha::Patrons', 'Koha::Patron->guarantees should return a Koha::Patrons result set in a scalar context' );
79 is( $guarantees->count, 2, 'new_patron_1 should have 2 guarantees' );
80 @guarantees = $new_patron_1->guarantees;
81 is( ref(\@guarantees), 'ARRAY', 'Koha::Patron->guarantees should return an array in a list context' );
82 is( scalar(@guarantees), 2, 'new_patron_1 should have 2 guarantees' );
83 $_->delete for @guarantees;
86 subtest 'siblings' => sub {
88 my $siblings = $new_patron_1->siblings;
89 is( $siblings, undef, 'Koha::Patron->siblings should not crashed if the patron has no guarantor' );
90 my $guarantee_1 = $builder->build( { source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber } } );
91 my $retrieved_guarantee_1 = Koha::Patrons->find($guarantee_1);
92 $siblings = $retrieved_guarantee_1->siblings;
93 is( ref($siblings), 'Koha::Patrons', 'Koha::Patron->siblings should return a Koha::Patrons result set in a scalar context' );
94 my @siblings = $retrieved_guarantee_1->siblings;
95 is( ref( \@siblings ), 'ARRAY', 'Koha::Patron->siblings should return an array in a list context' );
96 is( $siblings->count, 0, 'guarantee_1 should not have siblings yet' );
97 my $guarantee_2 = $builder->build( { source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber } } );
98 my $guarantee_3 = $builder->build( { source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber } } );
99 $siblings = $retrieved_guarantee_1->siblings;
100 is( $siblings->count, 2, 'guarantee_1 should have 2 siblings' );
101 is( $guarantee_2->{borrowernumber}, $siblings->next->borrowernumber, 'guarantee_2 should exist in the guarantees' );
102 is( $guarantee_3->{borrowernumber}, $siblings->next->borrowernumber, 'guarantee_3 should exist in the guarantees' );
103 $_->delete for $retrieved_guarantee_1->siblings;
104 $retrieved_guarantee_1->delete;
107 subtest 'update_password' => sub {
110 t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
111 my $original_userid = $new_patron_1->userid;
112 my $original_password = $new_patron_1->password;
113 warning_like { $retrieved_patron_1->update_password( $new_patron_2->userid, 'another_password' ) }
115 'Koha::Patron->update_password should warn if the userid is already used by another patron';
116 is( Koha::Patrons->find( $new_patron_1->borrowernumber )->userid, $original_userid, 'Koha::Patron->update_password should not have updated the userid' );
117 is( Koha::Patrons->find( $new_patron_1->borrowernumber )->password, $original_password, 'Koha::Patron->update_password should not have updated the userid' );
119 $retrieved_patron_1->update_password( 'another_nonexistent_userid_1', 'another_password' );
120 is( Koha::Patrons->find( $new_patron_1->borrowernumber )->userid, 'another_nonexistent_userid_1', 'Koha::Patron->update_password should have updated the userid' );
121 is( Koha::Patrons->find( $new_patron_1->borrowernumber )->password, 'another_password', 'Koha::Patron->update_password should have updated the password' );
123 my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'CHANGE PASS', object => $new_patron_1->borrowernumber } )->count;
124 is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->update_password should have logged' );
126 t::lib::Mocks::mock_preference( 'BorrowersLog', 0 );
127 $retrieved_patron_1->update_password( 'yet_another_nonexistent_userid_1', 'another_password' );
128 $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'CHANGE PASS', object => $new_patron_1->borrowernumber } )->count;
129 is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->update_password should not have logged' );
132 subtest 'extend_subscription' => sub {
134 my $a_month_ago = dt_from_string->add( months => -1 )->truncate( to => 'day' );
135 my $a_year_later = dt_from_string->add( months => 12 )->truncate( to => 'day' );
136 my $a_year_later_minus_a_month = dt_from_string->add( months => 11 )->truncate( to => 'day' );
137 my $patron_category = $builder->build(
138 { source => 'Category',
140 enrolmentperiod => 12,
141 enrolmentperioddate => undef,
145 my $patron = $builder->build(
146 { source => 'Borrower',
148 dateexpiry => $a_month_ago,
149 categorycode => $patron_category->{categorycode},
153 my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
155 t::lib::Mocks::mock_preference( 'BorrowerRenewalPeriodBase', 'dateexpiry' );
156 t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
157 my $expiry_date = $retrieved_patron->extend_subscription;
158 is( $expiry_date, $a_year_later_minus_a_month, );
159 my $retrieved_expiry_date = Koha::Patrons->find( $patron->{borrowernumber} )->dateexpiry;
160 is( dt_from_string($retrieved_expiry_date), $a_year_later_minus_a_month );
161 my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'RENEW', object => $retrieved_patron->borrowernumber } )->count;
162 is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->extend_subscription should have logged' );
164 t::lib::Mocks::mock_preference( 'BorrowerRenewalPeriodBase', 'now' );
165 t::lib::Mocks::mock_preference( 'BorrowersLog', 0 );
166 $expiry_date = $retrieved_patron->extend_subscription;
167 is( $expiry_date, $a_year_later, );
168 $retrieved_expiry_date = Koha::Patrons->find( $patron->{borrowernumber} )->dateexpiry;
169 is( dt_from_string($retrieved_expiry_date), $a_year_later );
170 $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'RENEW', object => $retrieved_patron->borrowernumber } )->count;
171 is( $number_of_logs, 1, 'Without BorrowerLogs, Koha::Patron->extend_subscription should not have logged' );
173 $retrieved_patron->delete;
176 $retrieved_patron_1->delete;
177 is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' );
179 $schema->storage->txn_rollback;