From 707b3e36fea86fe9c68c721c304ce70dec49c07a Mon Sep 17 00:00:00 2001 From: Jacob O'Mara Date: Thu, 30 Oct 2025 10:09:03 +0000 Subject: [PATCH] Bug 17387: add restore item/biblio tests Signed-off-by: Michaela --- t/db_dependent/api/v1/deleted_biblios.t | 71 ++++++++- t/db_dependent/api/v1/deleted_items.t | 187 ++++++++++++++++++++++++ 2 files changed, 257 insertions(+), 1 deletion(-) create mode 100755 t/db_dependent/api/v1/deleted_items.t diff --git a/t/db_dependent/api/v1/deleted_biblios.t b/t/db_dependent/api/v1/deleted_biblios.t index bd80fbacff..b859192236 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 => 3; +use Test::More tests => 4; use Test::MockModule; use Test::Mojo; use Test::Warn; @@ -261,3 +261,72 @@ subtest 'list() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'restore() tests' => sub { + + plan tests => 13; + + $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 => 9, + code => 'restore_records', + } + } + ); + + my $patron = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 0 } + } + ); + + $patron->set_password( { password => $password, skip_validation => 1 } ); + my $unauth_userid = $patron->userid; + + my $biblio = $builder->build_sample_biblio( + { + title => 'Test Restore Biblio', + author => 'Test Author' + } + ); + + my $biblionumber = $biblio->biblionumber; + + DelBiblio($biblionumber); + + is( Koha::Biblios->find($biblionumber), undef, 'Biblio deleted successfully' ); + my $deleted_biblio = Koha::Old::Biblios->find($biblionumber); + ok( $deleted_biblio, 'Biblio found in deleted table' ); + + $t->put_ok("//$unauth_userid:$password@/api/v1/deleted/biblios/$biblionumber")->status_is(403); + + $t->put_ok("//$userid:$password@/api/v1/deleted/biblios/$biblionumber")->status_is(200) + ->json_is( '/biblio_id' => $biblionumber ); + + my $restored_biblio = Koha::Biblios->find($biblionumber); + ok( $restored_biblio, 'Biblio restored successfully' ); + is( $restored_biblio->title, 'Test Restore Biblio', 'Biblio title preserved' ); + is( $restored_biblio->author, 'Test Author', 'Biblio author preserved' ); + + is( Koha::Old::Biblios->find($biblionumber), undef, 'Biblio removed from deleted table' ); + + $t->put_ok("//$userid:$password@/api/v1/deleted/biblios/$biblionumber")->status_is(404); + + $schema->storage->txn_rollback; +}; diff --git a/t/db_dependent/api/v1/deleted_items.t b/t/db_dependent/api/v1/deleted_items.t new file mode 100755 index 0000000000..464e1a18b7 --- /dev/null +++ b/t/db_dependent/api/v1/deleted_items.t @@ -0,0 +1,187 @@ +#!/usr/bin/env perl + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Test::NoWarnings; +use Test::More tests => 4; +use Test::Mojo; + +use t::lib::TestBuilder; +use t::lib::Mocks; + +use Koha::Items; +use Koha::Old::Items; +use Koha::Database; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +my $t = Test::Mojo->new('Koha::REST::V1'); +t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); + +subtest 'list() tests' => sub { + + plan tests => 9; + + $schema->storage->txn_begin; + + Koha::Old::Items->search->delete; + + my $librarian = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 2**2 } + } + ); + my $password = 'thePassword123'; + $librarian->set_password( { password => $password, skip_validation => 1 } ); + my $userid = $librarian->userid; + + my $patron = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 0 } + } + ); + + $patron->set_password( { password => $password, skip_validation => 1 } ); + my $unauth_userid = $patron->userid; + + $t->get_ok("//$userid:$password@/api/v1/deleted/items")->status_is(200)->json_is( [] ); + + my $item = $builder->build_sample_item(); + my $item_id = $item->itemnumber; + my $item_data = $item->unblessed; + my $deleted_item = Koha::Old::Item->new($item_data)->store; + $item->delete; + + $t->get_ok("//$userid:$password@/api/v1/deleted/items")->status_is(200); + + my $expected = $deleted_item->to_api; + + $t->json_has('/0')->json_is( '/0' => $expected ); + + $t->get_ok("//$unauth_userid:$password@/api/v1/deleted/items")->status_is(403); + + $schema->storage->txn_rollback; +}; + +subtest 'get() tests' => sub { + + plan tests => 8; + + $schema->storage->txn_begin; + + my $item = $builder->build_sample_item(); + my $item_id = $item->itemnumber; + my $item_data = $item->unblessed; + my $deleted_item = Koha::Old::Item->new($item_data)->store; + $item->delete; + + my $librarian = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 2**2 } + } + ); + my $password = 'thePassword123'; + $librarian->set_password( { password => $password, skip_validation => 1 } ); + my $userid = $librarian->userid; + + my $patron = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 0 } + } + ); + + $patron->set_password( { password => $password, skip_validation => 1 } ); + my $unauth_userid = $patron->userid; + + $t->get_ok("//$userid:$password@/api/v1/deleted/items/$item_id")->status_is(200)->json_is( $deleted_item->to_api ); + + $t->get_ok("//$unauth_userid:$password@/api/v1/deleted/items/$item_id")->status_is(403); + + my $non_existent_id = $item_id + 99999; + + $t->get_ok("//$userid:$password@/api/v1/deleted/items/$non_existent_id")->status_is(404) + ->json_is( '/error' => 'Item not found' ); + + $schema->storage->txn_rollback; +}; + +subtest 'restore() tests' => sub { + + plan tests => 11; + + $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 => 9, + code => 'restore_records', + } + } + ); + + my $patron = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 0 } + } + ); + + $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 $item_data = $item->unblessed; + my $deleted_item = Koha::Old::Item->new($item_data)->store; + $item->delete; + + is( Koha::Items->find($item_id), undef, 'Item deleted successfully' ); + ok( $deleted_item, 'Item found in deleted table' ); + + $t->put_ok("//$unauth_userid:$password@/api/v1/deleted/items/$item_id")->status_is(403); + + $t->put_ok("//$userid:$password@/api/v1/deleted/items/$item_id")->status_is(200); + + my $restored_item = Koha::Items->find($item_id); + ok( $restored_item, 'Item restored successfully' ); + is( $restored_item->barcode, 'TEST_RESTORE_ITEM', 'Item barcode preserved' ); + + is( Koha::Old::Items->find($item_id), undef, 'Item removed from deleted table' ); + + $t->put_ok("//$userid:$password@/api/v1/deleted/items/$item_id")->status_is(404); + + $schema->storage->txn_rollback; +}; -- 2.39.5