Bug 18420: Fix HoldFulfillmentPolicy.t and Passwordrecovery.t
[koha.git] / t / db_dependent / Passwordrecovery.t
blob893e025c466b81d0bfc4db7b7148912f67a1af0b
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18 use Modern::Perl;
20 use C4::Context;
21 use C4::Letters;
22 use Koha::Database;
23 use Koha::Patrons;
24 use t::lib::TestBuilder;
26 use Test::More tests => 18;
28 use_ok('Koha::Patron::Password::Recovery');
30 my $schema = Koha::Database->new()->schema();
31 $schema->storage->txn_begin();
33 my $dbh = C4::Context->dbh;
34 $dbh->{RaiseError} = 1;
37 # Start with fresh data
39 my $builder = t::lib::TestBuilder->new;
40 my $borrowernumber1 = '2000000000';
41 my $borrowernumber2 = '2000000001';
42 my $borrowernumber3 = '2000000002';
43 my $userid1 = "I83MFItzRpGPxD3vW0";
44 my $userid2 = "Gh5t43980hfSAOcvne";
45 my $userid3 = "adsfada80hfSAOcvne";
46 my $email1 = $userid1 . '@koha-community.org';
47 my $email2 = $userid2 . '@koha-community.org';
48 my $email3 = $userid3 . '@koha-community.org';
49 my $uuid1 = "ABCD1234";
50 my $uuid2 = "WXYZ0987";
51 my $uuid3 = "LMNO4561";
53 my $patron_category = $builder->build({ source => 'Category' });
54 my $branch = $builder->build({ source => 'Branch' });
56 $schema->resultset('BorrowerPasswordRecovery')->delete_all();
58 $schema->resultset('Borrower')->create(
60 borrowernumber => $borrowernumber1,
61 surname => '',
62 address => '',
63 city => '',
64 userid => $userid1,
65 email => $email1,
66 categorycode => $patron_category->{categorycode},
67 branchcode => $branch->{branchcode},
70 $schema->resultset('Borrower')->create(
72 borrowernumber => $borrowernumber2,
73 surname => '',
74 address => '',
75 city => '',
76 userid => $userid2,
77 email => $email2,
78 categorycode => $patron_category->{categorycode},
79 branchcode => $branch->{branchcode},
82 $schema->resultset('Borrower')->create(
84 borrowernumber => $borrowernumber3,
85 surname => '',
86 address => '',
87 city => '',
88 userid => $userid3,
89 email => $email3,
90 categorycode => $patron_category->{categorycode},
91 branchcode => $branch->{branchcode},
95 $schema->resultset('BorrowerPasswordRecovery')->create(
97 borrowernumber => $borrowernumber1,
98 uuid => $uuid1,
99 valid_until => DateTime->now( time_zone => C4::Context->tz() )->add( days => 2 )->datetime()
102 $schema->resultset('BorrowerPasswordRecovery')->create(
104 borrowernumber => $borrowernumber2,
105 uuid => $uuid2,
106 valid_until => DateTime->now( time_zone => C4::Context->tz() )->subtract( days => 2 )->datetime()
109 $schema->resultset('BorrowerPasswordRecovery')->create(
111 borrowernumber => $borrowernumber3,
112 uuid => $uuid3,
113 valid_until => DateTime->now( time_zone => C4::Context->tz() )->subtract( days => 3 )->datetime()
118 can_ok( "Koha::Patron::Password::Recovery", qw(ValidateBorrowernumber GetValidLinkInfo SendPasswordRecoveryEmail CompletePasswordRecovery) );
120 ############################################################
121 # Koha::Patron::Password::Recovery::ValidateBorrowernumber #
122 ############################################################
124 ok( Koha::Patron::Password::Recovery::ValidateBorrowernumber($borrowernumber1), "[ValidateBorrowernumber] Borrower has a password recovery entry" );
125 ok( ! Koha::Patron::Password::Recovery::ValidateBorrowernumber($borrowernumber2), "[ValidateBorrowernumber] Borrower's number is not found; password recovery entry is expired" );
126 ok( ! Koha::Patron::Password::Recovery::ValidateBorrowernumber(9999), "[ValidateBorrowernumber] Borrower has no password recovery entry" );
128 ######################################################
129 # Koha::Patron::Password::Recovery::GetValidLinkInfo #
130 ######################################################
132 my ($bnum1, $uname1) = Koha::Patron::Password::Recovery::GetValidLinkInfo($uuid1);
133 my ($bnum2, $uname2) = Koha::Patron::Password::Recovery::GetValidLinkInfo($uuid2);
134 my ($bnum3, $uname3) = Koha::Patron::Password::Recovery::GetValidLinkInfo("THISISANINVALIDUUID");
136 is( $bnum1, $borrowernumber1, "[GetValidLinkInfo] Borrower has a valid link" );
137 is( $uname1, $userid1, "[GetValidLinkInfo] Borrower's username is fetched when a valid link is found" );
138 ok( ! defined($bnum2), "[GetValidLinkInfo] Borrower's link is no longer valid; entry is expired" );
139 ok( ! defined($bnum3), "[GetValidLinkInfo] Invalid UUID returns no borrowernumber" );
141 ##############################################################
142 # Koha::Patron::Password::Recovery::CompletePasswordRecovery #
143 ##############################################################
145 is( Koha::Patron::Password::Recovery::CompletePasswordRecovery($uuid1), 3, "[CompletePasswordRecovery] Completing a password recovery deletes the used entry" );
147 $schema->resultset('BorrowerPasswordRecovery')->create(
149 borrowernumber => $borrowernumber2,
150 uuid => $uuid2,
151 valid_until => DateTime->now( time_zone => C4::Context->tz() )->subtract( days => 2 )->datetime()
155 ok( Koha::Patron::Password::Recovery::CompletePasswordRecovery($uuid2) == 1, "[CompletePasswordRecovery] An expired or invalid UUID purges expired entries" );
156 ok( Koha::Patron::Password::Recovery::CompletePasswordRecovery($uuid2) == 0, "[CompletePasswordRecovery] Returns 0 on a clean table" );
158 ###################################################################
159 # Koha::Patron::Password::Recovery::DeleteExpiredPasswordRecovery #
160 ###################################################################
162 $schema->resultset('BorrowerPasswordRecovery')->create(
164 borrowernumber => $borrowernumber3,
165 uuid => $uuid3,
166 valid_until => DateTime->now( time_zone => C4::Context->tz() )->subtract( days => 3 )->datetime()
170 ok( Koha::Patron::Password::Recovery::DeleteExpiredPasswordRecovery($borrowernumber3) == 1, "[DeleteExpiredPasswordRecovery] we can delete the unused entry" );
171 ok( Koha::Patron::Password::Recovery::DeleteExpiredPasswordRecovery($borrowernumber3) == 0, "[DeleteExpiredPasswordRecovery] Returns 0 on a clean table" );
173 ###############################################################
174 # Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail #
175 ###############################################################
177 my $borrower = Koha::Patrons->search( { userid => $userid1 } )->next;
178 ok( Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail($borrower, $email1, 0) == 1, "[SendPasswordRecoveryEmail] Returns 1 on success" );
179 my $letters = C4::Letters::GetQueuedMessages( { borrowernumber => $borrowernumber1, limit => 99 } );
180 ok( scalar @$letters == 1, "[SendPasswordRecoveryEmail] There is a letter in the queue for our borrower");
182 my $bpr = $schema->resultset('BorrowerPasswordRecovery')->search( { borrowernumber => $borrowernumber1 } );
183 my $tempuuid1 = $bpr->next->uuid;
185 Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail($borrower, $email1, 1);
187 $bpr = $schema->resultset('BorrowerPasswordRecovery')->search( { borrowernumber => $borrowernumber1 } );
188 my $tempuuid2 = $bpr->next->uuid;
190 $letters = C4::Letters::GetQueuedMessages( { borrowernumber => $borrowernumber1, limit => 99 } );
192 ok( $tempuuid1 ne $tempuuid2, "[SendPasswordRecoveryEmail] UPDATE == ON changes uuid in the database and updates the expirydate");
193 ok( scalar @$letters == 2, "[SendPasswordRecoveryEmail] UPDATE == ON sends a new letter with updated uuid");
195 $schema->storage->txn_rollback();