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 763-768 subtest 'cannot_be_transferred' => sub { Link Here
763
780
764
$schema->storage->txn_rollback;
781
$schema->storage->txn_rollback;
765
782
783
subtest 'filter_by_public() tests' => sub {
784
785
    plan tests => 4;
786
787
    $schema->storage->txn_begin;
788
789
    my $list_1 = $builder->build_object(
790
        {
791
            class => 'Koha::Virtualshelves',
792
            value => { public => 0 }
793
        }
794
    );
795
    my $list_2 = $builder->build_object(
796
        {
797
            class => 'Koha::Virtualshelves',
798
            value => { public => 1 }
799
        }
800
    );
801
    my $list_3 = $builder->build_object(
802
        {
803
            class => 'Koha::Virtualshelves',
804
            value => { public => 1 }
805
        }
806
    );
807
808
    my $lists = Koha::Virtualshelves->search( { shelfnumber => [ $list_1->id, $list_2->id, $list_3->id ] } );
809
810
    is( $lists->count, 3, 'Our three lists are returned' );
811
    $lists = $lists->filter_by_public;
812
813
    is( $lists->count, 2, 'Our two public lists are returned' );
814
815
    while ( my $list = $lists->next ) {
816
        ok( $list->public, 'Only public lists in the resultset' );
817
    }
818
819
    $schema->storage->txn_rollback;
820
};
821
822
subtest 'filter_by_readable() tests' => sub {
823
824
    plan tests => 6;
825
826
    $schema->storage->txn_begin;
827
828
    my $patron_1 = $builder->build_object( { class => 'Koha::Patrons' } );
829
    my $patron_2 = $builder->build_object( { class => 'Koha::Patrons' } );
830
831
    my $list_1 = $builder->build_object(
832
        {
833
            class => 'Koha::Virtualshelves',
834
            value => {
835
                public => 0,
836
                owner  => $patron_1->id,
837
            }
838
        }
839
    );
840
    my $list_2 = $builder->build_object(
841
        {
842
            class => 'Koha::Virtualshelves',
843
            value => {
844
                public => 1,
845
                owner  => $patron_1->id,
846
            }
847
        }
848
    );
849
    my $list_3 = $builder->build_object(
850
        {
851
            class => 'Koha::Virtualshelves',
852
            value => {
853
                public => 0,
854
                owner  => $patron_2->id,
855
            }
856
        }
857
    );
858
    my $list_4 = $builder->build_object(
859
        {
860
            class => 'Koha::Virtualshelves',
861
            value => {
862
                public => 1,
863
                owner  => $patron_2->id,
864
            }
865
        }
866
    );
867
868
    my $lists =
869
        Koha::Virtualshelves->search( { shelfnumber => [ $list_1->id, $list_2->id, $list_3->id, $list_4->id ] } );
870
871
    is( $lists->count, 4, 'Our four lists are returned' );
872
873
    throws_ok { $lists->filter_by_readable; }
874
    'Koha::Exceptions::MissingParameter',
875
        'Exception thrown on missing';
876
877
    $lists = $lists->filter_by_readable( { patron_id => $patron_1->id } );
878
879
    is( $lists->count, 3, 'Three lists are returned' );
880
881
    while ( my $list = $lists->next ) {
882
        ok( $list->owner == $patron_1->id || $list->public, 'Only public or self lists in the resultset' );
883
    }
884
885
    $schema->storage->txn_rollback;
886
};
887
766
sub teardown {
888
sub teardown {
767
    $dbh->do(q|DELETE FROM virtualshelfshares|);
889
    $dbh->do(q|DELETE FROM virtualshelfshares|);
768
    $dbh->do(q|DELETE FROM virtualshelfcontents|);
890
    $dbh->do(q|DELETE FROM virtualshelfcontents|);
769
- 

Return to bug 28965