@@ -, +, @@ $ kshell k$ prove t/db_dependent/Koha/Patron.t \ t/db_dependent/ArticleRequests.t --- Koha/ArticleRequest.pm | 8 +-- Koha/Patron.pm | 5 +- t/db_dependent/ArticleRequests.t | 97 ------------------------- t/db_dependent/Koha/Patron.t | 117 ++++++++++++++++++++++++++++++- 4 files changed, 122 insertions(+), 105 deletions(-) --- a/Koha/ArticleRequest.pm +++ a/Koha/ArticleRequest.pm @@ -48,6 +48,10 @@ Koha::ArticleRequest - Koha Article Request Object class sub request { my ($self) = @_; + Koha::Exceptions::ArticleRequest::LimitReached->throw( + error => 'Patron cannot request more articles for today' + ) unless $self->borrower->can_request_article; + $self->status(Koha::ArticleRequest::Status::Requested); $self->SUPER::store(); $self->notify(); @@ -61,10 +65,6 @@ sub request { sub set_pending { my ($self) = @_; - Koha::Exceptions::ArticleRequest::LimitReached->throw( - error => 'Patron cannot request more articles for today' - ) unless $self->borrower->can_request_article; - $self->status(Koha::ArticleRequest::Status::Pending); $self->SUPER::store(); $self->notify(); --- a/Koha/Patron.pm +++ a/Koha/Patron.pm @@ -970,18 +970,17 @@ sub can_request_article { my ($self) = @_; my $limit = $self->category->article_request_limit; - return 1 if !$limit; + 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 => ['PENDING','PROCESSING'] }, + { borrowernumber => $self->borrowernumber, status => ['REQUESTED','PENDING','PROCESSING'] }, { borrowernumber => $self->borrowernumber, status => 'COMPLETED', updated_on => { '>', $dtf->format_date($compdate) }}, ])->count; return $count < $limit ? 1 : 0; } - =head3 article_requests my @requests = $borrower->article_requests(); --- a/t/db_dependent/ArticleRequests.t +++ a/t/db_dependent/ArticleRequests.t @@ -252,103 +252,6 @@ subtest 'may_article_request' => sub { $cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY ); }; - -subtest 'article request limit' => sub { - plan tests => 13; - - t::lib::Mocks::mock_preference('ArticleRequests', 1); - - my $item = $builder->build_sample_item; - - my $category = $builder->build_object( - { - class => 'Koha::Patron::Categories', - value => { - article_request_limit => 1 - } - } - ); - my $patron = $builder->build_object( - { - class => 'Koha::Patrons', - value => { - categorycode => $category->categorycode - }, - } - ); - $patron->article_requests->delete(); - - is($patron->can_request_article, 1, 'There are no AR, so patron can request more articles'); - - my $article_request_1 = Koha::ArticleRequest->new( - { - borrowernumber => $patron->id, - biblionumber => $item->biblionumber, - itemnumber => $item->itemnumber, - title => 'an article request', - } - )->store(); - - is($patron->can_request_article, 0, 'Limit is 1, so patron cannot request more articles'); - is($patron->article_requests->count, 1, 'There is one current article request'); - - try { - Koha::ArticleRequest->new( - { - borrowernumber => $patron->id, - biblionumber => $item->biblionumber, - itemnumber => $item->itemnumber, - title => 'a second article request', - } - )->store(); - } - catch { - is(ref($_), 'Koha::Exceptions::ArticleRequest::LimitReached', 'When limit was reached and we ask for a new AR, Limit reached is thrown'); - }; - - is($patron->can_request_article, 0, 'There is still an AR, so patron cannot request more articles'); - is($patron->article_requests->count, 1, 'There is still one article request'); - - $article_request_1->complete(); - - is($patron->can_request_article, 0, 'AR was completed but within one day, so patron cannot request more articles'); - - $article_request_1->updated_on(dt_from_string->add(days => -2))->store(); - - is($patron->can_request_article, 1, 'There are no completed AR within one day, so patron can request more articles'); - - my $article_request_3 = Koha::ArticleRequest->new( - { - borrowernumber => $patron->id, - biblionumber => $item->biblionumber, - itemnumber => $item->itemnumber, - title => 'a third article request', - } - )->store(); - - is($patron->can_request_article, 0, 'A new AR was created, so patron cannot request more articles'); - is($patron->article_requests->count, 2, 'There are 2 article requests'); - - $article_request_3->cancel(); - - is($patron->can_request_article, 1, 'New AR was cancelled, so patron can request more articles'); - - my $article_request_4 = Koha::ArticleRequest->new( - { - borrowernumber => $patron->id, - biblionumber => $item->biblionumber, - itemnumber => $item->itemnumber, - title => 'an fourth article request', - } - )->store(); - - $article_request_4->updated_on(dt_from_string->add(days => -30))->store(); - - is($patron->can_request_article, 0, 'There is an old AR but not completed or cancelled, so patron cannot request more articles'); - is($patron->article_requests->count, 3, 'There are 3 current article requests'); - -}; - $schema->storage->txn_rollback(); subtest 'set_pending() tests' => sub { --- a/t/db_dependent/Koha/Patron.t +++ a/t/db_dependent/Koha/Patron.t @@ -19,12 +19,13 @@ use Modern::Perl; -use Test::More tests => 8; +use Test::More tests => 9; use Test::Exception; use Test::Warn; use Koha::Database; use Koha::DateUtils qw(dt_from_string); +use Koha::ArticleRequests; use Koha::Patrons; use Koha::Patron::Relationships; @@ -716,3 +717,117 @@ subtest 'can_log_into() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'can_request_article() tests' => sub { + + plan tests => 13; + + $schema->storage->txn_begin; + + t::lib::Mocks::mock_preference( 'ArticleRequests', 1 ); + + my $item = $builder->build_sample_item; + + my $category = $builder->build_object( + { + class => 'Koha::Patron::Categories', + value => { + article_request_limit => 1 + } + } + ); + my $patron = $builder->build_object( + { + class => 'Koha::Patrons', + value => { + categorycode => $category->categorycode + }, + } + ); + + is( $patron->can_request_article, + 1, 'There are no AR, so patron can request more articles' ); + + my $article_request_1 = Koha::ArticleRequest->new( + { + borrowernumber => $patron->id, + biblionumber => $item->biblionumber, + itemnumber => $item->itemnumber, + title => 'an article request', + } + )->request; + + is( $patron->can_request_article, + 0, 'Limit is 1, so patron cannot request more articles' ); + is( $patron->article_requests->count, + 1, 'There is one current article request' ); + + throws_ok { + Koha::ArticleRequest->new( + { + borrowernumber => $patron->id, + biblionumber => $item->biblionumber, + itemnumber => $item->itemnumber, + title => 'a second article request', + } + )->request; + } + 'Koha::Exceptions::ArticleRequest::LimitReached', + 'When limit was reached and we ask for a new AR, Limit reached is thrown'; + + is( $patron->can_request_article, + 0, 'There is still an AR, so patron cannot request more articles' ); + is( $patron->article_requests->count, + 1, 'There is still one article request' ); + + $article_request_1->complete(); + + is( $patron->can_request_article, 0, +'AR was completed but within one day, so patron cannot request more articles' + ); + + $article_request_1->updated_on( dt_from_string->add( days => -2 ) ) + ->store(); + + is( $patron->can_request_article, 1, +'There are no completed AR within one day, so patron can request more articles' + ); + + my $article_request_3 = Koha::ArticleRequest->new( + { + borrowernumber => $patron->id, + biblionumber => $item->biblionumber, + itemnumber => $item->itemnumber, + title => 'a third article request', + } + )->request; + + is( $patron->can_request_article, + 0, 'A new AR was created, so patron cannot request more articles' ); + is( $patron->article_requests->count, 2, 'There are 2 article requests' ); + + $article_request_3->cancel(); + + is( $patron->can_request_article, + 1, 'New AR was cancelled, so patron can request more articles' ); + + my $article_request_4 = Koha::ArticleRequest->new( + { + borrowernumber => $patron->id, + biblionumber => $item->biblionumber, + itemnumber => $item->itemnumber, + title => 'an fourth article request', + } + )->request; + + $article_request_4->updated_on( dt_from_string->add( days => -30 ) ) + ->store(); + + is( $patron->can_request_article, 0, +'There is an old AR but not completed or cancelled, so patron cannot request more articles' + ); + is( $patron->article_requests->count, + 3, 'There are 3 current article requests' ); + + $schema->storage->txn_rollback; +}; --