Bug 35277: (follow-up) Fix tests

This patch adjusts the tests to mock the Background to ensure it is called
and directly generate the pseudonymized transactions for testing

Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
This commit is contained in:
Nick Clemens 2024-01-16 14:26:25 +00:00 committed by Katrin Fischer
parent 71a881c4d6
commit a34593a95a
Signed by: kfischer
GPG key ID: 0EF6E2C03357A834

View file

@ -20,6 +20,7 @@
use Modern::Perl;
use Test::More tests => 3;
use Test::Warn;
use Try::Tiny;
use C4::Circulation qw( AddIssue AddReturn );
@ -52,7 +53,7 @@ subtest 'Config does not exist' => sub {
my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
try {
C4::Stats::UpdateStats(
my $stat = Koha::Statistic->new(
{
type => 'issue',
branch => $library->branchcode,
@ -63,9 +64,13 @@ subtest 'Config does not exist' => sub {
ccode => $item->ccode,
}
);
my $pseudo = Koha::PseudonymizedTransaction->new_from_statistic($stat);
} catch {
ok($_->isa('Koha::Exceptions::Config::MissingEntry'), "Koha::Patron->store should raise a Koha::Exceptions::Config::MissingEntry if 'bcrypt_settings' is not defined in the config");
ok(
$_->isa('Koha::Exceptions::Config::MissingEntry'),
"Koha::Patron->store should raise a Koha::Exceptions::Config::MissingEntry if 'bcrypt_settings' is not defined in the config"
);
is( $_->message, "Missing 'bcrypt_settings' entry in config file" );
};
@ -74,38 +79,54 @@ subtest 'Config does not exist' => sub {
subtest 'Koha::Anonymized::Transactions tests' => sub {
plan tests => 12;
plan tests => 15;
$schema->storage->txn_begin;
t::lib::Mocks::mock_config( 'bcrypt_settings', '$2a$08$9lmorEKnwQloheaCLFIfje' );
my $pseudo_background = Test::MockModule->new('Koha::BackgroundJob::PseudonymizeStatistic');
$pseudo_background->mock( enqueue => sub { warn "Called" } );
my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
t::lib::Mocks::mock_preference( 'Pseudonymization', 0 );
my $item = $builder->build_sample_item;
t::lib::Mocks::mock_userenv( { branchcode => $item->homebranch } );
warnings_are {
AddIssue( $patron, $item->barcode, dt_from_string );
}
undef, "No background job queued when pseudonymization disabled";
warnings_are {
AddReturn( $item->barcode, $item->homebranch, undef, dt_from_string );
my $pseudonymized= Koha::PseudonymizedTransactions->search(
{ itemnumber => $item->itemnumber } )->next;
is( $pseudonymized, undef,
'No pseudonymized transaction if Pseudonymization is off' );
}
undef, "No background job queued when pseudonymization disabled";
t::lib::Mocks::mock_preference( 'Pseudonymization', 1 );
t::lib::Mocks::mock_preference( 'PseudonymizationTransactionFields', 'datetime,transaction_branchcode,transaction_type,itemnumber,itemtype,holdingbranch,homebranch,location,itemcallnumber,ccode'
t::lib::Mocks::mock_preference(
'PseudonymizationTransactionFields',
'datetime,transaction_branchcode,transaction_type,itemnumber,itemtype,holdingbranch,homebranch,location,itemcallnumber,ccode'
);
$item = $builder->build_sample_item;
t::lib::Mocks::mock_userenv( { branchcode => $item->homebranch } );
warnings_are {
AddIssue( $patron, $item->barcode, dt_from_string );
}
["Called"], "Background job enqueued when pseudonymization enabled";
warnings_are {
AddReturn( $item->barcode, $item->homebranch, undef, dt_from_string );
}
["Called"], "Background job enqueued when pseudonymization enabled";
my $statistic = Koha::Statistics->search( { itemnumber => $item->itemnumber } )->next;
$pseudonymized = Koha::PseudonymizedTransactions->search( { itemnumber => $item->itemnumber } )->next;
like( $pseudonymized->hashed_borrowernumber,
qr{^\$2a\$08\$}, "The hashed_borrowernumber must be a bcrypt hash" );
my $pseudonymized = Koha::PseudonymizedTransaction->new_from_statistic($statistic);
like(
$pseudonymized->hashed_borrowernumber,
qr{^\$2a\$08\$}, "The hashed_borrowernumber must be a bcrypt hash"
);
is( $pseudonymized->datetime, $statistic->datetime, 'datetime attribute copied correctly' );
is( $pseudonymized->transaction_branchcode, $statistic->branch, 'transaction_branchcode copied correctly' );
is( $pseudonymized->transaction_type, $statistic->type, 'transacttion_type copied correctly' );
is( $pseudonymized->transaction_type, $statistic->type, 'transaction_type copied correctly' );
is( $pseudonymized->itemnumber, $item->itemnumber, 'itemnumber copied correctly' );
is( $pseudonymized->itemtype, $item->effective_itemtype, 'itemtype copied correctly' );
is( $pseudonymized->holdingbranch, $item->holdingbranch, 'holdingbranch copied correctly' );
@ -125,8 +146,10 @@ subtest 'PseudonymizedBorrowerAttributes tests' => sub {
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' );
t::lib::Mocks::mock_preference(
'PseudonymizationPatronFields',
'branchcode,categorycode,sort1'
);
my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
my $patron_info = $patron->unblessed;
@ -177,11 +200,10 @@ subtest 'PseudonymizedBorrowerAttributes tests' => sub {
$patron->extended_attributes($attribute_values);
my $library = $builder->build_object( { class => 'Koha::Libraries' } );
my $item = $builder->build_sample_item;
C4::Stats::UpdateStats(
my $statistic = Koha::Statistic->new(
{
type => 'issue',
branch => $library->branchcode,
@ -193,11 +215,12 @@ subtest 'PseudonymizedBorrowerAttributes tests' => sub {
}
);
my $p = Koha::PseudonymizedTransactions->search({itemnumber => $item->itemnumber})->next;
my $p = Koha::PseudonymizedTransaction->new_from_statistic($statistic)->store;
my $attributes =
Koha::Database->new->schema->resultset('PseudonymizedBorrowerAttribute')
->search( { transaction_id => $p->id }, { order_by => 'attribute' } );
is( $attributes->count, 2,
is(
$attributes->count, 2,
'Only the 2 attributes that have a type with keep_for_pseudonymization set should be kept'
);
my $attribute_1 = $attributes->next;