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