Bug 16407: Fix Koha_borrower_modifications.t
[koha.git] / t / db_dependent / Koha_borrower_modifications.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use Test::More tests => 14;
5
6 use C4::Context;
7 use t::lib::TestBuilder;
8 use C4::Members;
9
10 use Koha::Patron::Modifications;
11
12 my $dbh = C4::Context->dbh;
13 $dbh->{RaiseError} = 1;
14 $dbh->{AutoCommit} = 0;
15
16 $dbh->do("DELETE FROM borrower_modifications");
17
18 ## Create new pending modification
19 Koha::Patron::Modifications->new( verification_token => '1234567890' )
20   ->AddModifications( { surname => 'Hall', firstname => 'Kyle' } );
21
22 ## Get the new pending modification
23 my $borrower = Koha::Patron::Modifications->GetModifications(
24     { verification_token => '1234567890' } );
25
26 ## Verify we get the same data
27 ok( $borrower->{'surname'} = 'Hall',
28     'Test AddModifications() and GetModifications()' );
29
30 ## Check the Verify method
31 ok(
32     Koha::Patron::Modifications->Verify('1234567890'),
33     'Test that Verify() succeeds with a valid token'
34 );
35
36 ## Delete the pending modification
37 $borrower = Koha::Patron::Modifications->DelModifications(
38     { verification_token => '1234567890' } );
39
40 ## Verify it's no longer in the database
41 $borrower = Koha::Patron::Modifications->GetModifications(
42     { verification_token => '1234567890' } );
43 ok( !defined( $borrower->{'surname'} ), 'Test DelModifications()' );
44
45 ## Check the Verify method
46 ok(
47     !Koha::Patron::Modifications->Verify('1234567890'),
48     'Test that Verify() method fails for a bad token'
49 );
50
51 ## Create new pending modification, but for an existing borrower
52 ## But not a hardcoded borrowernumber of course (Bug 16407)
53 my $builder = t::lib::TestBuilder->new;
54 my $borr1 = $builder->build({ source => 'Borrower' })->{borrowernumber};
55 Koha::Patron::Modifications->new( borrowernumber => $borr1 )
56   ->AddModifications( { surname => 'Hall', firstname => 'Kyle' } );
57
58 ## Test the counter
59 ok( Koha::Patron::Modifications->GetPendingModificationsCount() == 1,
60     'Test GetPendingModificationsCount()' );
61
62 ## Create new pending modification for another existing borrower
63 my $borr2 = $builder->build({ source => 'Borrower' })->{borrowernumber};
64 Koha::Patron::Modifications->new( borrowernumber => $borr2 )
65   ->AddModifications( { surname => 'Smith', firstname => 'Sandy' } );
66
67 ## Test the counter
68 ok(
69     Koha::Patron::Modifications->GetPendingModificationsCount() == 2,
70 'Add a new pending modification and test GetPendingModificationsCount() again'
71 );
72
73 ## Check GetPendingModifications
74 my $pendings = Koha::Patron::Modifications->GetPendingModifications();
75 my @firstnames_mod = sort ( $pendings->[0]->{firstname}, $pendings->[1]->{firstname} );
76 ok( $firstnames_mod[0] eq 'Kyle', 'Test GetPendingModifications()' );
77 ok( $firstnames_mod[1] eq 'Sandy', 'Test GetPendingModifications() again' );
78
79 ## This should delete the row from the table
80 Koha::Patron::Modifications->DenyModifications( $borr2 );
81
82 ## Test the counter
83 ok( Koha::Patron::Modifications->GetPendingModificationsCount() == 1,
84     'Test DenyModifications()' );
85
86 ## Save a copy of the borrowers original data
87 my $old_borrower = GetMember( borrowernumber => $borr1 );
88
89 ## Apply the modifications
90 Koha::Patron::Modifications->ApproveModifications( $borr1 );
91
92 ## Test the counter
93 ok(
94     Koha::Patron::Modifications->GetPendingModificationsCount() == 0,
95     'Test ApproveModifications() removes pending modification from db'
96 );
97
98 ## Get a copy of the borrowers current data
99 my $new_borrower = GetMember( borrowernumber => $borr1 );
100
101 ## Check to see that the approved modifications were saved
102 ok( $new_borrower->{'surname'} eq 'Hall',
103     'Test ApproveModifications() applys modification to borrower' );
104
105 ## Now let's put it back the way it was
106 Koha::Patron::Modifications->new( borrowernumber => $borr1 )->AddModifications(
107     {
108         surname   => $old_borrower->{'surname'},
109         firstname => $old_borrower->{'firstname'}
110     }
111 );
112
113 ## Test the counter
114 ok( Koha::Patron::Modifications->GetPendingModificationsCount() == 1,
115     'Test GetPendingModificationsCount()' );
116
117 ## Apply the modifications
118 Koha::Patron::Modifications->ApproveModifications( $borr1 );
119
120 ## Test the counter
121 ok(
122     Koha::Patron::Modifications->GetPendingModificationsCount() == 0,
123     'Test ApproveModifications() removes pending modification from db, again'
124 );
125
126 $new_borrower = GetMember( borrowernumber => $borr1 );
127
128 ## Test to verify the borrower has been updated with the original values
129 ok(
130     $new_borrower->{'surname'} eq $old_borrower->{'surname'},
131     'Test ApproveModifications() applys modification to borrower, again'
132 );
133
134 $dbh->rollback();