From b3c2f7d6c29fd99da666f75d0a05c4076102d05f Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Wed, 5 Mar 2014 10:11:51 -0500 Subject: [PATCH] Bug 9011 - Re-engineer circ anonymization, move to subroutine in C4::Circulation --- C4/Circulation.pm | 45 ++++++-- Koha/Schema/Result/OldIssue.pm | 6 + t/db_dependent/Circulation_anonymization.t | 153 ++++++++++++++++++++++++++++ 3 files changed, 193 insertions(+), 11 deletions(-) create mode 100644 t/db_dependent/Circulation_anonymization.t diff --git a/C4/Circulation.pm b/C4/Circulation.pm index f8b204a..7ff06be 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -47,6 +47,7 @@ use Algorithm::CheckDigits; use Data::Dumper; use Koha::DateUtils; use Koha::Calendar; +use Koha::Database; use Koha::Borrower::Debarments; use Carp; use Date::Calc qw( @@ -2000,17 +2001,7 @@ sub MarkIssueReturned { WHERE borrowernumber = ? AND itemnumber = ?'); $sth_copy->execute($borrowernumber, $itemnumber); - # anonymise patron checkout immediately if $privacy set to 2 and AnonymousPatron is set to a valid borrowernumber - if ( $privacy == 2) { - # The default of 0 does not work due to foreign key constraints - # The anonymisation will fail quietly if AnonymousPatron is not a valid entry - # FIXME the above is unacceptable - bug 9942 relates - my $anonymouspatron = (C4::Context->preference('AnonymousPatron')) ? C4::Context->preference('AnonymousPatron') : 0; - my $sth_ano = $dbh->prepare("UPDATE old_issues SET borrowernumber=? - WHERE borrowernumber = ? - AND itemnumber = ?"); - $sth_ano->execute($anonymouspatron, $borrowernumber, $itemnumber); - } + AnonymizeItemIssueHistory({ itemnumber => $itemnumber }); my $sth_del = $dbh->prepare("DELETE FROM issues WHERE borrowernumber = ? AND itemnumber = ?"); @@ -3522,6 +3513,38 @@ sub IsItemIssued { return $sth->fetchrow; } +=head AnonymizeItemIssueHistory + + AnonymizeItemIssueHistory({ itemnumber => $itemnumber }); + +=cut + +sub AnonymizeItemIssueHistory { + my ( $params ) = @_; + + my $itemnumber = $params->{itemnumber}; + return unless $itemnumber; + + my $a = C4::Context->preference('AnonymousPatron'); + return unless $a; + + my $schema = Koha::Database->new()->schema(); + + my $old_issues_rs = $schema->resultset('OldIssue')->search( + { + 'me.itemnumber' => $itemnumber, + 'me.borrowernumber' => { '!=' => $a }, + }, + { prefetch => 'borrower', order_by => { -desc => 'returndate' } } + ); + + my $oi = $old_issues_rs->next(); + if ( $oi->borrower()->privacy() == 2 ) { + $oi->set_column( 'borrowernumber', $a ); + $oi->update(); + } +} + 1; __END__ diff --git a/Koha/Schema/Result/OldIssue.pm b/Koha/Schema/Result/OldIssue.pm index d63b867..62b44a8 100644 --- a/Koha/Schema/Result/OldIssue.pm +++ b/Koha/Schema/Result/OldIssue.pm @@ -156,6 +156,12 @@ __PACKAGE__->belongs_to( # Created by DBIx::Class::Schema::Loader v0.07000 @ 2013-11-07 08:15:21 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:np3nmo0XYIYp92QbCY6/cw +__PACKAGE__->belongs_to( + "borrower", + "Koha::Schema::Result::Borrower", + { borrowernumber => "borrowernumber" }, + { join_type => "LEFT", on_delete => "CASCADE", on_update => "CASCADE" }, +); # You can replace this text with custom content, and it will be preserved on regeneration 1; diff --git a/t/db_dependent/Circulation_anonymization.t b/t/db_dependent/Circulation_anonymization.t new file mode 100644 index 0000000..caec6b5 --- /dev/null +++ b/t/db_dependent/Circulation_anonymization.t @@ -0,0 +1,153 @@ +#!/usr/bin/perl + +use Modern::Perl; + +use C4::Context; +use C4::Circulation; +use Koha::Database; + +use Test::More tests => 4; + +BEGIN { + use_ok('C4::Circulation'); + use_ok('Koha::Database'); +} + +#Start transaction +my $dbh = C4::Context->dbh; +$dbh->{RaiseError} = 1; +$dbh->{AutoCommit} = 0; + +$dbh->do(q|DELETE FROM issues|); +$dbh->do(q|DELETE FROM items|); +$dbh->do(q|DELETE FROM borrowers|); +$dbh->do(q|DELETE FROM branches|); +$dbh->do(q|DELETE FROM categories|); +$dbh->do(q|DELETE FROM accountlines|); +$dbh->do(q|DELETE FROM issuingrules|); + +my $schema = Koha::Database->new()->schema(); + +#Add branch and category +my $samplebranch1 = $schema->resultset('Branch')->create( + { + branchcode => 'CPL', + branchname => 'Sample Branch', + branchaddress1 => 'sample adr1', + branchaddress2 => 'sample adr2', + branchaddress3 => 'sample adr3', + branchzip => 'sample zip', + branchcity => 'sample city', + branchstate => 'sample state', + branchcountry => 'sample country', + branchphone => 'sample phone', + branchfax => 'sample fax', + branchemail => 'sample email', + branchurl => 'sample url', + branchip => 'sample ip', + branchprinter => undef, + opac_info => 'sample opac', + } +); + +my $samplecat = $schema->resultset('Category')->create( + { + categorycode => 'CAT1', + description => 'Description1', + enrolmentperiod => 'Null', + enrolmentperioddate => 'Null', + dateofbirthrequired => 'Null', + finetype => 'Null', + bulk => 'Null', + enrolmentfee => 'Null', + overduenoticerequired => 'Null', + issuelimit => 'Null', + reservefee => 'Null', + hidelostitems => 0, + category_type => 'Null' + } +); + +#Add biblio and item +my $record = MARC::Record->new(); +$record->append_fields( + MARC::Field->new( '952', '0', '0', a => $samplebranch1->{branchcode} ) ); +my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '' ); + +my $itemnumber; +( $biblionumber, $biblioitemnumber, $itemnumber ) = C4::Items::AddItem( + { + barcode => 'barcode_1', + itemcallnumber => 'callnumber1', + homebranch => $samplebranch1->branchcode(), + holdingbranch => $samplebranch1->branchcode(), + issue => 1, + reserve => 1 + }, + $biblionumber +); + +my $anonymous_patron = $schema->resultset('Borrower')->create( + { + firstname => 'Anonymous', + surname => 'Patron', + categorycode => $samplecat->categorycode(), + branchcode => $samplebranch1->branchcode(), + } +); +C4::Context->set_preference( 'AnonymousPatron', $anonymous_patron->borrowernumber() ); + +my $borrower1 = $schema->resultset('Borrower')->create( + { + firstname => 'firstname', + surname => 'surname', + categorycode => $samplecat->categorycode(), + branchcode => $samplebranch1->branchcode(), + privacy => 2, + } +); +my $borrower_1 = C4::Members::GetMember( borrowernumber => $borrower1->borrowernumber() ); + +my $borrower2 = $schema->resultset('Borrower')->create( + { + firstname => 'firstname', + surname => 'surname', + categorycode => $samplecat->categorycode(), + branchcode => $samplebranch1->branchcode(), + privacy => 1, + } +); +my $borrower_2 = C4::Members::GetMember( borrowernumber => $borrower2->borrowernumber() ); + +# NEED TO BE FIXED !!! +# The first parameter for set_userenv is the class ref +#my @USERENV = ( $borrower_id1, 'test', 'MASTERTEST', 'firstname', 'username', 'CPL', 'CPL', 'email@example.org' ); +my @USERENV = ( $borrower1->borrowernumber(), 'test', 'MASTERTEST', 'firstname', 'CPL', 'CPL', 'email@example.org' ); + +C4::Context->_new_userenv('DUMMY_SESSION_ID'); +C4::Context->set_userenv(@USERENV); + +my $userenv = C4::Context->userenv + or BAIL_OUT("No userenv"); + +#Begin Tests + +AddIssue( $borrower_1, 'barcode_1' ); +AddReturn('barcode_1'); +my $old_issue = + $schema->resultset('OldIssue')->search( { itemnumber => $itemnumber }, + { order_by => { -desc => 'issue_id' } } )->next(); +ok( $old_issue->borrower()->borrowernumber() == $anonymous_patron->borrowernumber(), "Patron with privacy of 2 anonymized" ); + +my $next_issue_id = $old_issue->issue_id() + 1; + + +AddIssue( $borrower_2, 'barcode_1' ); +AddReturn('barcode_1'); +$old_issue = + $schema->resultset('OldIssue')->find( $next_issue_id ); +ok( $old_issue->borrower()->borrowernumber() == $borrower2->borrowernumber(), "Patron with privacy of 1 not anonymized" ); + + +#End transaction +$dbh->rollback; -- 1.7.2.5