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 for a patron
52 my $builder = t::lib::TestBuilder->new;
53 my $borr1 = $builder->build({ source => 'Borrower' })->{borrowernumber};
54 Koha::Patron::Modifications->new( borrowernumber => $borr1 )
55 ->AddModifications( { surname => 'Hall', firstname => 'Kyle' } );
58 ok( Koha::Patron::Modifications->GetPendingModificationsCount() == 1,
59 'Test GetPendingModificationsCount()' );
61 ## Create new pending modification for another patron
62 my $borr2 = $builder->build({ source => 'Borrower' })->{borrowernumber};
63 Koha::Patron::Modifications->new( borrowernumber => $borr2 )
64 ->AddModifications( { surname => 'Smith', firstname => 'Sandy' } );
68 Koha::Patron::Modifications->GetPendingModificationsCount() == 2,
69 'Add a new pending modification and test GetPendingModificationsCount() again'
72 ## Check GetPendingModifications
73 my $pendings = Koha::Patron::Modifications->GetPendingModifications();
74 my @firstnames_mod = sort ( $pendings->[0]->{firstname}, $pendings->[1]->{firstname} );
75 ok( $firstnames_mod[0] eq 'Kyle', 'Test GetPendingModifications()' );
76 ok( $firstnames_mod[1] eq 'Sandy', 'Test GetPendingModifications() again' );
78 ## This should delete the row from the table
79 Koha::Patron::Modifications->DenyModifications( $borr2 );
82 ok( Koha::Patron::Modifications->GetPendingModificationsCount() == 1,
83 'Test DenyModifications()' );
85 ## Save a copy of the borrowers original data
86 my $old_borrower = GetMember( borrowernumber => $borr1 );
88 ## Apply the modifications
89 Koha::Patron::Modifications->ApproveModifications( $borr1 );
93 Koha::Patron::Modifications->GetPendingModificationsCount() == 0,
94 'Test ApproveModifications() removes pending modification from db'
97 ## Get a copy of the borrowers current data
98 my $new_borrower = GetMember( borrowernumber => $borr1 );
100 ## Check to see that the approved modifications were saved
101 ok( $new_borrower->{'surname'} eq 'Hall',
102 'Test ApproveModifications() applys modification to borrower' );
104 ## Now let's put it back the way it was
105 Koha::Patron::Modifications->new( borrowernumber => $borr1 )->AddModifications(
107 surname => $old_borrower->{'surname'},
108 firstname => $old_borrower->{'firstname'}
113 ok( Koha::Patron::Modifications->GetPendingModificationsCount() == 1,
114 'Test GetPendingModificationsCount()' );
116 ## Apply the modifications
117 Koha::Patron::Modifications->ApproveModifications( $borr1 );
121 Koha::Patron::Modifications->GetPendingModificationsCount() == 0,
122 'Test ApproveModifications() removes pending modification from db, again'
125 $new_borrower = GetMember( borrowernumber => $borr1 );
127 ## Test to verify the borrower has been updated with the original values
129 $new_borrower->{'surname'} eq $old_borrower->{'surname'},
130 'Test ApproveModifications() applys modification to borrower, again'