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

(-)a/t/db_dependent/Koha/REST/Plugin/Objects.t (-2 / +107 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 79-86 get '/biblios' => sub { Link Here
79
    $c->render( status => 200, json => {count => scalar(@$biblios), biblios => $biblios} );
82
    $c->render( status => 200, json => {count => scalar(@$biblios), biblios => $biblios} );
80
};
83
};
81
84
85
get '/libraries/:library_id_1/:library_id_2' => sub {
86
87
    my $c = shift;
88
89
    # Emulate a public route by stashing the is_public value
90
    $c->stash( 'is_public' => 1 );
91
92
    my $library_id_1 = $c->param('library_id_1');
93
    my $library_id_2 = $c->param('library_id_2');
94
95
    my $libraries_rs = Koha::Libraries->search(
96
        { branchcode => [ $library_id_1, $library_id_2 ] },
97
        { order_by   => 'branchname' }
98
    );
99
    my $libraries    = $c->objects->search( $libraries_rs );
100
101
    $c->render(
102
        status => 200,
103
        json   => $libraries
104
    );
105
};
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
82
# The tests
119
# The tests
83
use Test::More tests => 12;
120
use Test::More tests => 14;
84
use Test::Mojo;
121
use Test::Mojo;
85
122
86
use t::lib::Mocks;
123
use t::lib::Mocks;
Lines 512-514 subtest 'objects.find helper, embed' => sub { Link Here
512
549
513
    $schema->storage->txn_rollback;
550
    $schema->storage->txn_rollback;
514
};
551
};
515
- 
552
553
subtest 'objects.search helper, public requests' => sub {
554
555
    plan tests => 3;
556
557
    $schema->storage->txn_begin;
558
559
    my $library_1 = $builder->build_object({ class => 'Koha::Libraries', value => { branchname => 'A' } });
560
    my $library_2 = $builder->build_object({ class => 'Koha::Libraries', value => { branchname => 'B' } });
561
562
    my $t = Test::Mojo->new;
563
564
    $t->get_ok( '/libraries/'.$library_1->id.'/'.$library_2->id )
565
      ->json_is('/0' => $library_1->to_api({ public => 1 }), 'Public representation of $library_1 is retrieved')
566
      ->json_is('/1' => $library_2->to_api({ public => 1 }), 'Public representation of $library_2 is retrieved');
567
568
    $schema->storage->txn_rollback;
569
};
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