From 1d6e4f934bf2ddbd8d731cdb1a2977b2acd8379e Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 6 Mar 2025 13:27:07 +0000 Subject: [PATCH] Bug 26869: Unit tests This patch adds comprehensive unit tests for the two newly introduced methods. Signed-off-by: Martin Renvoize --- .../Koha/BackgroundJob/BatchUpdateBiblio.t | 234 +++++++++++++++++- 1 file changed, 233 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Koha/BackgroundJob/BatchUpdateBiblio.t b/t/db_dependent/Koha/BackgroundJob/BatchUpdateBiblio.t index 9a21b3b2ed2..86418974b5f 100755 --- a/t/db_dependent/Koha/BackgroundJob/BatchUpdateBiblio.t +++ b/t/db_dependent/Koha/BackgroundJob/BatchUpdateBiblio.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 1; +use Test::More tests => 3; use Koha::Database; use Koha::BackgroundJobs; @@ -54,3 +54,235 @@ subtest 'enqueue() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'marc_record_contains_item_data tests' => sub { + plan tests => 6; + + # Mock the MARC21 flavor preference + t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' ); + + # Test with no item fields + my $record = MARC::Record->new(); + is( + Koha::BackgroundJob::BatchUpdateBiblio::marc_record_contains_item_data($record), 0, + 'Returns 0 with no item fields' + ); + + # Test with one item field + $record->append_fields( MARC::Field->new( '952', ' ', ' ', 'a' => 'test' ) ); + is( + Koha::BackgroundJob::BatchUpdateBiblio::marc_record_contains_item_data($record), 1, + 'Returns 1 with one item field' + ); + + # Test with multiple item fields + $record->append_fields( MARC::Field->new( '952', ' ', ' ', 'a' => 'test2' ) ); + is( + Koha::BackgroundJob::BatchUpdateBiblio::marc_record_contains_item_data($record), 2, + 'Returns 2 with two item fields' + ); + + # Mock the UNIMARC flavor preference + t::lib::Mocks::mock_preference( 'marcflavour', 'UNIMARC' ); + + # Test with no item fields + $record = MARC::Record->new(); + is( + Koha::BackgroundJob::BatchUpdateBiblio::marc_record_contains_item_data($record), 0, + 'Returns 0 with no item fields' + ); + + # Test with one item field + $record->append_fields( MARC::Field->new( '995', ' ', ' ', 'a' => 'test' ) ); + is( + Koha::BackgroundJob::BatchUpdateBiblio::marc_record_contains_item_data($record), 1, + 'Returns 1 with one item field' + ); + + # Test with multiple item fields + $record->append_fields( MARC::Field->new( '995', ' ', ' ', 'a' => 'test2' ) ); + is( + Koha::BackgroundJob::BatchUpdateBiblio::marc_record_contains_item_data($record), 2, + 'Returns 2 with two item fields' + ); +}; + +subtest 'can_add_item_from_marc_record tests' => sub { + plan tests => 7; + $schema->storage->txn_begin; + + t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' ); + + # Set item-level_itypes preference for testing + t::lib::Mocks::mock_preference( 'item-level_itypes', 1 ); + + # Tests for can_add_item_from_marc_record with all fields + subtest 'can_add_item_from_marc_record with all fields' => sub { + plan tests => 2; + + my $record = MARC::Record->new(); + $record->append_fields( + MARC::Field->new( + '952', ' ', ' ', + 'a' => 'CENTRAL', # homebranch + 'b' => 'CENTRAL', # holdingbranch + 'y' => 'BOOK' # itype + ) + ); + + my ( $result, $error ) = Koha::BackgroundJob::BatchUpdateBiblio::can_add_item_from_marc_record($record); + is( $result, 1, 'Returns 1 when all required fields exist' ); + is( $error, undef, 'No error message when successful' ); + }; + + # Test missing holdingbranch + subtest 'can_add_item_from_marc_record missing holdingbranch' => sub { + plan tests => 2; + + my $record = MARC::Record->new(); + $record->append_fields( + MARC::Field->new( + '952', ' ', ' ', + 'a' => 'CENTRAL', # homebranch + 'y' => 'BOOK' # itype + # No 'b' for holdingbranch + ) + ); + + my ( $result, $error ) = Koha::BackgroundJob::BatchUpdateBiblio::can_add_item_from_marc_record($record); + is( $result, 0, 'Returns 0 when holdingbranch is missing' ); + is( $error, 'No holdingbranch subfield found', 'Correct error message for missing holdingbranch' ); + }; + + # Test missing homebranch + subtest 'can_add_item_from_marc_record missing homebranch' => sub { + plan tests => 2; + + my $record = MARC::Record->new(); + $record->append_fields( + MARC::Field->new( + '952', ' ', ' ', + 'b' => 'CENTRAL', # holdingbranch + 'y' => 'BOOK' # itype + # No 'a' for homebranch + ) + ); + + my ( $result, $error ) = Koha::BackgroundJob::BatchUpdateBiblio::can_add_item_from_marc_record($record); + is( $result, 0, 'Returns 0 when homebranch is missing' ); + is( $error, 'No homebranch subfield found', 'Correct error message for missing homebranch' ); + }; + + # Test missing itemtype + subtest 'can_add_item_from_marc_record missing itemtype' => sub { + plan tests => 2; + + my $record = MARC::Record->new(); + $record->append_fields( + MARC::Field->new( + '952', ' ', ' ', + 'a' => 'CENTRAL', # homebranch + 'b' => 'CENTRAL' # holdingbranch + # No 'y' for itemtype + ) + ); + + my ( $result, $error ) = Koha::BackgroundJob::BatchUpdateBiblio::can_add_item_from_marc_record($record); + is( $result, 0, 'Returns 0 when itemtype is missing' ); + is( $error, 'No itemtype subfield found', 'Correct error message for missing itemtype' ); + }; + + # Test with bib-level itemtypes + subtest 'can_add_item_from_marc_record with bib-level itemtypes' => sub { + plan tests => 2; + + # Change preference to use bib-level itemtypes + t::lib::Mocks::mock_preference( 'item-level_itypes', 0 ); + + my $record = MARC::Record->new(); + $record->append_fields( + MARC::Field->new( + '952', ' ', ' ', + 'a' => 'CENTRAL', # homebranch + 'b' => 'CENTRAL', # holdingbranch + ) + ); + + $record->append_fields( + MARC::Field->new( + '942', ' ', ' ', + 'c' => 'BOOK', # itemtype + ) + ); + + my ( $result, $error ) = Koha::BackgroundJob::BatchUpdateBiblio::can_add_item_from_marc_record($record); + is( $result, 1, 'Returns 1 when using bib-level itemtypes' ); + is( $error, undef, 'No error message when successful' ); + + # Reset to item-level itemtypes + t::lib::Mocks::mock_preference( 'item-level_itypes', 1 ); + }; + + # Test with multiple item fields + subtest 'can_add_item_from_marc_record with multiple item fields' => sub { + plan tests => 2; + + my $record = MARC::Record->new(); + + # First field missing itemtype + $record->append_fields( + MARC::Field->new( + '952', ' ', ' ', + 'a' => 'CENTRAL', # homebranch + 'b' => 'CENTRAL' # holdingbranch + # No itemtype + ) + ); + + # Second field is complete + $record->append_fields( + MARC::Field->new( + '952', ' ', ' ', + 'a' => 'EAST', # homebranch + 'b' => 'EAST', # holdingbranch + 'y' => 'DVD' # itemtype + ) + ); + + my ( $result, $error ) = Koha::BackgroundJob::BatchUpdateBiblio::can_add_item_from_marc_record($record); + is( $result, 1, 'Returns 1 when at least one item field is complete' ); + is( $error, undef, 'No error message when successful' ); + }; + + # Test mixed valid/invalid fields + subtest 'can_add_item_from_marc_record with mix of valid/invalid fields' => sub { + plan tests => 2; + + my $record = MARC::Record->new(); + + # Add a non-item field that shouldn't be considered + $record->append_fields( + MARC::Field->new( + '245', '1', '0', + 'a' => 'Title', + 'b' => 'Subtitle' + ) + ); + + # Add a valid item field + $record->append_fields( + MARC::Field->new( + '952', ' ', ' ', + 'a' => 'CENTRAL', # homebranch + 'b' => 'CENTRAL', # holdingbranch + 'y' => 'BOOK' # itemtype + ) + ); + + my ( $result, $error ) = Koha::BackgroundJob::BatchUpdateBiblio::can_add_item_from_marc_record($record); + is( $result, 1, 'Returns 1 with mix of item and non-item fields' ); + is( $error, undef, 'No error message when successful' ); + }; + + $schema->storage->txn_rollback; +}; -- 2.48.1