From c6d6938ef9b8e40e2a51589ecbfc0d3d7736c228 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Tue, 7 Nov 2023 16:55:44 +0000 Subject: [PATCH] Bug 35277: Unit tests prove -v t/db_dependent/Koha/BackgroundJob/PseudonymizeStatistic.t Signed-off-by: Martin Renvoize Signed-off-by: Marcel de Rooy Signed-off-by: Katrin Fischer --- .../BackgroundJob/PseudonymizeStatistic.t | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100755 t/db_dependent/Koha/BackgroundJob/PseudonymizeStatistic.t diff --git a/t/db_dependent/Koha/BackgroundJob/PseudonymizeStatistic.t b/t/db_dependent/Koha/BackgroundJob/PseudonymizeStatistic.t new file mode 100755 index 0000000000..a5e1662ba6 --- /dev/null +++ b/t/db_dependent/Koha/BackgroundJob/PseudonymizeStatistic.t @@ -0,0 +1,98 @@ +#!/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 . + +use Modern::Perl; + +use Test::More tests => 2; +use Test::Exception; + +use Koha::Database; +use Koha::BackgroundJobs; +use Koha::BackgroundJob::PseudonymizeStatistic; + +use t::lib::Mocks; +use t::lib::TestBuilder; + +use Carp::Always; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +subtest 'enqueue() tests' => sub { + + plan tests => 4; + + $schema->storage->txn_begin; + + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + my $item = $builder->build_sample_item(); + + throws_ok { + Koha::BackgroundJob::PseudonymizeStatistic->new->enqueue() + } + 'Koha::Exceptions::MissingParameter', + "Exception thrown if 'statistic' param is missing"; + + my $statistic = $builder->build_object( + { + class => 'Koha::Statistics', + value => { type => 'issue', borrowernumber => $patron->id, itemnumber => $item->id } + } + ); + + my $job_id = Koha::BackgroundJob::PseudonymizeStatistic->new->enqueue( { statistic => $statistic->unblessed } ); + + my $job = Koha::BackgroundJobs->find($job_id)->_derived_class; + + is( $job->size, 1, 'Size is correct' ); + is( $job->status, 'new', 'Initial status set correctly' ); + is( $job->queue, 'default', 'PseudonymizeStatistic should use the default queue' ); + + $schema->storage->txn_rollback; +}; + +subtest 'process() tests' => sub { + + plan tests => 2; + + $schema->storage->txn_begin; + + t::lib::Mocks::mock_config( 'bcrypt_settings', '$2a$08$9lmorEKnwQloheaCLFIfje' ); + t::lib::Mocks::mock_preference( 'Pseudonymization', 1 ); + t::lib::Mocks::mock_preference( 'PseudonymizationPatronFields', 'branchcode,categorycode,sort1' ); + + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + my $item = $builder->build_sample_item(); + my $statistic = $builder->build_object( + { + class => 'Koha::Statistics', + value => { type => 'issue', borrowernumber => $patron->id, itemnumber => $item->id } + } + ); + + my $job_id = Koha::BackgroundJob::PseudonymizeStatistic->new->enqueue( { statistic => $statistic->unblessed } ); + my $pseudonymized_transactions_before = Koha::PseudonymizedTransactions->search()->count(); + my $job = Koha::BackgroundJobs->find($job_id)->_derived_class; + $job->process( { statistic => $statistic->unblessed } ); + my $pseudonymized_transactions_after = Koha::PseudonymizedTransactions->search()->count(); + is( $pseudonymized_transactions_after, $pseudonymized_transactions_before + 1, "Pseudonymized transaction added" ); + + $job->discard_changes; + is( $job->data, '{"data":""}', "Job data cleared after pseudonymization" ); + + $schema->storage->txn_rollback; +}; -- 2.39.2