From c4d0a0df3b448b6bb8facb9eb855cb514c785655 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Tue, 13 May 2025 20:14:35 +0100 Subject: [PATCH] Bug 35451: (follow-up) Save join in C4::Serials::DelSubscription We can use the new record_table field to save ourselves a join in here too. --- C4/Serials.pm | 5 +-- t/db_dependent/Serials.t | 81 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 82 insertions(+), 4 deletions(-) diff --git a/C4/Serials.pm b/C4/Serials.pm index d9bb117db10..ab80303d97d 100644 --- a/C4/Serials.pm +++ b/C4/Serials.pm @@ -1842,10 +1842,9 @@ sub DelSubscription { Koha::AdditionalFieldValues->search( { - 'field.tablename' => 'subscription', + 'me.record_table' => 'subscription', 'me.record_id' => $subscriptionid, - }, - { join => 'field' } + } )->delete; logaction( "SERIAL", "DELETE", $subscriptionid, "" ) if C4::Context->preference("SubscriptionLog"); diff --git a/t/db_dependent/Serials.t b/t/db_dependent/Serials.t index 1e2e5bbff69..0e71deea466 100755 --- a/t/db_dependent/Serials.t +++ b/t/db_dependent/Serials.t @@ -19,7 +19,7 @@ use t::lib::Mocks; use t::lib::TestBuilder; use Test::MockModule; use Test::NoWarnings; -use Test::More tests => 64; +use Test::More tests => 65; BEGIN { use_ok( @@ -738,6 +738,85 @@ subtest "test numbering pattern with dates in GetSeq GetNextSeq" => sub { }; +subtest "DelSubscription" => sub { + plan tests => 5; + + # Create a mock for C4::Context preferences + t::lib::Mocks::mock_preference( "SubscriptionLog", 1 ); + + # Create a Subscription for testing + my $subscription_to_delete = NewSubscription( + undef, "", undef, undef, $budget_id, $biblionumber, + '2013-01-01', $frequency_id, undef, undef, undef, + undef, undef, undef, undef, undef, undef, + 1, $notes, undef, '2013-01-01', undef, $pattern_id, + undef, undef, 0, $internalnotes, 0, + undef, undef, 0, undef, '2013-12-31', 0 + ); + + # Verify subscription was created + my $subscription = GetSubscription($subscription_to_delete); + is( + $subscription->{subscriptionid}, $subscription_to_delete, + 'Subscription created successfully for deletion test' + ); + + # Create an additional field value for this subscription + my $additional_field = $builder->build_object( + { + class => 'Koha::AdditionalFields', + value => { + tablename => 'subscription', + name => 'test_field', + authorised_value_category => undef, + } + } + ); + + my $field_value = Koha::AdditionalFieldValue->new( + { + field_id => $additional_field->id, + record_id => $subscription_to_delete, + value => 'test_value', + record_table => 'subscription', + } + )->store; + + # Verify additional field value exists + my $count = Koha::AdditionalFieldValues->search( + { + 'me.record_table' => 'subscription', + 'me.record_id' => $subscription_to_delete, + } + )->count; + is( $count, 1, 'Additional field value was created for the subscription' ); + + my $action_logs_before = $schema->resultset('ActionLog')->search()->count; + + # Delete the subscription + DelSubscription($subscription_to_delete); + + # Test 1: Subscription should be deleted + $subscription = GetSubscription($subscription_to_delete); + is( $subscription, undef, 'Subscription was successfully deleted' ); + + # Test 2: Additional field values should be deleted + $count = Koha::AdditionalFieldValues->search( + { + 'me.record_table' => 'subscription', + 'me.record_id' => $subscription_to_delete, + } + )->count; + is( $count, 0, 'Additional field values were deleted with the subscription' ); + + # Test 3: Logaction should have been called with correct parameters + my $action_logs_after = $schema->resultset('ActionLog')->search()->count; + is( + $action_logs_after, $action_logs_before + 1, + 'logaction was called when SubscriptionLog preference is enabled' + ); +}; + subtest "_numeration" => sub { plan tests => 6; -- 2.49.0