From 114a02bbdbfbe9f676ecf355a5aa4bb704c89b49 Mon Sep 17 00:00:00 2001 From: Lari Taskula Date: Thu, 4 Feb 2021 19:28:30 +0000 Subject: [PATCH] Bug 20028: Add Koha::Patron->takeout To test: 1. prove t/db_dependent/Koha/Patron.t 2. Observe success --- Koha/Patron.pm | 44 ++++++++++++++++++++ t/db_dependent/Koha/Patron.t | 79 +++++++++++++++++++++++++++++++++++- 2 files changed, 122 insertions(+), 1 deletion(-) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 6ea9e5aae7..d931ff0ecc 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -1700,6 +1700,50 @@ sub get_extended_attribute { return $attribute->next; } +=head3 takeout + +my $takeout = $patron->takeout() + +=cut + +sub takeout { + my ( $self ) = @_; + + my $takeout = {}; + + my $result_source = $self->_result()->result_source; + + foreach my $rel ($self->_result()->relationships()) { + my $related_source = $result_source->related_source( $re ); + + # relationships() returns related columns, not tables, and therefore + # we could arrive multiple times into the same table + if ($takeout->{$related_source->source_name}) { + next; + } + + # Skip all the "[borrower] belongs_to" relationships + my $info = $result_source->relationship_info( $rel ); + if ( $info->{'attrs'}->{'is_depends_on'} ) { + next; + } + + # 'cond' has format foreign.columnname, remove ^foreign. from it + ( my $rel_col = ( keys %{$info->{'cond'}} )[0] ) =~ s/^foreign\.//; + + my $rs = $related_source->resultset; + my $res = $rs->search( { $rel_col => $self->borrowernumber } ); + + $res->result_class('DBIx::Class::ResultClass::HashRefInflator'); + + $takeout->{$related_source->source_name} = [ $res->all() ]; + } + + $takeout->{'borrowers'} = $self->unblessed; + + return $takeout; +} + =head3 to_api my $json = $patron->to_api; diff --git a/t/db_dependent/Koha/Patron.t b/t/db_dependent/Koha/Patron.t index 1b0d24552a..fbc30869a5 100755 --- a/t/db_dependent/Koha/Patron.t +++ b/t/db_dependent/Koha/Patron.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 6; +use Test::More tests => 7; use Test::Exception; use Koha::Database; @@ -77,6 +77,83 @@ subtest 'add_guarantor() tests' => sub { $schema->storage->txn_rollback; }; +subtest 'takeout' => sub { + + plan tests => 66; + + $schema->storage->txn_begin; + + my $test_objects = {}; + + my $patron = $builder->build_object( + { + class => 'Koha::Patrons', + } + ); + + my $someone_else = $builder->build_object( + { + class => 'Koha::Patrons', + } + ); + + my $result_source = Koha::Patron->new->_result()->result_source; + foreach my $rel ( Koha::Patron->new->_result()->relationships() ) { + my $related_source = $result_source->related_source($rel); + + # relationships() returns related columns, not tables, and therefore + # we could arrive multiple times into the same table and had already + # generated test data for that object. next; in such case. + if ($test_objects->{$related_source->source_name}) { + next; + } + + my $rs = $related_source->resultset; + my $info = $result_source->relationship_info($rel); + + # We are not interested in the "belongs_to" relationship of borrowers. + # These are tables like branches, categories, sms_provider etc. + if ($info->{'attrs'}->{'is_depends_on'}) { + next; + } + + my $source_name = $related_source->source_name; + (my $rel_col = (keys %{$info->{'cond'}})[0]) =~ s/^foreign\.//; + + my $built = $builder->build( + { + source => $source_name, + value => { $rel_col => $patron->borrowernumber } + } + ); + + $test_objects->{$related_source->source_name} = [ $built ]; + + # Create test data for someone else, to ensure we are only returning + # this patron's data + $builder->build( + { + source => $source_name, + value => { $rel_col => $someone_else->borrowernumber } + } + ); + + } + + my $takeout = $patron->takeout; + + foreach my $rel ( Koha::Patron->new->_result()->relationships() ) { + my $related_source = $result_source->related_source($rel); + is_deeply( + $takeout->{$related_source->source_name}, + $test_objects->{$related_source->source_name}, + "$rel included in takeout" + ); + } + + $schema->storage->txn_rollback; +}; + subtest 'relationships_debt() tests' => sub { plan tests => 168; -- 2.17.1