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

(-)a/Koha/Object/Limit/Library.pm (-8 / +26 lines)
Lines 19-25 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
24
use Data::Printer colored => 1;
23
use Try::Tiny;
25
use Try::Tiny;
24
26
25
=head1 NAME
27
=head1 NAME
Lines 50-55 my $limits = $object->library_limits(); Link Here
50
52
51
$object->library_limits( \@branchcodes );
53
$object->library_limits( \@branchcodes );
52
54
55
Accessor method for library limits. When updating library limits, it accepts
56
a list of branchcodes. If requested to return the current library limits
57
it returns a Koha::Libraries object with the corresponding libraries.
58
53
=cut
59
=cut
54
60
55
sub library_limits {
61
sub library_limits {
Lines 67-72 sub library_limits { Link Here
67
73
68
my $limits = $object->get_library_limits();
74
my $limits = $object->get_library_limits();
69
75
76
Returns the current library limits in the form of a Koha::Libraries iterator object.
77
It returns undef if no libary limits defined.
78
70
=cut
79
=cut
71
80
72
sub get_library_limits {
81
sub get_library_limits {
Lines 77-83 sub get_library_limits { Link Here
77
        { $self->_library_limits->{id} => $self->id } )
86
        { $self->_library_limits->{id} => $self->id } )
78
        ->get_column( $self->_library_limits->{library} )->all();
87
        ->get_column( $self->_library_limits->{library} )->all();
79
88
80
    return \@branchcodes;
89
    return unless @branchcodes;
90
91
    my $filter = [ map { { branchcode => $_ } } @branchcodes ];
92
    my $libraries = Koha::Libraries->search( $filter );
93
94
    return $libraries;
81
}
95
}
82
96
83
=head3 add_library_limit
97
=head3 add_library_limit
Lines 93-101 sub add_library_limit { Link Here
93
        "Required parameter 'branchcode' missing")
107
        "Required parameter 'branchcode' missing")
94
        unless $branchcode;
108
        unless $branchcode;
95
109
96
    my $limitation;
97
    try {
110
    try {
98
        $limitation = $self->_library_limit_rs->update_or_create(
111
        $self->_library_limit_rs->update_or_create(
99
            {   $self->_library_limits->{id}      => $self->id,
112
            {   $self->_library_limits->{id}      => $self->id,
100
                $self->_library_limits->{library} => $branchcode
113
                $self->_library_limits->{library} => $branchcode
101
            }
114
            }
Lines 105-111 sub add_library_limit { Link Here
105
        Koha::Exceptions::CannotAddLibraryLimit->throw( $_->{msg} );
118
        Koha::Exceptions::CannotAddLibraryLimit->throw( $_->{msg} );
106
    };
119
    };
107
120
108
    return $limitation ? 1 : undef;
121
    return $self;
109
}
122
}
110
123
111
=head3 del_library_limit
124
=head3 del_library_limit
Lines 145-158 $object->replace_library_limits( \@branchcodes ); Link Here
145
sub replace_library_limits {
158
sub replace_library_limits {
146
    my ( $self, $branchcodes ) = @_;
159
    my ( $self, $branchcodes ) = @_;
147
160
148
    $self->_library_limit_rs->search(
161
    $self->_result->result_source->schema->txn_do(
149
        { $self->_library_limits->{id} => $self->id } )->delete;
162
        sub {
163
            $self->_library_limit_rs->search(
164
                { $self->_library_limits->{id} => $self->id } )->delete;
150
165
151
    my @return_values = map { $self->add_library_limit($_) } @$branchcodes;
166
            map { $self->add_library_limit($_) } @$branchcodes;
167
        }
168
    );
152
169
153
    return \@return_values;
170
    return $self;
154
}
171
}
155
172
173
156
=head3 Koha::Objects->_library_limit_rs
174
=head3 Koha::Objects->_library_limit_rs
157
175
158
Returns the internal resultset for the branch limitation table or creates it if undefined
176
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