Koha/t/db_dependent/Circulation/AnonymiseIssueHistory.t
Kyle M Hall ecd89d2030 Bug 14945 - Add the ability to store the last patron to return an item
Currently if the AnonymousPatron system preference is in use, all patron
data is anonymized. Some libraries would like to be able to see the last
patron who returned out an item ( in case of damage ) but still keep all
other patrons anonymized.

* Add the table items_last_borrower ( id, itemnumber, borrowernumber )
* Add new system preference StoreLastBorrower
* If StoreLastBorrower is enabled, upon checkin have Koha insert into
  this table the patron who last returned this item. Replace existing
  row based on itemnumber if exists.
* If table has a row for a given item, link to the patron from the item
  details page.

Test plan:
1) Apply patch
2) Run updatedatabase.pl
3) Enable StoreLastBorrower
4) Issue an item to a patron and return said item
5) Issue the same item to a second patron, do not return it.
6) View moredetail.pl for the given bib, find the given item. There
   should be a new field in the history list 'Last returned by' with a link
   to the last patron to return the item.

Optionally, you can also verify this works even if patron issuing
history has been set to anonymize issues upon return.

Signed-off-by: Nick Clemens <nick@quecheelibrary.org>

Signed-off-by: Jen DeMuth <JDeMuth@roseville.ca.us>
Signed-off-by: Tom Misilo <misilot@fit.edu>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

Signed-off-by: Brendan Gallagher brendan@bywatersolutions.com
2015-12-31 19:32:20 +00:00

248 lines
8 KiB
Perl

#!/usr/bin/perl
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use Test::More tests => 4;
use C4::Context;
use C4::Circulation;
use Koha::Database;
use Koha::Items;
use t::lib::Mocks;
use t::lib::TestBuilder;
my $schema = Koha::Database->new->schema;
$schema->storage->txn_begin;
my $builder = t::lib::TestBuilder->new;
# TODO create a subroutine in t::lib::Mocks
my $userenv_patron = $builder->build( { source => 'Borrower', }, );
C4::Context->_new_userenv('DUMMY SESSION');
C4::Context->set_userenv(
$userenv_patron->{borrowernumber},
$userenv_patron->{userid},
'usercnum', 'First name', 'Surname',
$userenv_patron->{_fk}{branchcode}{branchcode},
$userenv_patron->{_fk}{branchcode}{branchname}, 0
);
my $anonymous = $builder->build( { source => 'Borrower', }, );
t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous->{borrowernumber} );
subtest 'patron privacy is 1 (default)' => sub {
plan tests => 4;
my $patron = $builder->build(
{ source => 'Borrower',
value => { privacy => 1, }
}
);
my $item = $builder->build(
{ source => 'Item',
value => {
itemlost => 0,
withdrawn => 0,
},
}
);
my $issue = $builder->build(
{ source => 'Issue',
value => {
borrowernumber => $patron->{borrowernumber},
itemnumber => $item->{itemnumber},
},
}
);
my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' );
is( $returned, 1, 'The item should have been returned' );
my ( $rows_affected, $err ) = C4::Circulation::AnonymiseIssueHistory('2010-10-11');
ok( $rows_affected > 0, 'AnonymiseIssueHistory should affect at least 1 row' );
is( $err, undef, 'AnonymiseIssueHistory should not return any error if success' );
my $dbh = C4::Context->dbh;
my ($borrowernumber_used_to_anonymised) = $dbh->selectrow_array(q|
SELECT borrowernumber FROM old_issues where itemnumber = ?
|, undef, $item->{itemnumber});
is( $borrowernumber_used_to_anonymised, $anonymous->{borrowernumber}, 'With privacy=1, the issue should have been anonymised' );
};
subtest 'patron privacy is 0 (forever)' => sub {
plan tests => 3;
my $patron = $builder->build(
{ source => 'Borrower',
value => { privacy => 0, }
}
);
my $item = $builder->build(
{ source => 'Item',
value => {
itemlost => 0,
withdrawn => 0,
},
}
);
my $issue = $builder->build(
{ source => 'Issue',
value => {
borrowernumber => $patron->{borrowernumber},
itemnumber => $item->{itemnumber},
},
}
);
my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' );
is( $returned, 1, 'The item should have been returned' );
my ( $rows_affected, $err ) = C4::Circulation::AnonymiseIssueHistory('2010-10-11');
is( $err, undef, 'AnonymiseIssueHistory should not return any error if success' );
my $dbh = C4::Context->dbh;
my ($borrowernumber_used_to_anonymised) = $dbh->selectrow_array(q|
SELECT borrowernumber FROM old_issues where itemnumber = ?
|, undef, $item->{itemnumber});
is( $borrowernumber_used_to_anonymised, $patron->{borrowernumber}, 'With privacy=0, the issue should not be anonymised' );
};
t::lib::Mocks::mock_preference( 'AnonymousPatron', '' );
subtest 'AnonymousPatron is not defined' => sub {
plan tests => 4;
my $patron = $builder->build(
{ source => 'Borrower',
value => { privacy => 1, }
}
);
my $item = $builder->build(
{ source => 'Item',
value => {
itemlost => 0,
withdrawn => 0,
},
}
);
my $issue = $builder->build(
{ source => 'Issue',
value => {
borrowernumber => $patron->{borrowernumber},
itemnumber => $item->{itemnumber},
},
}
);
my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' );
is( $returned, 1, 'The item should have been returned' );
my ( $rows_affected, $err ) = C4::Circulation::AnonymiseIssueHistory('2010-10-11');
ok( $rows_affected > 0, 'AnonymiseIssueHistory should affect at least 1 row' );
is( $err, undef, 'AnonymiseIssueHistory should not return any error if success' );
my $dbh = C4::Context->dbh;
my ($borrowernumber_used_to_anonymised) = $dbh->selectrow_array(q|
SELECT borrowernumber FROM old_issues where itemnumber = ?
|, undef, $item->{itemnumber});
is( $borrowernumber_used_to_anonymised, undef, 'With AnonymousPatron is not defined, the issue should have been anonymised anyway' );
};
subtest 'Test StoreLastBorrower' => sub {
plan tests => 4;
t::lib::Mocks::mock_preference( 'StoreLastBorrower', '1' );
my $patron = $builder->build(
{
source => 'Borrower',
value => { privacy => 1, }
}
);
my $item = $builder->build(
{
source => 'Item',
value => {
itemlost => 0,
withdrawn => 0,
},
}
);
my $issue = $builder->build(
{
source => 'Issue',
value => {
borrowernumber => $patron->{borrowernumber},
itemnumber => $item->{itemnumber},
},
}
);
my $item_object = Koha::Items->find( $item->{itemnumber} );
my $patron_object = $item_object->last_returned_by();
is( $patron_object, undef, 'Koha::Item::last_returned_by returned undef' );
my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' );
$item_object = Koha::Items->find( $item->{itemnumber} );
$patron_object = $item_object->last_returned_by();
is( ref($patron_object), 'Koha::Borrower', 'Koha::Item::last_returned_by returned Koha::Borrower' );
$patron = $builder->build(
{
source => 'Borrower',
value => { privacy => 1, }
}
);
$issue = $builder->build(
{
source => 'Issue',
value => {
borrowernumber => $patron->{borrowernumber},
itemnumber => $item->{itemnumber},
},
}
);
( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' );
$item_object = Koha::Items->find( $item->{itemnumber} );
$patron_object = $item_object->last_returned_by();
is( $patron_object->id, $patron->{borrowernumber}, 'Second patron to return item replaces the first' );
$patron = $builder->build(
{
source => 'Borrower',
value => { privacy => 1, }
}
);
$patron_object = Koha::Borrowers->find( $patron->{borrowernumber} );
$item_object->last_returned_by($patron_object);
$item_object = Koha::Items->find( $item->{itemnumber} );
my $patron_object2 = $item_object->last_returned_by();
is( $patron_object->id, $patron_object2->id,
'Calling last_returned_by with Borrower object sets last_returned_by to that borrower' );
};
$schema->storage->txn_rollback;
1;