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

(-)a/Koha/Patron.pm (+93 lines)
Lines 1550-1555 sub add_extended_attribute { Link Here
1550
1550
1551
}
1551
}
1552
1552
1553
=head3 export
1554
1555
    my $export = $patron->export()
1556
1557
    Gathers patron's related data by checking DBIx relationships.
1558
1559
    Returns a hashref where keys are DBIx source names, and values are either
1560
    an instance of Koha::Objects or DBIx::Class:ResultSet, or when source
1561
    equals "Borrower", a Koha::Object.
1562
1563
    Koha::Objects is returned for sources that have a respective Koha::Object,
1564
    and for other sources DBIx::Class::ResultSet.
1565
1566
    Return example:
1567
1568
    {
1569
        "Borrower" => Koha::Patron,
1570
        "BorrowerMessagePreference" => DBIx::Class::ResultSet,
1571
        "ReturnClaim" => Koha::Checkouts::ReturnClaims
1572
    }
1573
1574
=cut
1575
1576
sub export {
1577
    my ( $self, $params ) = @_;
1578
1579
    my $schema = Koha::Database->new()->schema();
1580
1581
    my $export = {};
1582
    my $relationships = {};
1583
1584
    my $logger = Koha::Logger->get;
1585
    my $result_source = $self->_result()->result_source;
1586
1587
    foreach my $rel ( $self->_result()->relationships() ) {
1588
        my $related_source = $result_source->related_source( $rel );
1589
1590
        # Skip all the "[borrower] belongs_to" relationships
1591
        my $info = $result_source->relationship_info( $rel );
1592
        if ( $info->{'attrs'}->{'is_depends_on'} ) {
1593
            next;
1594
        }
1595
1596
        # 'cond' has format foreign.columnname, remove ^foreign. from it
1597
        ( my $rel_col = ( keys %{ $info->{'cond'} } )[0] ) =~ s/^foreign\.//;
1598
1599
        my $src_name = $related_source->source_name;
1600
1601
        $relationships->{$src_name} = [] unless $relationships->{$src_name};
1602
        push @{ $relationships->{$src_name} }, $rel_col;
1603
    }
1604
1605
    foreach my $src_name ( keys %$relationships ) {
1606
        my $related_source = $schema->source( $src_name );
1607
        my $res;
1608
        my $rs = $related_source->resultset;
1609
        my $search;
1610
1611
        if ( scalar @{ $relationships->{$src_name} } > 1 ) {
1612
            $search = { '-or' => [] };
1613
            foreach my $rel_col ( values @{ $relationships->{$src_name} } ) {
1614
                push @{ $search->{'-or'} }, $rel_col;
1615
                push @{ $search->{'-or'} }, $self->borrowernumber;
1616
            }
1617
        } else {
1618
            $search = { $relationships->{$src_name}->[0] => $self->borrowernumber };
1619
        }
1620
1621
        if ( $related_source->result_class->can('koha_objects_class') ) {
1622
            my $koha_object = $related_source->result_class->koha_objects_class;
1623
1624
            if ( ! Module::Load::Conditional::can_load(
1625
                    modules => { $koha_object => undef} )) {
1626
                Koha::Exceptions::Exception->throw(
1627
                    error => "Could not load '$koha_object'"
1628
                );
1629
            }
1630
1631
            $res = $koha_object->search( $search );
1632
        } else {
1633
            $res = $rs->search( $search );
1634
        }
1635
1636
        next unless $res->count;
1637
1638
        $export->{$src_name} = $res if $res;
1639
    }
1640
1641
    $export->{'Borrower'} = $self;
1642
1643
    return $export;
1644
}
1645
1553
=head3 extended_attributes
1646
=head3 extended_attributes
1554
1647
1555
Return object of Koha::Patron::Attributes type with all attributes set for this patron
1648
Return object of Koha::Patron::Attributes type with all attributes set for this patron
(-)a/t/db_dependent/Koha/Patron.t (-2 / +103 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 7;
22
use Test::More tests => 8;
23
use Test::Exception;
23
use Test::Exception;
24
use Test::Warn;
24
use Test::Warn;
25
25
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