From d6ed1bdb44ec385f81e0ff7babb413fdb159b074 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 21 Oct 2025 14:35:18 +0200 Subject: [PATCH] Bug 40777: Add tests for invalid_item_type --- Koha/Database/DataInconsistency.pm | 11 ++- .../Koha/Database/DataInconsistency.t | 82 ++++++++++++++++++- 2 files changed, 89 insertions(+), 4 deletions(-) diff --git a/Koha/Database/DataInconsistency.pm b/Koha/Database/DataInconsistency.pm index 24af88b04cd..f3f6a93bf86 100644 --- a/Koha/Database/DataInconsistency.pm +++ b/Koha/Database/DataInconsistency.pm @@ -122,13 +122,18 @@ sub invalid_item_type { } } } else { - my $biblioitems_with_invalid_itemtype = - Koha::Biblioitems->search( { biblionumber => $ids, itemtype => { not_in => \@itemtypes } } ); + my $biblioitems_with_invalid_itemtype = Koha::Biblioitems->search( + { + biblionumber => $ids, + -and => [ itemtype => { not_in => \@itemtypes }, itemtype => { '!=' => '' } ] + } + ); if ( $biblioitems_with_invalid_itemtype->count ) { while ( my $biblioitem = $biblioitems_with_invalid_itemtype->next ) { push @errors, __x( - "Biblioitem with biblioitemnumber={biblioitemnumber} does not have a valid itemtype value", + "Biblioitem with biblioitemnumber={biblioitemnumber} does not have a valid itemtype value ({itemtype})", biblioitemnumber => $biblioitem->biblioitemnumber, + itemtype => $biblioitem->itemtype, ); } } diff --git a/t/db_dependent/Koha/Database/DataInconsistency.t b/t/db_dependent/Koha/Database/DataInconsistency.t index cd7d8e97958..d8767217230 100755 --- a/t/db_dependent/Koha/Database/DataInconsistency.t +++ b/t/db_dependent/Koha/Database/DataInconsistency.t @@ -1,7 +1,7 @@ use Modern::Perl; #use Test::NoWarnings; -use Test::More tests => 2; +use Test::More tests => 3; use Koha::Items; use Koha::Database::DataInconsistency; @@ -184,3 +184,83 @@ subtest 'no_item_type' => sub { }; $schema->storage->txn_rollback(); }; + +subtest 'invalid_item_type' => sub { + + plan tests => 2; + + $schema->storage->txn_begin(); + + my $biblio_ok = $builder->build_sample_biblio; + my $item_ok = $builder->build_sample_item( { biblionumber => $biblio_ok->biblionumber } ); + my $biblio_ko = $builder->build_sample_biblio; + my $item_ko = $builder->build_sample_item( { biblionumber => $biblio_ko->biblionumber } ); + + my $biblios = Koha::Biblios->search( { biblionumber => [ $biblio_ok->biblionumber, $biblio_ko->biblionumber ] } ); + + my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } ); + my $deleted_itemtype = $itemtype->itemtype; + $itemtype->delete; + + subtest 'item-level_itypes = 1' => sub { + plan tests => 2; + t::lib::Mocks::mock_preference( 'item-level_itypes', 1 ); + subtest 'ok' => sub { + plan tests => 1; + my @errors = Koha::Database::DataInconsistency->invalid_item_type($biblios); + is_deeply( \@errors, [] ); + }; + subtest 'itype is invalid' => sub { + plan tests => 2; + $item_ko->set( { itype => $deleted_itemtype } ); + Koha::Object::store($item_ko); # Do not call Koha::Item->store, it will set itype + + my @errors = Koha::Database::DataInconsistency->invalid_item_type($biblios); + is_deeply( + \@errors, + [ + sprintf "Item with itemnumber=%s, biblionumber=%s does not have a valid itype value (%s)", + $item_ko->itemnumber, $item_ko->biblionumber, $item_ko->itype + ] + ); + + $item_ko->set( { itype => '' } ); + Koha::Object::store($item_ko); # Do not call Koha::Item->store, it will set itype + @errors = Koha::Database::DataInconsistency->invalid_item_type($biblios); + + # Does not alert here, it's caught already in no_item_type + is_deeply( \@errors, [] ); + }; + }; + + subtest 'item-level_itypes = 0' => sub { + plan tests => 2; + t::lib::Mocks::mock_preference( 'item-level_itypes', 0 ); + subtest 'ok' => sub { + plan tests => 1; + my @errors = Koha::Database::DataInconsistency->invalid_item_type($biblios); + is_deeply( \@errors, [] ); + }; + subtest 'itemtype is invalid' => sub { + plan tests => 2; + $biblio_ko->biblioitem->set( { itemtype => $deleted_itemtype } )->store; + + my @errors = Koha::Database::DataInconsistency->invalid_item_type($biblios); + is_deeply( + \@errors, + [ + sprintf "Biblioitem with biblioitemnumber=%s does not have a valid itemtype value (%s)", + $biblio_ko->biblioitem->biblioitemnumber, $biblio_ko->biblioitem->itemtype + ] + ); + + $biblio_ko->biblioitem->set( { itemtype => ' ' } )->store; + @errors = Koha::Database::DataInconsistency->invalid_item_type($biblios); + + # Does not alert here, it's caught already in no_item_type + is_deeply( \@errors, [] ); + }; + }; + + $schema->storage->txn_rollback(); +}; -- 2.34.1