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