From c3a1a0757393f2d917da033612a9e9935fcbba36 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 12 Jul 2016 00:18:56 +0100 Subject: [PATCH] Bug 16911: Koha::Patrons - Add tests for ->extend_subscription --- t/db_dependent/Koha/Patrons.t | 51 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Koha/Patrons.t b/t/db_dependent/Koha/Patrons.t index fafd829..644f22e 100644 --- a/t/db_dependent/Koha/Patrons.t +++ b/t/db_dependent/Koha/Patrons.t @@ -19,13 +19,18 @@ use Modern::Perl; -use Test::More tests => 5; +use Test::More tests => 6; + +use C4::Members; use Koha::Patron; use Koha::Patrons; use Koha::Database; +use Koha::DateUtils; +use Koha::Virtualshelves; use t::lib::TestBuilder; +use t::lib::Mocks; my $schema = Koha::Database->new->schema; $schema->storage->txn_begin; @@ -98,6 +103,50 @@ subtest 'siblings' => sub { $retrieved_guarantee_1->delete; }; +subtest 'extend_subscription' => sub { + plan tests => 6; + my $a_month_ago = dt_from_string->add( months => -1 )->truncate( to => 'day' ); + my $a_year_later = dt_from_string->add( months => 12 )->truncate( to => 'day' ); + my $a_year_later_minus_a_month = dt_from_string->add( months => 11 )->truncate( to => 'day' ); + my $patron_category = $builder->build( + { source => 'Category', + value => { + enrolmentperiod => 12, + enrolmentperioddate => undef, + } + } + ); + my $patron = $builder->build( + { source => 'Borrower', + value => { + dateexpiry => $a_month_ago, + categorycode => $patron_category->{categorycode}, + } + } + ); + my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} ); + + t::lib::Mocks::mock_preference( 'BorrowerRenewalPeriodBase', 'dateexpiry' ); + t::lib::Mocks::mock_preference( 'BorrowersLog', 1 ); + my $expiry_date = $retrieved_patron->extend_subscription; + is( $expiry_date, $a_year_later_minus_a_month, ); + my $retrieved_expiry_date = Koha::Patrons->find( $patron->{borrowernumber} )->dateexpiry; + is( dt_from_string($retrieved_expiry_date), $a_year_later_minus_a_month ); + my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'RENEW', object => $retrieved_patron->borrowernumber } )->count; + is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->extend_subscription should have logged' ); + + t::lib::Mocks::mock_preference( 'BorrowerRenewalPeriodBase', 'now' ); + t::lib::Mocks::mock_preference( 'BorrowersLog', 0 ); + $expiry_date = $retrieved_patron->extend_subscription; + is( $expiry_date, $a_year_later, ); + $retrieved_expiry_date = Koha::Patrons->find( $patron->{borrowernumber} )->dateexpiry; + is( dt_from_string($retrieved_expiry_date), $a_year_later ); + $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'RENEW', object => $retrieved_patron->borrowernumber } )->count; + is( $number_of_logs, 1, 'Without BorrowerLogs, Koha::Patron->extend_subscription should not have logged' ); + + $retrieved_patron->delete; +}; + $retrieved_patron_1->delete; is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' ); -- 2.8.1