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

(-)a/Koha/Patron.pm (+82 lines)
Lines 1536-1541 sub add_extended_attribute { Link Here
1536
    return Koha::Patron::Attribute->new($attribute)->store;
1536
    return Koha::Patron::Attribute->new($attribute)->store;
1537
}
1537
}
1538
1538
1539
=head3 export
1540
1541
    my $export = $patron->export()
1542
1543
    Gathers patron's related data. Works by checking DBIx relationships to
1544
    Borrower.
1545
1546
    Returns a HASHref containing keys of DBIx source names, e.g.
1547
1548
    {
1549
        "Accountline": [
1550
            { "accountline_id": 1, "borrowernumber": 1481, ... },
1551
            { "accountline_id": 2, "borrowernumber": 1481, ... }
1552
        ],
1553
        "Borrower": { "borrowernumber": 1481, "firstname": "Export", ... },
1554
        ...,
1555
        "Virtualshelve": [
1556
            { "shelfnumber": 1, "owner": 1481, ... },
1557
            { "shelfnumber": 2, "owner": 1481, ... }
1558
        ]
1559
    }
1560
1561
=cut
1562
1563
sub export {
1564
    my ( $self ) = @_;
1565
1566
    my $export = {};
1567
1568
    my $logger = Koha::Logger->get;
1569
    my $result_source = $self->_result()->result_source;
1570
1571
    foreach my $rel ( $self->_result()->relationships() ) {
1572
        my $related_source = $result_source->related_source( $rel );
1573
1574
        # Skip all the "[borrower] belongs_to" relationships
1575
        my $info = $result_source->relationship_info( $rel );
1576
        if ( $info->{'attrs'}->{'is_depends_on'} ) {
1577
            next;
1578
        }
1579
1580
        my $src_name = $related_source->source_name;
1581
1582
        # 'cond' has format foreign.columnname, remove ^foreign. from it
1583
        ( my $rel_col = ( keys %{$info->{'cond'}} )[0] ) =~ s/^foreign\.//;
1584
1585
        my $rs = $related_source->resultset;
1586
        my $res = $rs->search( { $rel_col => $self->borrowernumber } );
1587
1588
        $res->result_class('DBIx::Class::ResultClass::HashRefInflator');
1589
1590
        unless ( exists $export->{$src_name} ) {
1591
            $export->{$src_name} = [];
1592
        }
1593
1594
        foreach my $obj ( $res->all() ) {
1595
            # Check if object already exists in return array
1596
            my $already_present = 0;
1597
            my $ddsk = $Data::Dumper::Sortkeys;
1598
            $Data::Dumper::Sortkeys = 1; # sort keys temporarily
1599
            foreach my $already_existing ( @{ $export->{$src_name} } ) {
1600
1601
                if (   Data::Dumper::Dumper( $already_existing )
1602
                    eq Data::Dumper::Dumper( $obj ))
1603
                {
1604
                    $already_present = 1;
1605
                    last;
1606
                }
1607
            }
1608
            $Data::Dumper::Sortkeys = $ddsk; # restore previous value
1609
1610
            next if $already_present;
1611
1612
            push @{ $export->{$src_name} }, $obj;
1613
        }
1614
    }
1615
1616
    $export->{'Borrower'} = $self->unblessed;
1617
1618
    return $export;
1619
}
1620
1539
=head3 extended_attributes
1621
=head3 extended_attributes
1540
1622
1541
Return object of Koha::Patron::Attributes type with all attributes set for this patron
1623
Return object of Koha::Patron::Attributes type with all attributes set for this patron
(-)a/t/db_dependent/Koha/Patron.t (-2 / +105 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 'export' => sub {
81
82
    my @related_sources = _get_related_sources();
83
    plan tests => scalar @related_sources * 2;
84
85
    $schema->storage->txn_begin;
86
87
    my $patron = $builder->build_object(
88
        {
89
            class => 'Koha::Patrons',
90
        }
91
    );
92
93
    my $test_objects = {
94
        'Borrower' => $patron->unblessed,
95
    };
96
97
    my $limit_data     = 5; # build this many random test objects
98
    my $generated_data = 0;
99
100
    foreach my $src ( @related_sources ) {
101
        next if $src eq 'Borrower'; # Borrower is a hashref,
102
        $test_objects->{$src} = []; # the others are arrayrefs
103
    }
104
105
    my $result_source = Koha::Patron->new->_result()->result_source;
106
    foreach my $rel (   Koha::Patron->new->_result()->relationships() ) {
107
        if ($generated_data >= $limit_data) {
108
            last;
109
        }
110
111
        my $related_source = $result_source->related_source( $rel );
112
        my $source_name = $related_source->source_name;
113
114
                my $info = $result_source->relationship_info( $rel );
115
116
        # We are not interested in the "belongs_to" relationship of borrowers.
117
        # These are tables like branches, categories and sms_provider.
118
        if ( $info->{'attrs'}->{'is_depends_on'} ) {
119
            next;
120
        }
121
122
        ( my $rel_col = (keys %{$info->{'cond'}})[0] ) =~ s/^foreign\.//;
123
124
        # Generate test data into related tables
125
        my $built;
126
        if ( $related_source->result_class->can('koha_objects_class') ) {
127
            $built = $builder->build_object(
128
                {
129
                    class => $related_source->result_class->koha_objects_class,
130
                    value => { $rel_col => $patron->borrowernumber }
131
                }
132
            );
133
            $built = $built->unblessed;
134
        } else {
135
            $built = $builder->build(
136
                {
137
                    source => $source_name,
138
                    value  => { $rel_col => $patron->borrowernumber }
139
                }
140
            );
141
        }
142
        push @{ $test_objects->{$related_source->source_name} }, $built;
143
        $generated_data++;
144
    }
145
146
    # Generate test data into a random table. Just one because test data
147
    # generation takes so long.
148
    $builder->build( {
149
        source => (keys %$test_objects)[rand keys %$test_objects]
150
    } );
151
152
    my $export = $patron->export;
153
154
    foreach my $rel ( @related_sources ) {
155
        my $related_source = $schema->source( $rel );
156
        my $src_name = $related_source->source_name;
157
158
        ok( exists $export->{$src_name}, "$src_name exists in export" );
159
        is_deeply(
160
            $export->{$src_name},
161
            $test_objects->{$src_name},
162
            "$src_name export data matches built test data"
163
        );
164
    }
165
166
    sub _get_related_sources {
167
        my $sources = {};
168
        my $res_source = Koha::Patron->new->_result()->result_source;
169
        foreach my $rel ( Koha::Patron->new->_result()->relationships() ) {
170
            my $related_source = $res_source->related_source($rel);
171
            my $info = $res_source->relationship_info( $rel );
172
            next if $info->{'attrs'}->{'is_depends_on'};
173
            next if $sources->{$related_source->source_name};
174
            $sources->{$related_source->source_name} = 1;
175
        }
176
        $sources->{'Borrower'} = 1; # add Borrower itself
177
        my @sorted = sort keys %$sources;
178
        return @sorted;
179
    }
180
181
    $schema->storage->txn_rollback;
182
};
183
80
subtest 'relationships_debt() tests' => sub {
184
subtest 'relationships_debt() tests' => sub {
81
185
82
    plan tests => 168;
186
    plan tests => 168;
83
- 

Return to bug 20028