From a756666a6e2ed51aaaf1559a9d0413945530680e Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Fri, 20 Sep 2024 12:38:42 +0000 Subject: [PATCH] Bug 23486: Add creation to TrackLasPatronActivity triggers Some libraries would like to update the last seen when a patron is created to avoid NULL values and ensure new users are marked as 'active' To test: 1 - Apply patch 2 - Create a new patron, confirm their lastseen date is null SELECT lastseen FROM borrowers WHERE cardnumber={their cardnumber} 3 - Update preference 'TrackLastPatronActivity' to include 'Patron creation' 4 - Create a second patron, confirm their lastseen is set 5 - Update preference 'TrackLastPatronActivity' to include 'Checking out an item' 6 - Checkout an item to the second patron 7 - Confirm their lastseen is not updated (We only update once per day) Signed-off-by: David Nind Signed-off-by: Martin Renvoize --- Koha/Patron.pm | 3 ++ .../en/modules/admin/preferences/patrons.pref | 1 + t/db_dependent/Koha/Patron.t | 38 +++++++++++++++---- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index bc031f8bcf9..31fd4312fb0 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -330,6 +330,9 @@ sub store { $self->add_enrolment_fee_if_needed(0); + $self->discard_changes; + $self->update_lastseen('creation'); + logaction( "MEMBERS", "CREATE", $self->borrowernumber, "" ) if C4::Context->preference("BorrowersLog"); } else { #ModMember diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref index 3a9c6ec118c..2fcb525fc3a 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref @@ -102,6 +102,7 @@ Patrons: - Select which patron activities should be tracked to signify patron activity. Each time that one of these activities occurs, borrowers.lastseen will update with the current date and time. - pref: TrackLastPatronActivityTriggers multiple: + creation: "Patron creation" login: "Logging in" connection: "Connecting to Koha via SIP or ILSDI" check_out: "Checking out an item" diff --git a/t/db_dependent/Koha/Patron.t b/t/db_dependent/Koha/Patron.t index e4ab8af65a4..209dd0a3495 100755 --- a/t/db_dependent/Koha/Patron.t +++ b/t/db_dependent/Koha/Patron.t @@ -2129,27 +2129,49 @@ subtest 'test patron_consent' => sub { subtest 'update_lastseen tests' => sub { - plan tests => 24; + plan tests => 26; $schema->storage->txn_begin; + my $cache = Koha::Caches->get_instance(); + + t::lib::Mocks::mock_preference( + 'TrackLastPatronActivityTriggers', + 'creation,login,connection,check_in,check_out,renewal,hold,article' + ); + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); my $userid = $patron->userid; - $patron->lastseen(undef); - $patron->store(); + my $patron_info = $patron->unblessed; + delete $patron_info->{borrowernumber}; + $patron->delete(); + $patron = Koha::Patron->new($patron_info)->store(); - my $cache = Koha::Caches->get_instance(); - my $cache_key = "track_activity_" . $patron->borrowernumber; - $cache->clear_from_cache($cache_key); + my $now = dt_from_string(); + my $today = $now->ymd(); + + is( + dt_from_string( $patron->lastseen )->ymd(), $today, + 'Patron should have last seen when newly created if requested' + ); + + my $cache_key = "track_activity_" . $patron->borrowernumber; + my $cache_value = $cache->get_from_cache($cache_key); + is( $cache_value, $today, "Cache was updated as expected" ); + + $patron->delete(); t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', 'login,connection,check_in,check_out,renewal,hold,article' ); - is( $patron->lastseen, undef, 'Patron should have not last seen when newly created' ); + $patron = Koha::Patron->new($patron_info)->store(); + $cache_key = "track_activity_" . $patron->borrowernumber; + + is( $patron->lastseen, undef, 'Patron should have not last seen when newly created if trigger not set' ); - my $now = dt_from_string(); + $now = dt_from_string(); Time::Fake->offset( $now->epoch ); $patron->update_lastseen('login'); -- 2.47.0