Bug 17387: add restore item/biblio tests

Signed-off-by: Michaela <michaela.sieber@kit.edu>
Sponsored-by: ByWater Solutions
Signed-off-by: Martin Renvoize <martin.renvoize@openfifth.co.uk>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
This commit is contained in:
Jacob O'Mara 2025-10-30 10:09:03 +00:00 committed by Lucas Gass
parent 3dd662a8d7
commit 16637d8b4f
No known key found for this signature in database
GPG Key ID: 60B447398B0D36CA
2 changed files with 257 additions and 1 deletions

View File

@ -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;
@ -267,3 +267,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 => 13,
code => 'records_restore',
}
}
);
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;
};

View File

@ -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 <https://www.gnu.org/licenses>.
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 => 13,
code => 'records_restore',
}
}
);
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;
};