View | Details | Raw Unified | Return to bug 28965
Collapse All | Expand All

(-)a/Koha/Virtualshelves.pm (-1 / +35 lines)
Lines 17-24 package Koha::Virtualshelves; Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
21
use Koha::Database;
20
use Koha::Database;
21
use Koha::Exceptions;
22
22
23
use Koha::Patrons;
23
use Koha::Patrons;
24
use Koha::Virtualshelf;
24
use Koha::Virtualshelf;
Lines 246-251 sub get_shelves_containing_record { Link Here
246
    );
246
    );
247
}
247
}
248
248
249
=head3 filter_by_public
250
251
    my $public_lists = $lists->filter_by_public;
252
253
Returns a resultset of lists marked as public.
254
255
=cut
256
257
sub filter_by_public {
258
    my ($self) = @_;
259
260
    return $self->search({ public => 1 });
261
}
262
263
=head3 filter_by_readable
264
265
    my $readable_lists = $lists->filter_by_readable({ patron_id => $patron->id });
266
267
Returns a resultset of lists marked as public.
268
269
=cut
270
271
sub filter_by_readable {
272
    my ( $self, $params ) = @_;
273
274
    Koha::Exceptions::MissingParameter->throw("Mandatory patron_id parameter missing")
275
      unless $params->{patron_id};
276
277
    return $self->search( { '-or' => { public => 1, owner => $params->{patron_id} } } );
278
}
279
280
281
=head2 Internal methods
282
249
=head3 _type
283
=head3 _type
250
284
251
=cut
285
=cut
(-)a/t/db_dependent/Virtualshelves.t (-1 / +122 lines)
Lines 1-7 Link Here
1
#!/usr/bin/perl
1
#!/usr/bin/perl
2
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
3
use Modern::Perl;
18
use Modern::Perl;
4
use Test::More tests => 7;
19
use Test::More tests => 7;
20
use Test::Exception;
21
5
use DateTime::Duration;
22
use DateTime::Duration;
6
23
7
use C4::Context;
24
use C4::Context;
Lines 762-767 subtest 'cannot_be_transferred' => sub { Link Here
762
779
763
$schema->storage->txn_rollback;
780
$schema->storage->txn_rollback;
764
781
782
subtest 'filter_by_public() tests' => sub {
783
784
    plan tests => 4;
785
786
    $schema->storage->txn_begin;
787
788
    my $list_1 = $builder->build_object(
789
        {
790
            class => 'Koha::Virtualshelves',
791
            value => { public => 0 }
792
        }
793
    );
794
    my $list_2 = $builder->build_object(
795
        {
796
            class => 'Koha::Virtualshelves',
797
            value => { public => 1 }
798
        }
799
    );
800
    my $list_3 = $builder->build_object(
801
        {
802
            class => 'Koha::Virtualshelves',
803
            value => { public => 1 }
804
        }
805
    );
806
807
    my $lists = Koha::Virtualshelves->search( { shelfnumber => [ $list_1->id, $list_2->id, $list_3->id ] } );
808
809
    is( $lists->count, 3, 'Our three lists are returned' );
810
    $lists = $lists->filter_by_public;
811
812
    is( $lists->count, 2, 'Our two public lists are returned' );
813
814
    while ( my $list = $lists->next ) {
815
        ok( $list->public, 'Only public lists in the resultset' );
816
    }
817
818
    $schema->storage->txn_rollback;
819
};
820
821
subtest 'filter_by_readable() tests' => sub {
822
823
    plan tests => 6;
824
825
    $schema->storage->txn_begin;
826
827
    my $patron_1 = $builder->build_object( { class => 'Koha::Patrons' } );
828
    my $patron_2 = $builder->build_object( { class => 'Koha::Patrons' } );
829
830
    my $list_1 = $builder->build_object(
831
        {
832
            class => 'Koha::Virtualshelves',
833
            value => {
834
                public => 0,
835
                owner  => $patron_1->id,
836
            }
837
        }
838
    );
839
    my $list_2 = $builder->build_object(
840
        {
841
            class => 'Koha::Virtualshelves',
842
            value => {
843
                public => 1,
844
                owner  => $patron_1->id,
845
            }
846
        }
847
    );
848
    my $list_3 = $builder->build_object(
849
        {
850
            class => 'Koha::Virtualshelves',
851
            value => {
852
                public => 0,
853
                owner  => $patron_2->id,
854
            }
855
        }
856
    );
857
    my $list_4 = $builder->build_object(
858
        {
859
            class => 'Koha::Virtualshelves',
860
            value => {
861
                public => 1,
862
                owner  => $patron_2->id,
863
            }
864
        }
865
    );
866
867
    my $lists =
868
        Koha::Virtualshelves->search( { shelfnumber => [ $list_1->id, $list_2->id, $list_3->id, $list_4->id ] } );
869
870
    is( $lists->count, 4, 'Our four lists are returned' );
871
872
    throws_ok { $lists->filter_by_readable; }
873
    'Koha::Exceptions::MissingParameter',
874
        'Exception thrown on missing';
875
876
    $lists = $lists->filter_by_readable( { patron_id => $patron_1->id } );
877
878
    is( $lists->count, 3, 'Three lists are returned' );
879
880
    while ( my $list = $lists->next ) {
881
        ok( $list->owner == $patron_1->id || $list->public, 'Only public or self lists in the resultset' );
882
    }
883
884
    $schema->storage->txn_rollback;
885
};
886
765
sub teardown {
887
sub teardown {
766
    $dbh->do(q|DELETE FROM virtualshelfshares|);
888
    $dbh->do(q|DELETE FROM virtualshelfshares|);
767
    $dbh->do(q|DELETE FROM virtualshelfcontents|);
889
    $dbh->do(q|DELETE FROM virtualshelfcontents|);
768
- 

Return to bug 28965