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( $rel );
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 / +90 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
    plan tests => scalar Koha::Patron->new->_result()->relationships();
82
83
    $schema->storage->txn_begin;
84
85
    my $test_objects = {};
86
87
    my $patron = $builder->build_object(
88
        {
89
            class => 'Koha::Patrons',
90
        }
91
    );
92
93
    my $someone_else = $builder->build_object(
94
        {
95
            class => 'Koha::Patrons',
96
        }
97
    );
98
99
    my $result_source = Koha::Patron->new->_result()->result_source;
100
    foreach my $rel ( Koha::Patron->new->_result()->relationships() ) {
101
        my $related_source = $result_source->related_source($rel);
102
103
        # relationships() returns related columns, not tables, and therefore
104
        # we could arrive multiple times into the same table and had already
105
        # generated test data for that object. next; in such case.
106
        if ($test_objects->{$related_source->source_name}) {
107
            next;
108
        }
109
110
        my $rs = $related_source->resultset;
111
        my $info = $result_source->relationship_info($rel);
112
113
        # We are not interested in the "belongs_to" relationship of borrowers.
114
        # These are tables like branches, categories, sms_provider etc.
115
        if ($info->{'attrs'}->{'is_depends_on'}) {
116
            next;
117
        }
118
119
        my $source_name = $related_source->source_name;
120
        (my $rel_col = (keys %{$info->{'cond'}})[0]) =~ s/^foreign\.//;
121
122
        # Generate test data into related tables
123
        my $built;
124
        if ($related_source->result_class->can('koha_objects_class')) {
125
            $built = $builder->build_object(
126
                {
127
                    class => $related_source->result_class->koha_objects_class,
128
                    value => { $rel_col => $patron->borrowernumber }
129
                }
130
            );
131
            $built = $built->unblessed;
132
        } else {
133
            $built = $builder->build(
134
                {
135
                    source => $source_name,
136
                    value  => { $rel_col => $patron->borrowernumber }
137
                }
138
            );
139
        }
140
141
142
        $test_objects->{$related_source->source_name} = [ $built ];
143
144
        # Create test data for someone else, to ensure we are only returning
145
        # this patron's data
146
        $builder->build(
147
            {
148
                source => $source_name,
149
                value => { $rel_col => $someone_else->borrowernumber }
150
            }
151
        );
152
153
    }
154
155
    my $takeout = $patron->takeout;
156
157
    foreach my $rel ( Koha::Patron->new->_result()->relationships() ) {
158
        my $related_source = $result_source->related_source($rel);
159
        is_deeply(
160
            $takeout->{$related_source->source_name},
161
            $test_objects->{$related_source->source_name},
162
            "$rel included in takeout"
163
        );
164
    }
165
166
    $schema->storage->txn_rollback;
167
};
168
80
subtest 'relationships_debt() tests' => sub {
169
subtest 'relationships_debt() tests' => sub {
81
170
82
    plan tests => 168;
171
    plan tests => 168;
83
- 

Return to bug 20028