From 5193016fe6b739bb9e5d294ec49857111545de54 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 7 Sep 2021 18:11:25 -0300 Subject: [PATCH] Bug 28965: filter_by_public() and filter_by_readable() This patch adds helper methods for lists resultsets. Tests are added for their behavior. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/Virtualshelves.t => SUCCESS: Tests pass! 3. Sign off :-D Signed-off-by: Martin Renvoize Signed-off-by: Kyle M Hall --- Koha/Virtualshelves.pm | 36 +++++++++- t/db_dependent/Virtualshelves.t | 122 ++++++++++++++++++++++++++++++++ 2 files changed, 157 insertions(+), 1 deletion(-) diff --git a/Koha/Virtualshelves.pm b/Koha/Virtualshelves.pm index 9b71a016bb8..384ed45ce64 100644 --- a/Koha/Virtualshelves.pm +++ b/Koha/Virtualshelves.pm @@ -17,8 +17,8 @@ package Koha::Virtualshelves; use Modern::Perl; - use Koha::Database; +use Koha::Exceptions; use Koha::Patrons; use Koha::Virtualshelf; @@ -246,6 +246,40 @@ sub get_shelves_containing_record { ); } +=head3 filter_by_public + + my $public_lists = $lists->filter_by_public; + +Returns a resultset of lists marked as public. + +=cut + +sub filter_by_public { + my ($self) = @_; + + return $self->search({ public => 1 }); +} + +=head3 filter_by_readable + + my $readable_lists = $lists->filter_by_readable({ patron_id => $patron->id }); + +Returns a resultset of lists marked as public. + +=cut + +sub filter_by_readable { + my ( $self, $params ) = @_; + + Koha::Exceptions::MissingParameter->throw("Mandatory patron_id parameter missing") + unless $params->{patron_id}; + + return $self->search( { '-or' => { public => 1, owner => $params->{patron_id} } } ); +} + + +=head2 Internal methods + =head3 _type =cut diff --git a/t/db_dependent/Virtualshelves.t b/t/db_dependent/Virtualshelves.t index 0b8c5bf5e77..5861e1dbb35 100755 --- a/t/db_dependent/Virtualshelves.t +++ b/t/db_dependent/Virtualshelves.t @@ -1,7 +1,24 @@ #!/usr/bin/perl +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + use Modern::Perl; use Test::More tests => 7; +use Test::Exception; + use DateTime::Duration; use C4::Context; @@ -763,6 +780,111 @@ subtest 'cannot_be_transferred' => sub { $schema->storage->txn_rollback; +subtest 'filter_by_public() tests' => sub { + + plan tests => 4; + + $schema->storage->txn_begin; + + my $list_1 = $builder->build_object( + { + class => 'Koha::Virtualshelves', + value => { public => 0 } + } + ); + my $list_2 = $builder->build_object( + { + class => 'Koha::Virtualshelves', + value => { public => 1 } + } + ); + my $list_3 = $builder->build_object( + { + class => 'Koha::Virtualshelves', + value => { public => 1 } + } + ); + + my $lists = Koha::Virtualshelves->search( { shelfnumber => [ $list_1->id, $list_2->id, $list_3->id ] } ); + + is( $lists->count, 3, 'Our three lists are returned' ); + $lists = $lists->filter_by_public; + + is( $lists->count, 2, 'Our two public lists are returned' ); + + while ( my $list = $lists->next ) { + ok( $list->public, 'Only public lists in the resultset' ); + } + + $schema->storage->txn_rollback; +}; + +subtest 'filter_by_readable() tests' => sub { + + plan tests => 6; + + $schema->storage->txn_begin; + + my $patron_1 = $builder->build_object( { class => 'Koha::Patrons' } ); + my $patron_2 = $builder->build_object( { class => 'Koha::Patrons' } ); + + my $list_1 = $builder->build_object( + { + class => 'Koha::Virtualshelves', + value => { + public => 0, + owner => $patron_1->id, + } + } + ); + my $list_2 = $builder->build_object( + { + class => 'Koha::Virtualshelves', + value => { + public => 1, + owner => $patron_1->id, + } + } + ); + my $list_3 = $builder->build_object( + { + class => 'Koha::Virtualshelves', + value => { + public => 0, + owner => $patron_2->id, + } + } + ); + my $list_4 = $builder->build_object( + { + class => 'Koha::Virtualshelves', + value => { + public => 1, + owner => $patron_2->id, + } + } + ); + + my $lists = + Koha::Virtualshelves->search( { shelfnumber => [ $list_1->id, $list_2->id, $list_3->id, $list_4->id ] } ); + + is( $lists->count, 4, 'Our four lists are returned' ); + + throws_ok { $lists->filter_by_readable; } + 'Koha::Exceptions::MissingParameter', + 'Exception thrown on missing'; + + $lists = $lists->filter_by_readable( { patron_id => $patron_1->id } ); + + is( $lists->count, 3, 'Three lists are returned' ); + + while ( my $list = $lists->next ) { + ok( $list->owner == $patron_1->id || $list->public, 'Only public or self lists in the resultset' ); + } + + $schema->storage->txn_rollback; +}; + sub teardown { $dbh->do(q|DELETE FROM virtualshelfshares|); $dbh->do(q|DELETE FROM virtualshelfcontents|); -- 2.39.5 (Apple Git-154)