From 8d63dd37609bbdb07768dab1be93b3259199ad3d Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Fri, 4 Feb 2022 08:14:30 -0300 Subject: [PATCH] Bug 30007: Regression tests Signed-off-by: David Nind Signed-off-by: Martin Renvoize https://bugs.koha-community.org/show_bug.cgi?id=3007 Signed-off-by: Fridolin Somers --- t/db_dependent/Koha/Old/Checkouts.t | 27 ++++++++++++++++++++++++++- t/db_dependent/Koha/Old/Hold.t | 16 +++++++++------- t/db_dependent/Koha/Old/Holds.t | 29 ++++++++++++++++++++++++++++- 3 files changed, 63 insertions(+), 9 deletions(-) diff --git a/t/db_dependent/Koha/Old/Checkouts.t b/t/db_dependent/Koha/Old/Checkouts.t index 97559495a6..2c7c52ad62 100755 --- a/t/db_dependent/Koha/Old/Checkouts.t +++ b/t/db_dependent/Koha/Old/Checkouts.t @@ -18,6 +18,7 @@ use Modern::Perl; use Test::More tests => 2; +use Test::Exception; use Koha::Database; use Koha::DateUtils qw(dt_from_string); @@ -31,13 +32,26 @@ my $builder = t::lib::TestBuilder->new; subtest 'anonymize() tests' => sub { - plan tests => 5; + plan tests => 10; $schema->storage->txn_begin; my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + my $anonymous_patron = $builder->build_object({ class => 'Koha::Patrons' }); is( $patron->old_checkouts->count, 0, 'Patron has no old checkouts' ); + + t::lib::Mocks::mock_preference( 'AnonymousPatron', undef ); + + throws_ok + { $patron->old_checkouts->anonymize; } + 'Koha::Exceptions::SysPref::NotSet', + 'Exception thrown because AnonymousPatron not set'; + + is( $@->syspref, 'AnonymousPatron', 'syspref parameter is correctly passed' ); + + t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous_patron->id ); + is( $patron->old_checkouts->anonymize + 0, 0, 'Anonymizing an empty resultset returns 0' ); @@ -82,6 +96,17 @@ subtest 'anonymize() tests' => sub { my $checkouts = $patron->old_checkouts->filter_by_last_update( { days => 1, days_inclusive => 1 } ); + t::lib::Mocks::mock_preference( 'AnonymousPatron', undef ); + throws_ok + { $checkouts->anonymize; } + 'Koha::Exceptions::SysPref::NotSet', + 'Exception thrown because AnonymousPatron not set'; + + is( $@->syspref, 'AnonymousPatron', 'syspref parameter is correctly passed' ); + is( $patron->old_checkouts->count, 4, 'Patron has 4 completed checkouts' ); + + t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous_patron->id ); + # Anonymize them my $anonymized_count = $checkouts->anonymize(); is( $anonymized_count, 2, 'update() tells 2 rows were updated' ); diff --git a/t/db_dependent/Koha/Old/Hold.t b/t/db_dependent/Koha/Old/Hold.t index b660ec4914..91ed7a71c2 100755 --- a/t/db_dependent/Koha/Old/Hold.t +++ b/t/db_dependent/Koha/Old/Hold.t @@ -18,6 +18,7 @@ use Modern::Perl; use Test::More tests => 1; +use Test::Exception; use Koha::Database; @@ -29,7 +30,7 @@ my $builder = t::lib::TestBuilder->new; subtest 'anonymize() tests' => sub { - plan tests => 6; + plan tests => 8; $schema->storage->txn_begin; @@ -54,14 +55,15 @@ subtest 'anonymize() tests' => sub { t::lib::Mocks::mock_preference( 'AnonymousPatron', undef ); - $hold_1->anonymize; + throws_ok + { $hold_1->anonymize; } + 'Koha::Exceptions::SysPref::NotSet', + 'Exception thrown because AnonymousPatron not set'; - is( $patron->old_holds->count, 1, 'Patron has 1 linked completed holds' ); + is( $@->syspref, 'AnonymousPatron', 'syspref parameter is correctly passed' ); + is( $patron->old_holds->count, 2, 'No changes, patron has 2 linked completed holds' ); - # Reload - $hold_1->discard_changes; - $hold_2->discard_changes; - is( $hold_1->borrowernumber, undef, + is( $hold_1->borrowernumber, $patron->id, 'Anonymized hold not linked to patron' ); is( $hold_2->borrowernumber, $patron->id, 'Not anonymized hold still linked to patron' ); diff --git a/t/db_dependent/Koha/Old/Holds.t b/t/db_dependent/Koha/Old/Holds.t index befaefe7d1..cd791ad350 100755 --- a/t/db_dependent/Koha/Old/Holds.t +++ b/t/db_dependent/Koha/Old/Holds.t @@ -18,11 +18,13 @@ use Modern::Perl; use Test::More tests => 2; +use Test::Exception; use Koha::Database; use Koha::DateUtils qw(dt_from_string); use Koha::Old::Holds; +use t::lib::Mocks; use t::lib::TestBuilder; my $schema = Koha::Database->new->schema; @@ -30,13 +32,26 @@ my $builder = t::lib::TestBuilder->new; subtest 'anonymize() tests' => sub { - plan tests => 5; + plan tests => 10; $schema->storage->txn_begin; my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + my $anonymous_patron = $builder->build_object({ class => 'Koha::Patrons' }); is( $patron->old_holds->count, 0, 'Patron has no old holds' ); + + t::lib::Mocks::mock_preference( 'AnonymousPatron', undef ); + + throws_ok + { $patron->old_holds->anonymize; } + 'Koha::Exceptions::SysPref::NotSet', + 'Exception thrown because AnonymousPatron not set'; + + is( $@->syspref, 'AnonymousPatron', 'syspref parameter is correctly passed' ); + + t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous_patron->id ); + is( $patron->old_holds->anonymize + 0, 0, 'Anonymizing an empty resultset returns 0' ); my $hold_1 = $builder->build_object( @@ -78,6 +93,18 @@ subtest 'anonymize() tests' => sub { # filter them so only the older two are part of the resultset my $holds = $patron->old_holds->search({ timestamp => { '<=' => dt_from_string()->subtract( days => 2 ) } }); # Anonymize them + + t::lib::Mocks::mock_preference( 'AnonymousPatron', undef ); + throws_ok + { $holds->anonymize; } + 'Koha::Exceptions::SysPref::NotSet', + 'Exception thrown because AnonymousPatron not set'; + + is( $@->syspref, 'AnonymousPatron', 'syspref parameter is correctly passed' ); + is( $patron->old_holds->count, 4, 'Patron has 4 completed holds' ); + + t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous_patron->id ); + my $anonymized_count = $holds->anonymize(); is( $anonymized_count, 2, 'update() tells 2 rows were updated' );