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