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

(-)a/Koha/Patron.pm (+93 lines)
Lines 1585-1590 sub add_extended_attribute { Link Here
1585
1585
1586
}
1586
}
1587
1587
1588
=head3 export
1589
1590
    my $export = $patron->export()
1591
1592
    Gathers patron's related data by checking DBIx relationships.
1593
1594
    Returns a hashref where keys are DBIx source names, and values are either
1595
    an instance of Koha::Objects or DBIx::Class:ResultSet, or when source
1596
    equals "Borrower", a Koha::Object.
1597
1598
    Koha::Objects is returned for sources that have a respective Koha::Object,
1599
    and for other sources DBIx::Class::ResultSet.
1600
1601
    Return example:
1602
1603
    {
1604
        "Borrower" => Koha::Patron,
1605
        "BorrowerMessagePreference" => DBIx::Class::ResultSet,
1606
        "ReturnClaim" => Koha::Checkouts::ReturnClaims
1607
    }
1608
1609
=cut
1610
1611
sub export {
1612
    my ( $self, $params ) = @_;
1613
1614
    my $schema = Koha::Database->new()->schema();
1615
1616
    my $export = {};
1617
    my $relationships = {};
1618
1619
    my $logger = Koha::Logger->get;
1620
    my $result_source = $self->_result()->result_source;
1621
1622
    foreach my $rel ( $self->_result()->relationships() ) {
1623
        my $related_source = $result_source->related_source( $rel );
1624
1625
        # Skip all the "[borrower] belongs_to" relationships
1626
        my $info = $result_source->relationship_info( $rel );
1627
        if ( $info->{'attrs'}->{'is_depends_on'} ) {
1628
            next;
1629
        }
1630
1631
        # 'cond' has format foreign.columnname, remove ^foreign. from it
1632
        ( my $rel_col = ( keys %{ $info->{'cond'} } )[0] ) =~ s/^foreign\.//;
1633
1634
        my $src_name = $related_source->source_name;
1635
1636
        $relationships->{$src_name} = [] unless $relationships->{$src_name};
1637
        push @{ $relationships->{$src_name} }, $rel_col;
1638
    }
1639
1640
    foreach my $src_name ( keys %$relationships ) {
1641
        my $related_source = $schema->source( $src_name );
1642
        my $res;
1643
        my $rs = $related_source->resultset;
1644
        my $search;
1645
1646
        if ( scalar @{ $relationships->{$src_name} } > 1 ) {
1647
            $search = { '-or' => [] };
1648
            foreach my $rel_col ( values @{ $relationships->{$src_name} } ) {
1649
                push @{ $search->{'-or'} }, $rel_col;
1650
                push @{ $search->{'-or'} }, $self->borrowernumber;
1651
            }
1652
        } else {
1653
            $search = { $relationships->{$src_name}->[0] => $self->borrowernumber };
1654
        }
1655
1656
        if ( $related_source->result_class->can('koha_objects_class') ) {
1657
            my $koha_object = $related_source->result_class->koha_objects_class;
1658
1659
            if ( ! Module::Load::Conditional::can_load(
1660
                    modules => { $koha_object => undef} )) {
1661
                Koha::Exceptions::Exception->throw(
1662
                    error => "Could not load '$koha_object'"
1663
                );
1664
            }
1665
1666
            $res = $koha_object->search( $search );
1667
        } else {
1668
            $res = $rs->search( $search );
1669
        }
1670
1671
        next unless $res->count;
1672
1673
        $export->{$src_name} = $res if $res;
1674
    }
1675
1676
    $export->{'Borrower'} = $self;
1677
1678
    return $export;
1679
}
1680
1588
=head3 extended_attributes
1681
=head3 extended_attributes
1589
1682
1590
Return object of Koha::Patron::Attributes type with all attributes set for this patron
1683
Return object of Koha::Patron::Attributes type with all attributes set for this patron
(-)a/t/db_dependent/Koha/Patron.t (-1 / +102 lines)
Lines 78-83 subtest 'add_guarantor() tests' => sub { Link Here
78
    $schema->storage->txn_rollback;
78
    $schema->storage->txn_rollback;
79
};
79
};
80
80
81
subtest 'export' => sub {
82
    my $limit_data     = 5; # build this many random test objects
83
    my $generated_data = 0;
84
85
    plan tests => $limit_data * 2;
86
87
    my @related_sources = _get_related_sources();
88
89
    $schema->storage->txn_begin;
90
91
    my $patron = $builder->build_object(
92
        {
93
            class => 'Koha::Patrons',
94
        }
95
    );
96
97
    my $test_objects = {
98
        'Borrower' => $patron->unblessed,
99
    };
100
101
    my $result_source = Koha::Patron->new->_result()->result_source;
102
    foreach my $rel (   Koha::Patron->new->_result()->relationships() ) {
103
        if ( $generated_data >= $limit_data-1 ) {
104
            last;
105
        }
106
107
        my $related_source = $result_source->related_source( $rel );
108
        my $source_name = $related_source->source_name;
109
110
                my $info = $result_source->relationship_info( $rel );
111
112
        # We are not interested in the "belongs_to" relationship of borrowers.
113
        # These are tables like branches, categories and sms_provider.
114
        if ( $info->{'attrs'}->{'is_depends_on'} ) {
115
            next;
116
        }
117
118
        ( my $rel_col = ( keys %{ $info->{'cond'} } )[0] ) =~ s/^foreign\.//;
119
120
        # Generate test data into related tables
121
        my $built;
122
        if ( $related_source->result_class->can('koha_objects_class') ) {
123
            $built = $builder->build_object(
124
                {
125
                    class => $related_source->result_class->koha_objects_class,
126
                    value => { $rel_col => $patron->borrowernumber }
127
                }
128
            );
129
            $built = $built->unblessed;
130
        } else {
131
            $built = $builder->build(
132
                {
133
                    source => $source_name,
134
                    value  => { $rel_col => $patron->borrowernumber }
135
                }
136
            );
137
        }
138
139
        $test_objects->{$source_name} = [] unless $test_objects->{$source_name};
140
        push @{ $test_objects->{$source_name} }, $built;
141
        $generated_data++;
142
    }
143
144
    my $export = $patron->export;
145
146
    foreach my $src_name ( keys %$test_objects ) {
147
        my $related_source = $schema->source( $src_name );
148
149
        ok( exists $export->{$src_name}, "$src_name exists in export" );
150
        my $res;
151
        if ( $export->{$src_name}->can('unblessed') ) {
152
            $res = $export->{$src_name}->unblessed;
153
        } else {
154
            $export->{$src_name}->result_class('DBIx::Class::ResultClass::HashRefInflator');
155
            $res = [ $export->{$src_name}->all() ];
156
        }
157
158
        is_deeply(
159
            $res,
160
            $test_objects->{$src_name},
161
            "$src_name export data matches built test data"
162
        );
163
    }
164
165
    sub _get_related_sources {
166
        my $sources = {};
167
        my $res_source = Koha::Patron->new->_result()->result_source;
168
        foreach my $rel ( Koha::Patron->new->_result()->relationships() ) {
169
            my $related_source = $res_source->related_source($rel);
170
            my $info = $res_source->relationship_info( $rel );
171
            next if $info->{'attrs'}->{'is_depends_on'};
172
            next if $sources->{$related_source->source_name};
173
            $sources->{$related_source->source_name} = 1;
174
        }
175
        $sources->{'Borrower'} = 1; # add Borrower itself
176
        my @sorted = sort keys %$sources;
177
        return @sorted;
178
    }
179
180
    $schema->storage->txn_rollback;
181
};
182
81
subtest 'relationships_debt() tests' => sub {
183
subtest 'relationships_debt() tests' => sub {
82
184
83
    plan tests => 168;
185
    plan tests => 168;
84
- 

Return to bug 20028