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

(-)a/Koha/Patron.pm (+93 lines)
Lines 1582-1587 sub add_extended_attribute { Link Here
1582
1582
1583
}
1583
}
1584
1584
1585
=head3 export
1586
1587
    my $export = $patron->export()
1588
1589
    Gathers patron's related data by checking DBIx relationships.
1590
1591
    Returns a hashref where keys are DBIx source names, and values are either
1592
    an instance of Koha::Objects or DBIx::Class:ResultSet, or when source
1593
    equals "Borrower", a Koha::Object.
1594
1595
    Koha::Objects is returned for sources that have a respective Koha::Object,
1596
    and for other sources DBIx::Class::ResultSet.
1597
1598
    Return example:
1599
1600
    {
1601
        "Borrower" => Koha::Patron,
1602
        "BorrowerMessagePreference" => DBIx::Class::ResultSet,
1603
        "ReturnClaim" => Koha::Checkouts::ReturnClaims
1604
    }
1605
1606
=cut
1607
1608
sub export {
1609
    my ( $self, $params ) = @_;
1610
1611
    my $schema = Koha::Database->new()->schema();
1612
1613
    my $export = {};
1614
    my $relationships = {};
1615
1616
    my $logger = Koha::Logger->get;
1617
    my $result_source = $self->_result()->result_source;
1618
1619
    foreach my $rel ( $self->_result()->relationships() ) {
1620
        my $related_source = $result_source->related_source( $rel );
1621
1622
        # Skip all the "[borrower] belongs_to" relationships
1623
        my $info = $result_source->relationship_info( $rel );
1624
        if ( $info->{'attrs'}->{'is_depends_on'} ) {
1625
            next;
1626
        }
1627
1628
        # 'cond' has format foreign.columnname, remove ^foreign. from it
1629
        ( my $rel_col = ( keys %{ $info->{'cond'} } )[0] ) =~ s/^foreign\.//;
1630
1631
        my $src_name = $related_source->source_name;
1632
1633
        $relationships->{$src_name} = [] unless $relationships->{$src_name};
1634
        push @{ $relationships->{$src_name} }, $rel_col;
1635
    }
1636
1637
    foreach my $src_name ( keys %$relationships ) {
1638
        my $related_source = $schema->source( $src_name );
1639
        my $res;
1640
        my $rs = $related_source->resultset;
1641
        my $search;
1642
1643
        if ( scalar @{ $relationships->{$src_name} } > 1 ) {
1644
            $search = { '-or' => [] };
1645
            foreach my $rel_col ( values @{ $relationships->{$src_name} } ) {
1646
                push @{ $search->{'-or'} }, $rel_col;
1647
                push @{ $search->{'-or'} }, $self->borrowernumber;
1648
            }
1649
        } else {
1650
            $search = { $relationships->{$src_name}->[0] => $self->borrowernumber };
1651
        }
1652
1653
        if ( $related_source->result_class->can('koha_objects_class') ) {
1654
            my $koha_object = $related_source->result_class->koha_objects_class;
1655
1656
            if ( ! Module::Load::Conditional::can_load(
1657
                    modules => { $koha_object => undef} )) {
1658
                Koha::Exceptions::Exception->throw(
1659
                    error => "Could not load '$koha_object'"
1660
                );
1661
            }
1662
1663
            $res = $koha_object->search( $search );
1664
        } else {
1665
            $res = $rs->search( $search );
1666
        }
1667
1668
        next unless $res->count;
1669
1670
        $export->{$src_name} = $res if $res;
1671
    }
1672
1673
    $export->{'Borrower'} = $self;
1674
1675
    return $export;
1676
}
1677
1585
=head3 extended_attributes
1678
=head3 extended_attributes
1586
1679
1587
Return object of Koha::Patron::Attributes type with all attributes set for this patron
1680
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