Bug 7067 QA Followup
[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' )->AddModifications({ surname => 'Hall', firstname => 'Kyle' });
15
16 ## Get the new pending modification
17 my $borrower = Koha::Borrower::Modifications->GetModifications({
18     verification_token => '1234567890'
19 });
20
21 ## Verify we get the same data
22 ok( $borrower->{'surname'} = 'Hall' );
23
24 ## Check the Verify method
25 ok( Koha::Borrower::Modifications->Verify( '1234567890' ) );
26
27 ## Delete the pending modification
28 $borrower = Koha::Borrower::Modifications->DelModifications({
29     verification_token => '1234567890'
30 });
31
32 ## Verify it's no longer in the database
33 $borrower = Koha::Borrower::Modifications->GetModifications({
34     verification_token => '1234567890'
35 });
36 ok( !defined( $borrower->{'surname'} ) );
37
38 ## Check the Verify method
39 ok( !Koha::Borrower::Modifications->Verify( '1234567890' ) );
40
41 ## Create new pending modification, but for an existing borrower
42 Koha::Borrower::Modifications->new( borrowernumber => '2' )->AddModifications({ surname => 'Hall', firstname => 'Kyle' });
43
44 ## Test the counter
45 ok( Koha::Borrower::Modifications->GetPendingModificationsCount() == 1 );
46
47 ## Create new pending modification for another existing borrower
48 Koha::Borrower::Modifications->new( borrowernumber => '3' )->AddModifications({ surname => 'Smith', firstname => 'Sandy' });
49
50 ## Test the counter
51 ok( Koha::Borrower::Modifications->GetPendingModificationsCount() == 2 );
52
53 ## Check GetPendingModifications
54 my $pending = Koha::Borrower::Modifications->GetPendingModifications();
55 ok( $pending->[0]->{'firstname'} eq 'Kyle' );
56 ok( $pending->[1]->{'firstname'} eq 'Sandy' );
57
58 ## This should delete the row from the table
59 Koha::Borrower::Modifications->DenyModifications( '3' );
60
61 ## Test the counter
62 ok( Koha::Borrower::Modifications->GetPendingModificationsCount() == 1 );
63
64 ## Save a copy of the borrowers original data
65 my $old_borrower = GetMember(borrowernumber => '2' );
66
67 ## Apply the modifications
68 Koha::Borrower::Modifications->ApproveModifications( '2' );
69
70 ## Test the counter
71 ok( Koha::Borrower::Modifications->GetPendingModificationsCount() == 0 );
72
73 ## Get a copy of the borrowers current data
74 my $new_borrower = GetMember(borrowernumber => '2' );
75
76 ## Check to see that the approved modifications were saved
77 ok( $new_borrower->{'surname'} eq 'Hall' );
78
79 ## Now let's put it back the way it was
80 Koha::Borrower::Modifications->new( borrowernumber => '2' )->AddModifications({ surname => $old_borrower->{'surname'}, firstname => $old_borrower->{'firstname'}  });
81
82 ## Test the counter
83 ok( Koha::Borrower::Modifications->GetPendingModificationsCount() == 1 );
84
85 ## Apply the modifications
86 Koha::Borrower::Modifications->ApproveModifications( '2' );
87
88 ## Test the counter
89 ok( Koha::Borrower::Modifications->GetPendingModificationsCount() == 0 );
90
91 $new_borrower = GetMember(borrowernumber => '2' );
92
93 ## Test to verify the borrower has been updated with the original values
94 ok( $new_borrower->{'surname'} eq $old_borrower->{'surname'} );