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

(-)a/Koha/Old/Holds.pm (-1 / +32 lines)
Lines 19-25 package Koha::Old::Holds; Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
23
use Koha::Database;
22
use Koha::Database;
24
23
25
use Koha::Old::Hold;
24
use Koha::Old::Hold;
Lines 36-41 This object represents a set of holds that have been filled or canceled Link Here
36
35
37
=head2 Class methods
36
=head2 Class methods
38
37
38
=head3 filter_by_anonymizable
39
40
    my $holds = $patron->old_holds;
41
    my $anonymizable_holds = $holds->filter_by_anonymizable;
42
43
This method filters a I<Koha::Old::Holds> resultset, so it only contains holds that can be
44
anonymized given the patron privacy settings.
45
46
=cut
47
48
sub filter_by_anonymizable {
49
    my ( $self, $params ) = @_;
50
51
    my $anonymous_patron = C4::Context->preference('AnonymousPatron') || undef;
52
53
    return $self->search(
54
        {
55
            'me.borrowernumber' => { 'not' => undef },
56
            'patron.privacy' => { '<>'  => 0 },
57
            (
58
                $anonymous_patron
59
                ? ( 'me.borrowernumber' => { '!=' => $anonymous_patron } )
60
                : ()
61
            ),
62
        },
63
        {
64
            join     => ['patron'],
65
            order_by => ['me.reserve_id'],
66
        }
67
    );
68
}
69
39
=head3 anonymize
70
=head3 anonymize
40
71
41
    $patron->old_holds->anonymize();
72
    $patron->old_holds->anonymize();
(-)a/Koha/Schema/Result/OldReserve.pm (+12 lines)
Lines 404-409 __PACKAGE__->belongs_to( Link Here
404
  },
404
  },
405
);
405
);
406
406
407
__PACKAGE__->belongs_to(
408
  "patron",
409
  "Koha::Schema::Result::Borrower",
410
  { borrowernumber => "borrowernumber" },
411
  {
412
    is_deferrable => 1,
413
    join_type     => "LEFT",
414
    on_delete     => "SET NULL",
415
    on_update     => "SET NULL",
416
  },
417
);
418
407
__PACKAGE__->add_columns(
419
__PACKAGE__->add_columns(
408
    '+item_level_hold' => { is_boolean => 1 },
420
    '+item_level_hold' => { is_boolean => 1 },
409
    '+lowestPriority'  => { is_boolean => 1 },
421
    '+lowestPriority'  => { is_boolean => 1 },
(-)a/t/db_dependent/Koha/Old/Holds.t (-2 / +79 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 1;
20
use Test::More tests => 2;
21
21
22
use Koha::Database;
22
use Koha::Database;
23
use Koha::DateUtils qw(dt_from_string);
23
use Koha::DateUtils qw(dt_from_string);
Lines 85-87 subtest 'anonymize() tests' => sub { Link Here
85
85
86
    $schema->storage->txn_rollback;
86
    $schema->storage->txn_rollback;
87
};
87
};
88
- 
88
89
subtest 'filter_by_anonymizable() tests' => sub {
90
91
    plan tests => 7;
92
93
    $schema->storage->txn_begin;
94
95
    # patron_1 => keep records forever
96
    my $patron_1 = $builder->build_object(
97
        { class => 'Koha::Patrons', value => { privacy => 0 } } );
98
99
    # patron_2 => never keep records
100
    my $patron_2 = $builder->build_object(
101
        { class => 'Koha::Patrons', value => { privacy => 1 } } );
102
103
    is( $patron_1->old_holds->count, 0, 'patron_1 has no old holds' );
104
    is( $patron_2->old_holds->count, 0, 'patron_2 has no old holds' );
105
106
    my $hold_1 = $builder->build_object(
107
        {
108
            class => 'Koha::Holds',
109
            value => {
110
                borrowernumber => $patron_1->id,
111
            }
112
        }
113
    )->_move_to_old;
114
    my $hold_2 = $builder->build_object(
115
        {
116
            class => 'Koha::Holds',
117
            value => {
118
                borrowernumber => $patron_2->id,
119
            }
120
        }
121
    )->_move_to_old;
122
    my $hold_3 = $builder->build_object(
123
        {
124
            class => 'Koha::Holds',
125
            value => {
126
                borrowernumber => $patron_1->id,
127
            }
128
        }
129
    )->_move_to_old;
130
    my $hold_4 = $builder->build_object(
131
        {
132
            class => 'Koha::Holds',
133
            value => {
134
                borrowernumber => $patron_2->id,
135
            }
136
        }
137
    )->_move_to_old;
138
139
    $hold_1 = Koha::Old::Holds->find( $hold_1->id )
140
      ->set( { timestamp => dt_from_string() } )->store;
141
    $hold_2 = Koha::Old::Holds->find( $hold_2->id )
142
      ->set( { timestamp => dt_from_string()->subtract( days => 1 ) } )->store;
143
    $hold_3 = Koha::Old::Holds->find( $hold_3->id )
144
      ->set( { timestamp => dt_from_string()->subtract( days => 2 ) } )->store;
145
    $hold_4 = Koha::Old::Holds->find( $hold_4->id )
146
      ->set( { timestamp => dt_from_string()->subtract( days => 3 ) } )->store;
147
148
    is( $patron_1->old_holds->count, 2, 'patron_1 has 2 completed holds' );
149
    is( $patron_2->old_holds->count, 2, 'patron_2 has 2 completed holds' );
150
151
    # filter them so only the older two are part of the resultset
152
    my $holds = Koha::Old::Holds->search(
153
        { 'me.borrowernumber' => [ $patron_1->id, $patron_2->id ] } );
154
    is( $holds->count, 4, 'Total of 4 holds returned correctly' );
155
    my $rs = $holds->filter_by_anonymizable;
156
    is( $rs->count, 2, 'Only 2 can be anonymized' );
157
158
    $rs = $holds
159
            ->filter_by_anonymizable
160
            ->filter_by_last_update( { days => 1 } );
161
162
    is( $rs->count, 1, 'Only 1 can be anonymized with date filter applied' );
163
164
    $schema->storage->txn_rollback;
165
};

Return to bug 29525