3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Test::More tests => 2;
24 use t::lib::TestBuilder;
29 my $schema = Koha::Database->new->schema;
30 my $builder = t::lib::TestBuilder->new();
32 my $t = Test::Mojo->new('Koha::REST::V1');
34 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
36 subtest 'list() tests' => sub {
40 $schema->storage->txn_begin;
42 my $patron = $builder->build_object({
43 class => 'Koha::Patrons',
44 value => { flags => 2 ** 4 } # 'borrowers' flag == 4
46 my $password = 'thePassword123';
47 $patron->set_password({ password => $password, skip_validation => 1 });
48 my $userid = $patron->userid;
50 $t->get_ok("//$userid:$password@/api/v1/patrons/" . $patron->id . '/holds')
51 ->status_is( 200, 'SWAGGER3.2.2' )
54 my $hold_1 = $builder->build_object( { class => 'Koha::Holds', value => { borrowernumber => $patron->id } } );
55 my $hold_2 = $builder->build_object( { class => 'Koha::Holds', value => { borrowernumber => $patron->id } } );
56 my $hold_3 = $builder->build_object( { class => 'Koha::Holds', value => { borrowernumber => $patron->id } } );
58 $t->get_ok( "//$userid:$password@/api/v1/patrons/" . $patron->id . '/holds?_order_by=+me.hold_id' )
59 ->status_is( 200, 'SWAGGER3.2.2' )
60 ->json_is( '' => [ $hold_1->to_api, $hold_2->to_api, $hold_3->to_api ], 'Holds retrieved' );
65 $t->get_ok( "//$userid:$password@/api/v1/patrons/" . $patron->id . '/holds?_order_by=+me.hold_id' )
66 ->status_is( 200, 'SWAGGER3.2.2' )->json_is( '' => [ $hold_2->to_api ], 'Only current holds retrieved' );
68 $t->get_ok( "//$userid:$password@/api/v1/patrons/" . $patron->id . '/holds?old=1&_order_by=+me.hold_id' )
69 ->status_is( 200, 'SWAGGER3.2.2' )
70 ->json_is( '' => [ $hold_1->to_api, $hold_3->to_api ], 'Only old holds retrieved' );
72 my $old_hold_1 = Koha::Old::Holds->find( $hold_1->id );
73 $old_hold_1->item->delete;
74 $old_hold_1->pickup_library->delete;
76 $t->get_ok( "//$userid:$password@/api/v1/patrons/" . $patron->id . '/holds?old=1&_order_by=+me.hold_id' )
77 ->status_is( 200, 'SWAGGER3.2.2' )->json_is(
78 '' => [ $old_hold_1->get_from_storage->to_api, $hold_3->to_api ],
79 'Old holds even after item and library removed'
82 my $non_existent_patron = $builder->build_object({ class => 'Koha::Patrons' });
83 my $non_existent_patron_id = $non_existent_patron->id;
84 # get rid of the patron
85 $non_existent_patron->delete;
87 $t->get_ok("//$userid:$password@/api/v1/patrons/" . $non_existent_patron_id . '/holds')
89 ->json_is( '/error' => 'Patron not found' );
91 $schema->storage->txn_rollback;
94 subtest 'delete_public() tests' => sub {
98 $schema->storage->txn_begin;
100 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
102 my $patron = $builder->build_object(
104 class => 'Koha::Patrons',
105 value => { flags => 0 },
108 my $password = 'thePassword123';
109 $patron->set_password( { password => $password, skip_validation => 1 } );
110 my $userid = $patron->userid;
112 my $hold_to_delete = $builder->build_object( { class => 'Koha::Holds' } );
113 my $deleted_hold_id = $hold_to_delete->id;
114 $hold_to_delete->delete;
116 $t->delete_ok( "//$userid:$password@/api/v1/public/patrons/" . $patron->id . '/holds/' . $deleted_hold_id )
119 my $another_user_hold = $builder->build_object( { class => 'Koha::Holds' } );
121 $t->delete_ok( "//$userid:$password@/api/v1/public/patrons/" . $patron->id . '/holds/' . $another_user_hold->id )
122 ->status_is( 404, 'Invalid patron_id and hold_id combination yields 404' );
124 my $non_waiting_hold = $builder->build_object(
126 class => 'Koha::Holds',
128 borrowernumber => $patron->id,
135 $t->delete_ok( "//$userid:$password@/api/v1/public/patrons/" . $patron->id . '/holds/' . $non_waiting_hold->id )
136 ->status_is( 204, 'SWAGGER3.2.4' )->content_is( '', 'SWAGGER3.3.4' );
138 my $cancellation_requestable;
140 my $hold_mock = Test::MockModule->new('Koha::Hold');
141 $hold_mock->mock( 'cancellation_requestable_from_opac', sub { return $cancellation_requestable; } );
143 my $item = $builder->build_sample_item;
144 my $waiting_hold = $builder->build_object(
146 class => 'Koha::Holds',
148 borrowernumber => $patron->id,
150 itemnumber => $item->id,
155 $cancellation_requestable = 0;
157 $t->delete_ok( "//$userid:$password@/api/v1/public/patrons/" . $patron->id . '/holds/' . $waiting_hold->id )
158 ->status_is(403)->json_is( { error => 'Cancellation forbidden' } );
160 $cancellation_requestable = 1;
162 $t->delete_ok( "//$userid:$password@/api/v1/public/patrons/" . $patron->id . '/holds/' . $waiting_hold->id )
165 my $cancellation_requests = $waiting_hold->cancellation_requests;
166 is( $cancellation_requests->count, 1, 'Cancellation request recorded' );
168 $schema->storage->txn_rollback;