From 6636f4cacbb0af35d083cd08724ec2546eecb951 Mon Sep 17 00:00:00 2001 From: Agustin Moyano Date: Fri, 9 Oct 2020 09:59:46 -0300 Subject: [PATCH] Bug 26635: Add tests Add tests in t/db_dependent/Koha/Object.t --- t/db_dependent/Koha/Object.t | 43 +++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Koha/Object.t b/t/db_dependent/Koha/Object.t index cae049cbd2..7322666fb5 100755 --- a/t/db_dependent/Koha/Object.t +++ b/t/db_dependent/Koha/Object.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 20; +use Test::More tests => 21; use Test::Exception; use Test::Warn; use DateTime; @@ -33,6 +33,8 @@ use Koha::DateUtils qw( dt_from_string ); use Koha::Libraries; use Koha::Patrons; use Koha::ApiKeys; +use Koha::AuthorisedValueCategories; +use Koha::AuthorisedValues; use JSON; use Scalar::Util qw( isvstring ); @@ -867,3 +869,42 @@ subtest 'set_or_blank' => sub { $schema->storage->txn_rollback; }; + +subtest 'Authorised values expansion' => sub { + plan tests => 4; + + $schema->storage->txn_begin; + + Koha::AuthorisedValues->search({category => 'Countries'})->delete; + Koha::AuthorisedValueCategories->search({category_name =>'Countries'})->delete; + + my $cat = $builder->build_object({ class => 'Koha::AuthorisedValueCategories', value => {category_name =>'Countries'} }); + my $fr = $builder->build_object({ class => 'Koha::AuthorisedValues', value => {authorised_value => 'FR', lib=>'France', category=>$cat->category_name} }); + my $us = $builder->build_object({ class => 'Koha::AuthorisedValues', value => {authorised_value => 'US', lib=>'United States of America', category=>$cat->category_name} }); + my $ar = $builder->build_object({ class => 'Koha::AuthorisedValues', value => {authorised_value => 'AR', lib=>'Argentina', category=>$cat->category_name} }); + + my $city_class = Test::MockModule->new('Koha::City'); + $city_class->mock( '_fetch_authorised_values', + sub { + my ($self) = @_; + use Koha::AuthorisedValues; + my $av = Koha::AuthorisedValues->find({authorised_value => $self->city_country, category => 'Countries'}); + return {country => $av->unblessed}; + } + ); + + my $marseille = $builder->build_object({ class => 'Koha::Cities', value => {city_country => 'FR', city_name => 'Marseille'} }); + my $cordoba = $builder->build_object({ class => 'Koha::Cities', value => {city_country => 'AR', city_name => 'Córdoba'} }); + + my $mobj = $marseille->to_api({av_expand => 1}); + my $cobj = $cordoba->to_api({av_expand => 1}); + + isnt($mobj->{_authorised_values}, undef, '_authorised_values exists for Marseille'); + isnt($cobj->{_authorised_values}, undef, '_authorised_values exists for Córdoba'); + + is($mobj->{_authorised_values}->{country}->{lib}, $fr->lib, 'Authorised value for country expanded'); + is($cobj->{_authorised_values}->{country}->{lib}, $ar->lib, 'Authorised value for country expanded'); + + + $schema->storage->txn_rollback; +}; \ No newline at end of file -- 2.25.0