Bug 18403: Article requests
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Fri, 7 Apr 2017 20:53:35 +0000 (17:53 -0300)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Mon, 12 Feb 2018 18:41:41 +0000 (15:41 -0300)
Same as previously but for article requests.

Test plan:
Test article requests and make sure you do not need the requests for
patrons that
are attached to a group that is not part of your library's group

Signed-off-by: Signed-off-by: Jon McGowan <jon.mcgowan@ptfs-europe.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

Koha/ArticleRequests.pm
mainpage.pl
t/db_dependent/ArticleRequests.t

index bc47789..27be468 100644 (file)
@@ -38,6 +38,29 @@ Koha::ArticleRequests - Koha ArticleRequests Object class
 
 =cut
 
+=head3 search_limited
+
+my $article_requests = Koha::ArticleRequests->search_limited( $params, $attributes );
+
+Search for article requests according to logged in patron restrictions
+
+=cut
+
+sub search_limited {
+    my ( $self, $params, $attributes ) = @_;
+
+    my $userenv = C4::Context->userenv;
+    my @restricted_branchcodes;
+    if ( $userenv ) {
+        my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
+        @restricted_branchcodes = $logged_in_user->libraries_where_can_see_patrons;
+    }
+    # TODO This 'borrowernumber' relation name is confusing and needs to be renamed
+    $params->{'borrowernumber.branchcode'} = { -in => \@restricted_branchcodes } if @restricted_branchcodes;
+    $attributes->{join} = 'borrowernumber';
+    return $self->search( $params, $attributes );
+}
+
 =head3 pending
 
 =cut
@@ -45,8 +68,8 @@ Koha::ArticleRequests - Koha ArticleRequests Object class
 sub pending {
     my ( $self, $branchcode ) = @_;
     my $params = { status => Koha::ArticleRequest::Status::Pending };
-    $params->{branchcode} = $branchcode if $branchcode;
-    return Koha::ArticleRequests->search( $params );
+    $params->{'me.branchcode'} = $branchcode if $branchcode;
+    return Koha::ArticleRequests->search_limited( $params );
 }
 
 =head3 processing
@@ -56,8 +79,8 @@ sub pending {
 sub processing {
     my ( $self, $branchcode ) = @_;
     my $params = { status => Koha::ArticleRequest::Status::Processing };
-    $params->{branchcode} = $branchcode if $branchcode;
-    return Koha::ArticleRequests->search( $params );
+    $params->{'me.branchcode'} = $branchcode if $branchcode;
+    return Koha::ArticleRequests->search_limited( $params );
 }
 
 =head3 completed
@@ -67,8 +90,8 @@ sub processing {
 sub completed {
     my ( $self, $branchcode ) = @_;
     my $params = { status => Koha::ArticleRequest::Status::Completed };
-    $params->{branchcode} = $branchcode if $branchcode;
-    return Koha::ArticleRequests->search( $params );
+    $params->{'me.branchcode'} = $branchcode if $branchcode;
+    return Koha::ArticleRequests->search_limited( $params );
 }
 
 =head3 canceled
@@ -78,8 +101,8 @@ sub completed {
 sub canceled {
     my ( $self, $branchcode ) = @_;
     my $params = { status => Koha::ArticleRequest::Status::Canceled };
-    $params->{branchcode} = $branchcode if $branchcode;
-    return Koha::ArticleRequests->search( $params );
+    $params->{'me.branchcode'} = $branchcode if $branchcode;
+    return Koha::ArticleRequests->search_limited( $params );
 }
 
 =head3 _type
index f4105be..47f56ad 100755 (executable)
@@ -68,12 +68,12 @@ my $pendingtags        = get_count_by_tag_status(0);
 my $pendingsuggestions = CountSuggestion("ASKED");
 my $pending_borrower_modifications = Koha::Patron::Modifications->pending_count( $branch );
 my $pending_discharge_requests = Koha::Patron::Discharge::count({ pending => 1 });
-my $pending_article_requests = Koha::ArticleRequests->count(
+my $pending_article_requests = Koha::ArticleRequests->search_limited(
     {
         status => Koha::ArticleRequest::Status::Pending,
         $branch ? ( branchcode => $branch ) : (),
     }
-);
+)->count;
 
 $template->param(
     pendingcomments                => $pendingcomments,
index 4d56315..7c99657 100755 (executable)
@@ -19,7 +19,7 @@ use Modern::Perl;
 
 use POSIX qw(strftime);
 
-use Test::More tests => 54;
+use Test::More tests => 55;
 
 use t::lib::TestBuilder;
 
@@ -27,7 +27,8 @@ use Koha::Database;
 use Koha::Biblio;
 use Koha::Notice::Messages;
 use Koha::Patron;
-use Koha::Library;
+
+use t::lib::TestBuilder;
 
 BEGIN {
     use_ok('Koha::ArticleRequest');
@@ -70,9 +71,14 @@ my $patron   = Koha::Patron->new(
     {
         categorycode => $category->{categorycode},
         branchcode   => $branch->{branchcode},
+        flags        => 1,# superlibrarian
     }
 )->store();
 ok( $patron->id, 'Koha::Patron created' );
+my $patron_2 = $builder->build({ source => 'Borrower', value => { flags => 0 } });
+$patron_2 = Koha::Patrons->find( $patron_2->{borrowernumber} );
+
+my $nb_article_requests = Koha::ArticleRequests->count;
 
 # store
 Koha::Notice::Messages->delete;
@@ -198,4 +204,30 @@ ok( !$item->can_article_request($patron),   'Item is not requestable with rule t
 is( $item->article_request_type($patron), 'no', 'Item article request type is no' );
 $rule->delete();
 
+subtest 'search_limited' => sub {
+    plan tests => 4;
+    C4::Context->_new_userenv('xxx');
+    my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1' } )->store;
+    my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store;
+    Koha::Library::Group->new({ parent_id => $group_1->id,  branchcode => $patron->branchcode })->store();
+    Koha::Library::Group->new({ parent_id => $group_2->id,  branchcode => $patron_2->branchcode })->store();
+    set_logged_in_user( $patron ); # Is superlibrarian
+    is( Koha::ArticleRequests->count, $nb_article_requests + 1, 'Koha::ArticleRequests should return all article requests' );
+    is( Koha::ArticleRequests->search_limited->count, $nb_article_requests + 1, 'Koha::ArticleRequests->search_limited should return all article requests for superlibrarian' );
+    set_logged_in_user( $patron_2 ); # Is restricted
+    is( Koha::ArticleRequests->count, $nb_article_requests + 1, 'Koha::ArticleRequests should return all article requests' );
+    is( Koha::ArticleRequests->search_limited->count, $nb_article_requests, 'Koha::ArticleRequests->search_limited should not return all article requests for restricted patron' );
+};
+
 $schema->storage->txn_rollback();
+
+sub set_logged_in_user {
+    my ($patron) = @_;
+    C4::Context->set_userenv(
+        $patron->borrowernumber, $patron->userid,
+        $patron->cardnumber,     'firstname',
+        'surname',               $patron->library->branchcode,
+        'Midway Public Library', $patron->flags,
+        '',                      ''
+    );
+}