Bug 31196: Remove 'default_value_for_mod_marc-' clear_from_cache calls
[koha.git] / t / db_dependent / Passwordrecovery.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use C4::Context;
21 use C4::Letters qw( GetQueuedMessages );
22 use Koha::Database;
23 use Koha::DateUtils qw( dt_from_string );
24 use Koha::Patrons;
25
26 use t::lib::TestBuilder;
27 use t::lib::Mocks;
28
29 use Test::More tests => 22;
30 use Test::MockModule;
31 use Test::Warn;
32 use Carp;
33
34 my ( $email_object, $sendmail_params );
35
36 my $email_sender_module = Test::MockModule->new('Email::Stuffer');
37 $email_sender_module->mock(
38     'send_or_die',
39     sub {
40         ( $email_object, $sendmail_params ) = @_;
41         warn 'Fake sendmail';
42     }
43 );
44
45 use_ok('Koha::Patron::Password::Recovery');
46
47 t::lib::Mocks::mock_preference('KohaAdminEmailAddress', 'test@koha-community.org');
48
49 my $schema = Koha::Database->new()->schema();
50 $schema->storage->txn_begin();
51
52 #
53 # Start with fresh data
54 #
55 my $builder = t::lib::TestBuilder->new;
56 my $borrowernumber1 = '2000000000';
57 my $borrowernumber2 = '2000000001';
58 my $borrowernumber3 = '2000000002';
59 my $userid1 = "I83MFItzRpGPxD3vW0";
60 my $userid2 = "Gh5t43980hfSAOcvne";
61 my $userid3 = "adsfada80hfSAOcvne";
62 my $email1  = $userid1 . '@koha-community.org';
63 my $email2  = $userid2 . '@koha-community.org';
64 my $email3  = $userid3 . '@koha-community.org';
65 my $uuid1   = "ABCD1234";
66 my $uuid2   = "WXYZ0987";
67 my $uuid3   = "LMNO4561";
68
69 my $patron_category = $builder->build({ source => 'Category' });
70 my $branch = $builder->build({
71     source => 'Branch',
72     value => {
73         branchemail      => undef,
74         branchreplyto    => undef,
75         branchreturnpath => $email1,
76     },
77 });
78
79 $schema->resultset('BorrowerPasswordRecovery')->delete_all();
80
81 $schema->resultset('Borrower')->create(
82     {
83         borrowernumber  => $borrowernumber1,
84         surname         => '',
85         address         => '',
86         city            => '',
87         userid          => $userid1,
88         email           => $email1,
89         categorycode    => $patron_category->{categorycode},
90         branchcode      => $branch->{branchcode},
91     }
92 );
93 $schema->resultset('Borrower')->create(
94     {
95         borrowernumber  => $borrowernumber2,
96         surname         => '',
97         address         => '',
98         city            => '',
99         userid          => $userid2,
100         email           => $email2,
101         categorycode    => $patron_category->{categorycode},
102         branchcode      => $branch->{branchcode},
103     }
104 );
105 $schema->resultset('Borrower')->create(
106     {
107         borrowernumber => $borrowernumber3,
108         surname        => '',
109         address        => '',
110         city           => '',
111         userid         => $userid3,
112         email          => $email3,
113         categorycode   => $patron_category->{categorycode},
114         branchcode     => $branch->{branchcode},
115     }
116 );
117
118 $schema->resultset('BorrowerPasswordRecovery')->create(
119     {
120         borrowernumber => $borrowernumber1,
121         uuid           => $uuid1,
122         valid_until    => dt_from_string()->add( days => 2 )->datetime()
123     }
124 );
125 $schema->resultset('BorrowerPasswordRecovery')->create(
126     {
127         borrowernumber => $borrowernumber2,
128         uuid           => $uuid2,
129         valid_until    => dt_from_string()->subtract( days => 2 )->datetime()
130     }
131 );
132 $schema->resultset('BorrowerPasswordRecovery')->create(
133     {
134         borrowernumber => $borrowernumber3,
135         uuid           => $uuid3,
136         valid_until    => dt_from_string()->subtract( days => 3 )->datetime()
137     }
138 );
139
140
141 can_ok( "Koha::Patron::Password::Recovery", qw(ValidateBorrowernumber GetValidLinkInfo SendPasswordRecoveryEmail CompletePasswordRecovery) );
142
143 ############################################################
144 # Koha::Patron::Password::Recovery::ValidateBorrowernumber #
145 ############################################################
146
147 ok(   Koha::Patron::Password::Recovery::ValidateBorrowernumber($borrowernumber1), "[ValidateBorrowernumber] Borrower has a password recovery entry" );
148 ok( ! Koha::Patron::Password::Recovery::ValidateBorrowernumber($borrowernumber2), "[ValidateBorrowernumber] Borrower's number is not found; password recovery entry is expired" );
149 ok( ! Koha::Patron::Password::Recovery::ValidateBorrowernumber(9999), "[ValidateBorrowernumber] Borrower has no password recovery entry" );
150
151 ######################################################
152 # Koha::Patron::Password::Recovery::GetValidLinkInfo #
153 ######################################################
154
155 my ($bnum1, $uname1) = Koha::Patron::Password::Recovery::GetValidLinkInfo($uuid1);
156 my ($bnum2, $uname2) = Koha::Patron::Password::Recovery::GetValidLinkInfo($uuid2);
157 my ($bnum3, $uname3) = Koha::Patron::Password::Recovery::GetValidLinkInfo("THISISANINVALIDUUID");
158
159 is( $bnum1, $borrowernumber1, "[GetValidLinkInfo] Borrower has a valid link" );
160 is( $uname1, $userid1, "[GetValidLinkInfo] Borrower's username is fetched when a valid link is found" );
161 ok( ! defined($bnum2), "[GetValidLinkInfo] Borrower's link is no longer valid; entry is expired" );
162 ok( ! defined($bnum3), "[GetValidLinkInfo] Invalid UUID returns no borrowernumber" );
163
164 ##############################################################
165 # Koha::Patron::Password::Recovery::CompletePasswordRecovery #
166 ##############################################################
167
168 is( Koha::Patron::Password::Recovery::CompletePasswordRecovery($uuid1), 3, "[CompletePasswordRecovery] Completing a password recovery deletes the used entry" );
169
170 $schema->resultset('BorrowerPasswordRecovery')->create(
171     {
172         borrowernumber => $borrowernumber2,
173         uuid           => $uuid2,
174         valid_until    => dt_from_string()->subtract( days => 2 )->datetime()
175     }
176 );
177
178 ok( Koha::Patron::Password::Recovery::CompletePasswordRecovery($uuid2) == 1, "[CompletePasswordRecovery] An expired or invalid UUID purges expired entries" );
179 ok( Koha::Patron::Password::Recovery::CompletePasswordRecovery($uuid2) == 0, "[CompletePasswordRecovery] Returns 0 on a clean table" );
180
181 ###################################################################
182 # Koha::Patron::Password::Recovery::DeleteExpiredPasswordRecovery #
183 ###################################################################
184
185 $schema->resultset('BorrowerPasswordRecovery')->create(
186     {
187         borrowernumber => $borrowernumber3,
188         uuid           => $uuid3,
189         valid_until    => dt_from_string()->subtract( days => 3 )->datetime()
190     }
191 );
192
193 ok( Koha::Patron::Password::Recovery::DeleteExpiredPasswordRecovery($borrowernumber3) == 1, "[DeleteExpiredPasswordRecovery] we can delete the unused entry" );
194 ok( Koha::Patron::Password::Recovery::DeleteExpiredPasswordRecovery($borrowernumber3) == 0, "[DeleteExpiredPasswordRecovery] Returns 0 on a clean table" );
195
196 ###############################################################
197 # Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail #
198 ###############################################################
199
200 my $borrower = Koha::Patrons->search( { userid => $userid1 } )->next;
201 my $success;
202 warning_is {
203     $success = Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail($borrower, $email1 ); }
204     "Fake sendmail",
205     '[SendPasswordRecoveryEmail] expecting fake sendmail';
206 ok( $success == 1, '[SendPasswordRecoveryEmail] Returns 1 on success');
207
208 my $letters = C4::Letters::GetQueuedMessages( { borrowernumber => $borrowernumber1, limit => 99 } );
209 ok( scalar @$letters == 1, "[SendPasswordRecoveryEmail] There is a letter in the queue for our borrower");
210
211 my $bpr = $schema->resultset('BorrowerPasswordRecovery')->search( { borrowernumber => $borrowernumber1 } );
212 my $tempuuid1 = $bpr->next->uuid;
213
214 warning_is {
215     Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail($borrower, $email1 ); }
216     "Fake sendmail",
217     '[SendPasswordRecoveryEmail] expecting fake sendmail';
218
219 $bpr = $schema->resultset('BorrowerPasswordRecovery')->search( { borrowernumber => $borrowernumber1 } );
220 my $tempuuid2 = $bpr->next->uuid;
221
222 $letters = C4::Letters::GetQueuedMessages( { borrowernumber => $borrowernumber1, limit => 99 } );
223
224 ok( $tempuuid1 ne $tempuuid2, "[SendPasswordRecoveryEmail] UPDATE == ON changes uuid in the database and updates the expirydate");
225 ok( scalar @$letters == 2, "[SendPasswordRecoveryEmail] UPDATE == ON sends a new letter with updated uuid");
226
227 foreach my $letter (@$letters) {
228     ok( $letter->{status} eq 'sent',
229         'Test SendPasswordRecoverEmail sent due to TestBuilder Sender being a valid email address as expected.' );
230 }
231
232 $schema->storage->txn_rollback();
233