View | Details | Raw Unified | Return to bug 15504
Collapse All | Expand All

(-)a/t/db_dependent/Auth.t (-102 / +2 lines)
Lines 7-13 use CGI qw ( -utf8 ); Link Here
7
use Test::MockObject;
7
use Test::MockObject;
8
use Test::MockModule;
8
use Test::MockModule;
9
use List::MoreUtils qw/all any none/;
9
use List::MoreUtils qw/all any none/;
10
use Test::More tests => 19;
10
use Test::More tests => 18;
11
use Test::Warn;
11
use Test::Warn;
12
use t::lib::Mocks;
12
use t::lib::Mocks;
13
use t::lib::TestBuilder;
13
use t::lib::TestBuilder;
Lines 22-28 use Koha::Auth::TwoFactorAuth; Link Here
22
BEGIN {
22
BEGIN {
23
    use_ok(
23
    use_ok(
24
        'C4::Auth',
24
        'C4::Auth',
25
        qw( checkauth haspermission track_login_daily checkpw get_template_and_user checkpw_hash get_cataloguing_page_permissions )
25
        qw( checkauth haspermission checkpw get_template_and_user checkpw_hash get_cataloguing_page_permissions )
26
    );
26
    );
27
}
27
}
28
28
Lines 415-520 subtest 'checkauth() tests' => sub { Link Here
415
    C4::Context->_new_userenv; # For next tests
415
    C4::Context->_new_userenv; # For next tests
416
};
416
};
417
417
418
subtest 'track_login_daily tests' => sub {
419
420
    plan tests => 18;
421
422
    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
423
    my $userid = $patron->userid;
424
425
    $patron->lastseen( undef );
426
    $patron->store();
427
428
    my $cache     = Koha::Caches->get_instance();
429
    my $cache_key = "track_login_" . $patron->userid;
430
    $cache->clear_from_cache($cache_key);
431
432
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '1' );
433
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', 'login,connection,check_in,check_out,renewal' );
434
435
    is( $patron->lastseen, undef, 'Patron should have not last seen when newly created' );
436
437
    C4::Auth::track_login_daily( $userid, 'login' );
438
    $patron->_result()->discard_changes();
439
    isnt( $patron->lastseen, undef, 'Patron should have last seen set when TrackLastPatronActivity = 1' );
440
441
    sleep(1); # We need to wait a tiny bit to make sure the timestamp will be different
442
    my $last_seen = $patron->lastseen;
443
    C4::Auth::track_login_daily( $userid, 'login' );
444
    $patron->_result()->discard_changes();
445
    is( $patron->lastseen, $last_seen, 'Patron last seen should still be unchanged after a login' );
446
    C4::Auth::track_login_daily( $userid, 'connection' );
447
    $patron->_result()->discard_changes();
448
    is( $patron->lastseen, $last_seen, 'Patron last seen should still be unchanged after a SIP/ILSDI connection' );
449
    C4::Auth::track_login_daily( $userid, 'check_out' );
450
    $patron->_result()->discard_changes();
451
    is( $patron->lastseen, $last_seen, 'Patron last seen should still be unchanged after a check out' );
452
    C4::Auth::track_login_daily( $userid, 'check_in' );
453
    $patron->_result()->discard_changes();
454
    is( $patron->lastseen, $last_seen, 'Patron last seen should still be unchanged after a check in' );
455
    C4::Auth::track_login_daily( $userid, 'renewal' );
456
    $patron->_result()->discard_changes();
457
    is( $patron->lastseen, $last_seen, 'Patron last seen should still be unchanged after a renewal' );
458
459
    # Check that removing options stops tracking changes
460
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', 'connection,check_in,check_out,renewal' );
461
    $cache->clear_from_cache($cache_key);
462
    C4::Auth::track_login_daily( $userid, 'login' );
463
    $patron->_result()->discard_changes();
464
    is( $patron->lastseen, $last_seen, 'Patron last seen should be unchanged after a login if login is not selected as an option and the cache has been cleared' );
465
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', 'check_in,check_out,renewal' );
466
    $cache->clear_from_cache($cache_key);
467
    C4::Auth::track_login_daily( $userid, 'connection' );
468
    $patron->_result()->discard_changes();
469
    is( $patron->lastseen, $last_seen, 'Patron last seen should be unchanged after a connection if connection is not selected as an option and the cache has been cleared' );
470
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', 'check_out,renewal' );
471
    $cache->clear_from_cache($cache_key);
472
    C4::Auth::track_login_daily( $userid, 'check_in' );
473
    $patron->_result()->discard_changes();
474
    is( $patron->lastseen, $last_seen, 'Patron last seen should be unchanged after a check_in if check_in is not selected as an option and the cache has been cleared' );
475
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', 'renewal' );
476
    $cache->clear_from_cache($cache_key);
477
    C4::Auth::track_login_daily( $userid, 'check_out' );
478
    $patron->_result()->discard_changes();
479
    is( $patron->lastseen, $last_seen, 'Patron last seen should be unchanged after a check_out if check_out is not selected as an option and the cache has been cleared' );
480
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', '' );
481
    $cache->clear_from_cache($cache_key);
482
    C4::Auth::track_login_daily( $userid, 'renewal' );
483
    $patron->_result()->discard_changes();
484
    is( $patron->lastseen, $last_seen, 'Patron last seen should be unchanged after a renewal if renewal is not selected as an option and the cache has been cleared' );
485
486
    # Restore all options to test changes
487
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', 'login,connection,check_in,check_out,renewal' );
488
489
    $cache->clear_from_cache($cache_key);
490
    C4::Auth::track_login_daily( $userid, 'login' );
491
    $patron->_result()->discard_changes();
492
    isnt( $patron->lastseen, $last_seen, 'Patron last seen should be changed after a login if we cleared the cache' );
493
    $cache->clear_from_cache($cache_key);
494
    C4::Auth::track_login_daily( $userid, 'connection' );
495
    $patron->_result()->discard_changes();
496
    isnt( $patron->lastseen, $last_seen, 'Patron last seen should be changed after a connection if we cleared the cache' );
497
    $cache->clear_from_cache($cache_key);
498
    C4::Auth::track_login_daily( $userid, 'check_out' );
499
    $patron->_result()->discard_changes();
500
    isnt( $patron->lastseen, $last_seen, 'Patron last seen should be changed after a check_out if we cleared the cache' );
501
    $cache->clear_from_cache($cache_key);
502
    C4::Auth::track_login_daily( $userid, 'check_in' );
503
    $patron->_result()->discard_changes();
504
    isnt( $patron->lastseen, $last_seen, 'Patron last seen should be changed after a check_in if we cleared the cache' );
505
    $cache->clear_from_cache($cache_key);
506
    C4::Auth::track_login_daily( $userid, 'renewal' );
507
    $patron->_result()->discard_changes();
508
    isnt( $patron->lastseen, $last_seen, 'Patron last seen should be changed after a renewal if we cleared the cache' );
509
510
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '0' );
511
    $patron->lastseen( undef )->store;
512
    $cache->clear_from_cache($cache_key);
513
    C4::Auth::track_login_daily( $userid, 'login' );
514
    $patron->_result()->discard_changes();
515
    is( $patron->lastseen, undef, 'Patron should still have last seen unchanged when TrackLastPatronActivity = 0' );
516
};
517
518
subtest 'no_set_userenv parameter tests' => sub {
418
subtest 'no_set_userenv parameter tests' => sub {
519
419
520
    plan tests => 7;
420
    plan tests => 7;
(-)a/t/db_dependent/Koha/Patron.t (-1 / +112 lines)
Lines 19-27 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 28;
22
use Test::More tests => 29;
23
use Test::Exception;
23
use Test::Exception;
24
use Test::Warn;
24
use Test::Warn;
25
use Time::Fake;
25
26
26
use Koha::CirculationRules;
27
use Koha::CirculationRules;
27
use Koha::Database;
28
use Koha::Database;
Lines 1950-1952 subtest 'alert_subscriptions tests' => sub { Link Here
1950
1951
1951
    $patron->discard_changes;
1952
    $patron->discard_changes;
1952
};
1953
};
1954
1955
subtest 'update_lastseen tests' => sub {
1956
1957
    plan tests => 18;
1958
    $schema->storage->txn_begin;
1959
1960
    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
1961
    my $userid = $patron->userid;
1962
1963
    $patron->lastseen( undef );
1964
    $patron->store();
1965
1966
    my $cache     = Koha::Caches->get_instance();
1967
    my $cache_key = "track_login_" . $patron->userid;
1968
    $cache->clear_from_cache($cache_key);
1969
1970
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '1' );
1971
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', 'login,connection,check_in,check_out,renewal' );
1972
1973
    is( $patron->lastseen, undef, 'Patron should have not last seen when newly created' );
1974
1975
    my $now = dt_from_string();
1976
    Time::Fake->offset( $now->epoch );
1977
1978
    $patron->update_lastseen( 'login' );
1979
    $patron->_result()->discard_changes();
1980
    isnt( $patron->lastseen, undef, 'Patron should have last seen set when TrackLastPatronActivity = 1' );
1981
    my $last_seen = $patron->lastseen;
1982
1983
    Time::Fake->offset( $now->epoch + 5 );
1984
    # Test that lastseen isn't updated more than once in a day (regardless of activity passed)
1985
    $patron->update_lastseen( 'login' );
1986
    $patron->_result()->discard_changes();
1987
    is( $patron->lastseen, $last_seen, 'Patron last seen should still be unchanged after a login' );
1988
    $patron->update_lastseen( 'connection' );
1989
    $patron->_result()->discard_changes();
1990
    is( $patron->lastseen, $last_seen, 'Patron last seen should still be unchanged after a SIP/ILSDI connection' );
1991
    $patron->update_lastseen( 'check_out' );
1992
    $patron->_result()->discard_changes();
1993
    is( $patron->lastseen, $last_seen, 'Patron last seen should still be unchanged after a check out' );
1994
    $patron->update_lastseen( 'check_in' );
1995
    $patron->_result()->discard_changes();
1996
    is( $patron->lastseen, $last_seen, 'Patron last seen should still be unchanged after a check in' );
1997
    $patron->update_lastseen( 'renewal' );
1998
    $patron->_result()->discard_changes();
1999
    is( $patron->lastseen, $last_seen, 'Patron last seen should still be unchanged after a renewal' );
2000
2001
    # Check that tracking is disabled when the activity isn't listed
2002
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', '' );
2003
    $cache->clear_from_cache($cache_key); # Rule out the more than once day prevention above
2004
2005
    $patron->update_lastseen( 'login' );
2006
    $patron->_result()->discard_changes();
2007
    is( $patron->lastseen, $last_seen, 'Patron last seen should be unchanged after a login if login is not selected as an option and the cache has been cleared' );
2008
2009
    $patron->update_lastseen( 'connection' );
2010
    $patron->_result()->discard_changes();
2011
    is( $patron->lastseen, $last_seen, 'Patron last seen should be unchanged after a connection if connection is not selected as an option and the cache has been cleared' );
2012
2013
    $patron->update_lastseen( 'check_in' );
2014
    $patron->_result()->discard_changes();
2015
    is( $patron->lastseen, $last_seen, 'Patron last seen should be unchanged after a check_in if check_in is not selected as an option and the cache has been cleared' );
2016
2017
    $patron->update_lastseen( 'check_out' );
2018
    $patron->_result()->discard_changes();
2019
    is( $patron->lastseen, $last_seen, 'Patron last seen should be unchanged after a check_out if check_out is not selected as an option and the cache has been cleared' );
2020
2021
    $patron->update_lastseen( 'renewal' );
2022
    $patron->_result()->discard_changes();
2023
    is( $patron->lastseen, $last_seen, 'Patron last seen should be unchanged after a renewal if renewal is not selected as an option and the cache has been cleared' );
2024
2025
    # Check tracking for each activity
2026
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', 'login,connection,check_in,check_out,renewal' );
2027
2028
    $cache->clear_from_cache($cache_key);
2029
    $patron->update_lastseen( 'login' );
2030
    $patron->_result()->discard_changes();
2031
    isnt( $patron->lastseen, $last_seen, 'Patron last seen should be changed after a login if we cleared the cache' );
2032
2033
    $cache->clear_from_cache($cache_key);
2034
    $patron->update_lastseen( 'connection' );
2035
    $patron->_result()->discard_changes();
2036
    isnt( $patron->lastseen, $last_seen, 'Patron last seen should be changed after a connection if we cleared the cache' );
2037
2038
    $cache->clear_from_cache($cache_key);
2039
    $patron->update_lastseen( 'check_out' );
2040
    $patron->_result()->discard_changes();
2041
    isnt( $patron->lastseen, $last_seen, 'Patron last seen should be changed after a check_out if we cleared the cache' );
2042
2043
    $cache->clear_from_cache($cache_key);
2044
    $patron->update_lastseen( 'check_in' );
2045
    $patron->_result()->discard_changes();
2046
    isnt( $patron->lastseen, $last_seen, 'Patron last seen should be changed after a check_in if we cleared the cache' );
2047
2048
    $cache->clear_from_cache($cache_key);
2049
    $patron->update_lastseen( 'renewal' );
2050
    $patron->_result()->discard_changes();
2051
    isnt( $patron->lastseen, $last_seen, 'Patron last seen should be changed after a renewal if we cleared the cache' );
2052
2053
    # Check that the preference takes effect
2054
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '0' );
2055
    $patron->lastseen( undef )->store;
2056
    $cache->clear_from_cache($cache_key);
2057
    $patron->update_lastseen( 'login' );
2058
    $patron->_result()->discard_changes();
2059
    is( $patron->lastseen, undef, 'Patron should still have last seen unchanged when TrackLastPatronActivity = 0' );
2060
2061
    Time::Fake->reset;
2062
    $schema->storage->txn_rollback;
2063
};
(-)a/t/db_dependent/Koha/Patrons.t (-2 / +2 lines)
Lines 1618-1629 subtest 'BorrowersLog tests' => sub { Link Here
1618
    is( $log_info->{cardnumber}->{before}, $cardnumber, 'Got correct old cardnumber' );
1618
    is( $log_info->{cardnumber}->{before}, $cardnumber, 'Got correct old cardnumber' );
1619
    is( scalar @logs, 1, 'With BorrowerLogs, one detailed MODIFY action should be logged for the modification.' );
1619
    is( scalar @logs, 1, 'With BorrowerLogs, one detailed MODIFY action should be logged for the modification.' );
1620
1620
1621
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', 'connection' );
1621
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', 1 );
1622
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', 1 );
1622
    $patron->track_login();
1623
    $patron->update_lastseen('connection');
1623
    @logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'MODIFY', object => $patron->borrowernumber } );
1624
    @logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'MODIFY', object => $patron->borrowernumber } );
1624
    is( scalar @logs, 1, 'With BorrowerLogs and TrackLastPatronActivity we should not spam the logs');
1625
    is( scalar @logs, 1, 'With BorrowerLogs and TrackLastPatronActivity we should not spam the logs');
1625
};
1626
};
1626
1627
$schema->storage->txn_rollback;
1627
$schema->storage->txn_rollback;
1628
1628
1629
subtest 'Test Koha::Patrons::merge' => sub {
1629
subtest 'Test Koha::Patrons::merge' => sub {
(-)a/t/db_dependent/Members.t (-3 / +4 lines)
Lines 324-333 my $patron2 = $builder->build({ Link Here
324
        flags => undef,
324
        flags => undef,
325
    }
325
    }
326
});
326
});
327
t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', 'connection' );
327
t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '0' );
328
t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '0' );
328
Koha::Patrons->find( $patron2->{borrowernumber} )->track_login;
329
Koha::Patrons->find( $patron2->{borrowernumber} )->update_lastseen('connection');
329
is( Koha::Patrons->find( $patron2->{borrowernumber} )->lastseen, undef, 'Lastseen should not be changed' );
330
is( Koha::Patrons->find( $patron2->{borrowernumber} )->lastseen, undef, 'Lastseen should not be changed' );
330
Koha::Patrons->find( $patron2->{borrowernumber} )->track_login({ force => 1 });
331
t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '1' );
332
Koha::Patrons->find( $patron2->{borrowernumber} )->update_lastseen('connection');
331
isnt( Koha::Patrons->find( $patron2->{borrowernumber} )->lastseen, undef, 'Lastseen should be changed now' );
333
isnt( Koha::Patrons->find( $patron2->{borrowernumber} )->lastseen, undef, 'Lastseen should be changed now' );
332
334
333
# Test GetBorrowersToExpunge and regular patron with permission
335
# Test GetBorrowersToExpunge and regular patron with permission
334
- 

Return to bug 15504