4 use Test::More tests => 14;
7 use t::lib::TestBuilder;
10 use Koha::Patron::Modifications;
12 my $dbh = C4::Context->dbh;
13 $dbh->{RaiseError} = 1;
14 $dbh->{AutoCommit} = 0;
16 $dbh->do("DELETE FROM borrower_modifications");
18 ## Create new pending modification
19 Koha::Patron::Modifications->new( verification_token => '1234567890' )
20 ->AddModifications( { surname => 'Hall', firstname => 'Kyle' } );
22 ## Get the new pending modification
23 my $borrower = Koha::Patron::Modifications->GetModifications(
24 { verification_token => '1234567890' } );
26 ## Verify we get the same data
27 ok( $borrower->{'surname'} = 'Hall',
28 'Test AddModifications() and GetModifications()' );
30 ## Check the Verify method
32 Koha::Patron::Modifications->Verify('1234567890'),
33 'Test that Verify() succeeds with a valid token'
36 ## Delete the pending modification
37 $borrower = Koha::Patron::Modifications->DelModifications(
38 { verification_token => '1234567890' } );
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()' );
45 ## Check the Verify method
47 !Koha::Patron::Modifications->Verify('1234567890'),
48 'Test that Verify() method fails for a bad token'
51 ## Create new pending modification, but for an existing borrower
52 ## But not a hardcoded borrowernumber of course (Bug 16407)
53 my $builder = t::lib::TestBuilder->new;
54 my $borr1 = $builder->build({ source => 'Borrower' })->{borrowernumber};
55 Koha::Patron::Modifications->new( borrowernumber => $borr1 )
56 ->AddModifications( { surname => 'Hall', firstname => 'Kyle' } );
59 ok( Koha::Patron::Modifications->GetPendingModificationsCount() == 1,
60 'Test GetPendingModificationsCount()' );
62 ## Create new pending modification for another existing borrower
63 my $borr2 = $builder->build({ source => 'Borrower' })->{borrowernumber};
64 Koha::Patron::Modifications->new( borrowernumber => $borr2 )
65 ->AddModifications( { surname => 'Smith', firstname => 'Sandy' } );
69 Koha::Patron::Modifications->GetPendingModificationsCount() == 2,
70 'Add a new pending modification and test GetPendingModificationsCount() again'
73 ## Check GetPendingModifications
74 my $pendings = Koha::Patron::Modifications->GetPendingModifications();
75 my @firstnames_mod = sort ( $pendings->[0]->{firstname}, $pendings->[1]->{firstname} );
76 ok( $firstnames_mod[0] eq 'Kyle', 'Test GetPendingModifications()' );
77 ok( $firstnames_mod[1] eq 'Sandy', 'Test GetPendingModifications() again' );
79 ## This should delete the row from the table
80 Koha::Patron::Modifications->DenyModifications( $borr2 );
83 ok( Koha::Patron::Modifications->GetPendingModificationsCount() == 1,
84 'Test DenyModifications()' );
86 ## Save a copy of the borrowers original data
87 my $old_borrower = GetMember( borrowernumber => $borr1 );
89 ## Apply the modifications
90 Koha::Patron::Modifications->ApproveModifications( $borr1 );
94 Koha::Patron::Modifications->GetPendingModificationsCount() == 0,
95 'Test ApproveModifications() removes pending modification from db'
98 ## Get a copy of the borrowers current data
99 my $new_borrower = GetMember( borrowernumber => $borr1 );
101 ## Check to see that the approved modifications were saved
102 ok( $new_borrower->{'surname'} eq 'Hall',
103 'Test ApproveModifications() applys modification to borrower' );
105 ## Now let's put it back the way it was
106 Koha::Patron::Modifications->new( borrowernumber => $borr1 )->AddModifications(
108 surname => $old_borrower->{'surname'},
109 firstname => $old_borrower->{'firstname'}
114 ok( Koha::Patron::Modifications->GetPendingModificationsCount() == 1,
115 'Test GetPendingModificationsCount()' );
117 ## Apply the modifications
118 Koha::Patron::Modifications->ApproveModifications( $borr1 );
122 Koha::Patron::Modifications->GetPendingModificationsCount() == 0,
123 'Test ApproveModifications() removes pending modification from db, again'
126 $new_borrower = GetMember( borrowernumber => $borr1 );
128 ## Test to verify the borrower has been updated with the original values
130 $new_borrower->{'surname'} eq $old_borrower->{'surname'},
131 'Test ApproveModifications() applys modification to borrower, again'