From 20b1a151ad9619ae8ac27ea53e387baa84abcc60 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Thu, 16 Mar 2023 15:41:55 +0000 Subject: [PATCH] Bug 33245: Add unit test Content-Type: text/plain; charset=utf-8 Test plan: Run t/db_dependent/Koha/Patron.t Signed-off-by: Marcel de Rooy Signed-off-by: Matt Blenkinsop --- t/db_dependent/Koha/Patron.t | 86 +++++++++++++++++++++++++++++++++++- 1 file changed, 85 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Koha/Patron.t b/t/db_dependent/Koha/Patron.t index 5d05a16efd..7007a9febe 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 => 24; +use Test::More tests => 25; use Test::Exception; use Test::Warn; @@ -37,6 +37,90 @@ use t::lib::Mocks; my $schema = Koha::Database->new->schema; my $builder = t::lib::TestBuilder->new; +subtest 'is_active' => sub { + plan tests => 18; + $schema->storage->txn_begin; + + # Check expiry + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + $patron->dateexpiry( dt_from_string->subtract( days => 1 ) )->store; + is( $patron->is_active, 0, 'Expired patron is not active' ); + $patron->dateexpiry(undef)->store; + is( $patron->is_active, 1, 'Expiry date removed' ); + + # Change enrolled date now + $patron->dateenrolled('2020-01-01')->store; + + # Check lastseen, test days parameter + t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', 1 ); + $patron->track_login; + is( $patron->is_active, 1, 'Just logged in' ); + my $ago = dt_from_string->subtract( days => 2 ); + $patron->lastseen($ago)->store; + is( $patron->is_active( { days => 1 } ), 0, 'Not active since yesterday' ); + is( $patron->is_active( { days => 3 } ), 1, 'Active within last 3 days' ); + t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', 0 ); + is( $patron->is_active( { days => 3 } ), 0, 'Pref disabled' ); + + # Look at holds, test with weeks + $ago = dt_from_string->subtract( weeks => 2 ); + my $hold = $builder->build_object( + { + class => 'Koha::Holds', + value => { borrowernumber => $patron->id, timestamp => $ago }, + } + ); + is( $patron->is_active( { weeks => 1 } ), 0, 'No holds in 1 weeks' ); + is( $patron->is_active( { weeks => 3 } ), 1, 'Hold in last 3 weeks' ); + $hold->delete; + my $old_hold = $builder->build_object( + { + class => 'Koha::Old::Holds', + value => { borrowernumber => $patron->id, timestamp => $ago }, + } + ); + is( $patron->is_active( { weeks => 1 } ), 0, 'No old holds in 1 weeks' ); + is( $patron->is_active( { weeks => 3 } ), 1, 'Old hold in last 3 weeks' ); + $old_hold->delete; + + # Look at checkouts, test with months + $ago = dt_from_string->subtract( months => 2 ); + my $checkout = $builder->build_object( + { + class => 'Koha::Checkouts', + value => { borrowernumber => $patron->id, timestamp => $ago }, + } + ); + is( $patron->is_active( { months => 1 } ), 0, 'No checkouts in 1 months' ); + is( $patron->is_active( { months => 3 } ), 1, 'Checkout in last 3 months' ); + $checkout->delete; + my $old_checkout = $builder->build_object( + { + class => 'Koha::Old::Checkouts', + value => { borrowernumber => $patron->id, timestamp => $ago }, + } + ); + is( $patron->is_active( { months => 1 } ), 0, 'No old checkouts in 1 months' ); + is( $patron->is_active( { months => 3 } ), 1, 'Old checkout in last 3 months' ); + $old_checkout->delete; + + # Look at article_requests, test with since + $ago = dt_from_string->subtract( days => 10 ); + my $article_request = $builder->build_object( + { + class => 'Koha::ArticleRequests', + value => { borrowernumber => $patron->id, updated_on => $ago }, + } + ); + is( $patron->is_active( { days => 9 } ), 0, 'No article requests in 9 days' ); + is( $patron->is_active( { days => 10 } ), 1, 'Article requests in 10 days' ); + is( $patron->is_active( { since => $ago } ), 1, 'Article requests since ago' ); + is( $patron->is_active( { since => $ago->add( days => 1 ) } ), 0, 'No article requests since ago + 1 day' ); + $article_request->delete; + + $schema->storage->txn_rollback; +}; + subtest 'add_guarantor() tests' => sub { plan tests => 6; -- 2.30.2