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