From e5eaa2aa175b8c5c9ed48e87a574d67af22ce2e1 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Fri, 9 Aug 2024 15:40:43 +0100 Subject: [PATCH] Bug 37256: Ensure empty and null work as expected in PUT Sponsored-by: Glasgow Colleges Library Group Signed-off-by: George Harkins Signed-off-by: Kyle M Hall Signed-off-by: Katrin Fischer --- Koha/CirculationRules.pm | 40 +++-------------------- t/db_dependent/api/v1/circulation_rules.t | 18 +++++++++- 2 files changed, 22 insertions(+), 36 deletions(-) diff --git a/Koha/CirculationRules.pm b/Koha/CirculationRules.pm index 7eda2d3c90..56ff6ba970 100644 --- a/Koha/CirculationRules.pm +++ b/Koha/CirculationRules.pm @@ -96,10 +96,6 @@ our $RULE_KINDS = { cap_fine_to_replacement_price => { scope => [ 'branchcode', 'categorycode', 'itemtype' ], }, - expire_reserves_charge => { - scope => [ 'branchcode', 'categorycode', 'itemtype' ], - can_be_blank => 0, - }, chargeperiod => { scope => [ 'branchcode', 'categorycode', 'itemtype' ], }, @@ -416,6 +412,7 @@ sub set_rule { $rule->rule_value($rule_value); $rule->update(); } else { + $rule->rule_value(undef); $rule->delete(); } } else { @@ -438,6 +435,10 @@ sub set_rule { $memory_cache->clear_from_cache($k) if $k =~ m{^CircRules:}; } + $rule = Koha::CirculationRule->new( + { rule_name => $rule_name, branchcode => $branchcode, categorycode => $categorycode, itemtype => $itemtype } ) + unless $rule; + return $rule; } @@ -753,37 +754,6 @@ sub get_effective_daysmode { } -=head3 get_effective_expire_reserves_charge - -Return the value for expire_reserves_charge defined in the circulation rules. -If not defined (or empty string), the value of the system preference ExpireReservesMaxPickUpDelayCharge is returned - -=cut - -sub get_effective_expire_reserves_charge { - my ( $class, $params ) = @_; - - my $itemtype = $params->{itemtype}; - my $branchcode = $params->{branchcode}; - my $categorycode = $params->{categorycode}; - - my $expire_reserves_charge_rule = $class->get_effective_rule( - { - itemtype => $itemtype, - branchcode => $branchcode, - categorycode => $categorycode, - rule_name => 'expire_reserves_charge', - } - ); - - # return the rule value (incl. 0) if rule found so there's an object in the variable, - # or return default value from sysprefs when rule wasn't found and there's undef - return $expire_reserves_charge_rule - ? $expire_reserves_charge_rule->rule_value - : C4::Context->preference("ExpireReservesMaxPickUpDelayCharge"); - -} - =head3 type =cut diff --git a/t/db_dependent/api/v1/circulation_rules.t b/t/db_dependent/api/v1/circulation_rules.t index d83e7af48e..4b86748282 100755 --- a/t/db_dependent/api/v1/circulation_rules.t +++ b/t/db_dependent/api/v1/circulation_rules.t @@ -290,7 +290,7 @@ subtest 'list_rules() tests' => sub { }; subtest 'set_rules() tests' => sub { - plan tests => 23; + plan tests => 28; $schema->storage->txn_begin; @@ -388,5 +388,21 @@ subtest 'set_rules() tests' => sub { is( $json->{fine}, 10, "Fine rule set correctly for wildcard context" ); is( $json->{finedays}, 7, "Finedays rule set correctly for wildcard context" ); + # Setting rules empty and undefined + note("Setting rules to empty and undefined"); + $rules_to_set->{fine} = ''; + $rules_to_set->{finedays} = undef; + $t->put_ok( "//$userid:$password@/api/v1/circulation_rules" => json => $rules_to_set )->status_is(200); + + # Verify the rules were updated + $json = $t->tx->res->json; + is( $json->{fine}, '', "Fine rule updated correctly" ); + is( $json->{finedays}, undef, "Finedays rule remains the same" ); + + # Verify that the explicit undef results in a rule deletion + my $rules = Koha::CirculationRules->search( + { categorycode => undef, branchcode => undef, itemtype => undef, rule_name => 'finedays' } ); + is( $rules->count, 0, "Finedays rule deleted from database" ); + $schema->storage->txn_rollback; };