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

(-)a/t/db_dependent/Koha/REST/Plugin/Objects.t (-2 / +67 lines)
Lines 20-25 use Modern::Perl; Link Here
20
use Koha::Acquisition::Orders;
20
use Koha::Acquisition::Orders;
21
use Koha::Cities;
21
use Koha::Cities;
22
use Koha::Biblios;
22
use Koha::Biblios;
23
use Koha::Patrons;
24
25
use Mojo::JSON qw(encode_json);
23
26
24
# Dummy app for testing the plugin
27
# Dummy app for testing the plugin
25
use Mojolicious::Lite;
28
use Mojolicious::Lite;
Lines 101-108 get '/libraries/:library_id_1/:library_id_2' => sub { Link Here
101
    );
104
    );
102
};
105
};
103
106
107
get '/my_patrons' => sub {
108
109
    my $c = shift;
110
111
    my $patrons = $c->objects->search( scalar Koha::Patrons->search( {}, { order_by   => 'borrowernumber' }) );
112
113
    $c->render(
114
        status => 200,
115
        json   => $patrons
116
    );
117
};
118
104
# The tests
119
# The tests
105
use Test::More tests => 13;
120
use Test::More tests => 14;
106
use Test::Mojo;
121
use Test::Mojo;
107
122
108
use t::lib::Mocks;
123
use t::lib::Mocks;
Lines 552-554 subtest 'objects.search helper, public requests' => sub { Link Here
552
567
553
    $schema->storage->txn_rollback;
568
    $schema->storage->txn_rollback;
554
};
569
};
555
- 
570
571
subtest 'objects.search helper, search_limited() tests' => sub {
572
573
    plan tests => 9;
574
575
    $schema->storage->txn_begin;
576
577
    my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
578
    my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
579
580
    my $patron_1 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library_1->id } });
581
    my $patron_2 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library_1->id } });
582
    my $patron_3 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library_2->id } });
583
584
    my @libraries_where_can_see_patrons = ( $library_1->id, $library_2->id );
585
586
    my $t = Test::Mojo->new;
587
588
    my $mocked_patron = Test::MockModule->new('Koha::Patron');
589
    $mocked_patron->mock( 'libraries_where_can_see_patrons', sub
590
        {
591
            return @libraries_where_can_see_patrons;
592
        }
593
    );
594
595
    my $patron = $builder->build_object(
596
        {
597
            class => 'Koha::Patrons',
598
            value => { flags => 2**4 }    # borrowers flag = 4
599
        }
600
    );
601
602
    t::lib::Mocks::mock_userenv({ patron => $patron });
603
604
    $t->get_ok( "/my_patrons?q=" . encode_json( { library_id => [ $library_1->id, $library_2->id ] } ) )
605
      ->status_is(200)
606
      ->json_is( '/0/patron_id' => $patron_1->id )
607
      ->json_is( '/1/patron_id' => $patron_2->id )
608
      ->json_is( '/2/patron_id' => $patron_3->id );
609
610
    @libraries_where_can_see_patrons = ( $library_2->id );
611
612
    my $res = $t->get_ok( "/my_patrons?q=" . encode_json( { library_id => [ $library_1->id, $library_2->id ] } ) )
613
      ->status_is(200)
614
      ->json_is( '/0/patron_id' => $patron_3->id, 'Returns the only allowed patron' )
615
      ->tx->res->json;
616
617
    is( scalar @{$res}, 1, 'Only one patron returned' );
618
619
    $schema->storage->txn_rollback;
620
};

Return to bug 29506