From 887df6b8ab132835f18106f0dc5138b9d95091b8 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Mon, 20 Jun 2022 16:01:28 +0100 Subject: [PATCH] Bug 30889: Unit tests This patch adds a unit test for the 'enqueue' part of the bug. We check that the mocked context (and interface) are recorded with the job enqueue in the new 'context' field. We do not yet test the 'process' end, where we then read the context out and set the job Context from it. --- t/db_dependent/Koha/BackgroundJob.t | 23 +++++++++++++++++++++-- t/lib/Mocks.pm | 2 +- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/t/db_dependent/Koha/BackgroundJob.t b/t/db_dependent/Koha/BackgroundJob.t index 0cdaa2f93c..68bea92fe1 100755 --- a/t/db_dependent/Koha/BackgroundJob.t +++ b/t/db_dependent/Koha/BackgroundJob.t @@ -65,7 +65,7 @@ subtest '_derived_class() tests' => sub { subtest 'enqueue() tests' => sub { - plan tests => 6; + plan tests => 7; $schema->storage->txn_begin; @@ -77,15 +77,34 @@ subtest 'enqueue() tests' => sub { is( $job->status, 'new', 'Initial status set correctly' ); is( $job->borrowernumber, undef, 'No userenv, borrowernumber undef' ); + my $interface = C4::Context->interface; my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); t::lib::Mocks::mock_userenv( { patron => $patron } ); + my $job_context = { + number => $patron->borrowernumber, + id => $patron->userid, + cardnumber => $patron->cardnumber, + firstname => $patron->firstname, + surname => $patron->surname, + branch => $patron->library->branchcode, + branchname => $patron->library->branchname, + flags => $patron->flags, + emailaddress => $patron->email, + register_id => undef, + register_name => undef, + shibboleth => undef, + desk_id => undef, + desk_name => undef, + interface => $interface + }; $job_id = Koha::BackgroundJob::BatchUpdateItem->new->enqueue( { record_ids => [ 1, 2, 3 ] } ); $job = Koha::BackgroundJobs->find($job_id)->_derived_class; is( $job->size, 3, 'Three steps' ); is( $job->status, 'new', 'Initial status set correctly' ); - is( $job->borrowernumber, $patron->id, 'No userenv, borrowernumber undef' ); + is( $job->borrowernumber, $patron->id, 'Borrowernumber set from userenv' ); + is_deeply( decode_json( $job->context ), $job_context, 'Context set from userenv + interface' ); $schema->storage->txn_rollback; }; diff --git a/t/lib/Mocks.pm b/t/lib/Mocks.pm index 0eaf2106bc..68d8829ca6 100644 --- a/t/lib/Mocks.pm +++ b/t/lib/Mocks.pm @@ -131,7 +131,7 @@ sub mock_userenv { my $branchcode = $params->{branchcode} || $userenv->{branchcode} || 'Branch4T'; my $branchname = $params->{branchname} || $userenv->{branchname}; my $flags = $params->{flags} || $userenv->{flags} || 0; - my $emailaddress = $params->{emailaddress} || $userenv->{emailaddress}; + my $emailaddress = $params->{emailaddress} || $userenv->{email}; my $desk_id = $params->{desk_id} || $userenv->{desk_id}; my $desk_name = $params->{desk_name} || $userenv->{desk_name}; my $register_id = $params->{register_id} || $userenv->{register_id}; -- 2.20.1