@@ -, +, @@ $ kshell k$ prove t/db_dependent/Koha/BackgroundJob.t --- Koha/BackgroundJob.pm | 28 ++++++++++++++++++++++++++++ t/db_dependent/Koha/BackgroundJob.t | 19 +++++++++++++++++-- 2 files changed, 45 insertions(+), 2 deletions(-) --- a/Koha/BackgroundJob.pm +++ a/Koha/BackgroundJob.pm @@ -232,6 +232,34 @@ sub finish { )->store; } +=head3 decoded_data + + my $job_data = $self->decoded_data; + +Returns the decoded JSON contents from $self->data. + +=cut + +sub decoded_data { + my ($self) = @_; + + return decode_json($self->data); +} + +=head3 set_encoded_data + + $self->set_encoded_data( $data ); + +Serializes I<$data> as a JSON string and sets the I attribute with it. + +=cut + +sub set_encoded_data { + my ( $self, $data ) = @_; + + return $self->data( encode_json($data) ); +} + =head3 job_type Return the job type of the job. Must be a string. --- a/t/db_dependent/Koha/BackgroundJob.t +++ a/t/db_dependent/Koha/BackgroundJob.t @@ -17,14 +17,14 @@ use Modern::Perl; -use Test::More tests => 3; +use Test::More tests => 4; use Test::Exception; use Koha::Database; use Koha::BackgroundJobs; use Koha::BackgroundJob::BatchUpdateItem; -use JSON qw( decode_json ); +use JSON qw( decode_json encode_json ); use t::lib::Mocks; use t::lib::TestBuilder; @@ -143,3 +143,18 @@ subtest 'start(), step() and finish() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'decoded_data() and set_encoded_data() tests' => sub { + + plan tests => 3; + + my $job = Koha::BackgroundJob::BatchUpdateItem->new->set_encoded_data( undef ); + is( $job->decoded_data, undef ); + + my $data = { some => 'data' }; + + $job->set_encoded_data( $data ); + + is_deeply( decode_json($job->data), $data ); + is_deeply( $job->decoded_data, $data ); +}; --