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

(-)a/Koha/Object/Limit/LibraryGroup.pm (-2 / +7 lines)
Lines 53-59 A method that can be used to embed or simply retrieve the library group limits f Link Here
53
sub lib_group_limits {
53
sub lib_group_limits {
54
    my ($self) = @_;
54
    my ($self) = @_;
55
55
56
    my $lib_group_visibility_parameters = $self->object_class()->_library_group_visibility_parameters;
56
    my $lib_group_visibility_parameters = $self->_library_group_visibility_parameters;
57
    my $visibility_column               = $lib_group_visibility_parameters->{visibility_column};
57
    my $visibility_column               = $lib_group_visibility_parameters->{visibility_column};
58
58
59
    my $lib_group_visibility = $self->$visibility_column;
59
    my $lib_group_visibility = $self->$visibility_column;
Lines 78-86 sub set_lib_group_visibility { Link Here
78
    my ( $self, $args ) = @_;
78
    my ( $self, $args ) = @_;
79
79
80
    my $new_visibility                  = $args->{new_visibility} || $self->lib_group_visibility;
80
    my $new_visibility                  = $args->{new_visibility} || $self->lib_group_visibility;
81
    my $lib_group_visibility_parameters = $self->object_class()->_library_group_visibility_parameters;
81
    my $lib_group_visibility_parameters = $self->_library_group_visibility_parameters;
82
    my $visibility_column               = $lib_group_visibility_parameters->{visibility_column};
82
    my $visibility_column               = $lib_group_visibility_parameters->{visibility_column};
83
83
84
    if ( !$new_visibility ) {
85
        $self->$visibility_column(undef);
86
        return $self;
87
    }
88
84
    if ( ref $new_visibility eq 'ARRAY' ) {
89
    if ( ref $new_visibility eq 'ARRAY' ) {
85
        if ( scalar( @{$new_visibility} ) == 0 ) {
90
        if ( scalar( @{$new_visibility} ) == 0 ) {
86
            $self->$visibility_column(undef);
91
            $self->$visibility_column(undef);
(-)a/t/db_dependent/Koha/Object/Limit/LibraryGroup.t (+103 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Copyright PTFS Europe 2024
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
use Test::More tests => 2;
22
use t::lib::TestBuilder;
23
24
my $builder = t::lib::TestBuilder->new;
25
my $schema  = Koha::Database->schema;
26
27
subtest 'set_lib_group_visibility' => sub {
28
    plan tests => 3;
29
30
    $schema->txn_begin;
31
32
    my $root_group1 = Koha::Library::Group->new(
33
        {
34
            title => "LibGroup1",
35
        }
36
    )->store();
37
38
    my $lg1groupA =
39
        Koha::Library::Group->new( { parent_id => $root_group1->id, title => 'LibGroup1 SubGroupA' } )->store();
40
    my $lg1groupA1 =
41
        Koha::Library::Group->new( { parent_id => $lg1groupA->id, title => 'LibGroup1 SubGroupA SubGroup1' } )->store();
42
    my $lg1groupB =
43
        Koha::Library::Group->new( { parent_id => $root_group1->id, title => 'LibGroup1 SubGroupB' } )->store();
44
45
    my $vendor = $builder->build_object(
46
        { class => 'Koha::Acquisition::Booksellers', value => { lib_group_visibility => undef } } );
47
48
    # Pass in an array
49
    my @vendor_lib_groups = ( $lg1groupA->id, $lg1groupB->id );
50
    $vendor->set_lib_group_visibility( { new_visibility => \@vendor_lib_groups } );
51
    is(
52
        $vendor->lib_group_visibility, "|" . join( '|', @vendor_lib_groups ) . "|",
53
        'Got correct lib group visibility'
54
    );
55
56
    # Update with another array
57
    @vendor_lib_groups = ( $lg1groupA1->id, $lg1groupB->id );
58
    $vendor->set_lib_group_visibility( { new_visibility => \@vendor_lib_groups } );
59
    is(
60
        $vendor->lib_group_visibility, "|" . join( '|', @vendor_lib_groups ) . "|",
61
        'Got correct lib group visibility'
62
    );
63
64
    # Use the call in the object's "store" method
65
    $vendor->lib_group_visibility( "|" . $lg1groupA->id . "|" )->store;
66
    is(
67
        $vendor->lib_group_visibility, "|" . $lg1groupA->id . "|",
68
        'Lib group visibility has been overridden correctly'
69
    );
70
71
    $schema->txn_rollback;
72
};
73
74
subtest 'lib_group_limits' => sub {
75
    plan tests => 2;
76
77
    $schema->txn_begin;
78
79
    my $root_group1 = Koha::Library::Group->new(
80
        {
81
            title => "LibGroup1",
82
        }
83
    )->store();
84
85
    my $lg1groupA =
86
        Koha::Library::Group->new( { parent_id => $root_group1->id, title => 'LibGroup1 SubGroupA' } )->store();
87
    my $lg1groupB =
88
        Koha::Library::Group->new( { parent_id => $root_group1->id, title => 'LibGroup1 SubGroupB' } )->store();
89
90
    my $vendor = $builder->build_object(
91
        { class => 'Koha::Acquisition::Booksellers', value => { lib_group_visibility => undef } } );
92
93
    my @vendor_lib_groups = ( $lg1groupA->id, $lg1groupB->id );
94
    $vendor->set_lib_group_visibility( { new_visibility => \@vendor_lib_groups } );
95
96
    my $lib_groups = $vendor->lib_group_limits;
97
98
    is( scalar(@$lib_groups), 2, 'Got correct number of lib groups' );
99
    my @ids = sort { $a <=> $b } map { $_->id } @$lib_groups;
100
    is( join( '|', @ids ), join( '|', @vendor_lib_groups ), 'Got correct lib groups' );
101
102
    $schema->txn_rollback;
103
};
(-)a/t/db_dependent/Koha/Objects/Limit/LibraryGroup.t (-1 / +169 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Copyright PTFS Europe 2024
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
use Test::More tests => 1;
22
use t::lib::TestBuilder;
23
use t::lib::Mocks;
24
25
my $builder = t::lib::TestBuilder->new;
26
my $schema  = Koha::Database->schema;
27
28
subtest 'define_library_group_limits' => sub {
29
    plan tests => 8;
30
31
    $schema->txn_begin;
32
33
    Koha::Acquisition::Baskets->delete;
34
    Koha::Acquisition::Booksellers->delete;
35
36
    my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
37
    my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
38
    my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
39
    my $library4 = $builder->build_object( { class => 'Koha::Libraries' } );
40
41
    # Group 1 - two sub groups: A, B. Sub group A then has two sub groups: A1, A2
42
    my $root_group1 = Koha::Library::Group->new(
43
        {
44
            title => "LibGroup1",
45
        }
46
    )->store();
47
48
    my $lg1groupA =
49
        Koha::Library::Group->new( { parent_id => $root_group1->id, title => 'LibGroup1 SubGroupA' } )->store();
50
    my $lg1groupA1 =
51
        Koha::Library::Group->new( { parent_id => $lg1groupA->id, title => 'LibGroup1 SubGroupA SubGroup1' } )->store();
52
    my $lg1groupA2 =
53
        Koha::Library::Group->new( { parent_id => $lg1groupA->id, title => 'LibGroup1 SubGroupA SubGroup2' } )->store();
54
    my $lg1groupB =
55
        Koha::Library::Group->new( { parent_id => $root_group1->id, title => 'LibGroup1 SubGroupB' } )->store();
56
57
    my $lg1groupA_library1 =
58
        Koha::Library::Group->new( { parent_id => $lg1groupA->id, branchcode => $library1->branchcode } )->store();
59
    my $lg1groupA_library2 =
60
        Koha::Library::Group->new( { parent_id => $lg1groupA->id, branchcode => $library2->branchcode } )->store();
61
    my $lg1groupB_library1 =
62
        Koha::Library::Group->new( { parent_id => $lg1groupB->id, branchcode => $library1->branchcode } )->store();
63
    my $lg1groupA1_library1 =
64
        Koha::Library::Group->new( { parent_id => $lg1groupA1->id, branchcode => $library2->branchcode } )->store();
65
    my $lg1groupA2_library1 =
66
        Koha::Library::Group->new( { parent_id => $lg1groupA2->id, branchcode => $library1->branchcode } )->store();
67
68
    # Group 2 - three sub groups: A, B, C. Sub group A then has two sub groups: A1, A2 and sub group C has two: C1, C2
69
    my $root_group2 = Koha::Library::Group->new(
70
        {
71
            title => "LibGroup2",
72
        }
73
    )->store();
74
75
    my $lg2groupA =
76
        Koha::Library::Group->new( { parent_id => $root_group2->id, title => 'LibGroup2 SubGroupA' } )->store();
77
    my $lg2groupA1 =
78
        Koha::Library::Group->new( { parent_id => $lg2groupA->id, title => 'LibGroup2 SubGroupA SubGroup1' } )->store();
79
    my $lg2groupA2 =
80
        Koha::Library::Group->new( { parent_id => $lg2groupA->id, title => 'LibGroup2 SubGroupA SubGroup2' } )->store();
81
    my $lg2groupB =
82
        Koha::Library::Group->new( { parent_id => $root_group2->id, title => 'LibGroup2 SubGroupB' } )->store();
83
    my $lg2groupC =
84
        Koha::Library::Group->new( { parent_id => $root_group2->id, title => 'LibGroup2 SubGroupC' } )->store();
85
    my $lg2groupC1 =
86
        Koha::Library::Group->new( { parent_id => $lg2groupC->id, title => 'LibGroup2 SubGroupC SubGroup1' } )->store();
87
    my $lg2groupC2 =
88
        Koha::Library::Group->new( { parent_id => $lg2groupC->id, title => 'LibGroup2 SubGroupC SubGroup2' } )->store();
89
90
    my $lg2groupA_library1 =
91
        Koha::Library::Group->new( { parent_id => $lg2groupA->id, branchcode => $library1->branchcode } )->store();
92
    my $lg2groupA_library2 =
93
        Koha::Library::Group->new( { parent_id => $lg2groupA->id, branchcode => $library3->branchcode } )->store();
94
    my $lg2groupB_library1 =
95
        Koha::Library::Group->new( { parent_id => $lg2groupB->id, branchcode => $library1->branchcode } )->store();
96
    my $lg2groupC_library1 =
97
        Koha::Library::Group->new( { parent_id => $lg2groupC->id, branchcode => $library1->branchcode } )->store();
98
    my $lg2groupC_library2 =
99
        Koha::Library::Group->new( { parent_id => $lg2groupC->id, branchcode => $library3->branchcode } )->store();
100
    my $lg2groupC_library3 =
101
        Koha::Library::Group->new( { parent_id => $lg2groupC->id, branchcode => $library4->branchcode } )->store();
102
    my $lg2groupA1_library1 =
103
        Koha::Library::Group->new( { parent_id => $lg2groupA1->id, branchcode => $library3->branchcode } )->store();
104
    my $lg2groupA2_library1 =
105
        Koha::Library::Group->new( { parent_id => $lg2groupA2->id, branchcode => $library1->branchcode } )->store();
106
    my $lg2groupC1_library1 =
107
        Koha::Library::Group->new( { parent_id => $lg2groupC1->id, branchcode => $library3->branchcode } )->store();
108
    my $lg2groupC2_library1 =
109
        Koha::Library::Group->new( { parent_id => $lg2groupC2->id, branchcode => $library1->branchcode } )->store();
110
    my $lg2groupC2_library2 =
111
        Koha::Library::Group->new( { parent_id => $lg2groupC2->id, branchcode => $library4->branchcode } )->store();
112
113
    my $vendor1 = $builder->build_object(
114
        {
115
            class => 'Koha::Acquisition::Booksellers', value => { lib_group_visibility => "|" . $root_group1->id . "|" }
116
        }
117
    );
118
    my $vendor2 = $builder->build_object(
119
        {
120
            class => 'Koha::Acquisition::Booksellers', value => { lib_group_visibility => "|" . $root_group2->id . "|" }
121
        }
122
    );
123
124
    t::lib::Mocks::mock_userenv( { branchcode => $library1->branchcode } );
125
    my $vendors = Koha::Acquisition::Booksellers->search()->as_list;
126
    is( scalar(@$vendors), 2, "Two vendors returned" );
127
128
    t::lib::Mocks::mock_userenv( { branchcode => $library2->branchcode } );
129
    $vendors = Koha::Acquisition::Booksellers->search()->as_list;
130
    is( scalar(@$vendors), 1, "One vendor returned as library2 is not in root_group2" );
131
132
    my $vendor3 = $builder->build_object(
133
        { class => 'Koha::Acquisition::Booksellers', value => { lib_group_visibility => "|" . $lg2groupA->id . "|" } }
134
    );
135
    $vendors = Koha::Acquisition::Booksellers->search()->as_list;
136
    is( scalar(@$vendors), 1, "Still only one vendor returned as library2 is not in root_group2 or lg2groupA" );
137
138
    t::lib::Mocks::mock_userenv( { branchcode => $library3->branchcode } );
139
    $vendors = Koha::Acquisition::Booksellers->search()->as_list;
140
    is( scalar(@$vendors), 2, "Two vendors now returned as library3 is in lg2groupA and root_group2" );
141
142
    my $vendor4 = $builder->build_object(
143
        { class => 'Koha::Acquisition::Booksellers', value => { lib_group_visibility => "|" . $lg2groupB->id . "|" } }
144
    );
145
    $vendors = Koha::Acquisition::Booksellers->search()->as_list;
146
    is(
147
        scalar(@$vendors), 2,
148
        "Still two vendors returned as library3 is in lg2groupA and root_group2 but not lg2groupB"
149
    );
150
151
    t::lib::Mocks::mock_userenv( { branchcode => $library4->branchcode } );
152
    $vendors = Koha::Acquisition::Booksellers->search()->as_list;
153
    is(
154
        scalar(@$vendors), 1,
155
        "One vendor returned as library2 is only in root_group2 but not the previously assigned sub groups"
156
    );
157
    $vendor2->lib_group_visibility( "|" . $root_group1->id . "|" )->store;
158
    $vendors = Koha::Acquisition::Booksellers->search()->as_list;
159
    is( scalar(@$vendors), 0, "No vendors returned as none are visible to this branch code" );
160
161
    $vendor2->lib_group_visibility(undef)->store;
162
    $vendors = Koha::Acquisition::Booksellers->search()->as_list;
163
    is(
164
        scalar(@$vendors), 1,
165
        "One vendor returned as its lib_group_visibility is now undefined and so is visible to all"
166
    );
167
168
    $schema->txn_rollback;
169
};

Return to bug 38290