From a780157b6ece696a52bc24831513aa33c5080f1e Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 21 Oct 2025 13:46:41 +0200 Subject: [PATCH] Bug 40777: Add tests for invalid_item_library and no_item_type --- Koha/Database/DataInconsistency.pm | 19 +- .../Koha/Database/DataInconsistency.t | 186 ++++++++++++++++++ 2 files changed, 198 insertions(+), 7 deletions(-) create mode 100755 t/db_dependent/Koha/Database/DataInconsistency.t diff --git a/Koha/Database/DataInconsistency.pm b/Koha/Database/DataInconsistency.pm index 73178c97880..24af88b04cd 100644 --- a/Koha/Database/DataInconsistency.pm +++ b/Koha/Database/DataInconsistency.pm @@ -19,19 +19,19 @@ sub invalid_item_library { if ( not $item->homebranch and not $item->holdingbranch ) { push @errors, __x( - "Item with itemnumber={itemnumber} does not have homebranch and holdingbranch defined", + "Item with itemnumber={itemnumber} does not have home and holding library defined", itemnumber => $item->itemnumber ); } elsif ( not $item->homebranch ) { push @errors, __x( - "Item with itemnumber={itemnumber} does not have homebranch defined", + "Item with itemnumber={itemnumber} does not have a home library defined", itemnumber => $item->itemnumber ); } else { push @errors, __x( - "Item with itemnumber={itemnumber} does not have holdingbranch defined", + "Item with itemnumber={itemnumber} does not have a holding library defined", itemnumber => $item->itemnumber ); } @@ -67,13 +67,13 @@ sub no_item_type { while ( my $item = $items_without_itype->next ) { if ( defined $item->biblioitem->itemtype && $item->biblioitem->itemtype ne '' ) { push @errors, __x( - "Item with itemnumber={itemnumber} does not have a itype value, biblio's item type will be used ({itemtype})", + "Item with itemnumber={itemnumber} does not have an itype value, biblio's item type will be used ({itemtype})", itemnumber => $item->itemnumber, itemtype => $item->biblioitem->itemtype, ); } else { push @errors, __x( - "Item with itemnumber={itemnumber} does not have a itype value, additionally no item type defined for biblionumber={biblionumber}", + "Item with itemnumber={itemnumber} does not have an itype value, additionally no item type defined for biblionumber={biblionumber}", itemnumber => $item->itemnumber, biblionumber => $item->biblioitem->biblionumber, ); @@ -81,11 +81,16 @@ sub no_item_type { } } } else { - my $biblioitems_without_itemtype = Koha::Biblioitems->search( { biblionumber => $ids, itemtype => undef } ); + my $biblioitems_without_itemtype = Koha::Biblioitems->search( + { + biblionumber => $ids, + -or => [ itemtype => undef, itemtype => '' ] + } + ); if ( $biblioitems_without_itemtype->count ) { while ( my $biblioitem = $biblioitems_without_itemtype->next ) { push @errors, __x( - "Biblioitem with biblioitemnumber={biblioitemnumber} does not have a itemtype value", + "Biblioitem with biblioitemnumber={biblioitemnumber} does not have an itemtype value", biblioitemnumber => $biblioitem->biblioitemnumber, ); } diff --git a/t/db_dependent/Koha/Database/DataInconsistency.t b/t/db_dependent/Koha/Database/DataInconsistency.t new file mode 100755 index 00000000000..cd7d8e97958 --- /dev/null +++ b/t/db_dependent/Koha/Database/DataInconsistency.t @@ -0,0 +1,186 @@ +use Modern::Perl; + +#use Test::NoWarnings; +use Test::More tests => 2; + +use Koha::Items; +use Koha::Database::DataInconsistency; + +use t::lib::TestBuilder; +use t::lib::Mocks; + +my $builder = t::lib::TestBuilder->new; + +my $schema = Koha::Database->new()->schema(); + +subtest 'invalid_item_library' => sub { + + plan tests => 4; + + $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 $items = Koha::Items->search( { biblionumber => [ $biblio_ok->biblionumber, $biblio_ko->biblionumber ] } ); + + subtest 'ok' => sub { + plan tests => 1; + + my @errors = Koha::Database::DataInconsistency->invalid_item_library($items); + is_deeply( \@errors, [] ); + + }; + subtest 'no homebranch, no holdingbranch' => sub { + plan tests => 1; + $item_ko->set( { homebranch => undef, holdingbranch => undef } )->store; + + my @errors = Koha::Database::DataInconsistency->invalid_item_library($items); + is_deeply( + \@errors, + [ sprintf "Item with itemnumber=%s does not have home and holding library defined", $item_ko->itemnumber ] + ); + }; + + subtest 'no homebranch' => sub { + plan tests => 1; + $item_ko->set( { homebranch => undef, holdingbranch => $item_ok->holdingbranch } )->store; + + my @errors = Koha::Database::DataInconsistency->invalid_item_library($items); + is_deeply( + \@errors, + [ sprintf "Item with itemnumber=%s does not have a home library defined", $item_ko->itemnumber ] + ); + }; + + subtest 'no holdingbranch' => sub { + plan tests => 1; + $item_ko->set( { homebranch => $item_ok->homebranch, holdingbranch => undef } )->store; + + my @errors = Koha::Database::DataInconsistency->invalid_item_library($items); + is_deeply( + \@errors, + [ sprintf "Item with itemnumber=%s does not have a holding library defined", $item_ko->itemnumber ] + ); + }; + + $schema->storage->txn_rollback(); +}; + +subtest 'no_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 ] } ); + + subtest 'item-level_itypes = 1' => sub { + plan tests => 3; + t::lib::Mocks::mock_preference( 'item-level_itypes', 1 ); + subtest 'ok' => sub { + plan tests => 1; + my @errors = Koha::Database::DataInconsistency->no_item_type($biblios); + is_deeply( \@errors, [] ); + }; + subtest 'itype => undef' => sub { + plan tests => 2; + $item_ko->set( { itype => undef } ); + Koha::Object::store($item_ko); # Do not call Koha::Item->store, it will set itype + $biblio_ko->biblioitem->set( { itemtype => $item_ok->itype } )->store; + + my @errors = Koha::Database::DataInconsistency->no_item_type($biblios); + is_deeply( + \@errors, + [ + sprintf + "Item with itemnumber=%s does not have an itype value, biblio's item type will be used (%s)", + $item_ko->itemnumber, $biblio_ko->itemtype + ] + ); + + $biblio_ko->biblioitem->set( { itemtype => undef } )->store; + @errors = Koha::Database::DataInconsistency->no_item_type($biblios); + is_deeply( + \@errors, + [ + sprintf + "Item with itemnumber=%s does not have an itype value, additionally no item type defined for biblionumber=%s", + $item_ko->itemnumber, $biblio_ko->biblionumber + ] + ); + }; + subtest 'itype => ""' => sub { + plan tests => 2; + $item_ko->set( { itype => "" } ); + Koha::Object::store($item_ko); # Do not call Koha::Item->store, it will set itype + $biblio_ko->biblioitem->set( { itemtype => $item_ok->itype } )->store; + + my @errors = Koha::Database::DataInconsistency->no_item_type($biblios); + is_deeply( + \@errors, + [ + sprintf + "Item with itemnumber=%s does not have an itype value, biblio's item type will be used (%s)", + $item_ko->itemnumber, $biblio_ko->itemtype + ] + ); + + $biblio_ko->biblioitem->set( { itemtype => undef } )->store; + @errors = Koha::Database::DataInconsistency->no_item_type($biblios); + is_deeply( + \@errors, + [ + sprintf + "Item with itemnumber=%s does not have an itype value, additionally no item type defined for biblionumber=%s", + $item_ko->itemnumber, $biblio_ko->biblionumber + ] + ); + }; + }; + + subtest 'item-level_itypes = 0' => sub { + plan tests => 3; + t::lib::Mocks::mock_preference( 'item-level_itypes', 0 ); + subtest 'ok' => sub { + plan tests => 1; + $biblio_ko->biblioitem->set( { itemtype => $item_ok->itype } )->store; + my @errors = Koha::Database::DataInconsistency->no_item_type($biblios); + is_deeply( \@errors, [] ); + }; + subtest 'itemtype => undef' => sub { + plan tests => 1; + $biblio_ko->biblioitem->set( { itemtype => undef } )->store; + + my @errors = Koha::Database::DataInconsistency->no_item_type($biblios); + is_deeply( + \@errors, + [ + sprintf "Biblioitem with biblioitemnumber=%s does not have an itemtype value", + $biblio_ko->biblioitem->biblioitemnumber + ] + ); + }; + subtest 'itemtype => ""' => sub { + plan tests => 1; + $biblio_ko->biblioitem->set( { itemtype => '' } )->store; + + my @errors = Koha::Database::DataInconsistency->no_item_type($biblios); + is_deeply( + \@errors, + [ + sprintf "Biblioitem with biblioitemnumber=%s does not have an itemtype value", + $biblio_ko->biblioitem->biblioitemnumber + ] + ); + }; + }; + $schema->storage->txn_rollback(); +}; -- 2.34.1