From d8072192e700ebe83746569ad63daed5c7dcaa9a Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Wed, 29 Mar 2023 10:47:24 +0200 Subject: [PATCH] Bug 11844: Add tests Tests added for: - Koha::AdditionalField - TransferOrder - marcfield_mode Signed-off-by: Michaela Sieber Signed-off-by: Martin Renvoize --- Koha/Object/Mixin/AdditionalFields.pm | 1 + t/db_dependent/Acquisition/TransferOrder.t | 38 +++++++- t/db_dependent/Koha/AdditionalField.t | 82 +++++++++++++++++ .../Koha/Object/Mixin/AdditionalFields.t | 91 +++++++++++++++++++ 4 files changed, 211 insertions(+), 1 deletion(-) create mode 100755 t/db_dependent/Koha/AdditionalField.t create mode 100755 t/db_dependent/Koha/Object/Mixin/AdditionalFields.t diff --git a/Koha/Object/Mixin/AdditionalFields.pm b/Koha/Object/Mixin/AdditionalFields.pm index 7990eba37c..bf6598d8c9 100644 --- a/Koha/Object/Mixin/AdditionalFields.pm +++ b/Koha/Object/Mixin/AdditionalFields.pm @@ -1,6 +1,7 @@ package Koha::Object::Mixin::AdditionalFields; use Modern::Perl; +use Koha::AdditionalFields; use Koha::AdditionalFieldValues; =head1 NAME diff --git a/t/db_dependent/Acquisition/TransferOrder.t b/t/db_dependent/Acquisition/TransferOrder.t index a9e59a5637..fc38127049 100755 --- a/t/db_dependent/Acquisition/TransferOrder.t +++ b/t/db_dependent/Acquisition/TransferOrder.t @@ -2,7 +2,7 @@ use Modern::Perl; -use Test::More tests => 13; +use Test::More tests => 14; use C4::Context; use C4::Acquisition qw( NewBasket GetOrders GetOrder TransferOrder SearchOrders ModReceiveOrder CancelReceipt ); use C4::Biblio; @@ -14,6 +14,7 @@ use Koha::Acquisition::Booksellers; use Koha::Acquisition::Orders; use t::lib::TestBuilder; use MARC::Record; +use String::Random qw(random_string); my $schema = Koha::Database->new()->schema(); $schema->storage->txn_begin(); @@ -110,4 +111,39 @@ $order = GetOrder( $newordernumber ); is ( $order->{ordernumber}, $newordernumber, 'Regression test Bug 11549: After a transfer, receive and cancel the receive should be possible.' ); is ( $order->{basketno}, $basketno2, 'Regression test Bug 11549: The order still exist in the basket where the transfer has been done.'); +subtest 'TransferOrder should copy additional fields' => sub { + plan tests => 2; + + my $field = Koha::AdditionalField->new( + { + tablename => 'aqorders', + name => random_string('c' x 100), + } + ); + $field->store()->discard_changes(); + my $order = Koha::Acquisition::Order->new( + { + basketno => $basketno1, + quantity => 2, + biblionumber => $biblionumber, + budget_id => $budget->{budget_id}, + } + )->store; + $order->set_additional_fields( + [ + { + id => $field->id, + value => 'additional field value', + }, + ] + ); + + my $newordernumber = TransferOrder($order->ordernumber, $basketno2); + my $neworder = Koha::Acquisition::Orders->find($newordernumber); + my $field_values = $neworder->additional_field_values()->as_list; + + is(scalar @$field_values, 1, 'transfered order has one additional field value'); + is($field_values->[0]->value, 'additional field value', 'transfered order additional field has the correct value'); +}; + $schema->storage->txn_rollback(); diff --git a/t/db_dependent/Koha/AdditionalField.t b/t/db_dependent/Koha/AdditionalField.t new file mode 100755 index 0000000000..db2d50d8fa --- /dev/null +++ b/t/db_dependent/Koha/AdditionalField.t @@ -0,0 +1,82 @@ +#!/usr/bin/perl + +use Modern::Perl; + +use Test::More tests => 2; +use String::Random qw(random_string); + +use Koha::AuthorisedValueCategory; + +BEGIN { + use_ok('Koha::AdditionalField'); +} + +my $schema = Koha::Database->schema; + +subtest 'effective_authorised_value_category' => sub { + plan tests => 4; + + $schema->txn_begin; + + my $av_category_name = random_string('C' x 32); + my $av_category = Koha::AuthorisedValueCategory->new({ category_name => $av_category_name }); + $av_category->store()->discard_changes(); + + my $field = Koha::AdditionalField->new( + { + tablename => random_string('c' x 100), + name => random_string('c' x 100), + } + ); + $field->store()->discard_changes(); + + is($field->effective_authorised_value_category, '', 'no default category'); + + $field = Koha::AdditionalField->new( + { + tablename => random_string('c' x 100), + name => random_string('c' x 100), + authorised_value_category => $av_category_name, + } + ); + $field->store()->discard_changes(); + + is($field->effective_authorised_value_category, $av_category_name, 'returns authorised_value_category if set'); + + my $mss = Koha::MarcSubfieldStructure->new( + { + frameworkcode => '', + tagfield => '999', + tagsubfield => 'Z', + authorised_value => $av_category_name, + } + ); + $mss->store(); + $field = Koha::AdditionalField->new( + { + tablename => random_string('c' x 100), + name => random_string('c' x 100), + marcfield => '999$Z', + } + ); + $field->store()->discard_changes(); + + is($field->effective_authorised_value_category, $av_category_name, 'returns MARC subfield authorised value category if set'); + + my $av2_category_name = random_string('C' x 32); + my $av2_category = Koha::AuthorisedValueCategory->new({ category_name => $av2_category_name }); + $av2_category->store()->discard_changes(); + $field = Koha::AdditionalField->new( + { + tablename => random_string('c' x 100), + name => random_string('c' x 100), + authorised_value_category => $av2_category_name, + marcfield => '999$Z', + } + ); + $field->store()->discard_changes(); + + is($field->effective_authorised_value_category, $av2_category_name, 'returns authorised_value_category if both authorised_value_category and marcfield are set'); + + $schema->txn_rollback; +}; diff --git a/t/db_dependent/Koha/Object/Mixin/AdditionalFields.t b/t/db_dependent/Koha/Object/Mixin/AdditionalFields.t new file mode 100755 index 0000000000..c397701f8a --- /dev/null +++ b/t/db_dependent/Koha/Object/Mixin/AdditionalFields.t @@ -0,0 +1,91 @@ +#!/usr/bin/perl + +use Modern::Perl; +use Test::More tests => 2; +use t::lib::TestBuilder; +use String::Random qw(random_string); +use Koha::Database; +use Koha::Subscription; +use Koha::AdditionalField; +use C4::Context; + +my $builder = t::lib::TestBuilder->new; +my $schema = Koha::Database->schema; + +subtest 'set_additional_fields with marcfield_mode = "get"' => sub { + plan tests => 1; + + $schema->txn_begin; + + my $biblio = $builder->build_sample_biblio(); + my $record = $biblio->record; + $record->append_fields( + MARC::Field->new('999', '', '', 'Z' => 'some value'), + ); + $biblio->metadata->metadata($record->as_xml_record(C4::Context->preference('marcflavour'))); + $biblio->metadata->store()->discard_changes(); + my $subscription = Koha::Subscription->new( + { + biblionumber => $biblio->biblionumber, + } + ); + $subscription->store()->discard_changes(); + + my $field = Koha::AdditionalField->new( + { + tablename => 'subscription', + name => random_string('c' x 100), + marcfield => '999$Z', + marcfield_mode => 'get', + } + ); + $field->store()->discard_changes(); + $subscription->set_additional_fields( + [ + { id => $field->id }, + ] + ); + + my $values = $subscription->additional_field_values()->as_list(); + + is($values->[0]->value, 'some value', 'value was copied from the biblio record to the field'); + + $schema->txn_rollback; +}; + +subtest 'set_additional_fields with marcfield_mode = "set"' => sub { + plan tests => 1; + + $schema->txn_begin; + + my $biblio = $builder->build_sample_biblio(); + my $subscription = Koha::Subscription->new( + { + biblionumber => $biblio->biblionumber, + } + ); + $subscription->store()->discard_changes(); + + my $field = Koha::AdditionalField->new( + { + tablename => 'subscription', + name => random_string('c' x 100), + marcfield => '999$Z', + marcfield_mode => 'set', + } + ); + $field->store()->discard_changes(); + $subscription->set_additional_fields( + [ + { + id => $field->id, + value => 'some value', + }, + ] + ); + + my $record = $biblio->record; + is($record->subfield('999', 'Z'), 'some value', 'value was copied from the field to the biblio record'); + + $schema->txn_rollback; +}; -- 2.30.2