From 5cef97296121ae521d149e7ad2abbce2f1e4337f Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 16 Apr 2026 09:37:43 +0200 Subject: [PATCH] Bug 42394: Add regression test for userenv mutation during enqueue Verifies that BackgroundJob->enqueue() does not mutate the C4::Context userenv hashref as a side-effect. Signed-off-by: Nick Clemens Signed-off-by: Martin Renvoize Signed-off-by: Lucas Gass --- t/db_dependent/Koha/BackgroundJobs.t | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Koha/BackgroundJobs.t b/t/db_dependent/Koha/BackgroundJobs.t index f831cd0030..3839b038c7 100755 --- a/t/db_dependent/Koha/BackgroundJobs.t +++ b/t/db_dependent/Koha/BackgroundJobs.t @@ -20,14 +20,16 @@ use Modern::Perl; use Test::NoWarnings; -use Test::More tests => 15; +use Test::More tests => 16; use Test::MockModule; use List::MoreUtils qw(any); +use Storable qw( dclone ); use Koha::Database; use Koha::BackgroundJobs; use Koha::DateUtils qw( dt_from_string ); +use Koha::Session; use t::lib::TestBuilder; use t::lib::Mocks; @@ -147,3 +149,25 @@ subtest 'search_limited' => sub { $schema->storage->txn_rollback; }; + +subtest 'Do not mutate userenv' => sub { + plan tests => 1; + + $schema->storage->txn_begin; + my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { flags => 0 } } ); + + my $session = Koha::Session->get_session(); + C4::Context->set_userenv_from_session($session); + my $pre_userenv = dclone( C4::Context->userenv ); + my $job_id = t::lib::Koha::BackgroundJob::BatchTest->new->enqueue( + { + size => $job_size, + %$data + } + ); + + my $post_userenv = C4::Context->userenv; + is_deeply( $pre_userenv, $post_userenv ); + + $schema->storage->txn_rollback; +};