Bug 16853: Move changepassword to Koha::Patron->update_password
[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 => 6;
23 use Test::Warn;
24
25 use Koha::Patron;
26 use Koha::Patrons;
27 use Koha::Database;
28
29 use t::lib::TestBuilder;
30 use t::lib::Mocks;
31
32 my $schema = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
34
35 my $builder       = t::lib::TestBuilder->new;
36 my $library = $builder->build({source => 'Branch' });
37 my $category = $builder->build({source => 'Category' });
38 my $nb_of_patrons = Koha::Patrons->search->count;
39 my $new_patron_1  = Koha::Patron->new(
40     {   cardnumber => 'test_cn_1',
41         branchcode => $library->{branchcode},
42         categorycode => $category->{categorycode},
43         surname => 'surname for patron1',
44         firstname => 'firstname for patron1',
45         userid => 'a_nonexistent_userid_1',
46     }
47 )->store;
48 my $new_patron_2  = Koha::Patron->new(
49     {   cardnumber => 'test_cn_2',
50         branchcode => $library->{branchcode},
51         categorycode => $category->{categorycode},
52         surname => 'surname for patron2',
53         firstname => 'firstname for patron2',
54         userid => 'a_nonexistent_userid_2',
55     }
56 )->store;
57
58 is( Koha::Patrons->search->count, $nb_of_patrons + 2, 'The 2 patrons should have been added' );
59
60 my $retrieved_patron_1 = Koha::Patrons->find( $new_patron_1->borrowernumber );
61 is( $retrieved_patron_1->cardnumber, $new_patron_1->cardnumber, 'Find a patron by borrowernumber should return the correct patron' );
62
63 subtest 'guarantees' => sub {
64     plan tests => 8;
65     my $guarantees = $new_patron_1->guarantees;
66     is( ref($guarantees), 'Koha::Patrons', 'Koha::Patron->guarantees should return a Koha::Patrons result set in a scalar context' );
67     is( $guarantees->count, 0, 'new_patron_1 should have 0 guarantee' );
68     my @guarantees = $new_patron_1->guarantees;
69     is( ref(\@guarantees), 'ARRAY', 'Koha::Patron->guarantees should return an array in a list context' );
70     is( scalar(@guarantees), 0, 'new_patron_1 should have 0 guarantee' );
71
72     my $guarantee_1 = $builder->build({ source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber }});
73     my $guarantee_2 = $builder->build({ source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber }});
74
75     $guarantees = $new_patron_1->guarantees;
76     is( ref($guarantees), 'Koha::Patrons', 'Koha::Patron->guarantees should return a Koha::Patrons result set in a scalar context' );
77     is( $guarantees->count, 2, 'new_patron_1 should have 2 guarantees' );
78     @guarantees = $new_patron_1->guarantees;
79     is( ref(\@guarantees), 'ARRAY', 'Koha::Patron->guarantees should return an array in a list context' );
80     is( scalar(@guarantees), 2, 'new_patron_1 should have 2 guarantees' );
81     $_->delete for @guarantees;
82 };
83
84 subtest 'siblings' => sub {
85     plan tests => 7;
86     my $siblings = $new_patron_1->siblings;
87     is( $siblings, undef, 'Koha::Patron->siblings should not crashed if the patron has no guarantor' );
88     my $guarantee_1 = $builder->build( { source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber } } );
89     my $retrieved_guarantee_1 = Koha::Patrons->find($guarantee_1);
90     $siblings = $retrieved_guarantee_1->siblings;
91     is( ref($siblings), 'Koha::Patrons', 'Koha::Patron->siblings should return a Koha::Patrons result set in a scalar context' );
92     my @siblings = $retrieved_guarantee_1->siblings;
93     is( ref( \@siblings ), 'ARRAY', 'Koha::Patron->siblings should return an array in a list context' );
94     is( $siblings->count,  0,       'guarantee_1 should not have siblings yet' );
95     my $guarantee_2 = $builder->build( { source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber } } );
96     my $guarantee_3 = $builder->build( { source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber } } );
97     $siblings = $retrieved_guarantee_1->siblings;
98     is( $siblings->count,               2,                               'guarantee_1 should have 2 siblings' );
99     is( $guarantee_2->{borrowernumber}, $siblings->next->borrowernumber, 'guarantee_2 should exist in the guarantees' );
100     is( $guarantee_3->{borrowernumber}, $siblings->next->borrowernumber, 'guarantee_3 should exist in the guarantees' );
101     $_->delete for $retrieved_guarantee_1->siblings;
102     $retrieved_guarantee_1->delete;
103 };
104
105 subtest 'update_password' => sub {
106     plan tests => 7;
107
108     t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
109     my $original_userid   = $new_patron_1->userid;
110     my $original_password = $new_patron_1->password;
111     warning_like { $retrieved_patron_1->update_password( $new_patron_2->userid, 'another_password' ) }
112     qr{Duplicate entry},
113       'Koha::Patron->update_password should warn if the userid is already used by another patron';
114     is( Koha::Patrons->find( $new_patron_1->borrowernumber )->userid,   $original_userid,   'Koha::Patron->update_password should not have updated the userid' );
115     is( Koha::Patrons->find( $new_patron_1->borrowernumber )->password, $original_password, 'Koha::Patron->update_password should not have updated the userid' );
116
117     $retrieved_patron_1->update_password( 'another_nonexistent_userid_1', 'another_password' );
118     is( Koha::Patrons->find( $new_patron_1->borrowernumber )->userid,   'another_nonexistent_userid_1', 'Koha::Patron->update_password should have updated the userid' );
119     is( Koha::Patrons->find( $new_patron_1->borrowernumber )->password, 'another_password',             'Koha::Patron->update_password should have updated the password' );
120
121     my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'CHANGE PASS', object => $new_patron_1->borrowernumber } )->count;
122     is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->update_password should have logged' );
123
124     t::lib::Mocks::mock_preference( 'BorrowersLog', 0 );
125     $retrieved_patron_1->update_password( 'yet_another_nonexistent_userid_1', 'another_password' );
126     $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'CHANGE PASS', object => $new_patron_1->borrowernumber } )->count;
127     is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->update_password should not have logged' );
128 };
129
130 $retrieved_patron_1->delete;
131 is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' );
132
133 $schema->storage->txn_rollback;
134
135 1;