From 882aa3eb8af48699ebd1a026900a51afb3e8cec8 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Fri, 14 Apr 2023 13:42:03 +0000 Subject: [PATCH] Bug 33236: (follow-up) Handle new suggestion in ReNewSubscription Signed-off-by: Katrin Fischer Signed-off-by: Tomas Cohen Arazi --- C4/Serials.pm | 9 +++--- Koha/Suggestion.pm | 2 +- t/db_dependent/Serials/ReNewSubscription.t | 34 +++++++++++++++++----- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/C4/Serials.pm b/C4/Serials.pm index e67881035d..37f1c169a6 100644 --- a/C4/Serials.pm +++ b/C4/Serials.pm @@ -41,6 +41,7 @@ use Koha::Biblios; use Koha::Serial; use Koha::Subscriptions; use Koha::Subscription::Histories; +use Koha::Suggestions; use Koha::SharedContent; use Scalar::Util qw( looks_like_number ); @@ -1528,9 +1529,9 @@ sub ReNewSubscription { my $biblio = $sth->fetchrow_hashref; if ( C4::Context->preference("RenewSerialAddsSuggestion") ) { - require C4::Suggestions; - C4::Suggestions::NewSuggestion( - { 'suggestedby' => $user, + Koha::Suggestion->new( + { + 'suggestedby' => $user, 'title' => $subscription->{bibliotitle}, 'author' => $biblio->{author}, 'publishercode' => $biblio->{publishercode}, @@ -1538,7 +1539,7 @@ sub ReNewSubscription { 'biblionumber' => $subscription->{biblionumber}, 'branchcode' => $branchcode, } - ); + )->store; } $numberlength ||= 0; # Should not we raise an exception instead? diff --git a/Koha/Suggestion.pm b/Koha/Suggestion.pm index 3b88c8aefb..85f7861f96 100644 --- a/Koha/Suggestion.pm +++ b/Koha/Suggestion.pm @@ -48,7 +48,7 @@ sub store { $self->STATUS("ASKED") unless $self->STATUS; - $self->branchcode(undef) if $self->branchcode eq ''; + $self->branchcode(undef) if defined $self->branchcode && $self->branchcode eq ''; unless ( $self->suggesteddate() ) { $self->suggesteddate( dt_from_string()->ymd ); } diff --git a/t/db_dependent/Serials/ReNewSubscription.t b/t/db_dependent/Serials/ReNewSubscription.t index 6abe8364c8..4635da543b 100755 --- a/t/db_dependent/Serials/ReNewSubscription.t +++ b/t/db_dependent/Serials/ReNewSubscription.t @@ -22,7 +22,7 @@ use Modern::Perl; -use Test::More tests => 7; +use Test::More tests => 10; use Test::MockModule; use t::lib::TestBuilder; use t::lib::Mocks; @@ -85,8 +85,26 @@ my $subscriptionhistory = $builder->build({ } }); +t::lib::Mocks::mock_preference( 'RenewSerialAddsSuggestion', '0' ); +my $suggestions_count = Koha::Suggestions->search()->count; + # Actual testing starts here! -# Renew the subscription and check that enddate has not been set +# Renew the subscription and check that enddate has been set +ReNewSubscription( + { + subscriptionid => $subscription->{subscriptionid}, + startdate => "2016-01-01", + monthlength => 12 + } +); + +$subscription = Koha::Subscriptions->find( $subscription->{subscriptionid} ); +is( $subscription->enddate, '2017-01-01', "We don't update the subscription end date when renewing with a month length"); + +is( $suggestions_count, Koha::Suggestions->search()->count, "Suggestion not added when RenewSerialAddsSuggestion set to Don't add"); + +t::lib::Mocks::mock_preference( 'RenewSerialAddsSuggestion', '1' ); + ReNewSubscription( { subscriptionid => $subscription->{subscriptionid}, @@ -95,7 +113,13 @@ ReNewSubscription( } ); +is( $suggestions_count + 1, Koha::Suggestions->search()->count, "Suggestion added when RenewSerialAddsSuggestion set to add"); + +my $history = Koha::Subscription::Histories->find($subscription->subscriptionid); + +is ( $history->histenddate(), undef, 'subscription history not empty after renewal'); # Calculate the subscription length for the renewal for issues, days and months + my ($numberlength, $weeklength, $monthlength) = GetSubscriptionLength('issues', 7); is ( $numberlength, 7, "Subscription length is 7 issues"); @@ -117,12 +141,6 @@ is ($monthlength, undef, "Subscription length is undef issues, invalid issue dat ($numberlength, $weeklength, $monthlength) = GetSubscriptionLength('weeks', '!'); is ($weeklength, undef, "Subscription length is undef weeks, invalid weeks data was not stored"); -# Renew the subscription and check that enddate has not been set - -my $history = Koha::Subscription::Histories->find($subscription->{subscriptionid}); - -is ( $history->histenddate(), undef, 'subscription history not empty after renewal'); - # End of tests $schema->storage->txn_rollback; -- 2.39.5