From 98dd4163cdd61975a5aa3d8a8fd3586f34b63295 Mon Sep 17 00:00:00 2001 From: Lari Taskula Date: Tue, 15 Oct 2024 16:47:36 +0000 Subject: [PATCH] Bug 32440: Add missing unit test for Koha::Statistic->pseudonymize() To test: 1. prove t/db_dependent/Koha/Statistic.t Observe Subtest: pseudonymize() tests 1..6 ok 1 - some pseudonymization_types are defined ok 2 - Background job enqueued for type renew ok 3 - Background job enqueued for type issue ok 4 - Background job enqueued for type return ok 5 - Background job enqueued for type onsite_checkout ok 6 - Background job not enqueued for type not_gonna_pseudo ok 2 - pseudonymize() tests --- t/db_dependent/Koha/Statistic.t | 68 ++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Koha/Statistic.t b/t/db_dependent/Koha/Statistic.t index 1146c3330d..000ecb2133 100755 --- a/t/db_dependent/Koha/Statistic.t +++ b/t/db_dependent/Koha/Statistic.t @@ -17,7 +17,9 @@ use Modern::Perl; -use Test::More tests => 1; +use Test::MockModule; +use Test::More tests => 2; +use Test::Warn; use Koha::DateUtils qw(dt_from_string); use Koha::Statistics; @@ -56,3 +58,67 @@ subtest 'new() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'pseudonymize() tests' => sub { + + my @pseudonymization_types = qw(renew issue return onsite_checkout); + plan tests => scalar(@pseudonymization_types) + 2; + + $schema->storage->txn_begin; + + my $not_type = 'not_gonna_pseudo'; + + my $pseudo_background = Test::MockModule->new('Koha::BackgroundJob::PseudonymizeStatistic'); + $pseudo_background->mock( enqueue => sub { warn "Called" } ); + + my @statistics = (); + + ok( scalar(@pseudonymization_types) > 0, 'some pseudonymization_types are defined' ); + + my $sub_days = 0; # TestBuilder does not handle many rows of Koha::Statistics very intuitively + foreach my $type (@pseudonymization_types) { + push( + @statistics, + $builder->build_object( + { + class => 'Koha::Statistics', + value => { + datetime => $dtf->format_datetime( dt_from_string()->subtract( days => $sub_days ) ), + type => $type, + }, + } + ) + ); + $sub_days++; + } + + push( + @statistics, + $builder->build_object( + { + class => 'Koha::Statistics', + value => { + datetime => $dtf->format_datetime( dt_from_string()->subtract( days => 7 ) ), + type => $not_type, + }, + } + ) + ); + + foreach my $statistic (@statistics) { + my $type = $statistic->type; + if ( $type ne $not_type ) { + warnings_are { + $statistic->pseudonymize(); + } + ["Called"], "Background job enqueued for type $type"; + } else { + warnings_are { + $statistic->pseudonymize(); + } + undef, "Background job not enqueued for type $type"; + } + } + + $schema->storage->txn_rollback; +}; \ No newline at end of file -- 2.34.1