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

(-)a/t/db_dependent/api/v1/items.t (-2 / +119 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 2;
22
use Test::More tests => 3;
23
use Test::Mojo;
23
use Test::Mojo;
24
use Test::Warn;
24
use Test::Warn;
25
25
Lines 141-143 subtest 'get() tests' => sub { Link Here
141
141
142
    $schema->storage->txn_rollback;
142
    $schema->storage->txn_rollback;
143
};
143
};
144
- 
144
145
subtest 'pickup_locations() tests' => sub {
146
147
    plan tests => 15;
148
149
    $schema->storage->txn_begin;
150
151
    t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
152
153
    # Small trick to ease testing
154
    Koha::Libraries->search->update({ pickup_location => 0 });
155
156
    my $library_1 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'A', pickup_location => 1 } });
157
    my $library_2 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'B', pickup_location => 1 } });
158
    my $library_3 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'C', pickup_location => 1 } });
159
160
    my $library_1_api = $library_1->to_api();
161
    my $library_2_api = $library_2->to_api();
162
    my $library_3_api = $library_3->to_api();
163
164
    $library_1_api->{needs_override} = Mojo::JSON->false;
165
    $library_2_api->{needs_override} = Mojo::JSON->false;
166
    $library_3_api->{needs_override} = Mojo::JSON->true;
167
168
    my $patron = $builder->build_object(
169
        {
170
            class => 'Koha::Patrons',
171
            value => { userid => 'tomasito', flags => 0 }
172
        }
173
    );
174
    my $password = 'thePassword123';
175
    $patron->set_password( { password => $password, skip_validation => 1 } );
176
    my $userid = $patron->userid;
177
    $builder->build(
178
        {
179
            source => 'UserPermission',
180
            value  => {
181
                borrowernumber => $patron->borrowernumber,
182
                module_bit     => 6,
183
                code           => 'place_holds',
184
            },
185
        }
186
    );
187
188
    my $item = $builder->build_sample_item();
189
190
    my $item_class = Test::MockModule->new('Koha::Item');
191
    $item_class->mock(
192
        'pickup_locations',
193
        sub {
194
            my ( $self, $params ) = @_;
195
            my $mock_patron = $params->{patron};
196
            is( $mock_patron->borrowernumber,
197
                $patron->borrowernumber, 'Patron passed correctly' );
198
            return Koha::Libraries->search(
199
                {
200
                    branchcode => {
201
                        '-in' => [
202
                            $library_1->branchcode,
203
                            $library_2->branchcode
204
                        ]
205
                    }
206
                },
207
                {   # we make sure no surprises in the order of the result
208
                    order_by => { '-asc' => 'marcorgcode' }
209
                }
210
            );
211
        }
212
    );
213
214
    $t->get_ok( "//$userid:$password@/api/v1/items/"
215
          . $item->id
216
          . "/pickup_locations?patron_id=" . $patron->id )
217
      ->json_is( [ $library_1_api, $library_2_api ] );
218
219
    # filtering works!
220
    $t->get_ok( "//$userid:$password@/api/v1/items/"
221
          . $item->id
222
          . '/pickup_locations?'
223
          . 'patron_id=' . $patron->id . '&q={"marc_org_code": { "-like": "A%" }}' )
224
      ->json_is( [ $library_1_api ] );
225
226
    t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
227
228
    my $library_4 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 0, marcorgcode => 'X' } });
229
    my $library_5 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1, marcorgcode => 'Y' } });
230
231
    my $library_5_api = $library_5->to_api();
232
    $library_5_api->{needs_override} = Mojo::JSON->true;
233
234
    $t->get_ok( "//$userid:$password@/api/v1/items/"
235
          . $item->id
236
          . "/pickup_locations?"
237
          . "patron_id=" . $patron->id . "&_order_by=marc_org_code" )
238
      ->json_is( [ $library_1_api, $library_2_api, $library_3_api, $library_5_api ] );
239
240
    my $deleted_patron = $builder->build_object({ class => 'Koha::Patrons' });
241
    my $deleted_patron_id = $deleted_patron->id;
242
    $deleted_patron->delete;
243
244
    $t->get_ok( "//$userid:$password@/api/v1/items/"
245
          . $item->id
246
          . "/pickup_locations?"
247
          . "patron_id=" . $deleted_patron_id )
248
      ->status_is( 400 )
249
      ->json_is( '/error' => 'Patron not found' );
250
251
    $item->delete;
252
253
    $t->get_ok( "//$userid:$password@/api/v1/items/"
254
          . $item->id
255
          . "/pickup_locations?"
256
          . "patron_id=" . $patron->id )
257
      ->status_is( 404 )
258
      ->json_is( '/error' => 'Item not found' );
259
260
    $schema->storage->txn_rollback;
261
};

Return to bug 27931