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

(-)a/t/db_dependent/api/v1/biblios.t (-2 / +119 lines)
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 $biblio_class = Test::MockModule->new('Koha::Biblio');
377
    $biblio_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 = $builder->build_sample_biblio;
401
402
    $t->get_ok( "//$userid:$password@/api/v1/biblios/"
403
          . $biblio->id
404
          . "/pickup_locations?patron_id=" . $patron->id )
405
      ->json_is( [ $library_1_api, $library_2_api ] );
406
407
    # filtering works!
408
    $t->get_ok( "//$userid:$password@/api/v1/biblios/"
409
          . $biblio->id
410
          . '/pickup_locations?'
411
          . 'patron_id=' . $patron->id . '&q={"marc_org_code": { "-like": "A%" }}' )
412
      ->json_is( [ $library_1_api ] );
413
414
    t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
415
416
    my $library_4 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 0, marcorgcode => 'X' } });
417
    my $library_5 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1, marcorgcode => 'Y' } });
418
419
    my $library_5_api = $library_5->to_api();
420
    $library_5_api->{needs_override} = Mojo::JSON->true;
421
422
    $t->get_ok( "//$userid:$password@/api/v1/biblios/"
423
          . $biblio->id
424
          . "/pickup_locations?"
425
          . "patron_id=" . $patron->id . "&_order_by=marc_org_code" )
426
      ->json_is( [ $library_1_api, $library_2_api, $library_3_api, $library_5_api ] );
427
428
    my $deleted_patron = $builder->build_object({ class => 'Koha::Patrons' });
429
    my $deleted_patron_id = $deleted_patron->id;
430
    $deleted_patron->delete;
431
432
    $t->get_ok( "//$userid:$password@/api/v1/biblios/"
433
          . $biblio->id
434
          . "/pickup_locations?"
435
          . "patron_id=" . $deleted_patron_id )
436
      ->status_is( 400 )
437
      ->json_is( '/error' => 'Patron not found' );
438
439
    $biblio->delete;
440
441
    $t->get_ok( "//$userid:$password@/api/v1/biblios/"
442
          . $biblio->id
443
          . "/pickup_locations?"
444
          . "patron_id=" . $patron->id )
445
      ->status_is( 404 )
446
      ->json_is( '/error' => 'Biblio not found' );
447
448
    $schema->storage->txn_rollback;
449
};

Return to bug 27932