From a34593a95a6f93ccd9ec18beb5a20c4178fe328e Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Tue, 16 Jan 2024 14:26:25 +0000 Subject: [PATCH] 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 --- t/db_dependent/Koha/Pseudonymization.t | 87 ++++++++++++++++---------- 1 file changed, 55 insertions(+), 32 deletions(-) diff --git a/t/db_dependent/Koha/Pseudonymization.t b/t/db_dependent/Koha/Pseudonymization.t index ee7dd5732c..de0a103946 100755 --- a/t/db_dependent/Koha/Pseudonymization.t +++ b/t/db_dependent/Koha/Pseudonymization.t @@ -20,10 +20,11 @@ use Modern::Perl; use Test::More tests => 3; +use Test::Warn; use Try::Tiny; use C4::Circulation qw( AddIssue AddReturn ); -use C4::Stats qw( UpdateStats ); +use C4::Stats qw( UpdateStats ); use Koha::Database; use Koha::DateUtils qw( dt_from_string ); @@ -44,15 +45,15 @@ subtest 'Config does not exist' => sub { $schema->storage->txn_begin; 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' ); my $library = $builder->build_object( { class => 'Koha::Libraries' } ); my $item = $builder->build_sample_item; my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); - try{ - C4::Stats::UpdateStats( + try { + my $stat = Koha::Statistic->new( { type => 'issue', branch => $library->branchcode, @@ -63,10 +64,14 @@ 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"); - is( $_->message, "Missing 'bcrypt_settings' entry in config file"); + 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" ); }; $schema->storage->txn_rollback; @@ -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 }); - AddIssue( $patron, $item->barcode, dt_from_string ); - 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' ); + 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 ); + } + 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 }); - AddIssue( $patron, $item->barcode, dt_from_string ); - AddReturn( $item->barcode, $item->homebranch, undef, dt_from_string ); - 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" ); + 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; + 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,10 +146,12 @@ 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 = $builder->build_object( { class => 'Koha::Patrons' } ); my $patron_info = $patron->unblessed; delete $patron_info->{borrowernumber}; $patron->delete; @@ -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, + Koha::Database->new->schema->resultset('PseudonymizedBorrowerAttribute') + ->search( { transaction_id => $p->id }, { order_by => 'attribute' } ); + 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;