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

(-)a/t/db_dependent/api/v1/deleted_biblios.t (-1 / +95 lines)
Lines 21-27 use utf8; Link Here
21
use Encode;
21
use Encode;
22
22
23
use Test::NoWarnings;
23
use Test::NoWarnings;
24
use Test::More tests => 4;
24
use Test::More tests => 5;
25
use Test::MockModule;
25
use Test::MockModule;
26
use Test::Mojo;
26
use Test::Mojo;
27
use Test::Warn;
27
use Test::Warn;
Lines 40-45 use Koha::Database; Link Here
40
use Koha::DateUtils qw( dt_from_string output_pref );
40
use Koha::DateUtils qw( dt_from_string output_pref );
41
use Koha::Checkouts;
41
use Koha::Checkouts;
42
use Koha::Old::Checkouts;
42
use Koha::Old::Checkouts;
43
use Koha::Library::Groups;
44
use Koha::Old::Biblioitem;
45
use Koha::Old::Biblioitems;
46
use Koha::Old::Biblio::Metadata;
43
47
44
use Mojo::JSON qw(encode_json);
48
use Mojo::JSON qw(encode_json);
45
49
Lines 330-332 subtest 'restore() tests' => sub { Link Here
330
334
331
    $schema->storage->txn_rollback;
335
    $schema->storage->txn_rollback;
332
};
336
};
337
338
subtest 'restore() with library group permissions tests' => sub {
339
340
    plan tests => 9;
341
342
    $schema->storage->txn_begin;
343
344
    my $librarian = $builder->build_object(
345
        {
346
            class => 'Koha::Patrons',
347
            value => { flags => 0 }
348
        }
349
    );
350
    my $password = 'thePassword123';
351
    $librarian->set_password( { password => $password, skip_validation => 1 } );
352
    my $userid = $librarian->userid;
353
354
    $builder->build(
355
        {
356
            source => 'UserPermission',
357
            value  => {
358
                borrowernumber => $librarian->borrowernumber,
359
                module_bit     => 13,
360
                code           => 'records_restore',
361
            }
362
        }
363
    );
364
365
    my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
366
    my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
367
368
    $librarian->branchcode( $library1->branchcode )->store;
369
370
    my $library_group = Koha::Library::Group->new( { title => 'Test Group', ft_limit_item_editing => 1 } )->store;
371
    Koha::Library::Group->new(
372
        {
373
            parent_id  => $library_group->id,
374
            branchcode => $library1->branchcode,
375
        }
376
    )->store;
377
378
    my $biblio = $builder->build_sample_biblio;
379
    my $item1 =
380
        $builder->build_sample_item( { biblionumber => $biblio->biblionumber, homebranch => $library1->branchcode } );
381
    my $item2 =
382
        $builder->build_sample_item( { biblionumber => $biblio->biblionumber, homebranch => $library2->branchcode } );
383
384
    my $biblionumber = $biblio->biblionumber;
385
    my $item1_id     = $item1->itemnumber;
386
    my $item2_id     = $item2->itemnumber;
387
388
    my $biblio_data     = $biblio->unblessed;
389
    my $biblioitem_data = $biblio->biblioitem->unblessed;
390
    my $metadata_data   = $biblio->metadata->unblessed;
391
    my $item1_data      = $item1->unblessed;
392
    my $item2_data      = $item2->unblessed;
393
394
    my $deleted_biblio = Koha::Old::Biblio->new($biblio_data)->store;
395
    Koha::Old::Biblioitem->new($biblioitem_data)->store;
396
    Koha::Old::Biblio::Metadata->new($metadata_data)->store;
397
    Koha::Old::Item->new($item1_data)->store;
398
    Koha::Old::Item->new($item2_data)->store;
399
400
    $item1->delete;
401
    $item2->delete;
402
    $biblio->metadata->delete;
403
    $biblio->biblioitem->delete;
404
    $biblio->delete;
405
406
    my $body = encode_json( { item_ids => [ $item1_id, $item2_id ] } );
407
408
    $t->put_ok(
409
        "//$userid:$password@/api/v1/deleted/biblios/$biblionumber" => { 'Content-Type' => 'application/json' } =>
410
            $body )->status_is(200)->json_is( '/biblio_id' => $biblionumber )
411
        ->json_is( '/restored_items/0' => $item1_id )->json_is( '/skipped_items/0' => $item2_id );
412
413
    my $restored_biblio = Koha::Biblios->find($biblionumber);
414
    ok( $restored_biblio, 'Biblio restored' );
415
416
    my $restored_item1 = Koha::Items->find($item1_id);
417
    ok( $restored_item1, 'Item from allowed library restored' );
418
419
    my $restored_item2 = Koha::Items->find($item2_id);
420
    is( $restored_item2, undef, 'Item from restricted library not restored' );
421
422
    my $deleted_item2 = Koha::Old::Items->find($item2_id);
423
    ok( $deleted_item2, 'Restricted item still in deleted table' );
424
425
    $schema->storage->txn_rollback;
426
};
(-)a/t/db_dependent/api/v1/deleted_items.t (-5 / +78 lines)
Lines 18-24 Link Here
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::NoWarnings;
20
use Test::NoWarnings;
21
use Test::More tests => 4;
21
use Test::More tests => 5;
22
use Test::Mojo;
22
use Test::Mojo;
23
23
24
use t::lib::TestBuilder;
24
use t::lib::TestBuilder;
Lines 27-32 use t::lib::Mocks; Link Here
27
use Koha::Items;
27
use Koha::Items;
28
use Koha::Old::Items;
28
use Koha::Old::Items;
29
use Koha::Database;
29
use Koha::Database;
30
use Koha::Library::Groups;
30
31
31
my $schema  = Koha::Database->new->schema;
32
my $schema  = Koha::Database->new->schema;
32
my $builder = t::lib::TestBuilder->new;
33
my $builder = t::lib::TestBuilder->new;
Lines 162-169 subtest 'restore() tests' => sub { Link Here
162
    $patron->set_password( { password => $password, skip_validation => 1 } );
163
    $patron->set_password( { password => $password, skip_validation => 1 } );
163
    my $unauth_userid = $patron->userid;
164
    my $unauth_userid = $patron->userid;
164
165
165
    my $item         = $builder->build_sample_item( { barcode => 'TEST_RESTORE_ITEM' } );
166
    my $library = $builder->build_object( { class => 'Koha::Libraries' } );
166
    my $item_id      = $item->itemnumber;
167
    $librarian->branchcode( $library->branchcode )->store;
168
169
    my $item    = $builder->build_sample_item( { barcode => 'TEST_RESTORE_ITEM', homebranch => $library->branchcode } );
170
    my $item_id = $item->itemnumber;
167
    my $item_data    = $item->unblessed;
171
    my $item_data    = $item->unblessed;
168
    my $deleted_item = Koha::Old::Item->new($item_data)->store;
172
    my $deleted_item = Koha::Old::Item->new($item_data)->store;
169
    $item->delete;
173
    $item->delete;
Lines 183-189 subtest 'restore() tests' => sub { Link Here
183
187
184
    $t->put_ok("//$userid:$password@/api/v1/deleted/items/$item_id")->status_is(404);
188
    $t->put_ok("//$userid:$password@/api/v1/deleted/items/$item_id")->status_is(404);
185
189
186
    my $item_without_biblio = $builder->build_sample_item( { barcode => 'TEST_NO_BIBLIO' } );
190
    my $item_without_biblio =
191
        $builder->build_sample_item( { barcode => 'TEST_NO_BIBLIO', homebranch => $library->branchcode } );
187
    my $orphan_item_id      = $item_without_biblio->itemnumber;
192
    my $orphan_item_id      = $item_without_biblio->itemnumber;
188
    my $orphan_biblio_id    = $item_without_biblio->biblionumber;
193
    my $orphan_biblio_id    = $item_without_biblio->biblionumber;
189
    my $orphan_item_data    = $item_without_biblio->unblessed;
194
    my $orphan_item_data    = $item_without_biblio->unblessed;
Lines 196-198 subtest 'restore() tests' => sub { Link Here
196
201
197
    $schema->storage->txn_rollback;
202
    $schema->storage->txn_rollback;
198
};
203
};
199
- 
204
205
subtest 'restore() with library group permissions tests' => sub {
206
207
    plan tests => 7;
208
209
    $schema->storage->txn_begin;
210
211
    my $librarian = $builder->build_object(
212
        {
213
            class => 'Koha::Patrons',
214
            value => { flags => 0 }
215
        }
216
    );
217
    my $password = 'thePassword123';
218
    $librarian->set_password( { password => $password, skip_validation => 1 } );
219
    my $userid = $librarian->userid;
220
221
    $builder->build(
222
        {
223
            source => 'UserPermission',
224
            value  => {
225
                borrowernumber => $librarian->borrowernumber,
226
                module_bit     => 13,
227
                code           => 'records_restore',
228
            }
229
        }
230
    );
231
232
    my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
233
    my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
234
235
    $librarian->branchcode( $library1->branchcode )->store;
236
237
    my $library_group = Koha::Library::Group->new( { title => 'Test Group', ft_limit_item_editing => 1 } )->store;
238
    Koha::Library::Group->new(
239
        {
240
            parent_id  => $library_group->id,
241
            branchcode => $library1->branchcode,
242
        }
243
    )->store;
244
245
    my $item_allowed    = $builder->build_sample_item( { homebranch => $library1->branchcode } );
246
    my $item_restricted = $builder->build_sample_item( { homebranch => $library2->branchcode } );
247
248
    my $item_allowed_id    = $item_allowed->itemnumber;
249
    my $item_restricted_id = $item_restricted->itemnumber;
250
251
    my $item_allowed_data    = $item_allowed->unblessed;
252
    my $item_restricted_data = $item_restricted->unblessed;
253
254
    my $deleted_item_allowed    = Koha::Old::Item->new($item_allowed_data)->store;
255
    my $deleted_item_restricted = Koha::Old::Item->new($item_restricted_data)->store;
256
257
    $item_allowed->delete;
258
    $item_restricted->delete;
259
260
    $t->put_ok("//$userid:$password@/api/v1/deleted/items/$item_allowed_id")->status_is(200);
261
262
    my $restored_item = Koha::Items->find($item_allowed_id);
263
    ok( $restored_item, 'Item from allowed library restored' );
264
265
    $t->put_ok("//$userid:$password@/api/v1/deleted/items/$item_restricted_id")->status_is(403)
266
        ->json_is( '/error' => 'You do not have permission to restore items from this library.' );
267
268
    my $not_restored = Koha::Items->find($item_restricted_id);
269
    is( $not_restored, undef, 'Item from restricted library not restored' );
270
271
    $schema->storage->txn_rollback;
272
};

Return to bug 17387