From 98597eec3ca5cf91c5a55f5a3603e39f6b786c54 Mon Sep 17 00:00:00 2001 From: Ayoub Glizi-Vicioso Date: Wed, 4 Feb 2026 17:07:08 -0500 Subject: [PATCH] Bug 20956: Fix test failures --- t/db_dependent/Koha/Patron.t | 88 ++++++++++++++++++++++++++---------- 1 file changed, 65 insertions(+), 23 deletions(-) diff --git a/t/db_dependent/Koha/Patron.t b/t/db_dependent/Koha/Patron.t index 865bef3051..cc5b4a1d07 100755 --- a/t/db_dependent/Koha/Patron.t +++ b/t/db_dependent/Koha/Patron.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 46; +use Test::More tests => 47; use Test::NoWarnings; use Test::Exception; use Test::Warn; @@ -41,6 +41,7 @@ use C4::Circulation qw( AddIssue AddReturn ); use t::lib::TestBuilder; use t::lib::Mocks; +use Test::MockModule; my $schema = Koha::Database->new->schema; my $builder = t::lib::TestBuilder->new; @@ -816,14 +817,17 @@ subtest 'is_superlibrarian() tests' => sub { { class => 'Koha::Patrons', - value => { flags => 4 } + value => { flags => 16 } } ); - my %permissions = $patron->permissions; + my $permissions = $patron->permissions; + is( $permissions->{superlibrarian}, undef, 'Patron does not have superlibrarian permission' ); - is( scalar keys %permissions, 1, "Patron has one module permission" ); - is( $permissions{catalogue}, 1, "Patron has catalogue permission" ); + $patron->flags(1)->store->discard_changes; + + $permissions = $patron->permissions; + is( $permissions->{superlibrarian}, 1, 'Patron has superlibrarian permission' ); $schema->storage->txn_rollback; }; @@ -2382,10 +2386,21 @@ subtest 'alert_subscriptions tests' => sub { my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); - my $subscription1 = $builder->build_object( { class => 'Koha::Subscriptions' } ); + my $subscription1 = $builder->build_object( { + class => 'Koha::Subscriptions', + value => { + librarian => $patron->borrowernumber, + } + }); $subscription1->add_subscriber($patron); - my $subscription2 = $builder->build_object( { class => 'Koha::Subscriptions' } ); + + my $subscription2 = $builder->build_object( { + class => 'Koha::Subscriptions', + value => { + librarian => $patron->borrowernumber, + } + }); $subscription2->add_subscriber($patron); my @subscriptions = $patron->alert_subscriptions->as_list; @@ -2421,6 +2436,20 @@ subtest 'update_lastseen tests' => sub { plan tests => 26; $schema->storage->txn_begin; + my $mock_cache = Test::MockModule->new('Koha::Cache'); + my %fake_cache; + + $mock_cache->mock('set_in_cache', sub { + my ( $self, $key, $value ) = @_; + $fake_cache{$key} = $value; + return; + }); + + $mock_cache->mock('get_from_cache', sub { + my ( $self, $key ) = @_; + return $fake_cache{$key}; + }); + my $cache = Koha::Caches->get_instance(); t::lib::Mocks::mock_preference( @@ -2429,12 +2458,12 @@ subtest 'update_lastseen tests' => sub { ); my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); - my $userid = $patron->userid; - $patron->lastseen(undef); my $patron_info = $patron->unblessed; delete $patron_info->{borrowernumber}; $patron->delete(); $patron = Koha::Patron->new($patron_info)->store(); + $patron->update_lastseen({ activity => 'creation' }); + $patron->discard_changes(); my $now = dt_from_string(); my $today = $now->ymd(); @@ -2444,21 +2473,32 @@ subtest 'update_lastseen tests' => sub { '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" ); + my $cache_key = "track_activity_" . $patron->borrowernumber; + is( $cache->get_from_cache($cache_key), $today, "Cache was updated as expected" ); $patron->delete(); t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', - 'login,connection,check_in,check_out,renewal,hold,article' + '' + ); + my $patron_no_trigger = $builder->build_object( { class => 'Koha::Patrons' } ); + + $patron_no_trigger->lastseen(undef)->store(); + + $patron_no_trigger->update_lastseen({ activity => 'login' }); + $patron_no_trigger->discard_changes(); + + is( $patron_no_trigger->lastseen, undef, + 'Patron should have not last seen when newly created if trigger not set' ); - $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' ); + t::lib::Mocks::mock_preference( + 'TrackLastPatronActivityTriggers', + 'login,connection,check_in,check_out,renewal,hold,article' + ); + $now = dt_from_string(); Time::Fake->offset( $now->epoch ); @@ -2553,12 +2593,12 @@ subtest 'update_lastseen tests' => sub { 'login,connection,check_in,check_out,renewal,hold,article' ); - $cache->clear_from_cache($cache_key); + %fake_cache = (); $patron->update_lastseen('login'); $patron->_result()->discard_changes(); isnt( $patron->lastseen, $last_seen, 'Patron last seen should be changed after a login if we cleared the cache' ); - $cache->clear_from_cache($cache_key); + %fake_cache = (); $patron->update_lastseen('connection'); $patron->_result()->discard_changes(); isnt( @@ -2566,7 +2606,7 @@ subtest 'update_lastseen tests' => sub { 'Patron last seen should be changed after a connection if we cleared the cache' ); - $cache->clear_from_cache($cache_key); + %fake_cache = (); $patron->update_lastseen('check_out'); $patron->_result()->discard_changes(); isnt( @@ -2574,7 +2614,7 @@ subtest 'update_lastseen tests' => sub { 'Patron last seen should be changed after a check_out if we cleared the cache' ); - $cache->clear_from_cache($cache_key); + %fake_cache = (); $patron->update_lastseen('check_in'); $patron->_result()->discard_changes(); isnt( @@ -2582,13 +2622,15 @@ subtest 'update_lastseen tests' => sub { 'Patron last seen should be changed after a check_in if we cleared the cache' ); - $cache->clear_from_cache($cache_key); + %fake_cache = (); $patron->update_lastseen('hold'); $patron->_result()->discard_changes(); isnt( $patron->lastseen, $last_seen, 'Patron last seen should be changed after a hold if we cleared the cache' ); + + %fake_cache = (); $patron->update_lastseen('article'); $patron->_result()->discard_changes(); isnt( @@ -2596,7 +2638,7 @@ subtest 'update_lastseen tests' => sub { 'Patron last seen should be changed after a article if we cleared the cache' ); - $cache->clear_from_cache($cache_key); + %fake_cache = (); $patron->update_lastseen('renewal'); $patron->_result()->discard_changes(); isnt( $patron->lastseen, $last_seen, 'Patron last seen should be changed after a renewal if we cleared the cache' ); @@ -2604,7 +2646,7 @@ subtest 'update_lastseen tests' => sub { # Check that the preference takes effect t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', '' ); $patron->lastseen(undef)->store; - $cache->clear_from_cache($cache_key); + %fake_cache = (); $patron->update_lastseen('login'); $patron->_result()->discard_changes(); is( -- 2.52.0