From 15664e4080c642bb30e474b4c68e034e14002a34 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 --- Koha/Virtualshelves.pm | 43 ++++++++-- t/db_dependent/Virtualshelves.t | 143 +++++++++++++++++++++++++++++++- 2 files changed, 180 insertions(+), 6 deletions(-) diff --git a/Koha/Virtualshelves.pm b/Koha/Virtualshelves.pm index df75c3b59a..4eef3a8a12 100644 --- a/Koha/Virtualshelves.pm +++ b/Koha/Virtualshelves.pm @@ -17,9 +17,7 @@ package Koha::Virtualshelves; use Modern::Perl; - -use Koha::Database; - +use Koha::Exceptions; use Koha::Virtualshelf; use base qw(Koha::Objects); @@ -30,14 +28,39 @@ Koha::Virtualshelf - Koha Virtualshelf Object class =head1 API -=head2 Class Methods +=head2 Class methods + +=head3 filter_by_public + + my $public_lists = $lists->filter_by_public; + +Returns a resultset of lists marked as public. =cut -=head3 type +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} } } ); +} + sub get_private_shelves { my ( $self, $params ) = @_; my $page = $params->{page}; @@ -159,10 +182,20 @@ sub get_shelves_containing_record { ); } +=head2 Internal methods + +=head3 _type + +=cut + sub _type { return 'Virtualshelve'; } +=head3 object_class + +=cut + sub object_class { return 'Koha::Virtualshelf'; } diff --git a/t/db_dependent/Virtualshelves.t b/t/db_dependent/Virtualshelves.t index daba7766b3..af0baea2be 100755 --- a/t/db_dependent/Virtualshelves.t +++ b/t/db_dependent/Virtualshelves.t @@ -1,7 +1,25 @@ #!/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 => 6; + +use Test::More tests => 8; +use Test::Exception; + use DateTime::Duration; use C4::Context; @@ -494,6 +512,129 @@ subtest 'Get shelves containing biblios' => sub { teardown(); }; +$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 => 7; + + $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'; + + is( "$@", 'Mandatory patron_id parameter missing', 'Expected message in exception' ); + + $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.33.0