From 8c2416cb67c6246f845b236a65b23bc4691a2a56 Mon Sep 17 00:00:00 2001 From: Matt Blenkinsop Date: Wed, 2 Apr 2025 08:01:03 +0000 Subject: [PATCH] Bug 39518: Update object methods and unit tests --- Koha/MarcOrder.pm | 79 ++++++++++++++++++++++++++------- t/db_dependent/Koha/MarcOrder.t | 57 +++++++++++++++++++++++- 2 files changed, 119 insertions(+), 17 deletions(-) diff --git a/Koha/MarcOrder.pm b/Koha/MarcOrder.pm index fa8cc831fe4..56633fcae86 100644 --- a/Koha/MarcOrder.pm +++ b/Koha/MarcOrder.pm @@ -236,8 +236,10 @@ sub _create_basket_for_file { my $filename = $args->{filename}; my $vendor_id = $args->{vendor_id}; + my $basket_name = _check_file_for_basket_name($args); + # aqbasketname.basketname has a max length of 50 characters so long file names will need to be truncated - my $basketname = length($filename) > 50 ? substr( $filename, 0, 50 ) : $filename; + my $basketname = length($basket_name) > 50 ? substr( $basket_name, 0, 50 ) : $basket_name; my $basketno = NewBasket( $vendor_id, 0, $basketname, q{}, @@ -543,23 +545,11 @@ sub add_items_from_import_record { sub match_file_to_account { my ( $self, $args ) = @_; - my $match = 0; - my $filename = $args->{filename}; - my $filepath = $args->{filepath}; - my $profile = $args->{profile}; - my $format = index( $filename, '.mrc' ) != -1 ? 'ISO2709' : 'MARCXML'; + my $profile = $args->{profile}; - my ( $errors, $marcrecords ); - if ( $format eq 'MARCXML' ) { - ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromMARCXMLFile( $filepath, $profile->encoding ); - } elsif ( $format eq 'ISO2709' ) { - ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromISO2709File( - $filepath, $profile->record_type, - $profile->encoding - ); - } + my $match = 0; + my $match_record = _retrieve_first_record_from_batch($args); - my $match_record = @{$marcrecords}[0]; my ( $field, $subfield ) = split /\$/, $profile->match_field; my $field_value = $match_record->subfield( $field, $subfield ) || ''; @@ -572,6 +562,36 @@ sub match_file_to_account { return $match; } +=head3 _check_file_for_basket_name + + my $basket_name = _check_file_for_basket_name({ + filename => $filename, + filepath => $filepath, + profile => $profile + }); + + Checks to see if the account has a basket name field assigned. + If yes, it retrieves this value to use as the name. + If not, to uses the filename. + +=cut + +sub _check_file_for_basket_name { + my ( $self, $args ) = @_; + + my $profile = $args->{profile}; + my $filename = $args->{filename}; + return $filename if !$profile->basket_name_field; + + my $first_record = _retrieve_first_record_from_batch($args); + + my ( $field, $subfield ) = split /\$/, $profile->basket_name_field; + + my $field_value = $first_record->subfield( $field, $subfield ) || ''; + + return $field_value ? $field_value : $filename; +} + =head3 import_batches_list Fetches import batches matching the batch to be added to the basket and returns these to the template @@ -1251,4 +1271,31 @@ sub _create_item_fields_from_syspref { return $item_fields; } +=head3 _retrieve_first_record_from_batch + +=cut + +sub _retrieve_first_record_from_batch { + my ($args) = @_; + + my $filename = $args->{filename}; + my $filepath = $args->{filepath}; + my $profile = $args->{profile}; + my $format = index( $filename, '.mrc' ) != -1 ? 'ISO2709' : 'MARCXML'; + + my ( $errors, $marcrecords ); + if ( $format eq 'MARCXML' ) { + ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromMARCXMLFile( $filepath, $profile->encoding ); + } elsif ( $format eq 'ISO2709' ) { + ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromISO2709File( + $filepath, $profile->record_type, + $profile->encoding + ); + } + + my $first_record = @{$marcrecords}[0]; + return $first_record; + +} + 1; diff --git a/t/db_dependent/Koha/MarcOrder.t b/t/db_dependent/Koha/MarcOrder.t index b9e05cfb546..c849652a53c 100755 --- a/t/db_dependent/Koha/MarcOrder.t +++ b/t/db_dependent/Koha/MarcOrder.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 5; +use Test::More tests => 6; use Koha::MarcOrder; use Koha::MarcOrderAccount; @@ -627,3 +627,58 @@ subtest 'match_file_to_account' => sub { $schema->storage->txn_rollback; }; + +subtest '_check_file_for_basket_name' => sub { + plan tests => 2; + + $schema->storage->txn_begin; + + my ( $fh, $name ) = tempfile( SUFFIX => '.marcxml' ); + + my $rec = MARC::Record->new; + my $fld = MARC::Field->new( '975', '', '', 'p', 'This is a basket' ); + $rec->append_fields($fld); + my $str = $rec->as_xml; + + print $fh $str; + + close $fh; + + my $account1 = Koha::MarcOrderAccount->new( + { + basket_name_field => '975$p', + encoding => 'UTF-8', + description => 'test', + } + )->store; + + my $basket_name = Koha::MarcOrder->_check_file_for_basket_name( + { + filename => $name, + filepath => $name, + profile => $account1, + } + ); + + is( $basket_name, "This is a basket", 'Basket name identified correctly' ); + + my $account2 = Koha::MarcOrderAccount->new( + { + basket_name_field => '975$z', + encoding => 'UTF-8', + description => 'test', + } + )->store; + + my $basket_name2 = Koha::MarcOrder->_check_file_for_basket_name( + { + filename => $name, + filepath => $name, + profile => $account2, + } + ); + + is( $basket_name2, $name, 'No filename provided in the file' ); + + $schema->storage->txn_rollback; +}; -- 2.48.1