View | Details | Raw Unified | Return to bug 20028
Collapse All | Expand All

(-)a/Koha/Patron.pm (+44 lines)
Lines 1700-1705 sub get_extended_attribute { Link Here
1700
    return $attribute->next;
1700
    return $attribute->next;
1701
}
1701
}
1702
1702
1703
=head3 takeout
1704
1705
my $takeout = $patron->takeout()
1706
1707
=cut
1708
1709
sub takeout {
1710
    my ( $self ) = @_;
1711
1712
    my $takeout = {};
1713
1714
    my $result_source = $self->_result()->result_source;
1715
1716
    foreach my $rel ($self->_result()->relationships()) {
1717
        my $related_source = $result_source->related_source( $re );
1718
1719
        # relationships() returns related columns, not tables, and therefore
1720
        # we could arrive multiple times into the same table
1721
        if ($takeout->{$related_source->source_name}) {
1722
            next;
1723
        }
1724
1725
        # Skip all the "[borrower] belongs_to" relationships
1726
        my $info = $result_source->relationship_info( $rel );
1727
        if ( $info->{'attrs'}->{'is_depends_on'} ) {
1728
            next;
1729
        }
1730
1731
        # 'cond' has format foreign.columnname, remove ^foreign. from it
1732
        ( my $rel_col = ( keys %{$info->{'cond'}} )[0] ) =~ s/^foreign\.//;
1733
1734
        my $rs = $related_source->resultset;
1735
        my $res = $rs->search( { $rel_col => $self->borrowernumber } );
1736
1737
        $res->result_class('DBIx::Class::ResultClass::HashRefInflator');
1738
1739
        $takeout->{$related_source->source_name} = [ $res->all() ];
1740
    }
1741
1742
    $takeout->{'borrowers'} = $self->unblessed;
1743
1744
    return $takeout;
1745
}
1746
1703
=head3 to_api
1747
=head3 to_api
1704
1748
1705
    my $json = $patron->to_api;
1749
    my $json = $patron->to_api;
(-)a/t/db_dependent/Koha/Patron.t (-2 / +78 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 6;
22
use Test::More tests => 7;
23
use Test::Exception;
23
use Test::Exception;
24
24
25
use Koha::Database;
25
use Koha::Database;
Lines 77-82 subtest 'add_guarantor() tests' => sub { Link Here
77
    $schema->storage->txn_rollback;
77
    $schema->storage->txn_rollback;
78
};
78
};
79
79
80
subtest 'takeout' => sub {
81
82
    plan tests => 66;
83
84
    $schema->storage->txn_begin;
85
86
    my $test_objects = {};
87
88
    my $patron = $builder->build_object(
89
        {
90
            class => 'Koha::Patrons',
91
        }
92
    );
93
94
    my $someone_else = $builder->build_object(
95
        {
96
            class => 'Koha::Patrons',
97
        }
98
    );
99
100
    my $result_source = Koha::Patron->new->_result()->result_source;
101
    foreach my $rel ( Koha::Patron->new->_result()->relationships() ) {
102
        my $related_source = $result_source->related_source($rel);
103
104
        # relationships() returns related columns, not tables, and therefore
105
        # we could arrive multiple times into the same table and had already
106
        # generated test data for that object. next; in such case.
107
        if ($test_objects->{$related_source->source_name}) {
108
            next;
109
        }
110
111
        my $rs = $related_source->resultset;
112
        my $info = $result_source->relationship_info($rel);
113
114
        # We are not interested in the "belongs_to" relationship of borrowers.
115
        # These are tables like branches, categories, sms_provider etc.
116
        if ($info->{'attrs'}->{'is_depends_on'}) {
117
            next;
118
        }
119
120
        my $source_name = $related_source->source_name;
121
        (my $rel_col = (keys %{$info->{'cond'}})[0]) =~ s/^foreign\.//;
122
123
        my $built = $builder->build(
124
            {
125
                source => $source_name,
126
                value => { $rel_col => $patron->borrowernumber }
127
            }
128
        );
129
130
        $test_objects->{$related_source->source_name} = [ $built ];
131
132
        # Create test data for someone else, to ensure we are only returning
133
        # this patron's data
134
        $builder->build(
135
            {
136
                source => $source_name,
137
                value => { $rel_col => $someone_else->borrowernumber }
138
            }
139
        );
140
141
    }
142
143
    my $takeout = $patron->takeout;
144
145
    foreach my $rel ( Koha::Patron->new->_result()->relationships() ) {
146
        my $related_source = $result_source->related_source($rel);
147
        is_deeply(
148
            $takeout->{$related_source->source_name},
149
            $test_objects->{$related_source->source_name},
150
            "$rel included in takeout"
151
        );
152
    }
153
154
    $schema->storage->txn_rollback;
155
};
156
80
subtest 'relationships_debt() tests' => sub {
157
subtest 'relationships_debt() tests' => sub {
81
158
82
    plan tests => 168;
159
    plan tests => 168;
83
- 

Return to bug 20028