Lines 17-23
Link Here
|
17 |
|
17 |
|
18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
19 |
|
19 |
|
20 |
use Test::More tests => 4; |
20 |
use Test::More tests => 5; |
21 |
use Test::MockModule; |
21 |
use Test::MockModule; |
22 |
use Test::Mojo; |
22 |
use Test::Mojo; |
23 |
use Test::Warn; |
23 |
use Test::Warn; |
Lines 329-331
subtest 'get_public() tests' => sub {
Link Here
|
329 |
|
329 |
|
330 |
$schema->storage->txn_rollback; |
330 |
$schema->storage->txn_rollback; |
331 |
}; |
331 |
}; |
332 |
- |
332 |
|
|
|
333 |
subtest 'pickup_locations() tests' => sub { |
334 |
|
335 |
plan tests => 15; |
336 |
|
337 |
$schema->storage->txn_begin; |
338 |
|
339 |
t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 ); |
340 |
|
341 |
# Small trick to ease testing |
342 |
Koha::Libraries->search->update({ pickup_location => 0 }); |
343 |
|
344 |
my $library_1 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'A', pickup_location => 1 } }); |
345 |
my $library_2 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'B', pickup_location => 1 } }); |
346 |
my $library_3 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'C', pickup_location => 1 } }); |
347 |
|
348 |
my $library_1_api = $library_1->to_api(); |
349 |
my $library_2_api = $library_2->to_api(); |
350 |
my $library_3_api = $library_3->to_api(); |
351 |
|
352 |
$library_1_api->{needs_override} = Mojo::JSON->false; |
353 |
$library_2_api->{needs_override} = Mojo::JSON->false; |
354 |
$library_3_api->{needs_override} = Mojo::JSON->true; |
355 |
|
356 |
my $patron = $builder->build_object( |
357 |
{ |
358 |
class => 'Koha::Patrons', |
359 |
value => { userid => 'tomasito', flags => 0 } |
360 |
} |
361 |
); |
362 |
my $password = 'thePassword123'; |
363 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
364 |
my $userid = $patron->userid; |
365 |
$builder->build( |
366 |
{ |
367 |
source => 'UserPermission', |
368 |
value => { |
369 |
borrowernumber => $patron->borrowernumber, |
370 |
module_bit => 6, |
371 |
code => 'place_holds', |
372 |
}, |
373 |
} |
374 |
); |
375 |
|
376 |
my $item_class = Test::MockModule->new('Koha::Item'); |
377 |
$item_class->mock( |
378 |
'pickup_locations', |
379 |
sub { |
380 |
my ( $self, $params ) = @_; |
381 |
my $mock_patron = $params->{patron}; |
382 |
is( $mock_patron->borrowernumber, |
383 |
$patron->borrowernumber, 'Patron passed correctly' ); |
384 |
return Koha::Libraries->search( |
385 |
{ |
386 |
branchcode => { |
387 |
'-in' => [ |
388 |
$library_1->branchcode, |
389 |
$library_2->branchcode |
390 |
] |
391 |
} |
392 |
}, |
393 |
{ # we make sure no surprises in the order of the result |
394 |
order_by => { '-asc' => 'marcorgcode' } |
395 |
} |
396 |
); |
397 |
} |
398 |
); |
399 |
|
400 |
my $biblio_class = Test::MockModule->new('Koha::Biblio'); |
401 |
$biblio_class->mock( |
402 |
'pickup_locations', |
403 |
sub { |
404 |
my ( $self, $params ) = @_; |
405 |
my $mock_patron = $params->{patron}; |
406 |
is( $mock_patron->borrowernumber, |
407 |
$patron->borrowernumber, 'Patron passed correctly' ); |
408 |
return Koha::Libraries->search( |
409 |
{ |
410 |
branchcode => { |
411 |
'-in' => [ |
412 |
$library_1->branchcode, |
413 |
$library_2->branchcode |
414 |
] |
415 |
} |
416 |
}, |
417 |
{ # we make sure no surprises in the order of the result |
418 |
order_by => { '-asc' => 'marcorgcode' } |
419 |
} |
420 |
); |
421 |
} |
422 |
); |
423 |
|
424 |
my $biblio = $builder->build_sample_biblio; |
425 |
|
426 |
$t->get_ok( "//$userid:$password@/api/v1/biblios/" |
427 |
. $biblio->id |
428 |
. "/pickup_locations?patron_id=" . $patron->id ) |
429 |
->json_is( [ $library_1_api, $library_2_api ] ); |
430 |
|
431 |
# filtering works! |
432 |
$t->get_ok( "//$userid:$password@/api/v1/biblios/" |
433 |
. $biblio->id |
434 |
. '/pickup_locations?' |
435 |
. 'patron_id=' . $patron->id . '&q={"marc_org_code": { "-like": "A%" }}' ) |
436 |
->json_is( [ $library_1_api ] ); |
437 |
|
438 |
t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 ); |
439 |
|
440 |
my $library_4 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 0, marcorgcode => 'X' } }); |
441 |
my $library_5 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1, marcorgcode => 'Y' } }); |
442 |
|
443 |
my $library_5_api = $library_5->to_api(); |
444 |
$library_5_api->{needs_override} = Mojo::JSON->true; |
445 |
|
446 |
$t->get_ok( "//$userid:$password@/api/v1/biblios/" |
447 |
. $biblio->id |
448 |
. "/pickup_locations?" |
449 |
. "patron_id=" . $patron->id . "&_order_by=marc_org_code" ) |
450 |
->json_is( [ $library_1_api, $library_2_api, $library_3_api, $library_5_api ] ); |
451 |
|
452 |
my $deleted_patron = $builder->build_object({ class => 'Koha::Patrons' }); |
453 |
my $deleted_patron_id = $deleted_patron->id; |
454 |
$deleted_patron->delete; |
455 |
|
456 |
$t->get_ok( "//$userid:$password@/api/v1/biblios/" |
457 |
. $biblio->id |
458 |
. "/pickup_locations?" |
459 |
. "patron_id=" . $deleted_patron_id ) |
460 |
->status_is( 400 ) |
461 |
->json_is( '/error' => 'Patron not found' ); |
462 |
|
463 |
$biblio->delete; |
464 |
|
465 |
$t->get_ok( "//$userid:$password@/api/v1/biblios/" |
466 |
. $biblio->id |
467 |
. "/pickup_locations?" |
468 |
. "patron_id=" . $patron->id ) |
469 |
->status_is( 404 ) |
470 |
->json_is( '/error' => 'Biblio not found' ); |
471 |
|
472 |
$schema->storage->txn_rollback; |
473 |
}; |