From e680b06c72280c9445ef744c24063bf07cc6b324 Mon Sep 17 00:00:00 2001 From: Jacob O'Mara Date: Wed, 5 Nov 2025 15:14:12 +0000 Subject: [PATCH] Bug 17387: (follow-up) restore tests should account for new library groups checks --- t/db_dependent/api/v1/deleted_biblios.t | 96 ++++++++++++++++++++++++- t/db_dependent/api/v1/deleted_items.t | 82 +++++++++++++++++++-- 2 files changed, 173 insertions(+), 5 deletions(-) diff --git a/t/db_dependent/api/v1/deleted_biblios.t b/t/db_dependent/api/v1/deleted_biblios.t index bf9c8bccf4e..a8bd570a7b1 100755 --- a/t/db_dependent/api/v1/deleted_biblios.t +++ b/t/db_dependent/api/v1/deleted_biblios.t @@ -21,7 +21,7 @@ use utf8; use Encode; use Test::NoWarnings; -use Test::More tests => 4; +use Test::More tests => 5; use Test::MockModule; use Test::Mojo; use Test::Warn; @@ -40,6 +40,10 @@ use Koha::Database; use Koha::DateUtils qw( dt_from_string output_pref ); use Koha::Checkouts; use Koha::Old::Checkouts; +use Koha::Library::Groups; +use Koha::Old::Biblioitem; +use Koha::Old::Biblioitems; +use Koha::Old::Biblio::Metadata; use Mojo::JSON qw(encode_json); @@ -330,3 +334,93 @@ subtest 'restore() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'restore() with library group permissions tests' => sub { + + plan tests => 9; + + $schema->storage->txn_begin; + + my $librarian = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 0 } + } + ); + my $password = 'thePassword123'; + $librarian->set_password( { password => $password, skip_validation => 1 } ); + my $userid = $librarian->userid; + + $builder->build( + { + source => 'UserPermission', + value => { + borrowernumber => $librarian->borrowernumber, + module_bit => 13, + code => 'records_restore', + } + } + ); + + my $library1 = $builder->build_object( { class => 'Koha::Libraries' } ); + my $library2 = $builder->build_object( { class => 'Koha::Libraries' } ); + + $librarian->branchcode( $library1->branchcode )->store; + + my $library_group = Koha::Library::Group->new( { title => 'Test Group', ft_limit_item_editing => 1 } )->store; + Koha::Library::Group->new( + { + parent_id => $library_group->id, + branchcode => $library1->branchcode, + } + )->store; + + my $biblio = $builder->build_sample_biblio; + my $item1 = + $builder->build_sample_item( { biblionumber => $biblio->biblionumber, homebranch => $library1->branchcode } ); + my $item2 = + $builder->build_sample_item( { biblionumber => $biblio->biblionumber, homebranch => $library2->branchcode } ); + + my $biblionumber = $biblio->biblionumber; + my $item1_id = $item1->itemnumber; + my $item2_id = $item2->itemnumber; + + my $biblio_data = $biblio->unblessed; + my $biblioitem_data = $biblio->biblioitem->unblessed; + my $metadata_data = $biblio->metadata->unblessed; + my $item1_data = $item1->unblessed; + my $item2_data = $item2->unblessed; + + my $deleted_biblio = Koha::Old::Biblio->new($biblio_data)->store; + Koha::Old::Biblioitem->new($biblioitem_data)->store; + Koha::Old::Biblio::Metadata->new($metadata_data)->store; + Koha::Old::Item->new($item1_data)->store; + Koha::Old::Item->new($item2_data)->store; + + $item1->delete; + $item2->delete; + $biblio->metadata->delete; + $biblio->biblioitem->delete; + $biblio->delete; + + my $body = encode_json( { item_ids => [ $item1_id, $item2_id ] } ); + + $t->put_ok( + "//$userid:$password@/api/v1/deleted/biblios/$biblionumber" => { 'Content-Type' => 'application/json' } => + $body )->status_is(200)->json_is( '/biblio_id' => $biblionumber ) + ->json_is( '/restored_items/0' => $item1_id )->json_is( '/skipped_items/0' => $item2_id ); + + my $restored_biblio = Koha::Biblios->find($biblionumber); + ok( $restored_biblio, 'Biblio restored' ); + + my $restored_item1 = Koha::Items->find($item1_id); + ok( $restored_item1, 'Item from allowed library restored' ); + + my $restored_item2 = Koha::Items->find($item2_id); + is( $restored_item2, undef, 'Item from restricted library not restored' ); + + my $deleted_item2 = Koha::Old::Items->find($item2_id); + ok( $deleted_item2, 'Restricted item still in deleted table' ); + + $schema->storage->txn_rollback; +}; diff --git a/t/db_dependent/api/v1/deleted_items.t b/t/db_dependent/api/v1/deleted_items.t index 0492cffe172..76514504cf7 100755 --- a/t/db_dependent/api/v1/deleted_items.t +++ b/t/db_dependent/api/v1/deleted_items.t @@ -18,7 +18,7 @@ use Modern::Perl; use Test::NoWarnings; -use Test::More tests => 4; +use Test::More tests => 5; use Test::Mojo; use t::lib::TestBuilder; @@ -27,6 +27,7 @@ use t::lib::Mocks; use Koha::Items; use Koha::Old::Items; use Koha::Database; +use Koha::Library::Groups; my $schema = Koha::Database->new->schema; my $builder = t::lib::TestBuilder->new; @@ -162,8 +163,11 @@ subtest 'restore() tests' => sub { $patron->set_password( { password => $password, skip_validation => 1 } ); my $unauth_userid = $patron->userid; - my $item = $builder->build_sample_item( { barcode => 'TEST_RESTORE_ITEM' } ); - my $item_id = $item->itemnumber; + my $library = $builder->build_object( { class => 'Koha::Libraries' } ); + $librarian->branchcode( $library->branchcode )->store; + + my $item = $builder->build_sample_item( { barcode => 'TEST_RESTORE_ITEM', homebranch => $library->branchcode } ); + my $item_id = $item->itemnumber; my $item_data = $item->unblessed; my $deleted_item = Koha::Old::Item->new($item_data)->store; $item->delete; @@ -183,7 +187,8 @@ subtest 'restore() tests' => sub { $t->put_ok("//$userid:$password@/api/v1/deleted/items/$item_id")->status_is(404); - my $item_without_biblio = $builder->build_sample_item( { barcode => 'TEST_NO_BIBLIO' } ); + my $item_without_biblio = + $builder->build_sample_item( { barcode => 'TEST_NO_BIBLIO', homebranch => $library->branchcode } ); my $orphan_item_id = $item_without_biblio->itemnumber; my $orphan_biblio_id = $item_without_biblio->biblionumber; my $orphan_item_data = $item_without_biblio->unblessed; @@ -196,3 +201,72 @@ subtest 'restore() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'restore() with library group permissions tests' => sub { + + plan tests => 7; + + $schema->storage->txn_begin; + + my $librarian = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 0 } + } + ); + my $password = 'thePassword123'; + $librarian->set_password( { password => $password, skip_validation => 1 } ); + my $userid = $librarian->userid; + + $builder->build( + { + source => 'UserPermission', + value => { + borrowernumber => $librarian->borrowernumber, + module_bit => 13, + code => 'records_restore', + } + } + ); + + my $library1 = $builder->build_object( { class => 'Koha::Libraries' } ); + my $library2 = $builder->build_object( { class => 'Koha::Libraries' } ); + + $librarian->branchcode( $library1->branchcode )->store; + + my $library_group = Koha::Library::Group->new( { title => 'Test Group', ft_limit_item_editing => 1 } )->store; + Koha::Library::Group->new( + { + parent_id => $library_group->id, + branchcode => $library1->branchcode, + } + )->store; + + my $item_allowed = $builder->build_sample_item( { homebranch => $library1->branchcode } ); + my $item_restricted = $builder->build_sample_item( { homebranch => $library2->branchcode } ); + + my $item_allowed_id = $item_allowed->itemnumber; + my $item_restricted_id = $item_restricted->itemnumber; + + my $item_allowed_data = $item_allowed->unblessed; + my $item_restricted_data = $item_restricted->unblessed; + + my $deleted_item_allowed = Koha::Old::Item->new($item_allowed_data)->store; + my $deleted_item_restricted = Koha::Old::Item->new($item_restricted_data)->store; + + $item_allowed->delete; + $item_restricted->delete; + + $t->put_ok("//$userid:$password@/api/v1/deleted/items/$item_allowed_id")->status_is(200); + + my $restored_item = Koha::Items->find($item_allowed_id); + ok( $restored_item, 'Item from allowed library restored' ); + + $t->put_ok("//$userid:$password@/api/v1/deleted/items/$item_restricted_id")->status_is(403) + ->json_is( '/error' => 'You do not have permission to restore items from this library.' ); + + my $not_restored = Koha::Items->find($item_restricted_id); + is( $not_restored, undef, 'Item from restricted library not restored' ); + + $schema->storage->txn_rollback; +}; -- 2.39.5