From 8ef857af06e915fa88360dd5b6c9febc70ee1f3a Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Tue, 23 Aug 2016 17:42:44 +0000 Subject: [PATCH] Bug 12022 - Independent branches does not isolate pending and approved comments by branch. Test Plan: 1) Create some comments for two libraries 2) Log in as non-superlibrarian 3) Set the logged in library to one of those two libraries 4) View comments, not you only see the comments from patrons whose home library matches the logged in library 5) Log in as a superlibrarian 6) Note you see all the comments reguardless of the patron's home library --- C4/Review.pm | 19 +++++++++++++++++-- t/db_dependent/Review.t | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/C4/Review.pm b/C4/Review.pm index 5d56396..c35709b 100644 --- a/C4/Review.pm +++ b/C4/Review.pm @@ -172,9 +172,24 @@ $offset position and the ($offset + $row_count) position. sub getallreviews { my ( $status, $offset, $row_count ) = @_; - my @params = ( $status, ( $offset ? $offset : 0 ), ( $row_count ? $row_count : 20 ) ); + my @params; + push( @params, $status ); + + my $library_limit = C4::Context->IsSuperLibrarian() ? q{} : q{ AND borrowers.branchcode = ?}; + push( @params, C4::Context->userenv->{'branch'} ) if ( $library_limit && C4::Context->userenv ); + + push( @params, $offset ? $offset : 0 ); + push( @params, $row_count ? $row_count : 20 ); + my $dbh = C4::Context->dbh; - my $query = "SELECT * FROM reviews WHERE approved=? order by datereviewed desc LIMIT ?, ?"; + my $query = qq{ + SELECT reviews.* FROM reviews + LEFT JOIN borrowers USING ( borrowernumber ) + WHERE approved = ? + $library_limit + ORDER BY datereviewed DESC + LIMIT ?, ? + }; my $sth = $dbh->prepare($query); $sth->execute(@params); return $sth->fetchall_arrayref( {} ); diff --git a/t/db_dependent/Review.t b/t/db_dependent/Review.t index 248fe3e..0762973 100644 --- a/t/db_dependent/Review.t +++ b/t/db_dependent/Review.t @@ -18,11 +18,12 @@ use Modern::Perl; -use Test::More tests => 117; +use Test::More tests => 118; use t::lib::TestBuilder; use Koha::Database; use Time::Piece; +use Test::MockModule; BEGIN { use_ok('C4::Biblio'); @@ -58,6 +59,19 @@ my $builder = t::lib::TestBuilder->new; # ---------- Some borrowers for testing ------------------- my $categorycode = $builder->build({ source => 'Category' })->{ categorycode }; my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode }; +my $branchcode2 = $builder->build({ source => 'Branch' })->{ branchcode }; + +my $context = new Test::MockModule('C4::Context'); +$context->mock( + 'userenv', + sub { + return { + flags => 0, + id => 'my_userid', + branch => $branchcode, + }; + } +); my $b1 = Koha::Patron->new( { surname => 'Borrower 1', @@ -246,6 +260,30 @@ $reviews = getallreviews( $status, $offset, $row_count ); is( @$reviews, 1, 'There is only 1 Review here' ); is_deeply( $reviews->[0], $review2, 'We have only Review2' ); +$context->mock( + 'userenv', + sub { + return { + flags => 0, + id => 'my_userid', + branch => $branchcode2, + }; + } +); +my $no_reviews = getallreviews($status); +is( @{$no_reviews}, 0, "No reviews returned for unused branchcode" ); +$context->mock( + 'userenv', + sub { + return { + flags => 0, + id => 'my_userid', + branch => $branchcode, + }; + } +); + + # ---------- Testing numberofreviews ---------------------- $status = 0; $count = numberofreviews($status); -- 2.1.4