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

(-)a/t/db_dependent/Koha/Patron.t (-21 / +68 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 46;
22
use Test::More tests => 47;
23
use Test::NoWarnings;
23
use Test::NoWarnings;
24
use Test::Exception;
24
use Test::Exception;
25
use Test::Warn;
25
use Test::Warn;
Lines 41-46 use C4::Circulation qw( AddIssue AddReturn ); Link Here
41
41
42
use t::lib::TestBuilder;
42
use t::lib::TestBuilder;
43
use t::lib::Mocks;
43
use t::lib::Mocks;
44
use Test::MockModule;
44
45
45
my $schema  = Koha::Database->new->schema;
46
my $schema  = Koha::Database->new->schema;
46
my $builder = t::lib::TestBuilder->new;
47
my $builder = t::lib::TestBuilder->new;
Lines 818-823 subtest 'permissions() tests' => sub { Link Here
818
    my $patron = $builder->build_object(
819
    my $patron = $builder->build_object(
819
        {
820
        {
820
            class => 'Koha::Patrons',
821
            class => 'Koha::Patrons',
822
821
            value => { flags => 4 }
823
            value => { flags => 4 }
822
        }
824
        }
823
    );
825
    );
Lines 2420-2429 subtest 'alert_subscriptions tests' => sub { Link Here
2420
2422
2421
    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2423
    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2422
2424
2423
    my $subscription1 = $builder->build_object( { class => 'Koha::Subscriptions' } );
2425
    my $subscription1 = $builder->build_object(
2426
        {
2427
            class => 'Koha::Subscriptions',
2428
            value => {
2429
                librarian => $patron->borrowernumber,
2430
            }
2431
        }
2432
    );
2424
    $subscription1->add_subscriber($patron);
2433
    $subscription1->add_subscriber($patron);
2425
2434
2426
    my $subscription2 = $builder->build_object( { class => 'Koha::Subscriptions' } );
2435
    my $subscription2 = $builder->build_object(
2436
        {
2437
            class => 'Koha::Subscriptions',
2438
            value => {
2439
                librarian => $patron->borrowernumber,
2440
            }
2441
        }
2442
    );
2427
    $subscription2->add_subscriber($patron);
2443
    $subscription2->add_subscriber($patron);
2428
2444
2429
    my @subscriptions = $patron->alert_subscriptions->as_list;
2445
    my @subscriptions = $patron->alert_subscriptions->as_list;
Lines 2459-2464 subtest 'update_lastseen tests' => sub { Link Here
2459
    plan tests => 26;
2475
    plan tests => 26;
2460
    $schema->storage->txn_begin;
2476
    $schema->storage->txn_begin;
2461
2477
2478
    my $mock_cache = Test::MockModule->new('Koha::Cache');
2479
    my %fake_cache;
2480
2481
    $mock_cache->mock(
2482
        'set_in_cache',
2483
        sub {
2484
            my ( $self, $key, $value ) = @_;
2485
            $fake_cache{$key} = $value;
2486
            return;
2487
        }
2488
    );
2489
2490
    $mock_cache->mock(
2491
        'get_from_cache',
2492
        sub {
2493
            my ( $self, $key ) = @_;
2494
            return $fake_cache{$key};
2495
        }
2496
    );
2497
2462
    my $cache = Koha::Caches->get_instance();
2498
    my $cache = Koha::Caches->get_instance();
2463
2499
2464
    t::lib::Mocks::mock_preference(
2500
    t::lib::Mocks::mock_preference(
Lines 2466-2478 subtest 'update_lastseen tests' => sub { Link Here
2466
        'creation,login,connection,check_in,check_out,renewal,hold,article'
2502
        'creation,login,connection,check_in,check_out,renewal,hold,article'
2467
    );
2503
    );
2468
2504
2469
    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2505
    my $patron      = $builder->build_object( { class => 'Koha::Patrons' } );
2470
    my $userid = $patron->userid;
2471
    $patron->lastseen(undef);
2472
    my $patron_info = $patron->unblessed;
2506
    my $patron_info = $patron->unblessed;
2473
    delete $patron_info->{borrowernumber};
2507
    delete $patron_info->{borrowernumber};
2474
    $patron->delete();
2508
    $patron->delete();
2475
    $patron = Koha::Patron->new($patron_info)->store();
2509
    $patron = Koha::Patron->new($patron_info)->store();
2510
    $patron->update_lastseen( { activity => 'creation' } );
2511
    $patron->discard_changes();
2476
2512
2477
    my $now   = dt_from_string();
2513
    my $now   = dt_from_string();
2478
    my $today = $now->ymd();
2514
    my $today = $now->ymd();
Lines 2482-2502 subtest 'update_lastseen tests' => sub { Link Here
2482
        'Patron should have last seen when newly created if requested'
2518
        'Patron should have last seen when newly created if requested'
2483
    );
2519
    );
2484
2520
2485
    my $cache_key   = "track_activity_" . $patron->borrowernumber;
2521
    my $cache_key = "track_activity_" . $patron->borrowernumber;
2486
    my $cache_value = $cache->get_from_cache($cache_key);
2522
    is( $cache->get_from_cache($cache_key), $today, "Cache was updated as expected" );
2487
    is( $cache_value, $today, "Cache was updated as expected" );
2488
2523
2489
    $patron->delete();
2524
    $patron->delete();
2490
2525
2491
    t::lib::Mocks::mock_preference(
2526
    t::lib::Mocks::mock_preference(
2492
        'TrackLastPatronActivityTriggers',
2527
        'TrackLastPatronActivityTriggers',
2493
        'login,connection,check_in,check_out,renewal,hold,article'
2528
        ''
2494
    );
2529
    );
2530
    my $patron_no_trigger = $builder->build_object( { class => 'Koha::Patrons' } );
2495
2531
2496
    $patron    = Koha::Patron->new($patron_info)->store();
2532
    $patron_no_trigger->lastseen(undef)->store();
2497
    $cache_key = "track_activity_" . $patron->borrowernumber;
2498
2533
2499
    is( $patron->lastseen, undef, 'Patron should have not last seen when newly created if trigger not set' );
2534
    $patron_no_trigger->update_lastseen( { activity => 'login' } );
2535
    $patron_no_trigger->discard_changes();
2536
2537
    is(
2538
        $patron_no_trigger->lastseen, undef,
2539
        'Patron should have not last seen when newly created if trigger not set'
2540
    );
2541
2542
    t::lib::Mocks::mock_preference(
2543
        'TrackLastPatronActivityTriggers',
2544
        'login,connection,check_in,check_out,renewal,hold,article'
2545
    );
2500
2546
2501
    $now = dt_from_string();
2547
    $now = dt_from_string();
2502
    Time::Fake->offset( $now->epoch );
2548
    Time::Fake->offset( $now->epoch );
Lines 2591-2602 subtest 'update_lastseen tests' => sub { Link Here
2591
        'login,connection,check_in,check_out,renewal,hold,article'
2637
        'login,connection,check_in,check_out,renewal,hold,article'
2592
    );
2638
    );
2593
2639
2594
    $cache->clear_from_cache($cache_key);
2640
    %fake_cache = ();
2595
    $patron->update_lastseen('login');
2641
    $patron->update_lastseen('login');
2596
    $patron->_result()->discard_changes();
2642
    $patron->_result()->discard_changes();
2597
    isnt( $patron->lastseen, $last_seen, 'Patron last seen should be changed after a login if we cleared the cache' );
2643
    isnt( $patron->lastseen, $last_seen, 'Patron last seen should be changed after a login if we cleared the cache' );
2598
2644
2599
    $cache->clear_from_cache($cache_key);
2645
    %fake_cache = ();
2600
    $patron->update_lastseen('connection');
2646
    $patron->update_lastseen('connection');
2601
    $patron->_result()->discard_changes();
2647
    $patron->_result()->discard_changes();
2602
    isnt(
2648
    isnt(
Lines 2604-2610 subtest 'update_lastseen tests' => sub { Link Here
2604
        'Patron last seen should be changed after a connection if we cleared the cache'
2650
        'Patron last seen should be changed after a connection if we cleared the cache'
2605
    );
2651
    );
2606
2652
2607
    $cache->clear_from_cache($cache_key);
2653
    %fake_cache = ();
2608
    $patron->update_lastseen('check_out');
2654
    $patron->update_lastseen('check_out');
2609
    $patron->_result()->discard_changes();
2655
    $patron->_result()->discard_changes();
2610
    isnt(
2656
    isnt(
Lines 2612-2618 subtest 'update_lastseen tests' => sub { Link Here
2612
        'Patron last seen should be changed after a check_out if we cleared the cache'
2658
        'Patron last seen should be changed after a check_out if we cleared the cache'
2613
    );
2659
    );
2614
2660
2615
    $cache->clear_from_cache($cache_key);
2661
    %fake_cache = ();
2616
    $patron->update_lastseen('check_in');
2662
    $patron->update_lastseen('check_in');
2617
    $patron->_result()->discard_changes();
2663
    $patron->_result()->discard_changes();
2618
    isnt(
2664
    isnt(
Lines 2620-2632 subtest 'update_lastseen tests' => sub { Link Here
2620
        'Patron last seen should be changed after a check_in if we cleared the cache'
2666
        'Patron last seen should be changed after a check_in if we cleared the cache'
2621
    );
2667
    );
2622
2668
2623
    $cache->clear_from_cache($cache_key);
2669
    %fake_cache = ();
2624
    $patron->update_lastseen('hold');
2670
    $patron->update_lastseen('hold');
2625
    $patron->_result()->discard_changes();
2671
    $patron->_result()->discard_changes();
2626
    isnt(
2672
    isnt(
2627
        $patron->lastseen, $last_seen,
2673
        $patron->lastseen, $last_seen,
2628
        'Patron last seen should be changed after a hold if we cleared the cache'
2674
        'Patron last seen should be changed after a hold if we cleared the cache'
2629
    );
2675
    );
2676
2677
    %fake_cache = ();
2630
    $patron->update_lastseen('article');
2678
    $patron->update_lastseen('article');
2631
    $patron->_result()->discard_changes();
2679
    $patron->_result()->discard_changes();
2632
    isnt(
2680
    isnt(
Lines 2634-2640 subtest 'update_lastseen tests' => sub { Link Here
2634
        'Patron last seen should be changed after a article if we cleared the cache'
2682
        'Patron last seen should be changed after a article if we cleared the cache'
2635
    );
2683
    );
2636
2684
2637
    $cache->clear_from_cache($cache_key);
2685
    %fake_cache = ();
2638
    $patron->update_lastseen('renewal');
2686
    $patron->update_lastseen('renewal');
2639
    $patron->_result()->discard_changes();
2687
    $patron->_result()->discard_changes();
2640
    isnt( $patron->lastseen, $last_seen, 'Patron last seen should be changed after a renewal if we cleared the cache' );
2688
    isnt( $patron->lastseen, $last_seen, 'Patron last seen should be changed after a renewal if we cleared the cache' );
Lines 2642-2648 subtest 'update_lastseen tests' => sub { Link Here
2642
    # Check that the preference takes effect
2690
    # Check that the preference takes effect
2643
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', '' );
2691
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', '' );
2644
    $patron->lastseen(undef)->store;
2692
    $patron->lastseen(undef)->store;
2645
    $cache->clear_from_cache($cache_key);
2693
    %fake_cache = ();
2646
    $patron->update_lastseen('login');
2694
    $patron->update_lastseen('login');
2647
    $patron->_result()->discard_changes();
2695
    $patron->_result()->discard_changes();
2648
    is(
2696
    is(
2649
- 

Return to bug 20956