From f968a65793b877e3118fa347cba542e629acde9d Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 22 Sep 2021 16:19:21 -0300 Subject: [PATCH] Bug 29084: Update article requests-related Koha::Biblio methods to use relationships This patch makes Koha::Patron->article_requests use the underlying DBIC relationship and _new_from_dbic instead of a plain search. It also refactors 'article_requests_current' and 'article_requests_finished' to use ->article_requests, as well as the new methods introduced by bug 29082 for filtering. No behavior change should take place. To test: 1. Apply the unit tests patch 2. Run: $ kshell k$ prove t/db_dependent/Koha/Biblio.t \ t/db_dependent/ArticleRequests.t => SUCCESS: Tests pass! 3. Apply this patch 4. Repeat 2 => SUCCESS: Tests pass! 5. Sign off :-D Signed-off-by: Martin Renvoize Bug 29084: Unit tests This patch adds missing tests for Koha::Biblio->article_requests and reorganizes (and extends) the tests for 'article_requests_current' and 'article_requests_finished' that were originally in ArticleRequests.t into Koha/Biblio.t as we do now. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/ArticleRequests.t \ t/db_dependent/Koha/Biblio.t => SUCCESS: Tests pass! Signed-off-by: Tomas Cohen Arazi Signed-off-by: Martin Renvoize Bug 29084: Remove article_requests_finished and article_requests_current This patch removes those methods that are not really needed. Templates are adjusted to use the expected combination of ->article_requests->filter_by_current. To test: 1. Apply this patch 2. Visit a biblio with article requests => SUCCESS: All works 3. Run: $ kshell k$ prove t/db_dependent/Koha/Biblio.t => SUCCESS: Tests pass, less tests. 4. Sign off :-D Signed-off-by: Jonathan Druart --- Koha/Biblio.pm | 59 ++----------------- .../prog/en/modules/circ/request-article.tt | 6 +- t/db_dependent/ArticleRequests.t | 24 +------- t/db_dependent/Koha/Biblio.t | 36 ++++++++++- 4 files changed, 44 insertions(+), 81 deletions(-) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index 1cd6229458..f53d372ee9 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -342,67 +342,16 @@ sub article_request_type_for_items { =head3 article_requests -my @requests = $biblio->article_requests + my $article_requests = $biblio->article_requests -Returns the article requests associated with this Biblio +Returns the article requests associated with this biblio =cut sub article_requests { - my ( $self, $borrower ) = @_; - - $self->{_article_requests} ||= Koha::ArticleRequests->search( { biblionumber => $self->biblionumber() } ); - - return wantarray ? $self->{_article_requests}->as_list : $self->{_article_requests}; -} - -=head3 article_requests_current - -my @requests = $biblio->article_requests_current - -Returns the article requests associated with this Biblio that are incomplete - -=cut - -sub article_requests_current { - my ( $self, $borrower ) = @_; - - $self->{_article_requests_current} ||= Koha::ArticleRequests->search( - { - biblionumber => $self->biblionumber(), - -or => [ - { status => Koha::ArticleRequest::Status::Requested }, - { status => Koha::ArticleRequest::Status::Pending }, - { status => Koha::ArticleRequest::Status::Processing } - ] - } - ); - - return wantarray ? $self->{_article_requests_current}->as_list : $self->{_article_requests_current}; -} - -=head3 article_requests_finished - -my @requests = $biblio->article_requests_finished - -Returns the article requests associated with this Biblio that are completed - -=cut - -sub article_requests_finished { - my ( $self, $borrower ) = @_; - - $self->{_article_requests_finished} ||= Koha::ArticleRequests->search( - { - biblionumber => $self->biblionumber(), - -or => [ - { status => Koha::ArticleRequest::Status::Completed }, - { status => Koha::ArticleRequest::Status::Canceled } - ] - } - ); + my ( $self ) = @_; - return wantarray ? $self->{_article_requests_finished}->as_list : $self->{_article_requests_finished}; + return Koha::ArticleRequests->_new_from_dbic( scalar $self->_result->article_requests ); } =head3 items diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/request-article.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/request-article.tt index 7c2ea0fdb1..5ab9d5a079 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/request-article.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/request-article.tt @@ -1,5 +1,6 @@ [% USE raw %] [% USE Asset %] +[% USE Context %] [% USE KohaDates %] [% USE Branches %] [% USE ItemTypes %] @@ -284,7 +285,8 @@ [% END %] - [% IF biblio.article_requests_current && !patron %] + [% SET biblio_current_article_requests = Context.Scalar( Context.Scalar( biblio, 'article_requests' ), 'filter_by_current' ) %] + [% IF biblio_current_article_requests.count > 0 && !patron %]
Current article requests @@ -307,7 +309,7 @@   - [% FOREACH ar IN biblio.article_requests_current %] + [% FOREACH ar IN biblio_current_article_requests %] [% ar.created_on | $KohaDates %] [% ar.borrower.firstname | html %] [% ar.borrower.surname | html %] diff --git a/t/db_dependent/ArticleRequests.t b/t/db_dependent/ArticleRequests.t index acc5704121..c1e16d9424 100755 --- a/t/db_dependent/ArticleRequests.t +++ b/t/db_dependent/ArticleRequests.t @@ -19,7 +19,7 @@ use Modern::Perl; use POSIX qw(strftime); -use Test::More tests => 45; +use Test::More tests => 36; use Test::MockModule; use t::lib::TestBuilder; @@ -110,28 +110,6 @@ is( $article_request->biblio->id, $biblio->id, '$ar->biblio() gets correspondi is( $article_request->item->id, $item->id, '$ar->item() gets corresponding Koha::Item object' ); is( $article_request->borrower->id, $patron->id, '$ar->borrower() gets corresponding Koha::Patron object' ); -my $ar = $biblio->article_requests(); -is( ref($ar), 'Koha::ArticleRequests', '$biblio->article_requests returns Koha::ArticleRequests object' ); -is( $ar->next->id, $article_request->id, 'Returned article request matches' ); - -is( $biblio->article_requests_current()->count(), 1, 'Open request returned for article_requests_current' ); -$article_request->process(); -is( $biblio->article_requests_current()->count(), 1, 'Processing request returned for article_requests_current' ); -$article_request->complete(); -is( $biblio->article_requests_current()->count(), 0, 'Completed request not returned for article_requests_current' ); -$article_request->cancel(); -is( $biblio->article_requests_current()->count(), 0, 'Canceled request not returned for article_requests_current' ); - -$article_request->status(Koha::ArticleRequest::Status::Pending); -$article_request->store(); - -is( $biblio->article_requests_finished()->count(), 0, 'Open request returned for article_requests_finished' ); -$article_request->process(); -is( $biblio->article_requests_finished()->count(), 0, 'Processing request returned for article_requests_finished' ); -$article_request->complete(); -$article_request->cancel(); -is( $biblio->article_requests_finished()->count(), 1, 'Canceled request not returned for article_requests_finished' ); - my $rule = Koha::CirculationRules->set_rule( { categorycode => undef, diff --git a/t/db_dependent/Koha/Biblio.t b/t/db_dependent/Koha/Biblio.t index a9ed0ad98d..893608d933 100755 --- a/t/db_dependent/Koha/Biblio.t +++ b/t/db_dependent/Koha/Biblio.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 15; +use Test::More tests => 16; use C4::Biblio qw( AddBiblio ModBiblio ); use Koha::Database; @@ -690,3 +690,37 @@ subtest 'host_items() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'article_requests() tests' => sub { + + plan tests => 3; + + $schema->storage->txn_begin; + + my $item = $builder->build_sample_item; + my $biblio = $item->biblio; + + my $article_requests = $biblio->article_requests; + is( ref($article_requests), 'Koha::ArticleRequests', + 'In scalar context, type is correct' ); + is( $article_requests->count, 0, 'No article requests' ); + + foreach my $i ( 0 .. 3 ) { + + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + + Koha::ArticleRequest->new( + { + borrowernumber => $patron->id, + biblionumber => $biblio->id, + itemnumber => $item->id, + title => $biblio->title, + } + )->request; + } + + $article_requests = $biblio->article_requests; + is( $article_requests->count, 4, '4 article requests' ); + + $schema->storage->txn_rollback; +}; -- 2.39.2