From fd3506465737a388679b905b73d8310f7014ba1d Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 13 Jan 2020 16:45:58 -0300 Subject: [PATCH] Bug 24366: Make sure accessors are used on the right object This patch makes the Koha::Biblio->to_api method try the accessors on $self first, and fallback to $self->biblioitem on those he can't. This way it won't happen that (for example) ->holds will be tried against ->biblioitem. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/Koha/Biblio.t => SUCCESS: Tests pass! 3. Sign off :-D Signed-off-by: Tomas Cohen Arazi Signed-off-by: Martin Renvoize --- Koha/Biblio.pm | 10 +++++++++- t/db_dependent/Koha/Biblio.t | 7 ++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index e503bad718..a37330801f 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -737,8 +737,16 @@ on the API. sub to_api { my ($self, $args) = @_; + my @embeds = keys %{ $args->{embed} }; + my $remaining_embeds = {}; + + foreach my $embed (@embeds) { + $remaining_embeds = delete $args->{embed}->{$embed} + unless $self->can($embed); + } + my $response = $self->SUPER::to_api( $args ); - my $biblioitem = $self->biblioitem->to_api( $args ); + my $biblioitem = $self->biblioitem->to_api({ embed => $remaining_embeds }); return { %$response, %$biblioitem }; } diff --git a/t/db_dependent/Koha/Biblio.t b/t/db_dependent/Koha/Biblio.t index 45b375b369..91017eeaae 100644 --- a/t/db_dependent/Koha/Biblio.t +++ b/t/db_dependent/Koha/Biblio.t @@ -440,14 +440,19 @@ subtest 'to_api() tests' => sub { $schema->storage->txn_begin; my $biblio = $builder->build_sample_biblio(); + my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber }); + my $biblioitem_api = $biblio->biblioitem->to_api; my $biblio_api = $biblio->to_api; - plan tests => scalar keys %{ $biblioitem_api }; + plan tests => (scalar keys %{ $biblioitem_api }) + 1; foreach my $key ( keys %{ $biblioitem_api } ) { is( $biblio_api->{$key}, $biblioitem_api->{$key}, "$key is added to the biblio object" ); } + $biblio_api = $biblio->to_api({ embed => { items => {} } }); + is_deeply( $biblio_api->{items}, [ $item->to_api ], 'Item correctly embedded' ); + $schema->storage->txn_rollback; }; -- 2.39.5