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

(-)a/Koha/Virtualshelves.pm (-5 / +38 lines)
Lines 17-25 package Koha::Virtualshelves; Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
20
use Koha::Exceptions;
21
use Koha::Database;
22
23
use Koha::Virtualshelf;
21
use Koha::Virtualshelf;
24
22
25
use base qw(Koha::Objects);
23
use base qw(Koha::Objects);
Lines 30-43 Koha::Virtualshelf - Koha Virtualshelf Object class Link Here
30
28
31
=head1 API
29
=head1 API
32
30
33
=head2 Class Methods
31
=head2 Class methods
32
33
=head3 filter_by_public
34
35
    my $public_lists = $lists->filter_by_public;
36
37
Returns a resultset of lists marked as public.
34
38
35
=cut
39
=cut
36
40
37
=head3 type
41
sub filter_by_public {
42
    my ($self) = @_;
43
44
    return $self->search({ public => 1 });
45
}
46
47
=head3 filter_by_readable
48
49
    my $readable_lists = $lists->filter_by_readable({ patron_id => $patron->id });
50
51
Returns a resultset of lists marked as public.
38
52
39
=cut
53
=cut
40
54
55
sub filter_by_readable {
56
    my ( $self, $params ) = @_;
57
58
    Koha::Exceptions::MissingParameter->throw("Mandatory patron_id parameter missing")
59
      unless $params->{patron_id};
60
61
    return $self->search( { '-or' => { public => 1, owner => $params->{patron_id} } } );
62
}
63
41
sub get_private_shelves {
64
sub get_private_shelves {
42
    my ( $self, $params ) = @_;
65
    my ( $self, $params ) = @_;
43
    my $page = $params->{page};
66
    my $page = $params->{page};
Lines 159-168 sub get_shelves_containing_record { Link Here
159
    );
182
    );
160
}
183
}
161
184
185
=head2 Internal methods
186
187
=head3 _type
188
189
=cut
190
162
sub _type {
191
sub _type {
163
    return 'Virtualshelve';
192
    return 'Virtualshelve';
164
}
193
}
165
194
195
=head3 object_class
196
197
=cut
198
166
sub object_class {
199
sub object_class {
167
    return 'Koha::Virtualshelf';
200
    return 'Koha::Virtualshelf';
168
}
201
}
(-)a/t/db_dependent/Virtualshelves.t (-2 / +142 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 => 6;
19
20
use Test::More tests => 8;
21
use Test::Exception;
22
5
use DateTime::Duration;
23
use DateTime::Duration;
6
24
7
use C4::Context;
25
use C4::Context;
Lines 494-499 subtest 'Get shelves containing biblios' => sub { Link Here
494
    teardown();
512
    teardown();
495
};
513
};
496
514
515
$schema->storage->txn_rollback;
516
517
subtest 'filter_by_public() tests' => sub {
518
519
    plan tests => 4;
520
521
    $schema->storage->txn_begin;
522
523
    my $list_1 = $builder->build_object(
524
        {
525
            class => 'Koha::Virtualshelves',
526
            value => {
527
                public => 0
528
            }
529
        }
530
    );
531
    my $list_2 = $builder->build_object(
532
        {
533
            class => 'Koha::Virtualshelves',
534
            value => {
535
                public => 1
536
            }
537
        }
538
    );
539
    my $list_3 = $builder->build_object(
540
        {
541
            class => 'Koha::Virtualshelves',
542
            value => {
543
                public => 1
544
            }
545
        }
546
    );
547
548
    my $lists = Koha::Virtualshelves->search(
549
        {
550
            shelfnumber => [ $list_1->id, $list_2->id, $list_3->id ]
551
        }
552
    );
553
554
    is( $lists->count, 3, 'Our three lists are returned' );
555
    $lists = $lists->filter_by_public;
556
557
    is( $lists->count, 2, 'Our two public lists are returned' );
558
559
    while ( my $list = $lists->next ) {
560
        ok( $list->public, 'Only public lists in the resultset' );
561
    }
562
563
    $schema->storage->txn_rollback;
564
};
565
566
subtest 'filter_by_readable() tests' => sub {
567
568
    plan tests => 7;
569
570
    $schema->storage->txn_begin;
571
572
    my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
573
    my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
574
575
    my $list_1 = $builder->build_object(
576
        {
577
            class => 'Koha::Virtualshelves',
578
            value => {
579
                public => 0,
580
                owner  => $patron_1->id,
581
            }
582
        }
583
    );
584
    my $list_2 = $builder->build_object(
585
        {
586
            class => 'Koha::Virtualshelves',
587
            value => {
588
                public => 1,
589
                owner  => $patron_1->id,
590
            }
591
        }
592
    );
593
    my $list_3 = $builder->build_object(
594
        {
595
            class => 'Koha::Virtualshelves',
596
            value => {
597
                public => 0,
598
                owner  => $patron_2->id,
599
            }
600
        }
601
    );
602
    my $list_4 = $builder->build_object(
603
        {
604
            class => 'Koha::Virtualshelves',
605
            value => {
606
                public => 1,
607
                owner  => $patron_2->id,
608
            }
609
        }
610
    );
611
612
    my $lists = Koha::Virtualshelves->search(
613
        {
614
            shelfnumber => [ $list_1->id, $list_2->id, $list_3->id, $list_4->id ]
615
        }
616
    );
617
618
    is( $lists->count, 4, 'Our four lists are returned' );
619
620
    throws_ok
621
        { $lists->filter_by_readable; }
622
        'Koha::Exceptions::MissingParameter',
623
        'Exception thrown on missing';
624
625
    is( "$@", 'Mandatory patron_id parameter missing', 'Expected message in exception' );
626
627
    $lists = $lists->filter_by_readable({ patron_id => $patron_1->id });
628
629
    is( $lists->count, 3, 'Three lists are returned' );
630
631
    while ( my $list = $lists->next ) {
632
        ok( $list->owner == $patron_1->id || $list->public, 'Only public or self lists in the resultset' );
633
    }
634
635
    $schema->storage->txn_rollback;
636
};
637
497
sub teardown {
638
sub teardown {
498
    $dbh->do(q|DELETE FROM virtualshelfshares|);
639
    $dbh->do(q|DELETE FROM virtualshelfshares|);
499
    $dbh->do(q|DELETE FROM virtualshelfcontents|);
640
    $dbh->do(q|DELETE FROM virtualshelfcontents|);
500
- 

Return to bug 28965