From 42ecd97a7a91d5c63c6cdd35b48361a8d70cdf56 Mon Sep 17 00:00:00 2001 From: Yohann Dufour Date: Wed, 23 Jul 2014 14:24:04 +0200 Subject: [PATCH] [SIGNED-OFF] 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 --- Koha/Borrower/Modifications.pm | 55 +++++++++----------------- t/db_dependent/Koha_borrower_modifications.t | 8 +++- 2 files changed, 26 insertions(+), 37 deletions(-) diff --git a/Koha/Borrower/Modifications.pm b/Koha/Borrower/Modifications.pm index 1d32d16..00083fb 100644 --- a/Koha/Borrower/Modifications.pm +++ b/Koha/Borrower/Modifications.pm @@ -26,7 +26,6 @@ use Modern::Perl; use C4::Context; use C4::Debug; -use C4::SQLHelper qw(InsertInTable UpdateInTable); sub new { my ( $class, %args ) = @_; @@ -48,42 +47,20 @@ to be part of the passed in hash. sub AddModifications { my ( $self, $data ) = @_; - if ( $self->{'borrowernumber'} ) { - delete $data->{'borrowernumber'}; - - if ( keys %$data ) { - $data->{'borrowernumber'} = $self->{'borrowernumber'}; - my $dbh = C4::Context->dbh; - - my $query = " - SELECT COUNT(*) AS count - FROM borrower_modifications - WHERE borrowernumber = ? - "; - - my $sth = $dbh->prepare($query); - $sth->execute( $self->{'borrowernumber'} ); - my $result = $sth->fetchrow_hashref(); - - if ( $result->{'count'} ) { - $data->{'verification_token'} = q{}; - return UpdateInTable( "borrower_modifications", $data ); - } - else { - return InsertInTable( "borrower_modifications", $data ); - } - } - + delete $data->{borrowernumber}; + if( $self->{borrowernumber} ) { + return if( not keys %$data ); + $data->{borrowernumber} = $self->{borrowernumber}; } - elsif ( $self->{'verification_token'} ) { - delete $data->{'borrowernumber'}; - $data->{'verification_token'} = $self->{'verification_token'}; - - return InsertInTable( "borrower_modifications", $data ); + elsif( $self->{verification_token} ) { + $data->{verification_token} = $self->{verification_token}; } else { return; } + + my $rs = Koha::Database->new()->schema->resultset('BorrowerModification'); + return $rs->update_or_create($data); } =head2 Verify @@ -120,6 +97,7 @@ sub Verify { $count = Koha::Borrower::Modifications->GetPendingModificationsCount(); Returns the number of pending modifications for existing borrowers. + =cut sub GetPendingModificationsCount { @@ -203,10 +181,15 @@ sub ApproveModifications { return unless $borrowernumber; - my $data = $self->GetModifications({ borrowernumber => $borrowernumber }); + my $data = $self->GetModifications( { borrowernumber => $borrowernumber } ); + delete $data->{timestamp}; + delete $data->{verification_token}; - if ( UpdateInTable( "borrowers", $data ) ) { - $self->DelModifications({ borrowernumber => $borrowernumber }); + my $rs = Koha::Database->new()->schema->resultset('Borrower')->search({ + borrowernumber => $data->{borrowernumber}, + }); + if( $rs->update($data) ) { + $self->DelModifications( { borrowernumber => $borrowernumber } ); } } @@ -227,7 +210,7 @@ sub DenyModifications { return unless $borrowernumber; - return $self->DelModifications({ borrowernumber => $borrowernumber }); + return $self->DelModifications( { borrowernumber => $borrowernumber } ); } =head2 DelModifications diff --git a/t/db_dependent/Koha_borrower_modifications.t b/t/db_dependent/Koha_borrower_modifications.t index 9849502..08c92cd 100755 --- a/t/db_dependent/Koha_borrower_modifications.t +++ b/t/db_dependent/Koha_borrower_modifications.t @@ -8,7 +8,11 @@ use C4::Members; use Koha::Borrower::Modifications; -C4::Context->dbh->do("TRUNCATE TABLE borrower_modifications"); +my $dbh = C4::Context->dbh; +$dbh->{RaiseError} = 1; +$dbh->{AutoCommit} = 0; + +$dbh->do("TRUNCATE TABLE borrower_modifications"); ## Create new pending modification Koha::Borrower::Modifications->new( verification_token => '1234567890' ) @@ -123,3 +127,5 @@ ok( $new_borrower->{'surname'} eq $old_borrower->{'surname'}, 'Test ApproveModifications() applys modification to borrower, again' ); + +$dbh->rollback(); -- 1.7.2.5