From b4de64c0ed0b63f92fdecb5d60bdd0cb273757b4 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Fri, 1 Oct 2021 12:42:24 -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 --- Koha/Patron.pm | 11 ++++---- t/db_dependent/Koha/Patron.t | 54 +++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 6436d18bb6..7dd35cd455 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -973,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 => { '>=' => \'DATE(NOW())' } }, + ] + )->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.33.0