From 47211deee16ba3f9827926f3d76153a1fa3959ec Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Tue, 12 Dec 2023 12:02:53 -0500 Subject: [PATCH] Bug 25159: (QA follow-up) Allow passing of 'original' as hashref or Koha::Object Signed-off-by: Emmi Takkinen Signed-off-by: Martin Renvoize Signed-off-by: Katrin Fischer --- C4/Log.pm | 8 ++++++-- t/db_dependent/Log.t | 9 ++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/C4/Log.pm b/C4/Log.pm index cc3811a15b..f287e84d07 100644 --- a/C4/Log.pm +++ b/C4/Log.pm @@ -60,7 +60,7 @@ The functions in this module perform various functions in order to log all the o =item logaction - &logaction($modulename, $actionname, $objectnumber, $infos, $interface, $hashref_of_original); + &logaction($modulename, $actionname, $objectnumber, $infos, $interface, $original_as_hashref_or_object); Adds a record into action_logs table to report the different changes upon the database. Each log entry includes the number of the user currently logged in. For batch @@ -118,7 +118,11 @@ sub logaction { } my $trace = @trace ? to_json( \@trace, { utf8 => 1, pretty => 0 } ) : undef; - my $diff = ( $original && ref $updated eq 'HASH' ) ? encode_json( diff( $original, $updated, noU => 1 ) ) : undef; + my $diff = undef; + $diff = encode_json( diff( $original->unblessed, $updated, noU => 1 ) ) + if blessed($original) && $original->isa('Koha::Object'); + $diff //= encode_json( diff( $original, $updated, noU => 1 ) ) + if $original && ref $updated eq 'HASH'; Koha::ActionLog->new( { diff --git a/t/db_dependent/Log.t b/t/db_dependent/Log.t index b870421856..f1bec5108e 100755 --- a/t/db_dependent/Log.t +++ b/t/db_dependent/Log.t @@ -197,16 +197,23 @@ subtest 'Reduce log size by unblessing Koha objects' => sub { }; subtest 'Test storing diff of objects' => sub { - plan tests => 1; + plan tests => 2; my $builder = t::lib::TestBuilder->new; my $item = $builder->build_sample_item; my $original = $item->unblessed(); + my $original_item = $item->get_from_storage(); $item->barcode('_MY_TEST_BARCODE_')->store(); + logaction( 'MY_MODULE', 'TEST01', $item->itemnumber, $item, 'opac', $original ); my $logs = Koha::ActionLogs->search({ module => 'MY_MODULE', action => 'TEST01', object => $item->itemnumber })->next; my $diff = decode_json( $logs->diff ); is( $diff->{D}->{barcode}->{N}, '_MY_TEST_BARCODE_', 'Diff of changes logged successfully' ); + + logaction( 'MY_MODULE', 'TEST02', $item->itemnumber, $item, 'opac', $original_item ); + $logs = Koha::ActionLogs->search({ module => 'MY_MODULE', action => 'TEST02', object => $item->itemnumber })->next; + $diff = decode_json( $logs->diff ); + is( $diff->{D}->{barcode}->{N}, '_MY_TEST_BARCODE_', 'Diff of changes logged successfully' ); }; $schema->storage->txn_rollback; -- 2.39.5