@@ -, +, @@ --- Koha/Patron.pm | 11 ++++++++--- t/db_dependent/Members.t | 20 ++++++++++++++++++-- 2 files changed, 26 insertions(+), 5 deletions(-) --- a/Koha/Patron.pm +++ a/Koha/Patron.pm @@ -400,9 +400,14 @@ sub has_overdues { sub track_login { my ( $self, $params ) = @_; - return if - !$params->{force} && - !C4::Context->preference('TrackLastPatronActivity'); + + warn "track_login()"; + return unless C4::Context->preference('TrackLastPatronActivity') || $params->{force}; + warn "Pref set OR forced"; + return if C4::Context->userenv->{'tracked_for_session'} && !$params->{force}; + warn "Untracked OR forced"; + + C4::Context->userenv->{'tracked_for_session'} = 1; $self->lastseen( dt_from_string() )->store; } --- a/t/db_dependent/Members.t +++ a/t/db_dependent/Members.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 65; +use Test::More tests => 67; use Test::MockModule; use Data::Dumper qw/Dumper/; use C4::Context; @@ -356,12 +356,28 @@ $patstodel = GetBorrowersToExpunge( { last_seen => '2016-02-15' }); is( scalar @$patstodel, 2, 'TrackLastPatronActivity - 2 patrons must be deleted' ); $patstodel = GetBorrowersToExpunge( { last_seen => '2016-04-04' }); is( scalar @$patstodel, 3, 'TrackLastPatronActivity - 3 patrons must be deleted' ); + + +# Test method last_seen / TrackLastPatronActivity my $patron2 = $builder->build({ source => 'Borrower', value => { lastseen => undef } }); t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '0' ); Koha::Patrons->find( $patron2->{borrowernumber} )->track_login; is( Koha::Patrons->find( $patron2->{borrowernumber} )->lastseen, undef, 'Lastseen should not be changed' ); + Koha::Patrons->find( $patron2->{borrowernumber} )->track_login({ force => 1 }); -isnt( Koha::Patrons->find( $patron2->{borrowernumber} )->lastseen, undef, 'Lastseen should be changed now' ); +my $last_seen = Koha::Patrons->find( $patron2->{borrowernumber} )->lastseen; +isnt( $last_seen, undef, 'Lastseen should be changed now' ); + +# Last seen shouldn't be updated a second time for this session +t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '1' ); +Koha::Patrons->find( $patron2->{borrowernumber} )->track_login(); +is( Koha::Patrons->find( $patron2->{borrowernumber} )->lastseen, $last_seen, 'Lastseen should not be changed' ); + +# If it's forced, it should still be updated +sleep(1); # We need to wait a tiny bit to make sure the timestamp will be different +Koha::Patrons->find( $patron2->{borrowernumber} )->track_login({ force => 1 }); +isnt( Koha::Patrons->find( $patron2->{borrowernumber} )->lastseen, $last_seen, 'Lastseen should be changed if forced' ); + # Regression tests for BZ13502 ## Remove all entries with userid='' (should be only 1 max) --