From ed86ec73084937981092c623d548913954ae2214 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Thu, 30 Sep 2021 15:57:00 -0300 Subject: [PATCH] Bug 27945: Clarify 'same day' behavior This patch introduces tests for the 'same day' check of the ability to place article requests for a patron. The limit goes against current requests, and those that have been completed on the same day. The tests cover this specific situation. The current behavior is that it takes into account a 24 hr timespan, but consensus on the QA step was that we should do it as 'same day' and use a separate feature request to change this, if required. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/Koha/Patron.t => SUCCESS: Tests pass! 3. Sign off :-D Signed-off-by: Tomas Cohen Arazi Signed-off-by: Jonathan Druart --- Koha/Patron.pm | 16 +++++------ t/db_dependent/Koha/Patron.t | 54 +++++++++++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 9 deletions(-) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 967ae9a33b..7ffb11cbeb 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -960,9 +960,10 @@ sub move_to_deleted { =head3 can_request_article -my $can_request = $borrower->can_request_article + if ( $patron->can_request_article ) { ... } -Returns true if patron can request articles +Returns true if the patron can request articles. As limits apply for the patron +on on the same day, those completed the same day are considered as current. =cut @@ -972,12 +973,11 @@ sub can_request_article { return 1 unless defined $limit; - my $dtf = Koha::Database->new->schema->storage->datetime_parser; - my $compdate = dt_from_string->add( days => -1 ); - my $count = Koha::ArticleRequests->search([ - { borrowernumber => $self->borrowernumber, status => ['REQUESTED','PENDING','PROCESSING'] }, - { borrowernumber => $self->borrowernumber, status => 'COMPLETED', updated_on => { '>', $dtf->format_date($compdate) }}, - ])->count; + my $count = Koha::ArticleRequests->search( + [ { borrowernumber => $self->borrowernumber, status => [ 'REQUESTED', 'PENDING', 'PROCESSING' ] }, + { borrowernumber => $self->borrowernumber, status => 'COMPLETED', updated_on => { '>=' => \'CAST(NOW() AS DATE)' } }, + ] + )->count; return $count < $limit ? 1 : 0; } diff --git a/t/db_dependent/Koha/Patron.t b/t/db_dependent/Koha/Patron.t index b088d2a5b2..0d62df241c 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 => 9; +use Test::More tests => 10; use Test::Exception; use Test::Warn; @@ -831,3 +831,55 @@ subtest 'can_request_article() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'can_request_article() tests' => sub { + + plan tests => 3; + + $schema->storage->txn_begin; + + my $category = $builder->build_object( + { + class => 'Koha::Patron::Categories', + value => { article_request_limit => 4 } + } + ); + my $patron = $builder->build_object( + { + class => 'Koha::Patrons', + value => { categorycode => $category->id } + } + ); + + $builder->build_object( { class => 'Koha::ArticleRequests', value => { status => 'REQUESTED', borrowernumber => $patron->id } } ); + $builder->build_object( { class => 'Koha::ArticleRequests', value => { status => 'PENDING', borrowernumber => $patron->id } } ); + $builder->build_object( { class => 'Koha::ArticleRequests', value => { status => 'PROCESSING', borrowernumber => $patron->id } } ); + $builder->build_object( { class => 'Koha::ArticleRequests', value => { status => 'CANCELED', borrowernumber => $patron->id } } ); + + ok( $patron->can_request_article, 'Patron has 3 current requests, 4 is the limit: allowed' ); + + # Completed request, same day + my $completed = $builder->build_object( + { + class => 'Koha::ArticleRequests', + value => { + status => 'COMPLETED', + borrowernumber => $patron->id + } + } + ); + + ok( !$patron->can_request_article, 'Patron has 3 current requests and a completed one the same day: denied' ); + + $completed->updated_on( + dt_from_string->add( days => -1 )->set( + hour => 23, + minute => 59, + second => 59, + ) + )->store; + + ok( $patron->can_request_article, 'Patron has 3 current requests and a completed one the day before: allowed' ); + + $schema->storage->txn_rollback; +}; -- 2.39.5