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 336-338 subtest 'restore() tests' => sub { Link Here
336
340
337
    $schema->storage->txn_rollback;
341
    $schema->storage->txn_rollback;
338
};
342
};
343
344
subtest 'restore() with library group permissions tests' => sub {
345
346
    plan tests => 9;
347
348
    $schema->storage->txn_begin;
349
350
    my $librarian = $builder->build_object(
351
        {
352
            class => 'Koha::Patrons',
353
            value => { flags => 0 }
354
        }
355
    );
356
    my $password = 'thePassword123';
357
    $librarian->set_password( { password => $password, skip_validation => 1 } );
358
    my $userid = $librarian->userid;
359
360
    $builder->build(
361
        {
362
            source => 'UserPermission',
363
            value  => {
364
                borrowernumber => $librarian->borrowernumber,
365
                module_bit     => 13,
366
                code           => 'records_restore',
367
            }
368
        }
369
    );
370
371
    my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
372
    my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
373
374
    $librarian->branchcode( $library1->branchcode )->store;
375
376
    my $library_group = Koha::Library::Group->new( { title => 'Test Group', ft_limit_item_editing => 1 } )->store;
377
    Koha::Library::Group->new(
378
        {
379
            parent_id  => $library_group->id,
380
            branchcode => $library1->branchcode,
381
        }
382
    )->store;
383
384
    my $biblio = $builder->build_sample_biblio;
385
    my $item1 =
386
        $builder->build_sample_item( { biblionumber => $biblio->biblionumber, homebranch => $library1->branchcode } );
387
    my $item2 =
388
        $builder->build_sample_item( { biblionumber => $biblio->biblionumber, homebranch => $library2->branchcode } );
389
390
    my $biblionumber = $biblio->biblionumber;
391
    my $item1_id     = $item1->itemnumber;
392
    my $item2_id     = $item2->itemnumber;
393
394
    my $biblio_data     = $biblio->unblessed;
395
    my $biblioitem_data = $biblio->biblioitem->unblessed;
396
    my $metadata_data   = $biblio->metadata->unblessed;
397
    my $item1_data      = $item1->unblessed;
398
    my $item2_data      = $item2->unblessed;
399
400
    my $deleted_biblio = Koha::Old::Biblio->new($biblio_data)->store;
401
    Koha::Old::Biblioitem->new($biblioitem_data)->store;
402
    Koha::Old::Biblio::Metadata->new($metadata_data)->store;
403
    Koha::Old::Item->new($item1_data)->store;
404
    Koha::Old::Item->new($item2_data)->store;
405
406
    $item1->delete;
407
    $item2->delete;
408
    $biblio->metadata->delete;
409
    $biblio->biblioitem->delete;
410
    $biblio->delete;
411
412
    my $body = encode_json( { item_ids => [ $item1_id, $item2_id ] } );
413
414
    $t->put_ok(
415
        "//$userid:$password@/api/v1/deleted/biblios/$biblionumber" => { 'Content-Type' => 'application/json' } =>
416
            $body )->status_is(200)->json_is( '/biblio_id' => $biblionumber )
417
        ->json_is( '/restored_items/0' => $item1_id )->json_is( '/skipped_items/0' => $item2_id );
418
419
    my $restored_biblio = Koha::Biblios->find($biblionumber);
420
    ok( $restored_biblio, 'Biblio restored' );
421
422
    my $restored_item1 = Koha::Items->find($item1_id);
423
    ok( $restored_item1, 'Item from allowed library restored' );
424
425
    my $restored_item2 = Koha::Items->find($item2_id);
426
    is( $restored_item2, undef, 'Item from restricted library not restored' );
427
428
    my $deleted_item2 = Koha::Old::Items->find($item2_id);
429
    ok( $deleted_item2, 'Restricted item still in deleted table' );
430
431
    $schema->storage->txn_rollback;
432
};
(-)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