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

(-)a/Koha/Hold.pm (-6 / +27 lines)
Lines 222-237 sub set_waiting { Link Here
222
    return $self;
222
    return $self;
223
}
223
}
224
224
225
=head3 set_pickup_location
225
=head3 is_pickup_location_valid
226
226
227
    $hold->set_pickup_location({ library_id => $library->id });
227
    if ($hold->is_pickup_location_valid({ library_id => $library->id }) ) {
228
        ...
229
    }
228
230
229
Updates the hold pickup location. It throws a I<Koha::Exceptions::Hold::InvalidPickupLocation> if
231
Returns a I<boolean> representing if the passed pickup location is valid for the hold.
230
the passed pickup location is not valid.
232
It throws a I<Koha::Exceptions::_MissingParameter> if the library_id parameter is not
233
passed.
231
234
232
=cut
235
=cut
233
236
234
sub set_pickup_location {
237
sub is_pickup_location_valid {
235
    my ( $self, $params ) = @_;
238
    my ( $self, $params ) = @_;
236
239
237
    Koha::Exceptions::MissingParameter->throw('The library_id parameter is mandatory')
240
    Koha::Exceptions::MissingParameter->throw('The library_id parameter is mandatory')
Lines 246-252 sub set_pickup_location { Link Here
246
        @pickup_locations = $self->biblio->pickup_locations({ patron => $self->patron });
249
        @pickup_locations = $self->biblio->pickup_locations({ patron => $self->patron });
247
    }
250
    }
248
251
249
    if ( any { $_->branchcode eq $params->{library_id} } @pickup_locations ) {
252
    return any { $_->branchcode eq $params->{library_id} } @pickup_locations;
253
}
254
255
=head3 set_pickup_location
256
257
    $hold->set_pickup_location({ library_id => $library->id });
258
259
Updates the hold pickup location. It throws a I<Koha::Exceptions::Hold::InvalidPickupLocation> if
260
the passed pickup location is not valid.
261
262
=cut
263
264
sub set_pickup_location {
265
    my ( $self, $params ) = @_;
266
267
    Koha::Exceptions::MissingParameter->throw('The library_id parameter is mandatory')
268
        unless $params->{library_id};
269
270
    if ( $self->is_pickup_location_valid({ library_id => $params->{library_id} }) ) {
250
        # all good, set the new pickup location
271
        # all good, set the new pickup location
251
        $self->branchcode( $params->{library_id} )->store;
272
        $self->branchcode( $params->{library_id} )->store;
252
    }
273
    }
(-)a/t/db_dependent/Koha/Hold.t (-2 / +59 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 2;
22
use Test::More tests => 3;
23
23
24
use Test::Exception;
24
use Test::Exception;
25
use Test::MockModule;
25
use Test::MockModule;
Lines 140-142 subtest 'set_pickup_location() tests' => sub { Link Here
140
140
141
    $schema->storage->txn_rollback;
141
    $schema->storage->txn_rollback;
142
};
142
};
143
- 
143
144
subtest 'is_pickup_location_valid() tests' => sub {
145
146
    plan tests => 4;
147
148
    $schema->storage->txn_begin;
149
150
    my $mock_biblio = Test::MockModule->new('Koha::Biblio');
151
    my $mock_item   = Test::MockModule->new('Koha::Item');
152
153
    my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
154
    my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
155
    my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
156
157
    # let's control what Koha::Biblio->pickup_locations returns, for testing
158
    $mock_biblio->mock( 'pickup_locations', sub {
159
        return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
160
    });
161
    # let's mock what Koha::Item->pickup_locations returns, for testing
162
    $mock_item->mock( 'pickup_locations', sub {
163
        return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
164
    });
165
166
    my $biblio = $builder->build_sample_biblio;
167
    my $item   = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
168
169
    # Test biblio-level holds
170
    my $biblio_hold = $builder->build_object(
171
        {
172
            class => "Koha::Holds",
173
            value => {
174
                biblionumber => $biblio->biblionumber,
175
                branchcode   => $library_3->branchcode,
176
                itemnumber   => undef,
177
            }
178
        }
179
    );
180
181
    ok( !$biblio_hold->is_pickup_location_valid({ library_id => $library_1->branchcode }), 'Pickup location invalid');
182
    ok( $biblio_hold->is_pickup_location_valid({ library_id => $library_2->id }), 'Pickup location valid');
183
184
    # Test item-level holds
185
    my $item_hold = $builder->build_object(
186
        {
187
            class => "Koha::Holds",
188
            value => {
189
                biblionumber => $biblio->biblionumber,
190
                branchcode   => $library_3->branchcode,
191
                itemnumber   => $item->itemnumber,
192
            }
193
        }
194
    );
195
196
    ok( !$item_hold->is_pickup_location_valid({ library_id => $library_1->branchcode }), 'Pickup location invalid');
197
    ok( $item_hold->is_pickup_location_valid({ library_id => $library_2->id }), 'Pickup location valid' );
198
199
    $schema->storage->txn_rollback;
200
};

Return to bug 27209