Bug 15006: [QA Follow-up] Satisfy qa tools with one tab less
[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 for a patron
52 my $builder = t::lib::TestBuilder->new;
53 my $borr1 = $builder->build({ source => 'Borrower' })->{borrowernumber};
54 Koha::Patron::Modifications->new( borrowernumber => $borr1 )
55   ->AddModifications( { surname => 'Hall', firstname => 'Kyle' } );
56
57 ## Test the counter
58 ok( Koha::Patron::Modifications->GetPendingModificationsCount() == 1,
59     'Test GetPendingModificationsCount()' );
60
61 ## Create new pending modification for another patron
62 my $borr2 = $builder->build({ source => 'Borrower' })->{borrowernumber};
63 Koha::Patron::Modifications->new( borrowernumber => $borr2 )
64   ->AddModifications( { surname => 'Smith', firstname => 'Sandy' } );
65
66 ## Test the counter
67 ok(
68     Koha::Patron::Modifications->GetPendingModificationsCount() == 2,
69 'Add a new pending modification and test GetPendingModificationsCount() again'
70 );
71
72 ## Check GetPendingModifications
73 my $pendings = Koha::Patron::Modifications->GetPendingModifications();
74 my @firstnames_mod = sort ( $pendings->[0]->{firstname}, $pendings->[1]->{firstname} );
75 ok( $firstnames_mod[0] eq 'Kyle', 'Test GetPendingModifications()' );
76 ok( $firstnames_mod[1] eq 'Sandy', 'Test GetPendingModifications() again' );
77
78 ## This should delete the row from the table
79 Koha::Patron::Modifications->DenyModifications( $borr2 );
80
81 ## Test the counter
82 ok( Koha::Patron::Modifications->GetPendingModificationsCount() == 1,
83     'Test DenyModifications()' );
84
85 ## Save a copy of the borrowers original data
86 my $old_borrower = GetMember( borrowernumber => $borr1 );
87
88 ## Apply the modifications
89 Koha::Patron::Modifications->ApproveModifications( $borr1 );
90
91 ## Test the counter
92 ok(
93     Koha::Patron::Modifications->GetPendingModificationsCount() == 0,
94     'Test ApproveModifications() removes pending modification from db'
95 );
96
97 ## Get a copy of the borrowers current data
98 my $new_borrower = GetMember( borrowernumber => $borr1 );
99
100 ## Check to see that the approved modifications were saved
101 ok( $new_borrower->{'surname'} eq 'Hall',
102     'Test ApproveModifications() applys modification to borrower' );
103
104 ## Now let's put it back the way it was
105 Koha::Patron::Modifications->new( borrowernumber => $borr1 )->AddModifications(
106     {
107         surname   => $old_borrower->{'surname'},
108         firstname => $old_borrower->{'firstname'}
109     }
110 );
111
112 ## Test the counter
113 ok( Koha::Patron::Modifications->GetPendingModificationsCount() == 1,
114     'Test GetPendingModificationsCount()' );
115
116 ## Apply the modifications
117 Koha::Patron::Modifications->ApproveModifications( $borr1 );
118
119 ## Test the counter
120 ok(
121     Koha::Patron::Modifications->GetPendingModificationsCount() == 0,
122     'Test ApproveModifications() removes pending modification from db, again'
123 );
124
125 $new_borrower = GetMember( borrowernumber => $borr1 );
126
127 ## Test to verify the borrower has been updated with the original values
128 ok(
129     $new_borrower->{'surname'} eq $old_borrower->{'surname'},
130     'Test ApproveModifications() applys modification to borrower, again'
131 );
132
133 $dbh->rollback();