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 <martin.renvoize@ptfs-europe.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
This commit is contained in:
Tomas Cohen Arazi 2021-09-07 18:11:25 -03:00 committed by Katrin Fischer
parent d5cecc5a07
commit 9c5d7edf4f
No known key found for this signature in database
GPG Key ID: 0EF6E2C03357A834
2 changed files with 157 additions and 1 deletions

View File

@ -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

View File

@ -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 <http://www.gnu.org/licenses>.
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|);