4 use Test::More tests => 16;
7 use t::lib::TestBuilder;
12 use Koha::Borrower::Modifications;
14 my $dbh = C4::Context->dbh;
15 $dbh->{RaiseError} = 1;
16 $dbh->{AutoCommit} = 0;
18 $dbh->do("DELETE FROM borrower_modifications");
20 ## Create new pending modification
21 Koha::Borrower::Modifications->new( verification_token => '1234567890' )
22 ->AddModifications( { surname => 'Hall', firstname => 'Kyle' } );
24 ## Ensure duplicate verification tokens cannot be added to the database
26 Koha::Patron::Modification->new(
28 verification_token => '1234567890',
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' );
39 ## Get the new pending modification
40 my $borrower = Koha::Borrower::Modifications->GetModifications(
41 { verification_token => '1234567890' } );
43 ## Verify we get the same data
44 ok( $borrower->{'surname'} = 'Hall',
45 'Test AddModifications() and GetModifications()' );
47 ## Check the Verify method
49 Koha::Borrower::Modifications->Verify('1234567890'),
50 'Test that Verify() succeeds with a valid token'
53 ## Delete the pending modification
54 $borrower = Koha::Borrower::Modifications->DelModifications(
55 { verification_token => '1234567890' } );
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()' );
62 ## Check the Verify method
64 !Koha::Borrower::Modifications->Verify('1234567890'),
65 'Test that Verify() method fails for a bad token'
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' } );
75 ok( Koha::Borrower::Modifications->GetPendingModificationsCount() == 1,
76 'Test GetPendingModificationsCount()' );
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' } );
85 Koha::Borrower::Modifications->GetPendingModificationsCount() == 2,
86 'Add a new pending modification and test GetPendingModificationsCount() again'
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' );
95 ## This should delete the row from the table
96 Koha::Borrower::Modifications->DenyModifications($borr2);
99 ok( Koha::Borrower::Modifications->GetPendingModificationsCount() == 1,
100 'Test DenyModifications()' );
102 ## Save a copy of the borrowers original data
103 my $old_borrower = GetMember( borrowernumber => $borr1 );
105 ## Apply the modifications
106 Koha::Borrower::Modifications->ApproveModifications($borr1);
110 Koha::Borrower::Modifications->GetPendingModificationsCount() == 0,
111 'Test ApproveModifications() removes pending modification from db'
114 ## Get a copy of the borrowers current data
115 my $new_borrower = GetMember( borrowernumber => $borr1 );
117 ## Check to see that the approved modifications were saved
118 ok( $new_borrower->{'surname'} eq 'Hall',
119 'Test ApproveModifications() applys modification to borrower' );
121 ## Now let's put it back the way it was
122 Koha::Borrower::Modifications->new( borrowernumber => $borr1 )->AddModifications(
124 surname => $old_borrower->{'surname'},
125 firstname => $old_borrower->{'firstname'}
130 ok( Koha::Borrower::Modifications->GetPendingModificationsCount() == 1,
131 'Test GetPendingModificationsCount()' );
133 ## Apply the modifications
134 Koha::Borrower::Modifications->ApproveModifications($borr1);
138 Koha::Borrower::Modifications->GetPendingModificationsCount() == 0,
139 'Test ApproveModifications() removes pending modification from db, again'
142 $new_borrower = GetMember( borrowernumber => $borr1 );
144 ## Test to verify the borrower has been updated with the original values
146 $new_borrower->{'surname'} eq $old_borrower->{'surname'},
147 'Test ApproveModifications() applys modification to borrower, again'