From 77d30dda544f98cf87653f2d808d6b20b39691dc Mon Sep 17 00:00:00 2001 From: Agustin Moyano Date: Fri, 30 Oct 2020 13:14:12 -0300 Subject: [PATCH] Bug 23260: (follow-up) Update test --- t/db_dependent/Koha/Patrons.t | 68 ++++++++++++++++++++++++++++++++--- 1 file changed, 63 insertions(+), 5 deletions(-) diff --git a/t/db_dependent/Koha/Patrons.t b/t/db_dependent/Koha/Patrons.t index 97494eca3d..f8cf3b5291 100755 --- a/t/db_dependent/Koha/Patrons.t +++ b/t/db_dependent/Koha/Patrons.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 41; +use Test::More tests => 42; use Test::Warn; use Test::Exception; use Test::MockModule; @@ -1200,16 +1200,71 @@ subtest 'search_patrons_to_anonymise & anonymise_issue_history' => sub { }; subtest 'anonymise items_last_borrower' => sub { - plan tests => 1; + plan tests => 2; my $anonymous = $builder->build_object( { class => 'Koha::Patrons', }, ); t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous->borrowernumber ); t::lib::Mocks::mock_preference('StoreLastBorrower', 1); + t::lib::Mocks::mock_preference('AnonymiseLastBorrower', 1); + + subtest 'anonymise items_last_borrower by days' => sub { + plan tests => 4; + + t::lib::Mocks::mock_preference('AnonymiseLastBorrowerDays', 5); + + my $patron = $builder->build_object( + { + class => 'Koha::Patrons', + value => { privacy => 1, } + } + ); + my $item_1 = $builder->build_sample_item; + my $issue_1 = $builder->build_object( + { + class => 'Koha::Checkouts', + value => { + borrowernumber => $patron->borrowernumber, + itemnumber => $item_1->itemnumber, + }, + } + ); + my $item_2 = $builder->build_sample_item; + my $issue_2 = $builder->build_object( + { + class => 'Koha::Checkouts', + value => { + borrowernumber => $patron->borrowernumber, + itemnumber => $item_2->itemnumber, + }, + } + ); + + my $dbh = C4::Context->dbh; + my ( $returned_1 ) = C4::Circulation::AddReturn( $item_1->barcode, undef, undef, dt_from_string ); + my ( $returned_2 ) = C4::Circulation::AddReturn( $item_2->barcode, undef, undef, dt_from_string ); + is( $returned_1 && $returned_2, 1, 'The items should have been returned' ); + my $stu = $dbh->prepare(q|UPDATE items_last_borrower set created_on = ? where itemnumber = ?|); + $stu->execute(DateTime->today(time_zone => C4::Context->tz())->add( days => -6 ), $item_2->itemnumber); + + my $nb_rows = Koha::Patrons->anonymise_last_borrowers(); + is( $nb_rows, 1, 'anonymise_issue_history should have returned the number of last borrowers anonymised'); + + my $sth = $dbh->prepare(q|SELECT borrowernumber FROM items_last_borrower where itemnumber = ?|); + $sth->execute($item_1->itemnumber); + my ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array; + is( $borrowernumber_used_to_anonymised, $patron->borrowernumber, 'Last borrower younger than 5 days are not anonymised' ); + $sth->execute($item_2->itemnumber); + ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array; + is( $borrowernumber_used_to_anonymised, $anonymous->borrowernumber, 'Last borrower older than 5 days are anonymised' ); + + }; subtest 'withrawn, lost and damaged items' => sub { plan tests => 6; + t::lib::Mocks::mock_preference('AnonymiseLastBorrowerDays', 0); + my $patron = $builder->build_object( { class => 'Koha::Patrons', @@ -1266,10 +1321,13 @@ subtest 'anonymise items_last_borrower' => sub { $item_2->itemlost(1)->store; $item_3->damaged(1)->store; - my $nb_rows = Koha::Patrons->search_patrons_to_anonymise( { before => '2010-10-12' } )->anonymise_issue_history(); - is( $nb_rows, 4, 'anonymise_issue_history should have returned the number of checkouts anonymised'); - my $dbh = C4::Context->dbh; + my $stu = $dbh->prepare(q|UPDATE items_last_borrower set created_on = ? where itemnumber in (?, ?, ?, ?)|); + $stu->execute(DateTime->today(time_zone => C4::Context->tz())->add( days => -1 ), $item_1->itemnumber,$item_2->itemnumber,$item_3->itemnumber,$item_4->itemnumber); + + my $nb_rows = Koha::Patrons->anonymise_last_borrowers(); + is( $nb_rows, 1, 'anonymise_issue_history should have returned the number of last borrowers anonymised'); + my $sth = $dbh->prepare(q|SELECT borrowernumber FROM items_last_borrower where itemnumber = ?|); $sth->execute($item_1->itemnumber); my ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array; -- 2.25.0