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

(-)a/t/db_dependent/Auth.t (-102 / +2 lines)
Lines 10-16 use CGI qw ( -utf8 ); Link Here
10
use Test::MockObject;
10
use Test::MockObject;
11
use Test::MockModule;
11
use Test::MockModule;
12
use List::MoreUtils qw/all any none/;
12
use List::MoreUtils qw/all any none/;
13
use Test::More tests => 17;
13
use Test::More tests => 16;
14
use Test::Warn;
14
use Test::Warn;
15
use t::lib::Mocks;
15
use t::lib::Mocks;
16
use t::lib::TestBuilder;
16
use t::lib::TestBuilder;
Lines 23-29 use Koha::Patrons; Link Here
23
use Koha::Auth::TwoFactorAuth;
23
use Koha::Auth::TwoFactorAuth;
24
24
25
BEGIN {
25
BEGIN {
26
    use_ok('C4::Auth', qw( checkauth haspermission track_login_daily checkpw get_template_and_user checkpw_hash ));
26
    use_ok('C4::Auth', qw( checkauth haspermission checkpw get_template_and_user checkpw_hash ));
27
}
27
}
28
28
29
my $schema  = Koha::Database->schema;
29
my $schema  = Koha::Database->schema;
Lines 371-476 subtest 'checkauth() tests' => sub { Link Here
371
    C4::Context->_new_userenv; # For next tests
371
    C4::Context->_new_userenv; # For next tests
372
};
372
};
373
373
374
subtest 'track_login_daily tests' => sub {
375
376
    plan tests => 18;
377
378
    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
379
    my $userid = $patron->userid;
380
381
    $patron->lastseen( undef );
382
    $patron->store();
383
384
    my $cache     = Koha::Caches->get_instance();
385
    my $cache_key = "track_login_" . $patron->userid;
386
    $cache->clear_from_cache($cache_key);
387
388
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '1' );
389
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', 'login,connection,check_in,check_out,renewal' );
390
391
    is( $patron->lastseen, undef, 'Patron should have not last seen when newly created' );
392
393
    C4::Auth::track_login_daily( $userid, 'login' );
394
    $patron->_result()->discard_changes();
395
    isnt( $patron->lastseen, undef, 'Patron should have last seen set when TrackLastPatronActivity = 1' );
396
397
    sleep(1); # We need to wait a tiny bit to make sure the timestamp will be different
398
    my $last_seen = $patron->lastseen;
399
    C4::Auth::track_login_daily( $userid, 'login' );
400
    $patron->_result()->discard_changes();
401
    is( $patron->lastseen, $last_seen, 'Patron last seen should still be unchanged after a login' );
402
    C4::Auth::track_login_daily( $userid, 'connection' );
403
    $patron->_result()->discard_changes();
404
    is( $patron->lastseen, $last_seen, 'Patron last seen should still be unchanged after a SIP/ILSDI connection' );
405
    C4::Auth::track_login_daily( $userid, 'check_out' );
406
    $patron->_result()->discard_changes();
407
    is( $patron->lastseen, $last_seen, 'Patron last seen should still be unchanged after a check out' );
408
    C4::Auth::track_login_daily( $userid, 'check_in' );
409
    $patron->_result()->discard_changes();
410
    is( $patron->lastseen, $last_seen, 'Patron last seen should still be unchanged after a check in' );
411
    C4::Auth::track_login_daily( $userid, 'renewal' );
412
    $patron->_result()->discard_changes();
413
    is( $patron->lastseen, $last_seen, 'Patron last seen should still be unchanged after a renewal' );
414
415
    # Check that removing options stops tracking changes
416
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', 'connection,check_in,check_out,renewal' );
417
    $cache->clear_from_cache($cache_key);
418
    C4::Auth::track_login_daily( $userid, 'login' );
419
    $patron->_result()->discard_changes();
420
    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' );
421
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', 'check_in,check_out,renewal' );
422
    $cache->clear_from_cache($cache_key);
423
    C4::Auth::track_login_daily( $userid, 'connection' );
424
    $patron->_result()->discard_changes();
425
    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' );
426
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', 'check_out,renewal' );
427
    $cache->clear_from_cache($cache_key);
428
    C4::Auth::track_login_daily( $userid, 'check_in' );
429
    $patron->_result()->discard_changes();
430
    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' );
431
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', 'renewal' );
432
    $cache->clear_from_cache($cache_key);
433
    C4::Auth::track_login_daily( $userid, 'check_out' );
434
    $patron->_result()->discard_changes();
435
    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' );
436
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', '' );
437
    $cache->clear_from_cache($cache_key);
438
    C4::Auth::track_login_daily( $userid, 'renewal' );
439
    $patron->_result()->discard_changes();
440
    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' );
441
442
    # Restore all options to test changes
443
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', 'login,connection,check_in,check_out,renewal' );
444
445
    $cache->clear_from_cache($cache_key);
446
    C4::Auth::track_login_daily( $userid, 'login' );
447
    $patron->_result()->discard_changes();
448
    isnt( $patron->lastseen, $last_seen, 'Patron last seen should be changed after a login if we cleared the cache' );
449
    $cache->clear_from_cache($cache_key);
450
    C4::Auth::track_login_daily( $userid, 'connection' );
451
    $patron->_result()->discard_changes();
452
    isnt( $patron->lastseen, $last_seen, 'Patron last seen should be changed after a connection if we cleared the cache' );
453
    $cache->clear_from_cache($cache_key);
454
    C4::Auth::track_login_daily( $userid, 'check_out' );
455
    $patron->_result()->discard_changes();
456
    isnt( $patron->lastseen, $last_seen, 'Patron last seen should be changed after a check_out if we cleared the cache' );
457
    $cache->clear_from_cache($cache_key);
458
    C4::Auth::track_login_daily( $userid, 'check_in' );
459
    $patron->_result()->discard_changes();
460
    isnt( $patron->lastseen, $last_seen, 'Patron last seen should be changed after a check_in if we cleared the cache' );
461
    $cache->clear_from_cache($cache_key);
462
    C4::Auth::track_login_daily( $userid, 'renewal' );
463
    $patron->_result()->discard_changes();
464
    isnt( $patron->lastseen, $last_seen, 'Patron last seen should be changed after a renewal if we cleared the cache' );
465
466
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '0' );
467
    $patron->lastseen( undef )->store;
468
    $cache->clear_from_cache($cache_key);
469
    C4::Auth::track_login_daily( $userid, 'login' );
470
    $patron->_result()->discard_changes();
471
    is( $patron->lastseen, undef, 'Patron should still have last seen unchanged when TrackLastPatronActivity = 0' );
472
};
473
474
subtest 'no_set_userenv parameter tests' => sub {
374
subtest 'no_set_userenv parameter tests' => sub {
475
375
476
    plan tests => 7;
376
    plan tests => 7;
(-)a/t/db_dependent/Koha/Patron.t (-1 / +114 lines)
Lines 19-27 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 24;
22
use Test::More tests => 25;
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 1498-1500 subtest 'update privacy tests' => sub { Link Here
1498
    is( $old_checkout->borrowernumber, $anon_patron->id, "Checkout is successfully anonymized");
1499
    is( $old_checkout->borrowernumber, $anon_patron->id, "Checkout is successfully anonymized");
1499
    is( $patron->privacy(), 2, "Patron privacy is successfully updated");
1500
    is( $patron->privacy(), 2, "Patron privacy is successfully updated");
1500
};
1501
};
1502
1503
subtest 'update_lastseen tests' => sub {
1504
1505
    plan tests => 18;
1506
    $schema->storage->txn_begin;
1507
1508
    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
1509
    my $userid = $patron->userid;
1510
1511
    $patron->lastseen( undef );
1512
    $patron->store();
1513
1514
    my $cache     = Koha::Caches->get_instance();
1515
    my $cache_key = "track_login_" . $patron->userid;
1516
    $cache->clear_from_cache($cache_key);
1517
1518
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '1' );
1519
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', 'login,connection,check_in,check_out,renewal' );
1520
1521
    is( $patron->lastseen, undef, 'Patron should have not last seen when newly created' );
1522
1523
    my $now = dt_from_string();
1524
    Time::Fake->offset( $now->epoch );
1525
1526
    $patron->update_lastseen( 'login' );
1527
    $patron->_result()->discard_changes();
1528
    isnt( $patron->lastseen, undef, 'Patron should have last seen set when TrackLastPatronActivity = 1' );
1529
    my $last_seen = $patron->lastseen;
1530
1531
    Time::Fake->offset( $now->epoch + 5 );
1532
    # Test that lastseen isn't updated more than once in a day (regardless of activity passed)
1533
    $patron->update_lastseen( 'login' );
1534
    $patron->_result()->discard_changes();
1535
    is( $patron->lastseen, $last_seen, 'Patron last seen should still be unchanged after a login' );
1536
    $patron->update_lastseen( 'connection' );
1537
    $patron->_result()->discard_changes();
1538
    is( $patron->lastseen, $last_seen, 'Patron last seen should still be unchanged after a SIP/ILSDI connection' );
1539
    $patron->update_lastseen( 'check_out' );
1540
    $patron->_result()->discard_changes();
1541
    is( $patron->lastseen, $last_seen, 'Patron last seen should still be unchanged after a check out' );
1542
    $patron->update_lastseen( 'check_in' );
1543
    $patron->_result()->discard_changes();
1544
    is( $patron->lastseen, $last_seen, 'Patron last seen should still be unchanged after a check in' );
1545
    $patron->update_lastseen( 'renewal' );
1546
    $patron->_result()->discard_changes();
1547
    is( $patron->lastseen, $last_seen, 'Patron last seen should still be unchanged after a renewal' );
1548
1549
    # Check that tracking is disabled when the activity isn't listed
1550
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', '' );
1551
    $cache->clear_from_cache($cache_key); # Rule out the more than once day prevention above
1552
1553
    $patron->update_lastseen( 'login' );
1554
    $patron->_result()->discard_changes();
1555
    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' );
1556
1557
    $patron->update_lastseen( 'connection' );
1558
    $patron->_result()->discard_changes();
1559
    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' );
1560
1561
    $patron->update_lastseen( 'check_in' );
1562
    $patron->_result()->discard_changes();
1563
    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' );
1564
1565
    $patron->update_lastseen( 'check_out' );
1566
    $patron->_result()->discard_changes();
1567
    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' );
1568
1569
    $patron->update_lastseen( 'renewal' );
1570
    $patron->_result()->discard_changes();
1571
    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' );
1572
1573
    # Check tracking for each activity
1574
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', 'login,connection,check_in,check_out,renewal' );
1575
1576
    $cache->clear_from_cache($cache_key);
1577
    $patron->update_lastseen( 'login' );
1578
    $patron->_result()->discard_changes();
1579
    isnt( $patron->lastseen, $last_seen, 'Patron last seen should be changed after a login if we cleared the cache' );
1580
1581
    $cache->clear_from_cache($cache_key);
1582
    $patron->update_lastseen( 'connection' );
1583
    $patron->_result()->discard_changes();
1584
    isnt( $patron->lastseen, $last_seen, 'Patron last seen should be changed after a connection if we cleared the cache' );
1585
1586
    $cache->clear_from_cache($cache_key);
1587
    $patron->update_lastseen( 'check_out' );
1588
    $patron->_result()->discard_changes();
1589
    isnt( $patron->lastseen, $last_seen, 'Patron last seen should be changed after a check_out if we cleared the cache' );
1590
1591
    $cache->clear_from_cache($cache_key);
1592
    $patron->update_lastseen( 'check_in' );
1593
    $patron->_result()->discard_changes();
1594
    isnt( $patron->lastseen, $last_seen, 'Patron last seen should be changed after a check_in if we cleared the cache' );
1595
1596
    $cache->clear_from_cache($cache_key);
1597
    $patron->update_lastseen( 'renewal' );
1598
    $patron->_result()->discard_changes();
1599
    isnt( $patron->lastseen, $last_seen, 'Patron last seen should be changed after a renewal if we cleared the cache' );
1600
1601
    # Check that the preference takes effect
1602
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '0' );
1603
    $patron->lastseen( undef )->store;
1604
    $cache->clear_from_cache($cache_key);
1605
    $patron->update_lastseen( 'login' );
1606
    $patron->_result()->discard_changes();
1607
    is( $patron->lastseen, undef, 'Patron should still have last seen unchanged when TrackLastPatronActivity = 0' );
1608
1609
    Time::Fake->reset;
1610
    $schema->storage->txn_rollback;
1611
};
1612
1613
(-)a/t/db_dependent/Koha/Patrons.t (-2 / +2 lines)
Lines 1605-1616 subtest 'BorrowersLog tests' => sub { Link Here
1605
    is( $log_info->{cardnumber}->{before}, $cardnumber, 'Got correct old cardnumber' );
1605
    is( $log_info->{cardnumber}->{before}, $cardnumber, 'Got correct old cardnumber' );
1606
    is( scalar @logs, 1, 'With BorrowerLogs, one detailed MODIFY action should be logged for the modification.' );
1606
    is( scalar @logs, 1, 'With BorrowerLogs, one detailed MODIFY action should be logged for the modification.' );
1607
1607
1608
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', 'connection' );
1608
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', 1 );
1609
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', 1 );
1609
    $patron->track_login();
1610
    $patron->update_lastseen('connection');
1610
    @logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'MODIFY', object => $patron->borrowernumber } );
1611
    @logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'MODIFY', object => $patron->borrowernumber } );
1611
    is( scalar @logs, 1, 'With BorrowerLogs and TrackLastPatronActivity we should not spam the logs');
1612
    is( scalar @logs, 1, 'With BorrowerLogs and TrackLastPatronActivity we should not spam the logs');
1612
};
1613
};
1613
1614
$schema->storage->txn_rollback;
1614
$schema->storage->txn_rollback;
1615
1615
1616
subtest 'Test Koha::Patrons::merge' => sub {
1616
subtest 'Test Koha::Patrons::merge' => sub {
(-)a/t/db_dependent/Members.t (-3 / +4 lines)
Lines 339-349 my $patron2 = $builder->build({ Link Here
339
        updated_on => '2023-01-01 00:00:00'
339
        updated_on => '2023-01-01 00:00:00'
340
    }
340
    }
341
});
341
});
342
t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', 'connection' );
342
t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '0' );
343
t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '0' );
343
Koha::Patrons->find( $patron2->{borrowernumber} )->track_login;
344
Koha::Patrons->find( $patron2->{borrowernumber} )->update_lastseen('connection');
344
is( Koha::Patrons->find( $patron2->{borrowernumber} )->lastseen, undef, 'Lastseen should not be changed' );
345
is( Koha::Patrons->find( $patron2->{borrowernumber} )->lastseen, undef, 'Lastseen should not be changed' );
345
is( Koha::Patrons->find( $patron2->{borrowernumber} )->updated_on, '2023-01-01 00:00:00', 'Updated_on should not be changed' );
346
is( Koha::Patrons->find( $patron2->{borrowernumber} )->updated_on, '2023-01-01 00:00:00', 'Updated_on should not be changed' );
346
Koha::Patrons->find( $patron2->{borrowernumber} )->track_login({ force => 1 });
347
t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '1' );
348
Koha::Patrons->find( $patron2->{borrowernumber} )->update_lastseen('connection');
347
isnt( Koha::Patrons->find( $patron2->{borrowernumber} )->lastseen, undef, 'Lastseen should be changed now' );
349
isnt( Koha::Patrons->find( $patron2->{borrowernumber} )->lastseen, undef, 'Lastseen should be changed now' );
348
is( Koha::Patrons->find( $patron2->{borrowernumber} )->updated_on, '2023-01-01 00:00:00', 'Updated_on should not be changed' );
350
is( Koha::Patrons->find( $patron2->{borrowernumber} )->updated_on, '2023-01-01 00:00:00', 'Updated_on should not be changed' );
349
351
350
- 

Return to bug 15504