From 3ef756f5a0e9adb6ce9d71592bd82a88846c5e83 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Wed, 5 Aug 2026 09:05:17 +0100 Subject: [PATCH] Bug 42310: (QA follow-up) Refresh in-process closure state on add/delete_weekly_closure and add/delete_repeating_closure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit add_weekly_closure/delete_weekly_closure and add_repeating_closure/ delete_repeating_closure returned $self without refreshing $self->{weekly_closed_days}/{day_month_closed_days}, so a calendar object would keep reporting the pre-change state to is_holiday (and everything built on it - days_forward, next_open_days, etc.) until it was re-instantiated. This is distinct from the _holidays Koha::Caches entry, which single/exception closures already flush correctly on store/delete. Call _init to rebuild both structures from the database after each add/delete, so the same object reflects the change immediately. Test plan: 1) prove t/db_dependent/Koha/Library/Calendar.t t/db_dependent/Calendar.t t/db_dependent/Holidays.t t/db_dependent/HoldsQueue.t t/db_dependent/Holds/WaitingReserves.t t/db_dependent/Koha/CurbsidePickups.t t/db_dependent/Reserves/CancelExpiredReserves.t t/db_dependent/api/v1/libraries.t Signed-off-by: Tomás Cohen Arazi Signed-off-by: Pedro Amorim --- Koha/Library/Calendar.pm | 27 ++++++++++++++------ t/db_dependent/Koha/Library/Calendar.t | 34 +++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/Koha/Library/Calendar.pm b/Koha/Library/Calendar.pm index 5971c78387..98ce63a383 100644 --- a/Koha/Library/Calendar.pm +++ b/Koha/Library/Calendar.pm @@ -426,12 +426,18 @@ sub clear_weekly_closed_days { sub add_weekly_closure { my ( $self, $params ) = @_; - return Koha::Library::Calendar::WeeklyClosure->new( { library_id => $self->{branchcode}, %$params } )->store; + my $closure = + Koha::Library::Calendar::WeeklyClosure->new( { library_id => $self->{branchcode}, %$params } )->store; + $self->_init; + return $closure; } sub add_repeating_closure { my ( $self, $params ) = @_; - return Koha::Library::Calendar::RepeatingClosure->new( { library_id => $self->{branchcode}, %$params } )->store; + my $closure = + Koha::Library::Calendar::RepeatingClosure->new( { library_id => $self->{branchcode}, %$params } )->store; + $self->_init; + return $closure; } sub add_single_closure { @@ -450,7 +456,10 @@ sub delete_weekly_closure { { library_id => $self->{branchcode}, weekday => $params->{weekday} }, { rows => 1 } )->single; - $row->delete if $row; + if ($row) { + $row->delete; + $self->_init; + } return $self; } @@ -460,7 +469,10 @@ sub delete_repeating_closure { { library_id => $self->{branchcode}, day => $params->{day}, month => $params->{month} }, { rows => 1 } )->single; - $row->delete if $row; + if ($row) { + $row->delete; + $self->_init; + } return $self; } @@ -566,11 +578,12 @@ sub copy_to { } my $exceptions = - Koha::Library::Calendar::Exceptions->search( { library_id => $self->{branchcode}, date => { '>=' => $today } } ); + Koha::Library::Calendar::Exceptions->search( + { library_id => $self->{branchcode}, date => { '>=' => $today } } ); while ( my $row = $exceptions->next ) { next - if Koha::Library::Calendar::Exceptions->search( { library_id => $target_branchcode, date => $row->date } ) - ->count; + if Koha::Library::Calendar::Exceptions->search( + { library_id => $target_branchcode, date => $row->date } )->count; Koha::Library::Calendar::Exception->new( { library_id => $target_branchcode, date => $row->date, diff --git a/t/db_dependent/Koha/Library/Calendar.t b/t/db_dependent/Koha/Library/Calendar.t index e7b7cfccff..56808e04e7 100755 --- a/t/db_dependent/Koha/Library/Calendar.t +++ b/t/db_dependent/Koha/Library/Calendar.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 8; +use Test::More tests => 9; use Test::Exception; use Test::NoWarnings; @@ -371,3 +371,35 @@ subtest 'SingleClosure/Exception store() rejects an in-place date change' => sub $schema->storage->txn_rollback; }; + +subtest 'delete_weekly_closure/delete_repeating_closure refresh in-process state' => sub { + + plan tests => 4; + + $schema->storage->txn_begin; + + my $library = $builder->build_object( { class => 'Koha::Libraries' } ); + my $calendar = Koha::Library::Calendar->new( branchcode => $library->branchcode ); + + my $sunday = dt_from_string('2027-08-08'); # a Sunday + $calendar->add_weekly_closure( { weekday => 0, title => '', description => '' } ); + is( $calendar->is_holiday($sunday), 1, 'Sunday is closed after add_weekly_closure' ); + + $calendar->delete_weekly_closure( { weekday => 0 } ); + is( + $calendar->is_holiday($sunday), 0, + 'Sunday is open on the SAME object immediately after delete_weekly_closure, without re-instantiating' + ); + + my $christmas = dt_from_string('2027-12-25'); + $calendar->add_repeating_closure( { day => 25, month => 12, title => '', description => '' } ); + is( $calendar->is_holiday($christmas), 1, 'Christmas is closed after add_repeating_closure' ); + + $calendar->delete_repeating_closure( { day => 25, month => 12 } ); + is( + $calendar->is_holiday($christmas), 0, + 'Christmas is open on the SAME object immediately after delete_repeating_closure, without re-instantiating' + ); + + $schema->storage->txn_rollback; +};