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 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 / +112 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
};
(-)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 338-347 my $patron2 = $builder->build({ Link Here
338
        flags => undef,
338
        flags => undef,
339
    }
339
    }
340
});
340
});
341
t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', 'connection' );
341
t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '0' );
342
t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '0' );
342
Koha::Patrons->find( $patron2->{borrowernumber} )->track_login;
343
Koha::Patrons->find( $patron2->{borrowernumber} )->update_lastseen('connection');
343
is( Koha::Patrons->find( $patron2->{borrowernumber} )->lastseen, undef, 'Lastseen should not be changed' );
344
is( Koha::Patrons->find( $patron2->{borrowernumber} )->lastseen, undef, 'Lastseen should not be changed' );
344
Koha::Patrons->find( $patron2->{borrowernumber} )->track_login({ force => 1 });
345
t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '1' );
346
Koha::Patrons->find( $patron2->{borrowernumber} )->update_lastseen('connection');
345
isnt( Koha::Patrons->find( $patron2->{borrowernumber} )->lastseen, undef, 'Lastseen should be changed now' );
347
isnt( Koha::Patrons->find( $patron2->{borrowernumber} )->lastseen, undef, 'Lastseen should be changed now' );
346
348
347
# Test GetBorrowersToExpunge and regular patron with permission
349
# Test GetBorrowersToExpunge and regular patron with permission
348
- 

Return to bug 15504