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

(-)a/t/db_dependent/Koha/Old/Checkouts.t (-1 / +185 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This file is part of Koha
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 2;
21
22
use Koha::Database;
23
use Koha::DateUtils qw(dt_from_string);
24
use Koha::Old::Checkouts;
25
26
use t::lib::Mocks;
27
use t::lib::TestBuilder;
28
29
my $schema  = Koha::Database->new->schema;
30
my $builder = t::lib::TestBuilder->new;
31
32
subtest 'anonymize() tests' => sub {
33
34
    plan tests => 5;
35
36
    $schema->storage->txn_begin;
37
38
    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
39
40
    is( $patron->old_checkouts->count, 0, 'Patron has no old checkouts' );
41
    is( $patron->old_checkouts->anonymize + 0,
42
        0, 'Anonymizing an empty resultset returns 0' );
43
44
    my $checkout_1 = $builder->build_object(
45
        {
46
            class => 'Koha::Old::Checkouts',
47
            value =>
48
              { borrowernumber => $patron->id, timestamp => dt_from_string() }
49
        }
50
    );
51
    my $checkout_2 = $builder->build_object(
52
        {
53
            class => 'Koha::Old::Checkouts',
54
            value => {
55
                borrowernumber => $patron->id,
56
                timestamp      => dt_from_string()->subtract( days => 1 )
57
            }
58
        }
59
    );
60
    my $checkout_3 = $builder->build_object(
61
        {
62
            class => 'Koha::Old::Checkouts',
63
            value => {
64
                borrowernumber => $patron->id,
65
                timestamp      => dt_from_string()->subtract( days => 2 )
66
            }
67
        }
68
    );
69
    my $checkout_4 = $builder->build_object(
70
        {
71
            class => 'Koha::Old::Checkouts',
72
            value => {
73
                borrowernumber => $patron->id,
74
                timestamp      => dt_from_string()->subtract( days => 3 )
75
            }
76
        }
77
    );
78
79
    is( $patron->old_checkouts->count, 4, 'Patron has 4 completed checkouts' );
80
81
    # filter them so only the older two are part of the resultset
82
    my $checkouts = $patron->old_checkouts->filter_by_last_update(
83
        { days => 1, days_inclusive => 1 } );
84
85
    # Anonymize them
86
    my $anonymized_count = $checkouts->anonymize();
87
    is( $anonymized_count, 2, 'update() tells 2 rows were updated' );
88
89
    is( $patron->old_checkouts->count, 2, 'Patron has 2 completed checkouts' );
90
91
    $schema->storage->txn_rollback;
92
};
93
94
subtest 'filter_by_anonymizable() tests' => sub {
95
96
    plan tests => 7;
97
98
    $schema->storage->txn_begin;
99
100
    my $anonymous_patron = $builder->build_object({ class => 'Koha::Patrons' });
101
    t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous_patron->id );
102
103
    # patron_1 => keep records forever
104
    my $patron_1 = $builder->build_object(
105
        { class => 'Koha::Patrons', value => { privacy => 0 } } );
106
107
    # patron_2 => never keep records
108
    my $patron_2 = $builder->build_object(
109
        { class => 'Koha::Patrons', value => { privacy => 1 } } );
110
111
    is( $patron_1->old_checkouts->count, 0, 'patron_1 has no old checkouts' );
112
    is( $patron_2->old_checkouts->count, 0, 'patron_2 has no old checkouts' );
113
114
    my $checkout_1 = $builder->build_object(
115
        {
116
            class => 'Koha::Old::Checkouts',
117
            value => {
118
                borrowernumber => $patron_1->id,
119
            }
120
        }
121
    );
122
    my $checkout_2 = $builder->build_object(
123
        {
124
            class => 'Koha::Old::Checkouts',
125
            value => {
126
                borrowernumber => $patron_2->id,
127
            }
128
        }
129
    );
130
    my $checkout_3 = $builder->build_object(
131
        {
132
            class => 'Koha::Old::Checkouts',
133
            value => {
134
                borrowernumber => $patron_1->id,
135
            }
136
        }
137
    );
138
    my $checkout_4 = $builder->build_object(
139
        {
140
            class => 'Koha::Old::Checkouts',
141
            value => {
142
                borrowernumber => $patron_2->id,
143
            }
144
        }
145
    );
146
    # borrowernumber == undef => never listed as anonymizable
147
    my $checkout_5 = $builder->build_object(
148
        {
149
            class => 'Koha::Old::Checkouts',
150
            value => {
151
                borrowernumber => undef,
152
            }
153
        }
154
    );
155
    # borrowernumber == anonymous patron => never listed as anonymizable
156
    my $checkout_6 = $builder->build_object(
157
        {
158
            class => 'Koha::Old::Checkouts',
159
            value => {
160
                borrowernumber => $anonymous_patron->id,
161
            }
162
        }
163
    );
164
165
    $checkout_2->set( { timestamp => dt_from_string()->subtract( days => 1 ) } )->store;
166
    $checkout_3->set( { timestamp => dt_from_string()->subtract( days => 2 ) } )->store;
167
    $checkout_4->set( { timestamp => dt_from_string()->subtract( days => 3 ) } )->store;
168
169
    is( $patron_1->old_checkouts->count, 2, 'patron_1 has 2 completed checkouts' );
170
    is( $patron_2->old_checkouts->count, 2, 'patron_2 has 2 completed checkouts' );
171
172
    # filter them so only the older two are part of the resultset
173
    my $checkouts = Koha::Old::Checkouts->search(
174
        { 'me.borrowernumber' => [ $patron_1->id, $patron_2->id ] } );
175
    is( $checkouts->count, 4, 'Total of 4 checkouts returned correctly' );
176
    my $rs = $checkouts->filter_by_anonymizable;
177
    is( $rs->count, 2, 'Only 2 can be anonymized' );
178
179
    $rs = $checkouts->filter_by_anonymizable->filter_by_last_update(
180
        { days => 1 } );
181
182
    is( $rs->count, 1, 'Only 1 can be anonymized with date filter applied' );
183
184
    $schema->storage->txn_rollback;
185
};

Return to bug 29843