Koha/t/db_dependent/Koha_borrower_modifications.t
Yohann Dufour efee30f394 Bug 12623: SQLHelper replacement - Borrower::Modifications
With this patch, the subroutines AddModification and ApproveModifications uses DBIx::Class instead of C4::SQLHelper.
Moreover, the tests has been wrapped in a transaction.

Test plan:
1) Apply the patch

2) Execute the unit tests by launching :
prove t/db_dependent/Koha_borrower_modifications.t

3) The result has to be a success without error or warning :
t/db_dependent/Koha_borrower_modifications.t .. ok
All tests successful.
Files=1, Tests=14,  2 wallclock secs ( 0.03 usr  0.01 sys +  1.60 cusr  0.08 csys =  1.72 CPU)
Result: PASS

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@gmail.com>
2014-08-11 11:42:58 -03:00

129 lines
4.1 KiB
Perl
Executable file

#!/usr/bin/perl
use Modern::Perl;
use Test::More tests => 14;
use C4::Context;
use C4::Members;
use Koha::Borrower::Modifications;
my $dbh = C4::Context->dbh;
$dbh->{RaiseError} = 1;
$dbh->{AutoCommit} = 0;
$dbh->do("DELETE FROM borrower_modifications");
## Create new pending modification
Koha::Borrower::Modifications->new( verification_token => '1234567890' )
->AddModifications( { surname => 'Hall', firstname => 'Kyle' } );
## Get the new pending modification
my $borrower = Koha::Borrower::Modifications->GetModifications(
{ verification_token => '1234567890' } );
## Verify we get the same data
ok( $borrower->{'surname'} = 'Hall',
'Test AddModifications() and GetModifications()' );
## Check the Verify method
ok(
Koha::Borrower::Modifications->Verify('1234567890'),
'Test that Verify() succeeds with a valid token'
);
## Delete the pending modification
$borrower = Koha::Borrower::Modifications->DelModifications(
{ verification_token => '1234567890' } );
## Verify it's no longer in the database
$borrower = Koha::Borrower::Modifications->GetModifications(
{ verification_token => '1234567890' } );
ok( !defined( $borrower->{'surname'} ), 'Test DelModifications()' );
## Check the Verify method
ok(
!Koha::Borrower::Modifications->Verify('1234567890'),
'Test that Verify() method fails for a bad token'
);
## Create new pending modification, but for an existing borrower
Koha::Borrower::Modifications->new( borrowernumber => '2' )
->AddModifications( { surname => 'Hall', firstname => 'Kyle' } );
## Test the counter
ok( Koha::Borrower::Modifications->GetPendingModificationsCount() == 1,
'Test GetPendingModificationsCount()' );
## Create new pending modification for another existing borrower
Koha::Borrower::Modifications->new( borrowernumber => '3' )
->AddModifications( { surname => 'Smith', firstname => 'Sandy' } );
## Test the counter
ok(
Koha::Borrower::Modifications->GetPendingModificationsCount() == 2,
'Add a new pending modification and test GetPendingModificationsCount() again'
);
## Check GetPendingModifications
my $pendings = Koha::Borrower::Modifications->GetPendingModifications();
my @firstnames_mod = sort ( $pendings->[0]->{firstname}, $pendings->[1]->{firstname} );
ok( $firstnames_mod[0] eq 'Kyle', 'Test GetPendingModifications()' );
ok( $firstnames_mod[1] eq 'Sandy', 'Test GetPendingModifications() again' );
## This should delete the row from the table
Koha::Borrower::Modifications->DenyModifications('3');
## Test the counter
ok( Koha::Borrower::Modifications->GetPendingModificationsCount() == 1,
'Test DenyModifications()' );
## Save a copy of the borrowers original data
my $old_borrower = GetMember( borrowernumber => '2' );
## Apply the modifications
Koha::Borrower::Modifications->ApproveModifications('2');
## Test the counter
ok(
Koha::Borrower::Modifications->GetPendingModificationsCount() == 0,
'Test ApproveModifications() removes pending modification from db'
);
## Get a copy of the borrowers current data
my $new_borrower = GetMember( borrowernumber => '2' );
## Check to see that the approved modifications were saved
ok( $new_borrower->{'surname'} eq 'Hall',
'Test ApproveModifications() applys modification to borrower' );
## Now let's put it back the way it was
Koha::Borrower::Modifications->new( borrowernumber => '2' )->AddModifications(
{
surname => $old_borrower->{'surname'},
firstname => $old_borrower->{'firstname'}
}
);
## Test the counter
ok( Koha::Borrower::Modifications->GetPendingModificationsCount() == 1,
'Test GetPendingModificationsCount()' );
## Apply the modifications
Koha::Borrower::Modifications->ApproveModifications('2');
## Test the counter
ok(
Koha::Borrower::Modifications->GetPendingModificationsCount() == 0,
'Test ApproveModifications() removes pending modification from db, again'
);
$new_borrower = GetMember( borrowernumber => '2' );
## Test to verify the borrower has been updated with the original values
ok(
$new_borrower->{'surname'} eq $old_borrower->{'surname'},
'Test ApproveModifications() applys modification to borrower, again'
);
$dbh->rollback();