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

(-)a/Koha/Object/Limit/Library.pm (-8 / +25 lines)
Lines 19-24 use Modern::Perl; Link Here
19
19
20
use Koha::Database;
20
use Koha::Database;
21
use Koha::Exceptions;
21
use Koha::Exceptions;
22
use Koha::Libraries;
22
23
23
use Try::Tiny;
24
use Try::Tiny;
24
25
Lines 50-55 my $limits = $object->library_limits(); Link Here
50
51
51
$object->library_limits( \@branchcodes );
52
$object->library_limits( \@branchcodes );
52
53
54
Accessor method for library limits. When updating library limits, it accepts
55
a list of branchcodes. If requested to return the current library limits
56
it returns a Koha::Libraries object with the corresponding libraries.
57
53
=cut
58
=cut
54
59
55
sub library_limits {
60
sub library_limits {
Lines 67-72 sub library_limits { Link Here
67
72
68
my $limits = $object->get_library_limits();
73
my $limits = $object->get_library_limits();
69
74
75
Returns the current library limits in the form of a Koha::Libraries iterator object.
76
It returns undef if no library limits defined.
77
70
=cut
78
=cut
71
79
72
sub get_library_limits {
80
sub get_library_limits {
Lines 77-83 sub get_library_limits { Link Here
77
        { $self->_library_limits->{id} => $self->id } )
85
        { $self->_library_limits->{id} => $self->id } )
78
        ->get_column( $self->_library_limits->{library} )->all();
86
        ->get_column( $self->_library_limits->{library} )->all();
79
87
80
    return \@branchcodes;
88
    return unless @branchcodes;
89
90
    my $filter = [ map { { branchcode => $_ } } @branchcodes ];
91
    my $libraries = Koha::Libraries->search( $filter );
92
93
    return $libraries;
81
}
94
}
82
95
83
=head3 add_library_limit
96
=head3 add_library_limit
Lines 93-101 sub add_library_limit { Link Here
93
        "Required parameter 'branchcode' missing")
106
        "Required parameter 'branchcode' missing")
94
        unless $branchcode;
107
        unless $branchcode;
95
108
96
    my $limitation;
97
    try {
109
    try {
98
        $limitation = $self->_library_limit_rs->update_or_create(
110
        $self->_library_limit_rs->update_or_create(
99
            {   $self->_library_limits->{id}      => $self->id,
111
            {   $self->_library_limits->{id}      => $self->id,
100
                $self->_library_limits->{library} => $branchcode
112
                $self->_library_limits->{library} => $branchcode
101
            }
113
            }
Lines 105-111 sub add_library_limit { Link Here
105
        Koha::Exceptions::CannotAddLibraryLimit->throw( $_->{msg} );
117
        Koha::Exceptions::CannotAddLibraryLimit->throw( $_->{msg} );
106
    };
118
    };
107
119
108
    return $limitation ? 1 : undef;
120
    return $self;
109
}
121
}
110
122
111
=head3 del_library_limit
123
=head3 del_library_limit
Lines 145-158 $object->replace_library_limits( \@branchcodes ); Link Here
145
sub replace_library_limits {
157
sub replace_library_limits {
146
    my ( $self, $branchcodes ) = @_;
158
    my ( $self, $branchcodes ) = @_;
147
159
148
    $self->_library_limit_rs->search(
160
    $self->_result->result_source->schema->txn_do(
149
        { $self->_library_limits->{id} => $self->id } )->delete;
161
        sub {
162
            $self->_library_limit_rs->search(
163
                { $self->_library_limits->{id} => $self->id } )->delete;
150
164
151
    my @return_values = map { $self->add_library_limit($_) } @$branchcodes;
165
            map { $self->add_library_limit($_) } @$branchcodes;
166
        }
167
    );
152
168
153
    return \@return_values;
169
    return $self;
154
}
170
}
155
171
172
156
=head3 Koha::Objects->_library_limit_rs
173
=head3 Koha::Objects->_library_limit_rs
157
174
158
Returns the internal resultset for the branch limitation table or creates it if undefined
175
Returns the internal resultset for the branch limitation table or creates it if undefined
(-)a/t/db_dependent/Koha/Patron/Attribute/Types.t (-35 / +72 lines)
Lines 72-78 subtest 'new() tests' => sub { Link Here
72
72
73
subtest 'library_limits() tests' => sub {
73
subtest 'library_limits() tests' => sub {
74
74
75
    plan tests => 5;
75
    plan tests => 13;
76
76
77
    $schema->storage->txn_begin;
77
    $schema->storage->txn_begin;
78
78
Lines 89-131 subtest 'library_limits() tests' => sub { Link Here
89
    my $library = $builder->build( { source => 'Branch' } )->{branchcode};
89
    my $library = $builder->build( { source => 'Branch' } )->{branchcode};
90
90
91
    my $library_limits = $attribute_type->library_limits();
91
    my $library_limits = $attribute_type->library_limits();
92
    is_deeply( $library_limits, [],
92
    is( $library_limits, undef,
93
        'No branch limitations defined for attribute type' );
93
        'No branch limitations defined for attribute type' );
94
94
95
    my $print_error = $dbh->{PrintError};
95
    my $print_error = $dbh->{PrintError};
96
    $dbh->{PrintError} = 0;
96
    $dbh->{PrintError} = 0;
97
97
98
    throws_ok {
98
    throws_ok {
99
        $library_limits = $attribute_type->library_limits( ['fake'] );
99
        $attribute_type->library_limits( ['fake'] );
100
    }
100
    }
101
    'Koha::Exceptions::CannotAddLibraryLimit',
101
    'Koha::Exceptions::CannotAddLibraryLimit',
102
        'Exception thrown on single invalid branchcode';
102
        'Exception thrown on single invalid branchcode';
103
    $library_limits = $attribute_type->library_limits();
104
    is( $library_limits, undef,
105
        'No branch limitations defined for attribute type' );
103
106
104
    throws_ok {
107
    throws_ok {
105
        $library_limits
108
        $attribute_type->library_limits( [ 'fake', $library ] );
106
            = $attribute_type->library_limits( [ 'fake', $library ] );
107
    }
109
    }
108
    'Koha::Exceptions::CannotAddLibraryLimit',
110
    'Koha::Exceptions::CannotAddLibraryLimit',
109
        'Exception thrown on invalid branchcode present';
111
        'Exception thrown on invalid branchcode present';
110
112
113
    $library_limits = $attribute_type->library_limits();
114
    is( $library_limits, undef,
115
        'No branch limitations defined for attribute type' );
116
111
    $dbh->{PrintError} = $print_error;
117
    $dbh->{PrintError} = $print_error;
112
118
113
    $library_limits = $attribute_type->library_limits( [$library] );
119
    $attribute_type->library_limits( [$library] );
114
    is_deeply( $library_limits, [1], 'Library limits correctly set' );
120
    $library_limits = $attribute_type->library_limits;
121
    is( $library_limits->count, 1, 'Library limits correctly set (count)' );
122
    my $limit_library = $library_limits->next;
123
    ok( $limit_library->isa('Koha::Library'),
124
        'Library limits correctly set (type)'
125
    );
126
    is( $limit_library->branchcode,
127
        $library, 'Library limits correctly set (value)' );
115
128
116
    my $another_library
129
    my $another_library
117
        = $builder->build( { source => 'Branch' } )->{branchcode};
130
        = $builder->build( { source => 'Branch' } )->{branchcode};
118
131
    my @branchcodes_list = ( $library, $another_library );
119
    $library_limits
132
120
        = $attribute_type->library_limits( [ $library, $another_library ] );
133
    $attribute_type->library_limits( \@branchcodes_list );
121
    is_deeply( $library_limits, [ 1, 1 ], 'Library limits correctly set' );
134
    $library_limits = $attribute_type->library_limits;
135
    is( $library_limits->count, 2, 'Library limits correctly set (count)' );
136
137
    while ( $limit_library = $library_limits->next ) {
138
        ok( $limit_library->isa('Koha::Library'),
139
            'Library limits correctly set (type)'
140
        );
141
        ok( eval {
142
                grep { $limit_library->branchcode eq $_ } @branchcodes_list;
143
            },
144
            'Library limits correctly set (values)'
145
        );
146
    }
122
147
123
    $schema->storage->txn_rollback;
148
    $schema->storage->txn_rollback;
124
};
149
};
125
150
126
subtest 'add_library_limit() tests' => sub {
151
subtest 'add_library_limit() tests' => sub {
127
152
128
    plan tests => 3;
153
    plan tests => 4;
129
154
130
    $schema->storage->txn_begin;
155
    $schema->storage->txn_begin;
131
156
Lines 140-151 subtest 'add_library_limit() tests' => sub { Link Here
140
    )->store();
165
    )->store();
141
166
142
    throws_ok { $attribute_type->add_library_limit() }
167
    throws_ok { $attribute_type->add_library_limit() }
143
    'Koha::Exceptions::MissingParameter',
168
    'Koha::Exceptions::MissingParameter', 'branchcode parameter is mandatory';
144
        'branchcode parameter is mandatory';
145
169
146
    my $library = $builder->build( { source => 'Branch' } )->{branchcode};
170
    my $library = $builder->build( { source => 'Branch' } )->{branchcode};
147
    is( $attribute_type->add_library_limit($library),
171
    $attribute_type->add_library_limit($library);
148
        1, 'Library limit successfully added' );
172
    my $rs = Koha::Database->schema->resultset('BorrowerAttributeTypesBranch')
173
        ->search( { bat_code => 'code' } );
174
    is( $rs->count, 1, 'Library limit successfully added (count)' );
175
    is( $rs->next->b_branchcode->branchcode,
176
        $library, 'Library limit successfully added (value)' );
149
177
150
    my $print_error = $dbh->{PrintError};
178
    my $print_error = $dbh->{PrintError};
151
    $dbh->{PrintError} = 0;
179
    $dbh->{PrintError} = 0;
Lines 209-215 subtest 'del_library_limit() tests' => sub { Link Here
209
237
210
subtest 'replace_library_limits() tests' => sub {
238
subtest 'replace_library_limits() tests' => sub {
211
239
212
    plan tests => 6;
240
    plan tests => 10;
213
241
214
    $schema->storage->txn_begin;
242
    $schema->storage->txn_begin;
215
243
Lines 223-248 subtest 'replace_library_limits() tests' => sub { Link Here
223
        }
251
        }
224
    )->store();
252
    )->store();
225
253
226
    is_deeply( $attribute_type->replace_library_limits( [] ),
254
    $attribute_type->replace_library_limits( [] );
227
        [], 'Replacing with empty array returns an empty array as expected' );
255
    my $library_limits = $attribute_type->library_limits;
228
256
    is( $library_limits, undef, 'Replacing with empty array yields no library limits' );
229
    is_deeply( $attribute_type->library_limits(),
230
        [], 'Replacing with empty array yields no library limits' );
231
257
232
    my $library_1 = $builder->build({ source => 'Branch'})->{branchcode};
258
    my $library_1 = $builder->build({ source => 'Branch'})->{branchcode};
233
    my $library_2 = $builder->build({ source => 'Branch'})->{branchcode};
259
    my $library_2 = $builder->build({ source => 'Branch'})->{branchcode};
234
260
    my $library_3 = $builder->build({ source => 'Branch'})->{branchcode};
235
    is_deeply( $attribute_type->replace_library_limits( [$library_1] ),
261
236
        [ 1 ], 'Successfully adds a single library limit' );
262
    $attribute_type->replace_library_limits( [$library_1] );
237
263
    $library_limits = $attribute_type->library_limits;
238
    is_deeply( $attribute_type->library_limits(),
264
    is( $library_limits->count, 1, 'Successfully adds a single library limit' );
239
        [ $library_1 ], 'Library limit correctly set' );
265
    my $library_limit = $library_limits->next;
240
266
    is( $library_limit->branchcode, $library_1, 'Library limit correctly set' );
241
    is_deeply( $attribute_type->replace_library_limits( [$library_1, $library_2] ),
267
242
        [ 1, 1 ], 'Successfully adds two library limit' );
268
243
269
    my @branchcodes_list = ($library_1, $library_2, $library_3);
244
    is_deeply( $attribute_type->library_limits(),
270
    $attribute_type->replace_library_limits( [$library_1, $library_2, $library_3] );
245
        [ $library_1, $library_2 ], 'Library limit correctly set' );
271
    $library_limits = $attribute_type->library_limits;
272
    is( $library_limits->count, 3, 'Successfully adds two library limit' );
273
274
    while ( my $limit_library = $library_limits->next ) {
275
        ok( $limit_library->isa('Koha::Library'),
276
            'Library limits correctly set (type)'
277
        );
278
        ok( eval {
279
                grep { $limit_library->branchcode eq $_ } @branchcodes_list;
280
            },
281
            'Library limits correctly set (values)'
282
        );
283
    }
246
284
247
    $schema->storage->txn_rollback;
285
    $schema->storage->txn_rollback;
248
};
286
};
249
- 

Return to bug 17755