From 36e97b40231883057c0a5fbb0d1c97dac96b2c21 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 --- Koha/CirculationRules.pm | 5 +++++ t/db_dependent/api/v1/circulation_rules.t | 18 +++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/Koha/CirculationRules.pm b/Koha/CirculationRules.pm index d558c1e7810..c45d2ccc5aa 100644 --- a/Koha/CirculationRules.pm +++ b/Koha/CirculationRules.pm @@ -416,6 +416,7 @@ sub set_rule { $rule->update(); } else { + $rule->rule_value(undef); $rule->delete(); } } @@ -439,6 +440,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; } diff --git a/t/db_dependent/api/v1/circulation_rules.t b/t/db_dependent/api/v1/circulation_rules.t index d83e7af48ef..4b867482822 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; }; -- 2.46.0