From adf53163df557f02c4fcd02f433a913c6f9a57a7 Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Wed, 18 Oct 2023 09:31:11 +0000 Subject: [PATCH] Bug 35044: Add tests Test plan: prove t/db_dependent/Koha/Object/Mixin/AdditionalFields.t Signed-off-by: Martin Renvoize Signed-off-by: Julian Maurice --- .../Koha/Object/Mixin/AdditionalFields.t | 131 +++++++++++++++++- 1 file changed, 130 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Koha/Object/Mixin/AdditionalFields.t b/t/db_dependent/Koha/Object/Mixin/AdditionalFields.t index e33a77d068..051ea13736 100755 --- a/t/db_dependent/Koha/Object/Mixin/AdditionalFields.t +++ b/t/db_dependent/Koha/Object/Mixin/AdditionalFields.t @@ -1,7 +1,7 @@ #!/usr/bin/perl use Modern::Perl; -use Test::More tests => 2; +use Test::More tests => 4; use t::lib::TestBuilder; use String::Random qw(random_string); use Koha::Database; @@ -89,3 +89,132 @@ subtest 'set_additional_fields with marcfield_mode = "set"' => sub { $schema->txn_rollback; }; + +subtest 'get_additional_field_values_for_template' => sub { + plan tests => 2; + + $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 ) + } + ); + $field->store()->discard_changes(); + + my $field2 = Koha::AdditionalField->new( + { + tablename => 'subscription', + name => random_string( 'c' x 100 ) + } + ); + $field2->store()->discard_changes(); + + my $value = 'some value'; + $subscription->set_additional_fields( + [ + { + id => $field->id, + value => $value . ' 1', + }, + { + id => $field->id, + value => $value . ' 2', + }, + { + id => $field2->id, + value => 'second field', + }, + ] + ); + + my $template_additional_field_values = $subscription->get_additional_field_values_for_template; + + is( + ref($template_additional_field_values), 'HASH', + '->get_additional_field_values_for_template should return a hash' + ); + is_deeply( + $template_additional_field_values, + { $field->id => [ $value . ' 1', $value . ' 2' ], $field2->id => ['second field'] }, + '->get_additional_field_values_for_template should return the correct values' + ); + + $schema->txn_rollback; +}; + +subtest 'add_additional_fields' => sub { + plan tests => 1; + + $schema->txn_begin; + + Koha::AdditionalFields->search->delete; + + 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 ) + } + ); + $field->store()->discard_changes(); + + my $field2 = Koha::AdditionalField->new( + { + tablename => 'subscription', + name => random_string( 'c' x 100 ) + } + ); + $field2->store()->discard_changes(); + + my $value = 'some value'; + $subscription->set_additional_fields( + [ + { + id => $field->id, + value => $value . ' 1', + }, + { + id => $field->id, + value => $value . ' 2', + } + ] + ); + + $subscription->add_additional_fields( + { + $field2->id => [ + 'second field' + ], + }, + 'subscription' + ); + + my $template_additional_field_values = $subscription->get_additional_field_values_for_template; + + + + is_deeply( + $template_additional_field_values, + { $field->id => [ $value . ' 1', $value . ' 2' ], $field2->id => ['second field'] }, + '->add_additional_fields should have only added to existing field values' + ); + + $schema->txn_rollback; +}; -- 2.39.2